1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995 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 reloc_howto_type
*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 reloc_howto_type
*
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
;
1722 if (h
->root
.root
.type
== bfd_link_hash_common
)
1723 h
->esym
.asym
.value
= h
->root
.root
.u
.c
.size
;
1724 else if (h
->root
.root
.type
== bfd_link_hash_defined
)
1728 if (h
->esym
.asym
.sc
== scCommon
)
1729 h
->esym
.asym
.sc
= scBss
;
1730 else if (h
->esym
.asym
.sc
== scSCommon
)
1731 h
->esym
.asym
.sc
= scSBss
;
1733 sec
= h
->root
.root
.u
.def
.section
;
1734 h
->esym
.asym
.value
= (h
->root
.root
.u
.def
.value
1735 + sec
->output_offset
1736 + sec
->output_section
->vma
);
1739 if (! bfd_ecoff_debug_one_external (einfo
->abfd
, einfo
->debug
, einfo
->swap
,
1740 h
->root
.root
.root
.string
,
1743 einfo
->failed
= true;
1750 /* A comparison routine used to sort .gptab entries. */
1753 gptab_compare (p1
, p2
)
1757 const Elf32_gptab
*a1
= (const Elf32_gptab
*) p1
;
1758 const Elf32_gptab
*a2
= (const Elf32_gptab
*) p2
;
1760 return a1
->gt_entry
.gt_g_value
- a2
->gt_entry
.gt_g_value
;
1763 /* We need to use a special link routine to handle the .reginfo and
1764 the .mdebug sections. We need to merge all instances of these
1765 sections together, not write them all out sequentially. */
1768 mips_elf_final_link (abfd
, info
)
1770 struct bfd_link_info
*info
;
1774 struct bfd_link_order
*p
;
1775 asection
*reginfo_sec
, *mdebug_sec
, *gptab_data_sec
, *gptab_bss_sec
;
1776 Elf32_RegInfo reginfo
;
1777 struct ecoff_debug_info debug
;
1778 const struct ecoff_debug_swap
*swap
1779 = get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
1780 HDRR
*symhdr
= &debug
.symbolic_header
;
1781 PTR mdebug_handle
= NULL
;
1783 /* Drop the .options section, since it has special semantics which I
1784 haven't bothered to figure out. */
1785 for (secpp
= &abfd
->sections
; *secpp
!= NULL
; secpp
= &(*secpp
)->next
)
1787 if (strcmp ((*secpp
)->name
, ".options") == 0)
1789 for (p
= (*secpp
)->link_order_head
; p
!= NULL
; p
= p
->next
)
1790 if (p
->type
== bfd_indirect_link_order
)
1791 p
->u
.indirect
.section
->flags
&=~ SEC_HAS_CONTENTS
;
1792 (*secpp
)->link_order_head
= NULL
;
1793 *secpp
= (*secpp
)->next
;
1794 --abfd
->section_count
;
1799 /* Go through the sections and collect the .reginfo and .mdebug
1803 gptab_data_sec
= NULL
;
1804 gptab_bss_sec
= NULL
;
1805 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
1807 if (strcmp (o
->name
, ".reginfo") == 0)
1809 memset (®info
, 0, sizeof reginfo
);
1811 /* We have found the .reginfo section in the output file.
1812 Look through all the link_orders comprising it and merge
1813 the information together. */
1814 for (p
= o
->link_order_head
;
1815 p
!= (struct bfd_link_order
*) NULL
;
1818 asection
*input_section
;
1820 Elf32_External_RegInfo ext
;
1823 if (p
->type
!= bfd_indirect_link_order
)
1825 if (p
->type
== bfd_fill_link_order
)
1830 input_section
= p
->u
.indirect
.section
;
1831 input_bfd
= input_section
->owner
;
1832 BFD_ASSERT (input_section
->_raw_size
1833 == sizeof (Elf32_External_RegInfo
));
1834 if (! bfd_get_section_contents (input_bfd
, input_section
,
1840 bfd_mips_elf32_swap_reginfo_in (input_bfd
, &ext
, &sub
);
1842 reginfo
.ri_gprmask
|= sub
.ri_gprmask
;
1843 reginfo
.ri_cprmask
[0] |= sub
.ri_cprmask
[0];
1844 reginfo
.ri_cprmask
[1] |= sub
.ri_cprmask
[1];
1845 reginfo
.ri_cprmask
[2] |= sub
.ri_cprmask
[2];
1846 reginfo
.ri_cprmask
[3] |= sub
.ri_cprmask
[3];
1848 /* ri_gp_value is set by the function
1849 mips_elf_section_processing when the section is
1850 finally written out. */
1852 /* Hack: reset the SEC_HAS_CONTENTS flag so that
1853 elf_link_input_bfd ignores this section. */
1854 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
1857 /* Force the section size to the value we want. */
1858 o
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1860 /* Skip this section later on (I don't think this currently
1861 matters, but someday it might). */
1862 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
1867 if (strcmp (o
->name
, ".mdebug") == 0)
1869 struct extsym_info einfo
;
1871 /* We have found the .mdebug section in the output file.
1872 Look through all the link_orders comprising it and merge
1873 the information together. */
1874 symhdr
->magic
= swap
->sym_magic
;
1875 /* FIXME: What should the version stamp be? */
1877 symhdr
->ilineMax
= 0;
1881 symhdr
->isymMax
= 0;
1882 symhdr
->ioptMax
= 0;
1883 symhdr
->iauxMax
= 0;
1885 symhdr
->issExtMax
= 0;
1888 symhdr
->iextMax
= 0;
1890 /* We accumulate the debugging information itself in the
1891 debug_info structure. */
1893 debug
.external_dnr
= NULL
;
1894 debug
.external_pdr
= NULL
;
1895 debug
.external_sym
= NULL
;
1896 debug
.external_opt
= NULL
;
1897 debug
.external_aux
= NULL
;
1899 debug
.ssext
= debug
.ssext_end
= NULL
;
1900 debug
.external_fdr
= NULL
;
1901 debug
.external_rfd
= NULL
;
1902 debug
.external_ext
= debug
.external_ext_end
= NULL
;
1904 mdebug_handle
= bfd_ecoff_debug_init (abfd
, &debug
, swap
, info
);
1905 if (mdebug_handle
== (PTR
) NULL
)
1908 for (p
= o
->link_order_head
;
1909 p
!= (struct bfd_link_order
*) NULL
;
1912 asection
*input_section
;
1914 const struct ecoff_debug_swap
*input_swap
;
1915 struct ecoff_debug_info input_debug
;
1919 if (p
->type
!= bfd_indirect_link_order
)
1921 if (p
->type
== bfd_fill_link_order
)
1926 input_section
= p
->u
.indirect
.section
;
1927 input_bfd
= input_section
->owner
;
1929 if (bfd_get_flavour (input_bfd
) != bfd_target_elf_flavour
1930 || (get_elf_backend_data (input_bfd
)
1931 ->elf_backend_ecoff_debug_swap
) == NULL
)
1933 /* I don't know what a non MIPS ELF bfd would be
1934 doing with a .mdebug section, but I don't really
1935 want to deal with it. */
1939 input_swap
= (get_elf_backend_data (input_bfd
)
1940 ->elf_backend_ecoff_debug_swap
);
1942 BFD_ASSERT (p
->size
== input_section
->_raw_size
);
1944 /* The ECOFF linking code expects that we have already
1945 read in the debugging information and set up an
1946 ecoff_debug_info structure, so we do that now. */
1947 if (! mips_elf_read_ecoff_info (input_bfd
, input_section
,
1951 if (! (bfd_ecoff_debug_accumulate
1952 (mdebug_handle
, abfd
, &debug
, swap
, input_bfd
,
1953 &input_debug
, input_swap
, info
)))
1956 /* Loop through the external symbols. For each one with
1957 interesting information, try to find the symbol in
1958 the linker global hash table and save the information
1959 for the output external symbols. */
1960 eraw_src
= input_debug
.external_ext
;
1961 eraw_end
= (eraw_src
1962 + (input_debug
.symbolic_header
.iextMax
1963 * input_swap
->external_ext_size
));
1965 eraw_src
< eraw_end
;
1966 eraw_src
+= input_swap
->external_ext_size
)
1970 struct mips_elf_link_hash_entry
*h
;
1972 (*input_swap
->swap_ext_in
) (input_bfd
, (PTR
) eraw_src
, &ext
);
1973 if (ext
.asym
.sc
== scNil
1974 || ext
.asym
.sc
== scUndefined
1975 || ext
.asym
.sc
== scSUndefined
)
1978 name
= input_debug
.ssext
+ ext
.asym
.iss
;
1979 h
= mips_elf_link_hash_lookup (mips_elf_hash_table (info
),
1980 name
, false, false, true);
1981 if (h
== NULL
|| h
->esym
.ifd
!= -2)
1987 < input_debug
.symbolic_header
.ifdMax
);
1988 ext
.ifd
= input_debug
.ifdmap
[ext
.ifd
];
1994 /* Free up the information we just read. */
1995 free (input_debug
.line
);
1996 free (input_debug
.external_dnr
);
1997 free (input_debug
.external_pdr
);
1998 free (input_debug
.external_sym
);
1999 free (input_debug
.external_opt
);
2000 free (input_debug
.external_aux
);
2001 free (input_debug
.ss
);
2002 free (input_debug
.ssext
);
2003 free (input_debug
.external_fdr
);
2004 free (input_debug
.external_rfd
);
2005 free (input_debug
.external_ext
);
2007 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2008 elf_link_input_bfd ignores this section. */
2009 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
2012 /* Build the external symbol information. */
2015 einfo
.debug
= &debug
;
2017 einfo
.failed
= false;
2018 mips_elf_link_hash_traverse (mips_elf_hash_table (info
),
2019 mips_elf_output_extsym
,
2024 /* Set the size of the .mdebug section. */
2025 o
->_raw_size
= bfd_ecoff_debug_size (abfd
, &debug
, swap
);
2027 /* Skip this section later on (I don't think this currently
2028 matters, but someday it might). */
2029 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2034 if (strncmp (o
->name
, ".gptab.", sizeof ".gptab." - 1) == 0)
2036 const char *subname
;
2039 Elf32_External_gptab
*ext_tab
;
2042 /* The .gptab.sdata and .gptab.sbss sections hold
2043 information describing how the small data area would
2044 change depending upon the -G switch. These sections
2045 not used in executables files. */
2046 if (! info
->relocateable
)
2050 for (p
= o
->link_order_head
;
2051 p
!= (struct bfd_link_order
*) NULL
;
2054 asection
*input_section
;
2056 if (p
->type
!= bfd_indirect_link_order
)
2058 if (p
->type
== bfd_fill_link_order
)
2063 input_section
= p
->u
.indirect
.section
;
2065 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2066 elf_link_input_bfd ignores this section. */
2067 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
2070 /* Skip this section later on (I don't think this
2071 currently matters, but someday it might). */
2072 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2074 /* Really remove the section. */
2075 for (secpp
= &abfd
->sections
;
2077 secpp
= &(*secpp
)->next
)
2079 *secpp
= (*secpp
)->next
;
2080 --abfd
->section_count
;
2085 /* There is one gptab for initialized data, and one for
2086 uninitialized data. */
2087 if (strcmp (o
->name
, ".gptab.sdata") == 0)
2089 else if (strcmp (o
->name
, ".gptab.sbss") == 0)
2093 bfd_set_error (bfd_error_nonrepresentable_section
);
2097 /* The linker script always combines .gptab.data and
2098 .gptab.sdata into .gptab.sdata, and likewise for
2099 .gptab.bss and .gptab.sbss. It is possible that there is
2100 no .sdata or .sbss section in the output file, in which
2101 case we must change the name of the output section. */
2102 subname
= o
->name
+ sizeof ".gptab" - 1;
2103 if (bfd_get_section_by_name (abfd
, subname
) == NULL
)
2105 if (o
== gptab_data_sec
)
2106 o
->name
= ".gptab.data";
2108 o
->name
= ".gptab.bss";
2109 subname
= o
->name
+ sizeof ".gptab" - 1;
2110 BFD_ASSERT (bfd_get_section_by_name (abfd
, subname
) != NULL
);
2113 /* Set up the first entry. */
2115 tab
= (Elf32_gptab
*) malloc (c
* sizeof (Elf32_gptab
));
2118 bfd_set_error (bfd_error_no_memory
);
2121 tab
[0].gt_header
.gt_current_g_value
= elf_gp_size (abfd
);
2122 tab
[0].gt_header
.gt_unused
= 0;
2124 /* Combine the input sections. */
2125 for (p
= o
->link_order_head
;
2126 p
!= (struct bfd_link_order
*) NULL
;
2129 asection
*input_section
;
2133 bfd_size_type gpentry
;
2135 if (p
->type
!= bfd_indirect_link_order
)
2137 if (p
->type
== bfd_fill_link_order
)
2142 input_section
= p
->u
.indirect
.section
;
2143 input_bfd
= input_section
->owner
;
2145 /* Combine the gptab entries for this input section one
2146 by one. We know that the input gptab entries are
2147 sorted by ascending -G value. */
2148 size
= bfd_section_size (input_bfd
, input_section
);
2150 for (gpentry
= sizeof (Elf32_External_gptab
);
2152 gpentry
+= sizeof (Elf32_External_gptab
))
2154 Elf32_External_gptab ext_gptab
;
2155 Elf32_gptab int_gptab
;
2161 if (! (bfd_get_section_contents
2162 (input_bfd
, input_section
, (PTR
) &ext_gptab
,
2163 gpentry
, sizeof (Elf32_External_gptab
))))
2169 bfd_mips_elf32_swap_gptab_in (input_bfd
, &ext_gptab
,
2171 val
= int_gptab
.gt_entry
.gt_g_value
;
2172 add
= int_gptab
.gt_entry
.gt_bytes
- last
;
2175 for (look
= 1; look
< c
; look
++)
2177 if (tab
[look
].gt_entry
.gt_g_value
>= val
)
2178 tab
[look
].gt_entry
.gt_bytes
+= add
;
2180 if (tab
[look
].gt_entry
.gt_g_value
== val
)
2186 Elf32_gptab
*new_tab
;
2189 /* We need a new table entry. */
2190 new_tab
= ((Elf32_gptab
*)
2192 (c
+ 1) * sizeof (Elf32_gptab
)));
2193 if (new_tab
== NULL
)
2195 bfd_set_error (bfd_error_no_memory
);
2200 tab
[c
].gt_entry
.gt_g_value
= val
;
2201 tab
[c
].gt_entry
.gt_bytes
= add
;
2203 /* Merge in the size for the next smallest -G
2204 value, since that will be implied by this new
2207 for (look
= 1; look
< c
; look
++)
2209 if (tab
[look
].gt_entry
.gt_g_value
< val
2211 || (tab
[look
].gt_entry
.gt_g_value
2212 > tab
[max
].gt_entry
.gt_g_value
)))
2216 tab
[c
].gt_entry
.gt_bytes
+=
2217 tab
[max
].gt_entry
.gt_bytes
;
2222 last
= int_gptab
.gt_entry
.gt_bytes
;
2225 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2226 elf_link_input_bfd ignores this section. */
2227 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
2230 /* The table must be sorted by -G value. */
2232 qsort (tab
+ 1, c
- 1, sizeof (tab
[0]), gptab_compare
);
2234 /* Swap out the table. */
2235 ext_tab
= ((Elf32_External_gptab
*)
2236 bfd_alloc (abfd
, c
* sizeof (Elf32_External_gptab
)));
2237 if (ext_tab
== NULL
)
2239 bfd_set_error (bfd_error_no_memory
);
2244 for (i
= 0; i
< c
; i
++)
2245 bfd_mips_elf32_swap_gptab_out (abfd
, tab
+ i
, ext_tab
+ i
);
2248 o
->_raw_size
= c
* sizeof (Elf32_External_gptab
);
2249 o
->contents
= (bfd_byte
*) ext_tab
;
2251 /* Skip this section later on (I don't think this currently
2252 matters, but someday it might). */
2253 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2257 /* Get a value for the GP register. */
2258 if (elf_gp (abfd
) == 0)
2260 struct bfd_link_hash_entry
*h
;
2262 h
= bfd_link_hash_lookup (info
->hash
, "_gp", false, false, true);
2263 if (h
!= (struct bfd_link_hash_entry
*) NULL
2264 && h
->type
== bfd_link_hash_defined
)
2265 elf_gp (abfd
) = (h
->u
.def
.value
2266 + h
->u
.def
.section
->output_section
->vma
2267 + h
->u
.def
.section
->output_offset
);
2268 else if (info
->relocateable
)
2272 /* Make up a value. */
2274 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
2277 && (strcmp (o
->name
, ".sbss") == 0
2278 || strcmp (o
->name
, ".sdata") == 0
2279 || strcmp (o
->name
, ".lit4") == 0
2280 || strcmp (o
->name
, ".lit8") == 0))
2283 elf_gp (abfd
) = lo
+ 0x8000;
2287 /* If the relocate_section function needs to do a reloc
2288 involving the GP value, it should make a reloc_dangerous
2289 callback to warn that GP is not defined. */
2293 /* Invoke the regular ELF backend linker to do all the work. */
2294 if (! bfd_elf32_bfd_final_link (abfd
, info
))
2297 /* Now write out the computed sections. */
2299 if (reginfo_sec
!= (asection
*) NULL
)
2301 Elf32_External_RegInfo ext
;
2303 bfd_mips_elf32_swap_reginfo_out (abfd
, ®info
, &ext
);
2304 if (! bfd_set_section_contents (abfd
, reginfo_sec
, (PTR
) &ext
,
2305 (file_ptr
) 0, sizeof ext
))
2309 if (mdebug_sec
!= (asection
*) NULL
)
2311 BFD_ASSERT (abfd
->output_has_begun
);
2312 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle
, abfd
, &debug
,
2314 mdebug_sec
->filepos
))
2317 bfd_ecoff_debug_free (mdebug_handle
, abfd
, &debug
, swap
, info
);
2320 if (gptab_data_sec
!= (asection
*) NULL
)
2322 if (! bfd_set_section_contents (abfd
, gptab_data_sec
,
2323 gptab_data_sec
->contents
,
2325 gptab_data_sec
->_raw_size
))
2329 if (gptab_bss_sec
!= (asection
*) NULL
)
2331 if (! bfd_set_section_contents (abfd
, gptab_bss_sec
,
2332 gptab_bss_sec
->contents
,
2334 gptab_bss_sec
->_raw_size
))
2341 /* Handle a MIPS ELF HI16 reloc. */
2344 mips_elf_relocate_hi16 (input_bfd
, relhi
, rello
, contents
, addend
)
2346 Elf_Internal_Rela
*relhi
;
2347 Elf_Internal_Rela
*rello
;
2354 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
2356 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
2359 addend
+= ((insn
& 0xffff) << 16) + addlo
;
2361 if ((addlo
& 0x8000) != 0)
2363 if ((addend
& 0x8000) != 0)
2366 bfd_put_32 (input_bfd
,
2367 (insn
& 0xffff0000) | ((addend
>> 16) & 0xffff),
2368 contents
+ relhi
->r_offset
);
2371 /* Relocate a MIPS ELF section. */
2374 mips_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
2375 contents
, relocs
, local_syms
, local_sections
)
2377 struct bfd_link_info
*info
;
2379 asection
*input_section
;
2381 Elf_Internal_Rela
*relocs
;
2382 Elf_Internal_Sym
*local_syms
;
2383 asection
**local_sections
;
2385 Elf_Internal_Shdr
*symtab_hdr
;
2388 Elf_Internal_Rela
*rel
;
2389 Elf_Internal_Rela
*relend
;
2391 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2393 if (elf_bad_symtab (input_bfd
))
2395 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
2400 locsymcount
= symtab_hdr
->sh_info
;
2401 extsymoff
= symtab_hdr
->sh_info
;
2405 relend
= relocs
+ input_section
->reloc_count
;
2406 for (; rel
< relend
; rel
++)
2409 reloc_howto_type
*howto
;
2412 struct elf_link_hash_entry
*h
;
2414 Elf_Internal_Sym
*sym
;
2415 bfd_reloc_status_type r
;
2417 r_type
= ELF32_R_TYPE (rel
->r_info
);
2418 if (r_type
< 0 || r_type
>= (int) R_MIPS_max
)
2420 bfd_set_error (bfd_error_bad_value
);
2423 howto
= elf_mips_howto_table
+ r_type
;
2425 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2427 /* Mix in the change in GP address for a GP relative reloc. */
2428 if (r_type
!= R_MIPS_GPREL16
2429 && r_type
!= R_MIPS_LITERAL
2430 && r_type
!= R_MIPS_GPREL32
)
2434 if (elf_gp (output_bfd
) == 0)
2436 if (! ((*info
->callbacks
->reloc_dangerous
)
2438 "GP relative relocation when GP not defined",
2439 input_bfd
, input_section
,
2442 /* Only give the error once per link. */
2443 elf_gp (output_bfd
) = 4;
2446 if (r_symndx
< extsymoff
2447 || (elf_bad_symtab (input_bfd
)
2448 && local_sections
[r_symndx
] != NULL
))
2450 /* This is a relocation against a section. The current
2451 addend in the instruction is the difference between
2452 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
2453 must change this to be the difference between the
2454 final definition (which will end up in RELOCATION)
2455 and the GP value of OUTPUT_BFD (which is in GP). */
2456 addend
= elf_gp (input_bfd
) - elf_gp (output_bfd
);
2458 else if (! info
->relocateable
)
2460 /* We are doing a final link. The current addend in the
2461 instruction is simply the desired offset into the
2462 symbol (normally zero). We want the instruction to
2463 hold the difference between the final definition of
2464 the symbol (which will end up in RELOCATION) and the
2465 GP value of OUTPUT_BFD (which is in GP). */
2466 addend
= - elf_gp (output_bfd
);
2470 /* We are generating relocateable output, and we aren't
2471 going to define this symbol, so we just leave the
2472 instruction alone. */
2480 if (info
->relocateable
)
2482 /* This is a relocateable link. We don't have to change
2483 anything, unless the reloc is against a section symbol,
2484 in which case we have to adjust according to where the
2485 section symbol winds up in the output section. */
2486 if (r_symndx
>= locsymcount
2487 || (elf_bad_symtab (input_bfd
)
2488 && local_sections
[r_symndx
] == NULL
))
2492 sym
= local_syms
+ r_symndx
;
2493 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2497 sec
= local_sections
[r_symndx
];
2499 /* It would be logical to add sym->st_value here,
2500 but Irix 5 sometimes generates a garbage symbol
2502 addend
+= sec
->output_offset
;
2504 /* If this is HI16 with an associated LO16, adjust
2505 the addend accordingly. Otherwise, just
2507 if (r_type
!= R_MIPS_HI16
2508 || (rel
+ 1) >= relend
2509 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
2510 r
= _bfd_relocate_contents (howto
, input_bfd
,
2512 contents
+ rel
->r_offset
);
2515 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
2526 /* This is a final link. */
2528 if (r_symndx
< extsymoff
2529 || (elf_bad_symtab (input_bfd
)
2530 && local_sections
[r_symndx
] != NULL
))
2532 sym
= local_syms
+ r_symndx
;
2533 sec
= local_sections
[r_symndx
];
2534 relocation
= (sec
->output_section
->vma
2535 + sec
->output_offset
);
2537 /* It would be logical to always add sym->st_value here,
2538 but Irix 5 sometimes generates a garbage symbol
2540 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2541 relocation
+= sym
->st_value
;
2547 indx
= r_symndx
- extsymoff
;
2548 h
= elf_sym_hashes (input_bfd
)[indx
];
2549 if (h
->root
.type
== bfd_link_hash_defined
)
2551 sec
= h
->root
.u
.def
.section
;
2552 relocation
= (h
->root
.u
.def
.value
2553 + sec
->output_section
->vma
2554 + sec
->output_offset
);
2556 else if (h
->root
.type
== bfd_link_hash_weak
)
2560 if (! ((*info
->callbacks
->undefined_symbol
)
2561 (info
, h
->root
.root
.string
, input_bfd
,
2562 input_section
, rel
->r_offset
)))
2568 if (r_type
!= R_MIPS_HI16
2569 || (rel
+ 1) >= relend
2570 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
2571 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2572 contents
, rel
->r_offset
,
2573 relocation
, addend
);
2576 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
2577 contents
, relocation
+ addend
);
2582 if (r
!= bfd_reloc_ok
)
2587 case bfd_reloc_outofrange
:
2589 case bfd_reloc_overflow
:
2594 name
= h
->root
.root
.string
;
2597 name
= elf_string_from_elf_section (input_bfd
,
2598 symtab_hdr
->sh_link
,
2603 name
= bfd_section_name (input_bfd
, sec
);
2605 if (! ((*info
->callbacks
->reloc_overflow
)
2606 (info
, name
, howto
->name
, (bfd_vma
) 0,
2607 input_bfd
, input_section
, rel
->r_offset
)))
2618 /* ECOFF swapping routines. These are used when dealing with the
2619 .mdebug section, which is in the ECOFF debugging format. */
2620 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap
=
2622 /* Symbol table magic number. */
2624 /* Alignment of debugging information. E.g., 4. */
2626 /* Sizes of external symbolic information. */
2627 sizeof (struct hdr_ext
),
2628 sizeof (struct dnr_ext
),
2629 sizeof (struct pdr_ext
),
2630 sizeof (struct sym_ext
),
2631 sizeof (struct opt_ext
),
2632 sizeof (struct fdr_ext
),
2633 sizeof (struct rfd_ext
),
2634 sizeof (struct ext_ext
),
2635 /* Functions to swap in external symbolic data. */
2644 _bfd_ecoff_swap_tir_in
,
2645 _bfd_ecoff_swap_rndx_in
,
2646 /* Functions to swap out external symbolic data. */
2655 _bfd_ecoff_swap_tir_out
,
2656 _bfd_ecoff_swap_rndx_out
,
2657 /* Function to read in symbolic data. */
2658 mips_elf_read_ecoff_info
2661 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
2662 #define TARGET_LITTLE_NAME "elf32-littlemips"
2663 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
2664 #define TARGET_BIG_NAME "elf32-bigmips"
2665 #define ELF_ARCH bfd_arch_mips
2666 #define ELF_MACHINE_CODE EM_MIPS
2667 #define ELF_MAXPAGESIZE 0x10000
2668 #define elf_backend_collect true
2669 #define elf_info_to_howto 0
2670 #define elf_info_to_howto_rel mips_info_to_howto_rel
2671 #define elf_backend_sym_is_global mips_elf_sym_is_global
2672 #define elf_backend_object_p mips_elf_object_p
2673 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
2674 #define elf_backend_fake_sections mips_elf_fake_sections
2675 #define elf_backend_section_from_bfd_section \
2676 mips_elf_section_from_bfd_section
2677 #define elf_backend_section_processing mips_elf_section_processing
2678 #define elf_backend_symbol_processing mips_elf_symbol_processing
2679 #define elf_backend_final_write_processing \
2680 mips_elf_final_write_processing
2681 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
2683 #define bfd_elf32_bfd_link_hash_table_create \
2684 mips_elf_link_hash_table_create
2685 #define bfd_elf32_bfd_final_link mips_elf_final_link
2686 #define elf_backend_relocate_section mips_elf_relocate_section
2687 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
2689 #include "elf32-target.h"