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 boolean mips_elf_find_nearest_line
88 PARAMS ((bfd
*, asection
*, asymbol
**, bfd_vma
, const char **,
89 const char **, unsigned int *));
90 static struct bfd_hash_entry
*mips_elf_link_hash_newfunc
91 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
92 static struct bfd_link_hash_table
*mips_elf_link_hash_table_create
94 static int gptab_compare
PARAMS ((const void *, const void *));
95 static boolean mips_elf_final_link
96 PARAMS ((bfd
*, struct bfd_link_info
*));
97 static void mips_elf_relocate_hi16
98 PARAMS ((bfd
*, Elf_Internal_Rela
*, Elf_Internal_Rela
*, bfd_byte
*,
100 static boolean mips_elf_relocate_section
101 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
102 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
103 static boolean mips_elf_add_symbol_hook
104 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
105 const char **, flagword
*, asection
**, bfd_vma
*));
107 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
112 R_MIPS_16
, R_MIPS_32
,
113 R_MIPS_REL32
, R_MIPS_26
,
114 R_MIPS_HI16
, R_MIPS_LO16
,
115 R_MIPS_GPREL16
, R_MIPS_LITERAL
,
116 R_MIPS_GOT16
, R_MIPS_PC16
,
117 R_MIPS_CALL16
, R_MIPS_GPREL32
,
118 /* The remaining relocs are defined on Irix, although they are not
119 in the MIPS ELF ABI. */
120 R_MIPS_UNUSED1
, R_MIPS_UNUSED2
,
122 R_MIPS_SHIFT5
, R_MIPS_SHIFT6
,
123 R_MIPS_64
, R_MIPS_GOT_DISP
,
124 R_MIPS_GOT_PAGE
, R_MIPS_GOT_OFST
,
125 R_MIPS_GOT_HI16
, R_MIPS_GOT_LO16
,
126 R_MIPS_SUB
, R_MIPS_INSERT_A
,
127 R_MIPS_INSERT_B
, R_MIPS_DELETE
,
128 R_MIPS_HIGHER
, R_MIPS_HIGHEST
,
129 R_MIPS_CALL_HI16
, R_MIPS_CALL_LO16
,
133 static reloc_howto_type elf_mips_howto_table
[] =
136 HOWTO (R_MIPS_NONE
, /* type */
138 0, /* size (0 = byte, 1 = short, 2 = long) */
140 false, /* pc_relative */
142 complain_overflow_dont
, /* complain_on_overflow */
143 bfd_elf_generic_reloc
, /* special_function */
144 "R_MIPS_NONE", /* name */
145 false, /* partial_inplace */
148 false), /* pcrel_offset */
150 /* 16 bit relocation. */
151 HOWTO (R_MIPS_16
, /* type */
153 1, /* size (0 = byte, 1 = short, 2 = long) */
155 false, /* pc_relative */
157 complain_overflow_bitfield
, /* complain_on_overflow */
158 bfd_elf_generic_reloc
, /* special_function */
159 "R_MIPS_16", /* name */
160 true, /* partial_inplace */
161 0xffff, /* src_mask */
162 0xffff, /* dst_mask */
163 false), /* pcrel_offset */
165 /* 32 bit relocation. */
166 HOWTO (R_MIPS_32
, /* type */
168 2, /* size (0 = byte, 1 = short, 2 = long) */
170 false, /* pc_relative */
172 complain_overflow_bitfield
, /* complain_on_overflow */
173 bfd_elf_generic_reloc
, /* special_function */
174 "R_MIPS_32", /* name */
175 true, /* partial_inplace */
176 0xffffffff, /* src_mask */
177 0xffffffff, /* dst_mask */
178 false), /* pcrel_offset */
180 /* 32 bit symbol relative relocation. */
181 HOWTO (R_MIPS_REL32
, /* type */
183 2, /* size (0 = byte, 1 = short, 2 = long) */
185 false, /* pc_relative */
187 complain_overflow_bitfield
, /* complain_on_overflow */
188 bfd_elf_generic_reloc
, /* special_function */
189 "R_MIPS_REL32", /* name */
190 true, /* partial_inplace */
191 0xffffffff, /* src_mask */
192 0xffffffff, /* dst_mask */
193 false), /* pcrel_offset */
195 /* 26 bit branch address. */
196 HOWTO (R_MIPS_26
, /* type */
198 2, /* size (0 = byte, 1 = short, 2 = long) */
200 false, /* pc_relative */
202 complain_overflow_dont
, /* complain_on_overflow */
203 /* This needs complex overflow
204 detection, because the upper four
205 bits must match the PC. */
206 bfd_elf_generic_reloc
, /* special_function */
207 "R_MIPS_26", /* name */
208 true, /* partial_inplace */
209 0x3ffffff, /* src_mask */
210 0x3ffffff, /* dst_mask */
211 false), /* pcrel_offset */
213 /* High 16 bits of symbol value. */
214 HOWTO (R_MIPS_HI16
, /* type */
216 2, /* size (0 = byte, 1 = short, 2 = long) */
218 false, /* pc_relative */
220 complain_overflow_dont
, /* complain_on_overflow */
221 mips_elf_hi16_reloc
, /* special_function */
222 "R_MIPS_HI16", /* name */
223 true, /* partial_inplace */
224 0xffff, /* src_mask */
225 0xffff, /* dst_mask */
226 false), /* pcrel_offset */
228 /* Low 16 bits of symbol value. */
229 HOWTO (R_MIPS_LO16
, /* type */
231 2, /* size (0 = byte, 1 = short, 2 = long) */
233 false, /* pc_relative */
235 complain_overflow_dont
, /* complain_on_overflow */
236 mips_elf_lo16_reloc
, /* special_function */
237 "R_MIPS_LO16", /* name */
238 true, /* partial_inplace */
239 0xffff, /* src_mask */
240 0xffff, /* dst_mask */
241 false), /* pcrel_offset */
243 /* GP relative reference. */
244 HOWTO (R_MIPS_GPREL16
, /* type */
246 2, /* size (0 = byte, 1 = short, 2 = long) */
248 false, /* pc_relative */
250 complain_overflow_signed
, /* complain_on_overflow */
251 mips_elf_gprel16_reloc
, /* special_function */
252 "R_MIPS_GPREL16", /* name */
253 true, /* partial_inplace */
254 0xffff, /* src_mask */
255 0xffff, /* dst_mask */
256 false), /* pcrel_offset */
258 /* Reference to literal section. */
259 HOWTO (R_MIPS_LITERAL
, /* type */
261 2, /* size (0 = byte, 1 = short, 2 = long) */
263 false, /* pc_relative */
265 complain_overflow_signed
, /* complain_on_overflow */
266 mips_elf_gprel16_reloc
, /* special_function */
267 "R_MIPS_LITERAL", /* name */
268 true, /* partial_inplace */
269 0xffff, /* src_mask */
270 0xffff, /* dst_mask */
271 false), /* pcrel_offset */
273 /* Reference to global offset table. */
274 /* FIXME: This is not handled correctly. */
275 HOWTO (R_MIPS_GOT16
, /* type */
277 2, /* size (0 = byte, 1 = short, 2 = long) */
279 false, /* pc_relative */
281 complain_overflow_signed
, /* complain_on_overflow */
282 mips_elf_got16_reloc
, /* special_function */
283 "R_MIPS_GOT16", /* name */
284 false, /* partial_inplace */
286 0xffff, /* dst_mask */
287 false), /* pcrel_offset */
289 /* 16 bit PC relative reference. */
290 HOWTO (R_MIPS_PC16
, /* type */
292 2, /* size (0 = byte, 1 = short, 2 = long) */
294 true, /* pc_relative */
296 complain_overflow_signed
, /* complain_on_overflow */
297 bfd_elf_generic_reloc
, /* special_function */
298 "R_MIPS_PC16", /* name */
299 true, /* partial_inplace */
300 0xffff, /* src_mask */
301 0xffff, /* dst_mask */
302 false), /* pcrel_offset */
304 /* 16 bit call through global offset table. */
305 /* FIXME: This is not handled correctly. */
306 HOWTO (R_MIPS_CALL16
, /* type */
308 2, /* size (0 = byte, 1 = short, 2 = long) */
310 false, /* pc_relative */
312 complain_overflow_signed
, /* complain_on_overflow */
313 bfd_elf_generic_reloc
, /* special_function */
314 "R_MIPS_CALL16", /* name */
315 false, /* partial_inplace */
317 0xffff, /* dst_mask */
318 false), /* pcrel_offset */
320 /* 32 bit GP relative reference. */
321 /* FIXME: This is not handled correctly. */
322 HOWTO (R_MIPS_GPREL32
, /* type */
324 2, /* size (0 = byte, 1 = short, 2 = long) */
326 false, /* pc_relative */
328 complain_overflow_bitfield
, /* complain_on_overflow */
329 bfd_elf_generic_reloc
, /* special_function */
330 "R_MIPS_GPREL32", /* name */
331 true, /* partial_inplace */
332 0xffffffff, /* src_mask */
333 0xffffffff, /* dst_mask */
334 false), /* pcrel_offset */
336 /* The remaining relocs are defined on Irix 5, although they are
337 not defined by the ABI. */
342 /* A 5 bit shift field. */
343 HOWTO (R_MIPS_SHIFT5
, /* type */
345 2, /* size (0 = byte, 1 = short, 2 = long) */
347 false, /* pc_relative */
349 complain_overflow_bitfield
, /* complain_on_overflow */
350 bfd_elf_generic_reloc
, /* special_function */
351 "R_MIPS_SHIFT5", /* name */
352 true, /* partial_inplace */
353 0x000007c0, /* src_mask */
354 0x000007c0, /* dst_mask */
355 false), /* pcrel_offset */
357 /* A 6 bit shift field. */
358 /* FIXME: This is not handled correctly; a special function is
359 needed to put the most significant bit in the right place. */
360 HOWTO (R_MIPS_SHIFT6
, /* type */
362 2, /* size (0 = byte, 1 = short, 2 = long) */
364 false, /* pc_relative */
366 complain_overflow_bitfield
, /* complain_on_overflow */
367 bfd_elf_generic_reloc
, /* special_function */
368 "R_MIPS_SHIFT6", /* name */
369 true, /* partial_inplace */
370 0x000007c4, /* src_mask */
371 0x000007c4, /* dst_mask */
372 false), /* pcrel_offset */
374 /* A 64 bit relocation. Presumably not used in 32 bit ELF. */
377 /* Displacement in the global offset table. */
378 /* FIXME: Not handled correctly. */
379 HOWTO (R_MIPS_GOT_DISP
, /* type */
381 2, /* size (0 = byte, 1 = short, 2 = long) */
383 false, /* pc_relative */
385 complain_overflow_bitfield
, /* complain_on_overflow */
386 bfd_elf_generic_reloc
, /* special_function */
387 "R_MIPS_GOT_DISP", /* name */
388 true, /* partial_inplace */
389 0x0000ffff, /* src_mask */
390 0x0000ffff, /* dst_mask */
391 false), /* pcrel_offset */
393 /* Displacement to page pointer in the global offset table. */
394 /* FIXME: Not handled correctly. */
395 HOWTO (R_MIPS_GOT_PAGE
, /* type */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
399 false, /* pc_relative */
401 complain_overflow_bitfield
, /* complain_on_overflow */
402 bfd_elf_generic_reloc
, /* special_function */
403 "R_MIPS_GOT_PAGE", /* name */
404 true, /* partial_inplace */
405 0x0000ffff, /* src_mask */
406 0x0000ffff, /* dst_mask */
407 false), /* pcrel_offset */
409 /* Offset from page pointer in the global offset table. */
410 /* FIXME: Not handled correctly. */
411 HOWTO (R_MIPS_GOT_OFST
, /* type */
413 2, /* size (0 = byte, 1 = short, 2 = long) */
415 false, /* pc_relative */
417 complain_overflow_bitfield
, /* complain_on_overflow */
418 bfd_elf_generic_reloc
, /* special_function */
419 "R_MIPS_GOT_OFST", /* name */
420 true, /* partial_inplace */
421 0x0000ffff, /* src_mask */
422 0x0000ffff, /* dst_mask */
423 false), /* pcrel_offset */
425 /* High 16 bits of displacement in global offset table. */
426 /* FIXME: Not handled correctly. */
427 HOWTO (R_MIPS_GOT_HI16
, /* type */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
431 false, /* pc_relative */
433 complain_overflow_dont
, /* complain_on_overflow */
434 bfd_elf_generic_reloc
, /* special_function */
435 "R_MIPS_GOT_HI16", /* name */
436 true, /* partial_inplace */
437 0x0000ffff, /* src_mask */
438 0x0000ffff, /* dst_mask */
439 false), /* pcrel_offset */
441 /* Low 16 bits of displacement in global offset table. */
442 /* FIXME: Not handled correctly. */
443 HOWTO (R_MIPS_GOT_LO16
, /* type */
445 2, /* size (0 = byte, 1 = short, 2 = long) */
447 false, /* pc_relative */
449 complain_overflow_dont
, /* complain_on_overflow */
450 bfd_elf_generic_reloc
, /* special_function */
451 "R_MIPS_GOT_LO16", /* name */
452 true, /* partial_inplace */
453 0x0000ffff, /* src_mask */
454 0x0000ffff, /* dst_mask */
455 false), /* pcrel_offset */
457 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
460 /* Used to cause the linker to insert and delete instructions? */
465 /* Get the higher values of a 64 bit addend. Presumably not used in
470 /* High 16 bits of displacement in global offset table. */
471 /* FIXME: Not handled correctly. */
472 HOWTO (R_MIPS_CALL_HI16
, /* type */
474 2, /* size (0 = byte, 1 = short, 2 = long) */
476 false, /* pc_relative */
478 complain_overflow_dont
, /* complain_on_overflow */
479 bfd_elf_generic_reloc
, /* special_function */
480 "R_MIPS_CALL_HI16", /* name */
481 true, /* partial_inplace */
482 0x0000ffff, /* src_mask */
483 0x0000ffff, /* dst_mask */
484 false), /* pcrel_offset */
486 /* Low 16 bits of displacement in global offset table. */
487 /* FIXME: Not handled correctly. */
488 HOWTO (R_MIPS_CALL_LO16
, /* type */
490 2, /* size (0 = byte, 1 = short, 2 = long) */
492 false, /* pc_relative */
494 complain_overflow_dont
, /* complain_on_overflow */
495 bfd_elf_generic_reloc
, /* special_function */
496 "R_MIPS_CALL_LO16", /* name */
497 true, /* partial_inplace */
498 0x0000ffff, /* src_mask */
499 0x0000ffff, /* dst_mask */
500 false) /* pcrel_offset */
503 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
504 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
505 the HI16. Here we just save the information we need; we do the
506 actual relocation when we see the LO16. MIPS ELF requires that the
507 LO16 immediately follow the HI16, so this ought to work. */
509 static bfd_byte
*mips_hi16_addr
;
510 static bfd_vma mips_hi16_addend
;
512 static bfd_reloc_status_type
513 mips_elf_hi16_reloc (abfd
,
521 arelent
*reloc_entry
;
524 asection
*input_section
;
526 char **error_message
;
528 bfd_reloc_status_type ret
;
531 /* If we're relocating, and this an external symbol, we don't want
532 to change anything. */
533 if (output_bfd
!= (bfd
*) NULL
534 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
535 && reloc_entry
->addend
== 0)
537 reloc_entry
->address
+= input_section
->output_offset
;
541 /* FIXME: The symbol _gp_disp requires special handling, which we do
543 if (strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
547 if (bfd_is_und_section (symbol
->section
)
548 && output_bfd
== (bfd
*) NULL
)
549 ret
= bfd_reloc_undefined
;
551 if (bfd_is_com_section (symbol
->section
))
554 relocation
= symbol
->value
;
556 relocation
+= symbol
->section
->output_section
->vma
;
557 relocation
+= symbol
->section
->output_offset
;
558 relocation
+= reloc_entry
->addend
;
560 if (reloc_entry
->address
> input_section
->_cooked_size
)
561 return bfd_reloc_outofrange
;
563 /* Save the information, and let LO16 do the actual relocation. */
564 mips_hi16_addr
= (bfd_byte
*) data
+ reloc_entry
->address
;
565 mips_hi16_addend
= relocation
;
567 if (output_bfd
!= (bfd
*) NULL
)
568 reloc_entry
->address
+= input_section
->output_offset
;
573 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
574 inplace relocation; this function exists in order to do the
575 R_MIPS_HI16 relocation described above. */
577 static bfd_reloc_status_type
578 mips_elf_lo16_reloc (abfd
,
586 arelent
*reloc_entry
;
589 asection
*input_section
;
591 char **error_message
;
593 /* FIXME: The symbol _gp_disp requires special handling, which we do
595 if (output_bfd
== (bfd
*) NULL
596 && strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
599 if (mips_hi16_addr
!= (bfd_byte
*) NULL
)
605 /* Do the HI16 relocation. Note that we actually don't need to
606 know anything about the LO16 itself, except where to find the
607 low 16 bits of the addend needed by the LO16. */
608 insn
= bfd_get_32 (abfd
, mips_hi16_addr
);
609 vallo
= (bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
)
611 val
= ((insn
& 0xffff) << 16) + vallo
;
612 val
+= mips_hi16_addend
;
614 /* The low order 16 bits are always treated as a signed value.
615 Therefore, a negative value in the low order bits requires an
616 adjustment in the high order bits. We need to make this
617 adjustment in two ways: once for the bits we took from the
618 data, and once for the bits we are putting back in to the
620 if ((vallo
& 0x8000) != 0)
622 if ((val
& 0x8000) != 0)
625 insn
= (insn
&~ 0xffff) | ((val
>> 16) & 0xffff);
626 bfd_put_32 (abfd
, insn
, mips_hi16_addr
);
628 mips_hi16_addr
= (bfd_byte
*) NULL
;
631 /* Now do the LO16 reloc in the usual way. */
632 return bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
633 input_section
, output_bfd
, error_message
);
636 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
637 table used for PIC code. If the symbol is an external symbol, the
638 instruction is modified to contain the offset of the appropriate
639 entry in the global offset table. If the symbol is a section
640 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
641 addends are combined to form the real addend against the section
642 symbol; the GOT16 is modified to contain the offset of an entry in
643 the global offset table, and the LO16 is modified to offset it
644 appropriately. Thus an offset larger than 16 bits requires a
645 modified value in the global offset table.
647 This implementation suffices for the assembler, but the linker does
648 not yet know how to create global offset tables. */
650 static bfd_reloc_status_type
651 mips_elf_got16_reloc (abfd
,
659 arelent
*reloc_entry
;
662 asection
*input_section
;
664 char **error_message
;
666 /* If we're relocating, and this an external symbol, we don't want
667 to change anything. */
668 if (output_bfd
!= (bfd
*) NULL
669 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
670 && reloc_entry
->addend
== 0)
672 reloc_entry
->address
+= input_section
->output_offset
;
676 /* If we're relocating, and this is a local symbol, we can handle it
678 if (output_bfd
!= (bfd
*) NULL
679 && (symbol
->flags
& BSF_SECTION_SYM
) != 0)
680 return mips_elf_hi16_reloc (abfd
, reloc_entry
, symbol
, data
,
681 input_section
, output_bfd
, error_message
);
686 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
687 become the offset from the gp register. This function also handles
688 R_MIPS_LITERAL relocations, although those can be handled more
689 cleverly because the entries in the .lit8 and .lit4 sections can be
692 static bfd_reloc_status_type gprel16_with_gp
PARAMS ((bfd
*, asymbol
*,
693 arelent
*, asection
*,
694 boolean
, PTR
, bfd_vma
));
696 static bfd_reloc_status_type
697 mips_elf_gprel16_reloc (abfd
,
705 arelent
*reloc_entry
;
708 asection
*input_section
;
710 char **error_message
;
712 boolean relocateable
;
714 /* If we're relocating, and this is an external symbol with no
715 addend, we don't want to change anything. We will only have an
716 addend if this is a newly created reloc, not read from an ELF
718 if (output_bfd
!= (bfd
*) NULL
719 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
720 && reloc_entry
->addend
== 0)
722 reloc_entry
->address
+= input_section
->output_offset
;
726 if (output_bfd
!= (bfd
*) NULL
)
730 relocateable
= false;
731 output_bfd
= symbol
->section
->output_section
->owner
;
734 if (bfd_is_und_section (symbol
->section
)
735 && relocateable
== false)
736 return bfd_reloc_undefined
;
738 /* Some of the code below assumes the output bfd is ELF too. */
739 if (output_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
742 /* We have to figure out the gp value, so that we can adjust the
743 symbol value correctly. We look up the symbol _gp in the output
744 BFD. If we can't find it, we're stuck. We cache it in the ELF
745 target data. We don't need to adjust the symbol value for an
746 external symbol if we are producing relocateable output. */
747 if (elf_gp (output_bfd
) == 0
748 && (relocateable
== false
749 || (symbol
->flags
& BSF_SECTION_SYM
) != 0))
751 if (relocateable
!= false)
753 /* Make up a value. */
754 elf_gp (output_bfd
) =
755 symbol
->section
->output_section
->vma
+ 0x4000;
763 count
= bfd_get_symcount (output_bfd
);
764 sym
= bfd_get_outsymbols (output_bfd
);
766 if (sym
== (asymbol
**) NULL
)
770 for (i
= 0; i
< count
; i
++, sym
++)
772 register CONST
char *name
;
774 name
= bfd_asymbol_name (*sym
);
775 if (*name
== '_' && strcmp (name
, "_gp") == 0)
777 elf_gp (output_bfd
) = bfd_asymbol_value (*sym
);
785 /* Only get the error once. */
786 elf_gp (output_bfd
) = 4;
788 (char *) "GP relative relocation when _gp not defined";
789 return bfd_reloc_dangerous
;
794 return gprel16_with_gp (symbol
, reloc_entry
, input_section
, relocateable
,
795 data
, elf_gp (output_bfd
));
798 static bfd_reloc_status_type
799 gprel16_with_gp (abfd
, symbol
, reloc_entry
, input_section
, relocateable
, data
,
803 arelent
*reloc_entry
;
804 asection
*input_section
;
805 boolean relocateable
;
813 if (bfd_is_com_section (symbol
->section
))
816 relocation
= symbol
->value
;
818 relocation
+= symbol
->section
->output_section
->vma
;
819 relocation
+= symbol
->section
->output_offset
;
821 if (reloc_entry
->address
> input_section
->_cooked_size
)
822 return bfd_reloc_outofrange
;
824 insn
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
);
826 /* Set val to the offset into the section or symbol. */
827 val
= ((insn
& 0xffff) + reloc_entry
->addend
) & 0xffff;
831 /* Adjust val for the final section location and GP value. If we
832 are producing relocateable output, we don't want to do this for
833 an external symbol. */
834 if (relocateable
== false
835 || (symbol
->flags
& BSF_SECTION_SYM
) != 0)
836 val
+= relocation
- gp
;
838 insn
= (insn
&~ 0xffff) | (val
& 0xffff);
839 bfd_put_32 (abfd
, insn
, (bfd_byte
*) data
+ reloc_entry
->address
);
841 if (relocateable
!= false)
842 reloc_entry
->address
+= input_section
->output_offset
;
844 /* Make sure it fit in 16 bits. */
845 if (val
>= 0x8000 && val
< 0xffff8000)
846 return bfd_reloc_overflow
;
851 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
853 struct elf_reloc_map
{
854 bfd_reloc_code_real_type bfd_reloc_val
;
855 enum reloc_type elf_reloc_val
;
858 static CONST
struct elf_reloc_map mips_reloc_map
[] =
860 { BFD_RELOC_NONE
, R_MIPS_NONE
, },
861 { BFD_RELOC_16
, R_MIPS_16
},
862 { BFD_RELOC_32
, R_MIPS_32
},
863 { BFD_RELOC_CTOR
, R_MIPS_32
},
864 { BFD_RELOC_32_PCREL
, R_MIPS_REL32
},
865 { BFD_RELOC_MIPS_JMP
, R_MIPS_26
},
866 { BFD_RELOC_HI16_S
, R_MIPS_HI16
},
867 { BFD_RELOC_LO16
, R_MIPS_LO16
},
868 { BFD_RELOC_MIPS_GPREL
, R_MIPS_GPREL16
},
869 { BFD_RELOC_MIPS_LITERAL
, R_MIPS_LITERAL
},
870 { BFD_RELOC_MIPS_GOT16
, R_MIPS_GOT16
},
871 { BFD_RELOC_16_PCREL
, R_MIPS_PC16
},
872 { BFD_RELOC_MIPS_CALL16
, R_MIPS_CALL16
},
873 { BFD_RELOC_MIPS_GPREL32
, R_MIPS_GPREL32
}
876 /* Given a BFD reloc type, return a howto structure. */
878 static reloc_howto_type
*
879 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
881 bfd_reloc_code_real_type code
;
885 for (i
= 0; i
< sizeof (mips_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
887 if (mips_reloc_map
[i
].bfd_reloc_val
== code
)
888 return &elf_mips_howto_table
[(int) mips_reloc_map
[i
].elf_reloc_val
];
893 /* Given a MIPS reloc type, fill in an arelent structure. */
896 mips_info_to_howto_rel (abfd
, cache_ptr
, dst
)
899 Elf32_Internal_Rel
*dst
;
903 r_type
= ELF32_R_TYPE (dst
->r_info
);
904 BFD_ASSERT (r_type
< (unsigned int) R_MIPS_max
);
905 cache_ptr
->howto
= &elf_mips_howto_table
[r_type
];
907 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
908 value for the object file. We get the addend now, rather than
909 when we do the relocation, because the symbol manipulations done
910 by the linker may cause us to lose track of the input BFD. */
911 if (((*cache_ptr
->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) != 0
912 && (r_type
== (unsigned int) R_MIPS_GPREL16
913 || r_type
== (unsigned int) R_MIPS_LITERAL
))
914 cache_ptr
->addend
= elf_gp (abfd
);
917 /* A .reginfo section holds a single Elf32_RegInfo structure. These
918 routines swap this structure in and out. They are used outside of
919 BFD, so they are globally visible. */
922 bfd_mips_elf32_swap_reginfo_in (abfd
, ex
, in
)
924 const Elf32_External_RegInfo
*ex
;
927 in
->ri_gprmask
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gprmask
);
928 in
->ri_cprmask
[0] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[0]);
929 in
->ri_cprmask
[1] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[1]);
930 in
->ri_cprmask
[2] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[2]);
931 in
->ri_cprmask
[3] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[3]);
932 in
->ri_gp_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gp_value
);
936 bfd_mips_elf32_swap_reginfo_out (abfd
, in
, ex
)
938 const Elf32_RegInfo
*in
;
939 Elf32_External_RegInfo
*ex
;
941 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gprmask
,
942 (bfd_byte
*) ex
->ri_gprmask
);
943 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[0],
944 (bfd_byte
*) ex
->ri_cprmask
[0]);
945 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[1],
946 (bfd_byte
*) ex
->ri_cprmask
[1]);
947 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[2],
948 (bfd_byte
*) ex
->ri_cprmask
[2]);
949 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[3],
950 (bfd_byte
*) ex
->ri_cprmask
[3]);
951 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gp_value
,
952 (bfd_byte
*) ex
->ri_gp_value
);
955 /* Swap an entry in a .gptab section. Note that these routines rely
956 on the equivalence of the two elements of the union. */
959 bfd_mips_elf32_swap_gptab_in (abfd
, ex
, in
)
961 const Elf32_External_gptab
*ex
;
964 in
->gt_entry
.gt_g_value
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_g_value
);
965 in
->gt_entry
.gt_bytes
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_bytes
);
969 bfd_mips_elf32_swap_gptab_out (abfd
, in
, ex
)
971 const Elf32_gptab
*in
;
972 Elf32_External_gptab
*ex
;
974 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_g_value
,
975 ex
->gt_entry
.gt_g_value
);
976 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_bytes
,
977 ex
->gt_entry
.gt_bytes
);
980 /* Determine whether a symbol is global for the purposes of splitting
981 the symbol table into global symbols and local symbols. At least
982 on Irix 5, this split must be between section symbols and all other
983 symbols. On most ELF targets the split is between static symbols
984 and externally visible symbols. */
988 mips_elf_sym_is_global (abfd
, sym
)
992 return (sym
->flags
& BSF_SECTION_SYM
) == 0 ? true : false;
995 /* Set the right machine number for a MIPS ELF file. */
998 mips_elf_object_p (abfd
)
1001 switch (elf_elfheader (abfd
)->e_flags
& EF_MIPS_ARCH
)
1005 /* Just use the default, which was set in elfcode.h. */
1009 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 6000);
1013 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 4000);
1017 /* Irix 5 is broken. Object file symbol tables are not always
1018 sorted correctly such that local symbols precede global symbols,
1019 and the sh_info field in the symbol table is not always right.
1020 We try to quickly check whether the symbol table is broken for
1021 this BFD, and, if it is, we set elf_bad_symtab in tdata. */
1022 if (elf_onesymtab (abfd
) != 0)
1024 Elf_Internal_Shdr
*symtab_hdr
;
1025 Elf32_External_Sym esym
;
1027 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1029 (symtab_hdr
->sh_offset
1030 + symtab_hdr
->sh_size
1031 - sizeof (Elf32_External_Sym
)),
1033 || (bfd_read ((PTR
) &esym
, 1, sizeof (Elf32_External_Sym
), abfd
)
1034 != sizeof (Elf32_External_Sym
)))
1036 if (ELF_ST_BIND (bfd_h_get_8 (abfd
, (bfd_byte
*) esym
.st_info
))
1038 elf_bad_symtab (abfd
) = true;
1044 /* The final processing done just before writing out a MIPS ELF object
1045 file. This gets the MIPS architecture right based on the machine
1050 mips_elf_final_write_processing (abfd
, linker
)
1056 Elf_Internal_Shdr
**hdrpp
;
1058 switch (bfd_get_mach (abfd
))
1061 val
= E_MIPS_ARCH_1
;
1065 val
= E_MIPS_ARCH_2
;
1069 val
= E_MIPS_ARCH_3
;
1076 elf_elfheader (abfd
)->e_flags
&=~ EF_MIPS_ARCH
;
1077 elf_elfheader (abfd
)->e_flags
|= val
;
1079 /* Set the sh_info field for .gptab sections. */
1080 for (i
= 1, hdrpp
= elf_elfsections (abfd
) + 1;
1081 i
< elf_elfheader (abfd
)->e_shnum
;
1084 if ((*hdrpp
)->sh_type
== SHT_MIPS_GPTAB
)
1089 BFD_ASSERT ((*hdrpp
)->bfd_section
!= NULL
);
1090 name
= bfd_get_section_name (abfd
, (*hdrpp
)->bfd_section
);
1091 BFD_ASSERT (name
!= NULL
1092 && strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0);
1093 sec
= bfd_get_section_by_name (abfd
, name
+ sizeof ".gptab" - 1);
1094 BFD_ASSERT (sec
!= NULL
);
1095 (*hdrpp
)->sh_info
= elf_section_data (sec
)->this_idx
;
1100 /* Handle a MIPS specific section when reading an object file. This
1101 is called when elfcode.h finds a section with an unknown type.
1102 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1106 mips_elf_section_from_shdr (abfd
, hdr
, name
)
1108 Elf32_Internal_Shdr
*hdr
;
1113 /* There ought to be a place to keep ELF backend specific flags, but
1114 at the moment there isn't one. We just keep track of the
1115 sections by their name, instead. Fortunately, the ABI gives
1116 suggested names for all the MIPS specific sections, so we will
1117 probably get away with this. */
1118 switch (hdr
->sh_type
)
1120 case SHT_MIPS_LIBLIST
:
1121 if (strcmp (name
, ".liblist") != 0)
1125 if (strcmp (name
, ".msym") != 0)
1128 case SHT_MIPS_CONFLICT
:
1129 if (strcmp (name
, ".conflict") != 0)
1132 case SHT_MIPS_GPTAB
:
1133 if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) != 0)
1136 case SHT_MIPS_UCODE
:
1137 if (strcmp (name
, ".ucode") != 0)
1140 case SHT_MIPS_DEBUG
:
1141 if (strcmp (name
, ".mdebug") != 0)
1144 case SHT_MIPS_REGINFO
:
1145 if (strcmp (name
, ".reginfo") != 0
1146 || hdr
->sh_size
!= sizeof (Elf32_External_RegInfo
))
1149 case SHT_MIPS_OPTIONS
:
1150 if (strcmp (name
, ".options") != 0)
1153 case SHT_MIPS_DWARF
:
1154 if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) != 0)
1157 case SHT_MIPS_EVENTS
:
1158 if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1165 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1167 newsect
= hdr
->bfd_section
;
1169 if (hdr
->sh_type
== SHT_MIPS_DEBUG
)
1171 if (! bfd_set_section_flags (abfd
, newsect
,
1172 (bfd_get_section_flags (abfd
, newsect
)
1177 /* FIXME: We should record sh_info for a .gptab section. */
1179 /* For a .reginfo section, set the gp value in the tdata information
1180 from the contents of this section. We need the gp value while
1181 processing relocs, so we just get it now. */
1182 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1184 Elf32_External_RegInfo ext
;
1187 if (! bfd_get_section_contents (abfd
, newsect
, (PTR
) &ext
,
1188 (file_ptr
) 0, sizeof ext
))
1190 bfd_mips_elf32_swap_reginfo_in (abfd
, &ext
, &s
);
1191 elf_gp (abfd
) = s
.ri_gp_value
;
1197 /* Set the correct type for a MIPS ELF section. We do this by the
1198 section name, which is a hack, but ought to work. */
1201 mips_elf_fake_sections (abfd
, hdr
, sec
)
1203 Elf32_Internal_Shdr
*hdr
;
1206 register const char *name
;
1208 name
= bfd_get_section_name (abfd
, sec
);
1210 if (strcmp (name
, ".liblist") == 0)
1212 hdr
->sh_type
= SHT_MIPS_LIBLIST
;
1213 hdr
->sh_info
= sec
->_raw_size
/ sizeof (Elf32_Lib
);
1214 /* FIXME: Set the sh_link field. */
1216 else if (strcmp (name
, ".msym") == 0)
1218 hdr
->sh_type
= SHT_MIPS_MSYM
;
1219 hdr
->sh_entsize
= 8;
1220 /* FIXME: Set the sh_info field. */
1222 else if (strcmp (name
, ".conflict") == 0)
1223 hdr
->sh_type
= SHT_MIPS_CONFLICT
;
1224 else if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0)
1226 hdr
->sh_type
= SHT_MIPS_GPTAB
;
1227 hdr
->sh_entsize
= sizeof (Elf32_External_gptab
);
1228 /* The sh_info field is set in mips_elf_final_write_processing. */
1230 else if (strcmp (name
, ".ucode") == 0)
1231 hdr
->sh_type
= SHT_MIPS_UCODE
;
1232 else if (strcmp (name
, ".mdebug") == 0)
1234 hdr
->sh_type
= SHT_MIPS_DEBUG
;
1235 hdr
->sh_entsize
= 1;
1237 else if (strcmp (name
, ".reginfo") == 0)
1239 hdr
->sh_type
= SHT_MIPS_REGINFO
;
1240 hdr
->sh_entsize
= 1;
1242 /* Force the section size to the correct value, even if the
1243 linker thinks it is larger. The link routine below will only
1244 write out this much data for .reginfo. */
1245 hdr
->sh_size
= sec
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1247 else if (strcmp (name
, ".options") == 0)
1249 hdr
->sh_type
= SHT_MIPS_OPTIONS
;
1250 hdr
->sh_entsize
= 1;
1252 else if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) == 0)
1253 hdr
->sh_type
= SHT_MIPS_DWARF
;
1254 else if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1255 hdr
->sh_type
= SHT_MIPS_EVENTS
;
1260 /* Given a BFD section, try to locate the corresponding ELF section
1264 mips_elf_section_from_bfd_section (abfd
, hdr
, sec
, retval
)
1266 Elf32_Internal_Shdr
*hdr
;
1270 if (strcmp (bfd_get_section_name (abfd
, sec
), ".scommon") == 0)
1272 *retval
= SHN_MIPS_SCOMMON
;
1275 if (strcmp (bfd_get_section_name (abfd
, sec
), ".acommon") == 0)
1277 *retval
= SHN_MIPS_ACOMMON
;
1283 /* Work over a section just before writing it out. We update the GP
1284 value in the .reginfo section based on the value we are using.
1285 FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
1286 name; there has to be a better way. */
1289 mips_elf_section_processing (abfd
, hdr
)
1291 Elf32_Internal_Shdr
*hdr
;
1293 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1297 BFD_ASSERT (hdr
->sh_size
== sizeof (Elf32_External_RegInfo
));
1298 BFD_ASSERT (hdr
->contents
== NULL
);
1301 hdr
->sh_offset
+ sizeof (Elf32_External_RegInfo
) - 4,
1304 bfd_h_put_32 (abfd
, (bfd_vma
) elf_gp (abfd
), buf
);
1305 if (bfd_write (buf
, (bfd_size_type
) 1, (bfd_size_type
) 4, abfd
) != 4)
1309 if (hdr
->bfd_section
!= NULL
)
1311 const char *name
= bfd_get_section_name (abfd
, hdr
->bfd_section
);
1313 if (strcmp (name
, ".sdata") == 0)
1315 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1316 hdr
->sh_type
= SHT_PROGBITS
;
1318 else if (strcmp (name
, ".sbss") == 0)
1320 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1321 hdr
->sh_type
= SHT_NOBITS
;
1323 else if (strcmp (name
, ".lit8") == 0
1324 || strcmp (name
, ".lit4") == 0)
1326 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1327 hdr
->sh_type
= SHT_PROGBITS
;
1334 /* MIPS ELF uses two common sections. One is the usual one, and the
1335 other is for small objects. All the small objects are kept
1336 together, and then referenced via the gp pointer, which yields
1337 faster assembler code. This is what we use for the small common
1338 section. This approach is copied from ecoff.c. */
1339 static asection mips_elf_scom_section
;
1340 static asymbol mips_elf_scom_symbol
;
1341 static asymbol
*mips_elf_scom_symbol_ptr
;
1343 /* MIPS ELF also uses an acommon section, which represents an
1344 allocated common symbol which may be overridden by a
1345 definition in a shared library. */
1346 static asection mips_elf_acom_section
;
1347 static asymbol mips_elf_acom_symbol
;
1348 static asymbol
*mips_elf_acom_symbol_ptr
;
1350 /* Handle the special MIPS section numbers that a symbol may use. */
1353 mips_elf_symbol_processing (abfd
, asym
)
1357 elf_symbol_type
*elfsym
;
1359 elfsym
= (elf_symbol_type
*) asym
;
1360 switch (elfsym
->internal_elf_sym
.st_shndx
)
1362 case SHN_MIPS_ACOMMON
:
1363 /* This section is used in a dynamically linked executable file.
1364 It is an allocated common section. The dynamic linker can
1365 either resolve these symbols to something in a shared
1366 library, or it can just leave them here. For our purposes,
1367 we can consider these symbols to be in a new section. */
1368 if (mips_elf_acom_section
.name
== NULL
)
1370 /* Initialize the acommon section. */
1371 mips_elf_acom_section
.name
= ".acommon";
1372 mips_elf_acom_section
.flags
= SEC_NO_FLAGS
;
1373 mips_elf_acom_section
.output_section
= &mips_elf_acom_section
;
1374 mips_elf_acom_section
.symbol
= &mips_elf_acom_symbol
;
1375 mips_elf_acom_section
.symbol_ptr_ptr
= &mips_elf_acom_symbol_ptr
;
1376 mips_elf_acom_symbol
.name
= ".acommon";
1377 mips_elf_acom_symbol
.flags
= BSF_SECTION_SYM
;
1378 mips_elf_acom_symbol
.section
= &mips_elf_acom_section
;
1379 mips_elf_acom_symbol_ptr
= &mips_elf_acom_symbol
;
1381 asym
->section
= &mips_elf_acom_section
;
1385 /* Common symbols less than the GP size are automatically
1386 treated as SHN_MIPS_SCOMMON symbols. */
1387 if (asym
->value
> elf_gp_size (abfd
))
1390 case SHN_MIPS_SCOMMON
:
1391 if (mips_elf_scom_section
.name
== NULL
)
1393 /* Initialize the small common section. */
1394 mips_elf_scom_section
.name
= ".scommon";
1395 mips_elf_scom_section
.flags
= SEC_IS_COMMON
;
1396 mips_elf_scom_section
.output_section
= &mips_elf_scom_section
;
1397 mips_elf_scom_section
.symbol
= &mips_elf_scom_symbol
;
1398 mips_elf_scom_section
.symbol_ptr_ptr
= &mips_elf_scom_symbol_ptr
;
1399 mips_elf_scom_symbol
.name
= ".scommon";
1400 mips_elf_scom_symbol
.flags
= BSF_SECTION_SYM
;
1401 mips_elf_scom_symbol
.section
= &mips_elf_scom_section
;
1402 mips_elf_scom_symbol_ptr
= &mips_elf_scom_symbol
;
1404 asym
->section
= &mips_elf_scom_section
;
1405 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
1408 case SHN_MIPS_SUNDEFINED
:
1409 asym
->section
= bfd_und_section_ptr
;
1414 /* Read ECOFF debugging information from a .mdebug section into a
1415 ecoff_debug_info structure. */
1418 mips_elf_read_ecoff_info (abfd
, section
, debug
)
1421 struct ecoff_debug_info
*debug
;
1424 const struct ecoff_debug_swap
*swap
;
1425 char *ext_hdr
= NULL
;
1427 swap
= get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
1429 ext_hdr
= (char *) malloc (swap
->external_hdr_size
);
1430 if (ext_hdr
== NULL
&& swap
->external_hdr_size
!= 0)
1432 bfd_set_error (bfd_error_no_memory
);
1436 if (bfd_get_section_contents (abfd
, section
, ext_hdr
, (file_ptr
) 0,
1437 swap
->external_hdr_size
)
1441 symhdr
= &debug
->symbolic_header
;
1442 (*swap
->swap_hdr_in
) (abfd
, ext_hdr
, symhdr
);
1444 /* The symbolic header contains absolute file offsets and sizes to
1446 #define READ(ptr, offset, count, size, type) \
1447 if (symhdr->count == 0) \
1448 debug->ptr = NULL; \
1451 debug->ptr = (type) malloc (size * symhdr->count); \
1452 if (debug->ptr == NULL) \
1454 bfd_set_error (bfd_error_no_memory); \
1455 goto error_return; \
1457 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
1458 || (bfd_read (debug->ptr, size, symhdr->count, \
1459 abfd) != size * symhdr->count)) \
1460 goto error_return; \
1463 READ (line
, cbLineOffset
, cbLine
, sizeof (unsigned char), unsigned char *);
1464 READ (external_dnr
, cbDnOffset
, idnMax
, swap
->external_dnr_size
, PTR
);
1465 READ (external_pdr
, cbPdOffset
, ipdMax
, swap
->external_pdr_size
, PTR
);
1466 READ (external_sym
, cbSymOffset
, isymMax
, swap
->external_sym_size
, PTR
);
1467 READ (external_opt
, cbOptOffset
, ioptMax
, swap
->external_opt_size
, PTR
);
1468 READ (external_aux
, cbAuxOffset
, iauxMax
, sizeof (union aux_ext
),
1470 READ (ss
, cbSsOffset
, issMax
, sizeof (char), char *);
1471 READ (ssext
, cbSsExtOffset
, issExtMax
, sizeof (char), char *);
1472 READ (external_fdr
, cbFdOffset
, ifdMax
, swap
->external_fdr_size
, PTR
);
1473 READ (external_rfd
, cbRfdOffset
, crfd
, swap
->external_rfd_size
, PTR
);
1474 READ (external_ext
, cbExtOffset
, iextMax
, swap
->external_ext_size
, PTR
);
1478 debug
->adjust
= NULL
;
1483 if (ext_hdr
!= NULL
)
1485 if (debug
->line
!= NULL
)
1487 if (debug
->external_dnr
!= NULL
)
1488 free (debug
->external_dnr
);
1489 if (debug
->external_pdr
!= NULL
)
1490 free (debug
->external_pdr
);
1491 if (debug
->external_sym
!= NULL
)
1492 free (debug
->external_sym
);
1493 if (debug
->external_opt
!= NULL
)
1494 free (debug
->external_opt
);
1495 if (debug
->external_aux
!= NULL
)
1496 free (debug
->external_aux
);
1497 if (debug
->ss
!= NULL
)
1499 if (debug
->ssext
!= NULL
)
1500 free (debug
->ssext
);
1501 if (debug
->external_fdr
!= NULL
)
1502 free (debug
->external_fdr
);
1503 if (debug
->external_rfd
!= NULL
)
1504 free (debug
->external_rfd
);
1505 if (debug
->external_ext
!= NULL
)
1506 free (debug
->external_ext
);
1510 /* MIPS ELF uses a special find_nearest_line routine in order the
1511 handle the ECOFF debugging information. */
1513 struct mips_elf_find_line
1515 struct ecoff_debug_info d
;
1516 struct ecoff_find_line i
;
1520 mips_elf_find_nearest_line (abfd
, section
, symbols
, offset
, filename_ptr
,
1521 functionname_ptr
, line_ptr
)
1526 const char **filename_ptr
;
1527 const char **functionname_ptr
;
1528 unsigned int *line_ptr
;
1532 msec
= bfd_get_section_by_name (abfd
, ".mdebug");
1535 struct mips_elf_find_line
*fi
;
1536 const struct ecoff_debug_swap
* const swap
=
1537 get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
1539 fi
= elf_tdata (abfd
)->find_line_info
;
1542 bfd_size_type external_fdr_size
;
1545 struct fdr
*fdr_ptr
;
1547 fi
= ((struct mips_elf_find_line
*)
1548 bfd_alloc (abfd
, sizeof (struct mips_elf_find_line
)));
1551 bfd_set_error (bfd_error_no_memory
);
1555 memset (fi
, 0, sizeof (struct mips_elf_find_line
));
1557 if (! mips_elf_read_ecoff_info (abfd
, msec
, &fi
->d
))
1560 /* Swap in the FDR information. */
1561 fi
->d
.fdr
= ((struct fdr
*)
1563 (fi
->d
.symbolic_header
.ifdMax
*
1564 sizeof (struct fdr
))));
1565 if (fi
->d
.fdr
== NULL
)
1567 bfd_set_error (bfd_error_no_memory
);
1570 external_fdr_size
= swap
->external_fdr_size
;
1571 fdr_ptr
= fi
->d
.fdr
;
1572 fraw_src
= (char *) fi
->d
.external_fdr
;
1573 fraw_end
= (fraw_src
1574 + fi
->d
.symbolic_header
.ifdMax
* external_fdr_size
);
1575 for (; fraw_src
< fraw_end
; fraw_src
+= external_fdr_size
, fdr_ptr
++)
1576 (*swap
->swap_fdr_in
) (abfd
, (PTR
) fraw_src
, fdr_ptr
);
1578 elf_tdata (abfd
)->find_line_info
= fi
;
1580 /* Note that we don't bother to ever free this information.
1581 find_nearest_line is either called all the time, as in
1582 objdump -l, so the information should be saved, or it is
1583 rarely called, as in ld error messages, so the memory
1584 wasted is unimportant. Still, it would probably be a
1585 good idea for free_cached_info to throw it away. */
1588 if (_bfd_ecoff_locate_line (abfd
, section
, offset
, &fi
->d
, swap
,
1589 &fi
->i
, filename_ptr
, functionname_ptr
,
1594 /* Fall back on the generic ELF find_nearest_line routine. */
1596 return bfd_elf32_find_nearest_line (abfd
, section
, symbols
, offset
,
1597 filename_ptr
, functionname_ptr
,
1601 /* The MIPS ELF linker needs additional information for each symbol in
1602 the global hash table. */
1604 struct mips_elf_link_hash_entry
1606 struct elf_link_hash_entry root
;
1608 /* External symbol information. */
1612 /* MIPS ELF linker hash table. */
1614 struct mips_elf_link_hash_table
1616 struct elf_link_hash_table root
;
1619 /* Look up an entry in a MIPS ELF linker hash table. */
1621 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1622 ((struct mips_elf_link_hash_entry *) \
1623 elf_link_hash_lookup (&(table)->root, (string), (create), \
1626 /* Traverse a MIPS ELF linker hash table. */
1628 #define mips_elf_link_hash_traverse(table, func, info) \
1629 (elf_link_hash_traverse \
1631 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1634 /* Get the MIPS ELF linker hash table from a link_info structure. */
1636 #define mips_elf_hash_table(p) \
1637 ((struct mips_elf_link_hash_table *) ((p)->hash))
1639 static boolean mips_elf_output_extsym
1640 PARAMS ((struct mips_elf_link_hash_entry
*, PTR
));
1642 /* Create an entry in a MIPS ELF linker hash table. */
1644 static struct bfd_hash_entry
*
1645 mips_elf_link_hash_newfunc (entry
, table
, string
)
1646 struct bfd_hash_entry
*entry
;
1647 struct bfd_hash_table
*table
;
1650 struct mips_elf_link_hash_entry
*ret
=
1651 (struct mips_elf_link_hash_entry
*) entry
;
1653 /* Allocate the structure if it has not already been allocated by a
1655 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
1656 ret
= ((struct mips_elf_link_hash_entry
*)
1657 bfd_hash_allocate (table
,
1658 sizeof (struct mips_elf_link_hash_entry
)));
1659 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
1661 bfd_set_error (bfd_error_no_memory
);
1662 return (struct bfd_hash_entry
*) ret
;
1665 /* Call the allocation method of the superclass. */
1666 ret
= ((struct mips_elf_link_hash_entry
*)
1667 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
1669 if (ret
!= (struct mips_elf_link_hash_entry
*) NULL
)
1671 /* Set local fields. */
1672 memset (&ret
->esym
, 0, sizeof (EXTR
));
1673 /* We use -2 as a marker to indicate that the information has
1674 not been set. -1 means there is no associated ifd. */
1678 return (struct bfd_hash_entry
*) ret
;
1681 /* Create a MIPS ELF linker hash table. */
1683 static struct bfd_link_hash_table
*
1684 mips_elf_link_hash_table_create (abfd
)
1687 struct mips_elf_link_hash_table
*ret
;
1689 ret
= ((struct mips_elf_link_hash_table
*)
1690 bfd_alloc (abfd
, sizeof (struct mips_elf_link_hash_table
)));
1691 if (ret
== (struct mips_elf_link_hash_table
*) NULL
)
1693 bfd_set_error (bfd_error_no_memory
);
1697 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
1698 mips_elf_link_hash_newfunc
))
1700 bfd_release (abfd
, ret
);
1704 return &ret
->root
.root
;
1707 /* Hook called by the linker routine which adds symbols from an object
1708 file. We must handle the special MIPS section numbers here. */
1712 mips_elf_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
1714 struct bfd_link_info
*info
;
1715 const Elf_Internal_Sym
*sym
;
1721 switch (sym
->st_shndx
)
1724 /* Common symbols less than the GP size are automatically
1725 treated as SHN_MIPS_SCOMMON symbols. */
1726 if (sym
->st_size
> elf_gp_size (abfd
))
1729 case SHN_MIPS_SCOMMON
:
1730 *secp
= bfd_make_section_old_way (abfd
, ".scommon");
1731 (*secp
)->flags
|= SEC_IS_COMMON
;
1732 *valp
= sym
->st_size
;
1735 case SHN_MIPS_SUNDEFINED
:
1736 *secp
= bfd_und_section_ptr
;
1743 /* Structure used to pass information to mips_elf_output_extsym. */
1748 struct bfd_link_info
*info
;
1749 struct ecoff_debug_info
*debug
;
1750 const struct ecoff_debug_swap
*swap
;
1754 /* This routine is used to write out ECOFF debugging external symbol
1755 information. It is called via mips_elf_link_hash_traverse. The
1756 ECOFF external symbol information must match the ELF external
1757 symbol information. Unfortunately, at this point we don't know
1758 whether a symbol is required by reloc information, so the two
1759 tables may wind up being different. We must sort out the external
1760 symbol information before we can set the final size of the .mdebug
1761 section, and we must set the size of the .mdebug section before we
1762 can relocate any sections, and we can't know which symbols are
1763 required by relocation until we relocate the sections.
1764 Fortunately, it is relatively unlikely that any symbol will be
1765 stripped but required by a reloc. In particular, it can not happen
1766 when generating a final executable. */
1769 mips_elf_output_extsym (h
, data
)
1770 struct mips_elf_link_hash_entry
*h
;
1773 struct extsym_info
*einfo
= (struct extsym_info
*) data
;
1776 if (h
->root
.indx
== -2)
1778 else if (((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1779 || (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
1780 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
1781 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
1783 else if (einfo
->info
->strip
== strip_all
1784 || (einfo
->info
->strip
== strip_some
1785 && bfd_hash_lookup (einfo
->info
->keep_hash
,
1786 h
->root
.root
.root
.string
,
1787 false, false) == NULL
))
1795 if (h
->esym
.ifd
== -2)
1798 h
->esym
.cobol_main
= 0;
1799 h
->esym
.weakext
= 0;
1800 h
->esym
.reserved
= 0;
1801 h
->esym
.ifd
= ifdNil
;
1802 h
->esym
.asym
.value
= 0;
1803 h
->esym
.asym
.st
= stGlobal
;
1805 if (h
->root
.root
.type
!= bfd_link_hash_defined
1806 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
1807 h
->esym
.asym
.sc
= scAbs
;
1810 asection
*output_section
;
1813 output_section
= h
->root
.root
.u
.def
.section
->output_section
;
1814 name
= bfd_section_name (output_section
->owner
, output_section
);
1816 if (strcmp (name
, ".text") == 0)
1817 h
->esym
.asym
.sc
= scText
;
1818 else if (strcmp (name
, ".data") == 0)
1819 h
->esym
.asym
.sc
= scData
;
1820 else if (strcmp (name
, ".sdata") == 0)
1821 h
->esym
.asym
.sc
= scSData
;
1822 else if (strcmp (name
, ".rodata") == 0
1823 || strcmp (name
, ".rdata") == 0)
1824 h
->esym
.asym
.sc
= scRData
;
1825 else if (strcmp (name
, ".bss") == 0)
1826 h
->esym
.asym
.sc
= scBss
;
1827 else if (strcmp (name
, ".sbss") == 0)
1828 h
->esym
.asym
.sc
= scSBss
;
1829 else if (strcmp (name
, ".init") == 0)
1830 h
->esym
.asym
.sc
= scInit
;
1831 else if (strcmp (name
, ".fini") == 0)
1832 h
->esym
.asym
.sc
= scFini
;
1834 h
->esym
.asym
.sc
= scAbs
;
1837 h
->esym
.asym
.reserved
= 0;
1838 h
->esym
.asym
.index
= indexNil
;
1841 if (h
->root
.root
.type
== bfd_link_hash_common
)
1842 h
->esym
.asym
.value
= h
->root
.root
.u
.c
.size
;
1843 else if (h
->root
.root
.type
== bfd_link_hash_defined
1844 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1848 if (h
->esym
.asym
.sc
== scCommon
)
1849 h
->esym
.asym
.sc
= scBss
;
1850 else if (h
->esym
.asym
.sc
== scSCommon
)
1851 h
->esym
.asym
.sc
= scSBss
;
1853 sec
= h
->root
.root
.u
.def
.section
;
1854 h
->esym
.asym
.value
= (h
->root
.root
.u
.def
.value
1855 + sec
->output_offset
1856 + sec
->output_section
->vma
);
1859 if (! bfd_ecoff_debug_one_external (einfo
->abfd
, einfo
->debug
, einfo
->swap
,
1860 h
->root
.root
.root
.string
,
1863 einfo
->failed
= true;
1870 /* A comparison routine used to sort .gptab entries. */
1873 gptab_compare (p1
, p2
)
1877 const Elf32_gptab
*a1
= (const Elf32_gptab
*) p1
;
1878 const Elf32_gptab
*a2
= (const Elf32_gptab
*) p2
;
1880 return a1
->gt_entry
.gt_g_value
- a2
->gt_entry
.gt_g_value
;
1883 /* We need to use a special link routine to handle the .reginfo and
1884 the .mdebug sections. We need to merge all instances of these
1885 sections together, not write them all out sequentially. */
1888 mips_elf_final_link (abfd
, info
)
1890 struct bfd_link_info
*info
;
1894 struct bfd_link_order
*p
;
1895 asection
*reginfo_sec
, *mdebug_sec
, *gptab_data_sec
, *gptab_bss_sec
;
1896 Elf32_RegInfo reginfo
;
1897 struct ecoff_debug_info debug
;
1898 const struct ecoff_debug_swap
*swap
1899 = get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
1900 HDRR
*symhdr
= &debug
.symbolic_header
;
1901 PTR mdebug_handle
= NULL
;
1903 /* Drop the .options section, since it has special semantics which I
1904 haven't bothered to figure out. */
1905 for (secpp
= &abfd
->sections
; *secpp
!= NULL
; secpp
= &(*secpp
)->next
)
1907 if (strcmp ((*secpp
)->name
, ".options") == 0)
1909 for (p
= (*secpp
)->link_order_head
; p
!= NULL
; p
= p
->next
)
1910 if (p
->type
== bfd_indirect_link_order
)
1911 p
->u
.indirect
.section
->flags
&=~ SEC_HAS_CONTENTS
;
1912 (*secpp
)->link_order_head
= NULL
;
1913 *secpp
= (*secpp
)->next
;
1914 --abfd
->section_count
;
1919 /* Go through the sections and collect the .reginfo and .mdebug
1923 gptab_data_sec
= NULL
;
1924 gptab_bss_sec
= NULL
;
1925 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
1927 if (strcmp (o
->name
, ".reginfo") == 0)
1929 memset (®info
, 0, sizeof reginfo
);
1931 /* We have found the .reginfo section in the output file.
1932 Look through all the link_orders comprising it and merge
1933 the information together. */
1934 for (p
= o
->link_order_head
;
1935 p
!= (struct bfd_link_order
*) NULL
;
1938 asection
*input_section
;
1940 Elf32_External_RegInfo ext
;
1943 if (p
->type
!= bfd_indirect_link_order
)
1945 if (p
->type
== bfd_fill_link_order
)
1950 input_section
= p
->u
.indirect
.section
;
1951 input_bfd
= input_section
->owner
;
1953 /* The linker emulation code has probably clobbered the
1954 size to be zero bytes. */
1955 if (input_section
->_raw_size
== 0)
1956 input_section
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1958 if (! bfd_get_section_contents (input_bfd
, input_section
,
1964 bfd_mips_elf32_swap_reginfo_in (input_bfd
, &ext
, &sub
);
1966 reginfo
.ri_gprmask
|= sub
.ri_gprmask
;
1967 reginfo
.ri_cprmask
[0] |= sub
.ri_cprmask
[0];
1968 reginfo
.ri_cprmask
[1] |= sub
.ri_cprmask
[1];
1969 reginfo
.ri_cprmask
[2] |= sub
.ri_cprmask
[2];
1970 reginfo
.ri_cprmask
[3] |= sub
.ri_cprmask
[3];
1972 /* ri_gp_value is set by the function
1973 mips_elf_section_processing when the section is
1974 finally written out. */
1976 /* Hack: reset the SEC_HAS_CONTENTS flag so that
1977 elf_link_input_bfd ignores this section. */
1978 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
1981 /* Force the section size to the value we want. */
1982 o
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1984 /* Skip this section later on (I don't think this currently
1985 matters, but someday it might). */
1986 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
1991 if (strcmp (o
->name
, ".mdebug") == 0)
1993 struct extsym_info einfo
;
1995 /* We have found the .mdebug section in the output file.
1996 Look through all the link_orders comprising it and merge
1997 the information together. */
1998 symhdr
->magic
= swap
->sym_magic
;
1999 /* FIXME: What should the version stamp be? */
2001 symhdr
->ilineMax
= 0;
2005 symhdr
->isymMax
= 0;
2006 symhdr
->ioptMax
= 0;
2007 symhdr
->iauxMax
= 0;
2009 symhdr
->issExtMax
= 0;
2012 symhdr
->iextMax
= 0;
2014 /* We accumulate the debugging information itself in the
2015 debug_info structure. */
2017 debug
.external_dnr
= NULL
;
2018 debug
.external_pdr
= NULL
;
2019 debug
.external_sym
= NULL
;
2020 debug
.external_opt
= NULL
;
2021 debug
.external_aux
= NULL
;
2023 debug
.ssext
= debug
.ssext_end
= NULL
;
2024 debug
.external_fdr
= NULL
;
2025 debug
.external_rfd
= NULL
;
2026 debug
.external_ext
= debug
.external_ext_end
= NULL
;
2028 mdebug_handle
= bfd_ecoff_debug_init (abfd
, &debug
, swap
, info
);
2029 if (mdebug_handle
== (PTR
) NULL
)
2032 for (p
= o
->link_order_head
;
2033 p
!= (struct bfd_link_order
*) NULL
;
2036 asection
*input_section
;
2038 const struct ecoff_debug_swap
*input_swap
;
2039 struct ecoff_debug_info input_debug
;
2043 if (p
->type
!= bfd_indirect_link_order
)
2045 if (p
->type
== bfd_fill_link_order
)
2050 input_section
= p
->u
.indirect
.section
;
2051 input_bfd
= input_section
->owner
;
2053 if (bfd_get_flavour (input_bfd
) != bfd_target_elf_flavour
2054 || (get_elf_backend_data (input_bfd
)
2055 ->elf_backend_ecoff_debug_swap
) == NULL
)
2057 /* I don't know what a non MIPS ELF bfd would be
2058 doing with a .mdebug section, but I don't really
2059 want to deal with it. */
2063 input_swap
= (get_elf_backend_data (input_bfd
)
2064 ->elf_backend_ecoff_debug_swap
);
2066 BFD_ASSERT (p
->size
== input_section
->_raw_size
);
2068 /* The ECOFF linking code expects that we have already
2069 read in the debugging information and set up an
2070 ecoff_debug_info structure, so we do that now. */
2071 if (! mips_elf_read_ecoff_info (input_bfd
, input_section
,
2075 if (! (bfd_ecoff_debug_accumulate
2076 (mdebug_handle
, abfd
, &debug
, swap
, input_bfd
,
2077 &input_debug
, input_swap
, info
)))
2080 /* Loop through the external symbols. For each one with
2081 interesting information, try to find the symbol in
2082 the linker global hash table and save the information
2083 for the output external symbols. */
2084 eraw_src
= input_debug
.external_ext
;
2085 eraw_end
= (eraw_src
2086 + (input_debug
.symbolic_header
.iextMax
2087 * input_swap
->external_ext_size
));
2089 eraw_src
< eraw_end
;
2090 eraw_src
+= input_swap
->external_ext_size
)
2094 struct mips_elf_link_hash_entry
*h
;
2096 (*input_swap
->swap_ext_in
) (input_bfd
, (PTR
) eraw_src
, &ext
);
2097 if (ext
.asym
.sc
== scNil
2098 || ext
.asym
.sc
== scUndefined
2099 || ext
.asym
.sc
== scSUndefined
)
2102 name
= input_debug
.ssext
+ ext
.asym
.iss
;
2103 h
= mips_elf_link_hash_lookup (mips_elf_hash_table (info
),
2104 name
, false, false, true);
2105 if (h
== NULL
|| h
->esym
.ifd
!= -2)
2111 < input_debug
.symbolic_header
.ifdMax
);
2112 ext
.ifd
= input_debug
.ifdmap
[ext
.ifd
];
2118 /* Free up the information we just read. */
2119 free (input_debug
.line
);
2120 free (input_debug
.external_dnr
);
2121 free (input_debug
.external_pdr
);
2122 free (input_debug
.external_sym
);
2123 free (input_debug
.external_opt
);
2124 free (input_debug
.external_aux
);
2125 free (input_debug
.ss
);
2126 free (input_debug
.ssext
);
2127 free (input_debug
.external_fdr
);
2128 free (input_debug
.external_rfd
);
2129 free (input_debug
.external_ext
);
2131 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2132 elf_link_input_bfd ignores this section. */
2133 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
2136 /* Build the external symbol information. */
2139 einfo
.debug
= &debug
;
2141 einfo
.failed
= false;
2142 mips_elf_link_hash_traverse (mips_elf_hash_table (info
),
2143 mips_elf_output_extsym
,
2148 /* Set the size of the .mdebug section. */
2149 o
->_raw_size
= bfd_ecoff_debug_size (abfd
, &debug
, swap
);
2151 /* Skip this section later on (I don't think this currently
2152 matters, but someday it might). */
2153 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2158 if (strncmp (o
->name
, ".gptab.", sizeof ".gptab." - 1) == 0)
2160 const char *subname
;
2163 Elf32_External_gptab
*ext_tab
;
2166 /* The .gptab.sdata and .gptab.sbss sections hold
2167 information describing how the small data area would
2168 change depending upon the -G switch. These sections
2169 not used in executables files. */
2170 if (! info
->relocateable
)
2174 for (p
= o
->link_order_head
;
2175 p
!= (struct bfd_link_order
*) NULL
;
2178 asection
*input_section
;
2180 if (p
->type
!= bfd_indirect_link_order
)
2182 if (p
->type
== bfd_fill_link_order
)
2187 input_section
= p
->u
.indirect
.section
;
2189 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2190 elf_link_input_bfd ignores this section. */
2191 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
2194 /* Skip this section later on (I don't think this
2195 currently matters, but someday it might). */
2196 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2198 /* Really remove the section. */
2199 for (secpp
= &abfd
->sections
;
2201 secpp
= &(*secpp
)->next
)
2203 *secpp
= (*secpp
)->next
;
2204 --abfd
->section_count
;
2209 /* There is one gptab for initialized data, and one for
2210 uninitialized data. */
2211 if (strcmp (o
->name
, ".gptab.sdata") == 0)
2213 else if (strcmp (o
->name
, ".gptab.sbss") == 0)
2217 bfd_set_error (bfd_error_nonrepresentable_section
);
2221 /* The linker script always combines .gptab.data and
2222 .gptab.sdata into .gptab.sdata, and likewise for
2223 .gptab.bss and .gptab.sbss. It is possible that there is
2224 no .sdata or .sbss section in the output file, in which
2225 case we must change the name of the output section. */
2226 subname
= o
->name
+ sizeof ".gptab" - 1;
2227 if (bfd_get_section_by_name (abfd
, subname
) == NULL
)
2229 if (o
== gptab_data_sec
)
2230 o
->name
= ".gptab.data";
2232 o
->name
= ".gptab.bss";
2233 subname
= o
->name
+ sizeof ".gptab" - 1;
2234 BFD_ASSERT (bfd_get_section_by_name (abfd
, subname
) != NULL
);
2237 /* Set up the first entry. */
2239 tab
= (Elf32_gptab
*) malloc (c
* sizeof (Elf32_gptab
));
2242 bfd_set_error (bfd_error_no_memory
);
2245 tab
[0].gt_header
.gt_current_g_value
= elf_gp_size (abfd
);
2246 tab
[0].gt_header
.gt_unused
= 0;
2248 /* Combine the input sections. */
2249 for (p
= o
->link_order_head
;
2250 p
!= (struct bfd_link_order
*) NULL
;
2253 asection
*input_section
;
2257 bfd_size_type gpentry
;
2259 if (p
->type
!= bfd_indirect_link_order
)
2261 if (p
->type
== bfd_fill_link_order
)
2266 input_section
= p
->u
.indirect
.section
;
2267 input_bfd
= input_section
->owner
;
2269 /* Combine the gptab entries for this input section one
2270 by one. We know that the input gptab entries are
2271 sorted by ascending -G value. */
2272 size
= bfd_section_size (input_bfd
, input_section
);
2274 for (gpentry
= sizeof (Elf32_External_gptab
);
2276 gpentry
+= sizeof (Elf32_External_gptab
))
2278 Elf32_External_gptab ext_gptab
;
2279 Elf32_gptab int_gptab
;
2285 if (! (bfd_get_section_contents
2286 (input_bfd
, input_section
, (PTR
) &ext_gptab
,
2287 gpentry
, sizeof (Elf32_External_gptab
))))
2293 bfd_mips_elf32_swap_gptab_in (input_bfd
, &ext_gptab
,
2295 val
= int_gptab
.gt_entry
.gt_g_value
;
2296 add
= int_gptab
.gt_entry
.gt_bytes
- last
;
2299 for (look
= 1; look
< c
; look
++)
2301 if (tab
[look
].gt_entry
.gt_g_value
>= val
)
2302 tab
[look
].gt_entry
.gt_bytes
+= add
;
2304 if (tab
[look
].gt_entry
.gt_g_value
== val
)
2310 Elf32_gptab
*new_tab
;
2313 /* We need a new table entry. */
2314 new_tab
= ((Elf32_gptab
*)
2316 (c
+ 1) * sizeof (Elf32_gptab
)));
2317 if (new_tab
== NULL
)
2319 bfd_set_error (bfd_error_no_memory
);
2324 tab
[c
].gt_entry
.gt_g_value
= val
;
2325 tab
[c
].gt_entry
.gt_bytes
= add
;
2327 /* Merge in the size for the next smallest -G
2328 value, since that will be implied by this new
2331 for (look
= 1; look
< c
; look
++)
2333 if (tab
[look
].gt_entry
.gt_g_value
< val
2335 || (tab
[look
].gt_entry
.gt_g_value
2336 > tab
[max
].gt_entry
.gt_g_value
)))
2340 tab
[c
].gt_entry
.gt_bytes
+=
2341 tab
[max
].gt_entry
.gt_bytes
;
2346 last
= int_gptab
.gt_entry
.gt_bytes
;
2349 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2350 elf_link_input_bfd ignores this section. */
2351 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
2354 /* The table must be sorted by -G value. */
2356 qsort (tab
+ 1, c
- 1, sizeof (tab
[0]), gptab_compare
);
2358 /* Swap out the table. */
2359 ext_tab
= ((Elf32_External_gptab
*)
2360 bfd_alloc (abfd
, c
* sizeof (Elf32_External_gptab
)));
2361 if (ext_tab
== NULL
)
2363 bfd_set_error (bfd_error_no_memory
);
2368 for (i
= 0; i
< c
; i
++)
2369 bfd_mips_elf32_swap_gptab_out (abfd
, tab
+ i
, ext_tab
+ i
);
2372 o
->_raw_size
= c
* sizeof (Elf32_External_gptab
);
2373 o
->contents
= (bfd_byte
*) ext_tab
;
2375 /* Skip this section later on (I don't think this currently
2376 matters, but someday it might). */
2377 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2381 /* Get a value for the GP register. */
2382 if (elf_gp (abfd
) == 0)
2384 struct bfd_link_hash_entry
*h
;
2386 h
= bfd_link_hash_lookup (info
->hash
, "_gp", false, false, true);
2387 if (h
!= (struct bfd_link_hash_entry
*) NULL
2388 && h
->type
== bfd_link_hash_defined
)
2389 elf_gp (abfd
) = (h
->u
.def
.value
2390 + h
->u
.def
.section
->output_section
->vma
2391 + h
->u
.def
.section
->output_offset
);
2392 else if (info
->relocateable
)
2396 /* Make up a value. */
2398 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
2401 && (strcmp (o
->name
, ".sbss") == 0
2402 || strcmp (o
->name
, ".sdata") == 0
2403 || strcmp (o
->name
, ".lit4") == 0
2404 || strcmp (o
->name
, ".lit8") == 0))
2407 elf_gp (abfd
) = lo
+ 0x8000;
2411 /* If the relocate_section function needs to do a reloc
2412 involving the GP value, it should make a reloc_dangerous
2413 callback to warn that GP is not defined. */
2417 /* Invoke the regular ELF backend linker to do all the work. */
2418 if (! bfd_elf32_bfd_final_link (abfd
, info
))
2421 /* Now write out the computed sections. */
2423 if (reginfo_sec
!= (asection
*) NULL
)
2425 Elf32_External_RegInfo ext
;
2427 bfd_mips_elf32_swap_reginfo_out (abfd
, ®info
, &ext
);
2428 if (! bfd_set_section_contents (abfd
, reginfo_sec
, (PTR
) &ext
,
2429 (file_ptr
) 0, sizeof ext
))
2433 if (mdebug_sec
!= (asection
*) NULL
)
2435 BFD_ASSERT (abfd
->output_has_begun
);
2436 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle
, abfd
, &debug
,
2438 mdebug_sec
->filepos
))
2441 bfd_ecoff_debug_free (mdebug_handle
, abfd
, &debug
, swap
, info
);
2444 if (gptab_data_sec
!= (asection
*) NULL
)
2446 if (! bfd_set_section_contents (abfd
, gptab_data_sec
,
2447 gptab_data_sec
->contents
,
2449 gptab_data_sec
->_raw_size
))
2453 if (gptab_bss_sec
!= (asection
*) NULL
)
2455 if (! bfd_set_section_contents (abfd
, gptab_bss_sec
,
2456 gptab_bss_sec
->contents
,
2458 gptab_bss_sec
->_raw_size
))
2465 /* Handle a MIPS ELF HI16 reloc. */
2468 mips_elf_relocate_hi16 (input_bfd
, relhi
, rello
, contents
, addend
)
2470 Elf_Internal_Rela
*relhi
;
2471 Elf_Internal_Rela
*rello
;
2478 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
2480 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
2483 addend
+= ((insn
& 0xffff) << 16) + addlo
;
2485 if ((addlo
& 0x8000) != 0)
2487 if ((addend
& 0x8000) != 0)
2490 bfd_put_32 (input_bfd
,
2491 (insn
& 0xffff0000) | ((addend
>> 16) & 0xffff),
2492 contents
+ relhi
->r_offset
);
2495 /* Relocate a MIPS ELF section. */
2498 mips_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
2499 contents
, relocs
, local_syms
, local_sections
)
2501 struct bfd_link_info
*info
;
2503 asection
*input_section
;
2505 Elf_Internal_Rela
*relocs
;
2506 Elf_Internal_Sym
*local_syms
;
2507 asection
**local_sections
;
2509 Elf_Internal_Shdr
*symtab_hdr
;
2512 Elf_Internal_Rela
*rel
;
2513 Elf_Internal_Rela
*relend
;
2515 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2517 if (elf_bad_symtab (input_bfd
))
2519 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
2524 locsymcount
= symtab_hdr
->sh_info
;
2525 extsymoff
= symtab_hdr
->sh_info
;
2529 relend
= relocs
+ input_section
->reloc_count
;
2530 for (; rel
< relend
; rel
++)
2533 reloc_howto_type
*howto
;
2536 struct elf_link_hash_entry
*h
;
2538 Elf_Internal_Sym
*sym
;
2539 bfd_reloc_status_type r
;
2541 r_type
= ELF32_R_TYPE (rel
->r_info
);
2542 if (r_type
< 0 || r_type
>= (int) R_MIPS_max
)
2544 bfd_set_error (bfd_error_bad_value
);
2547 howto
= elf_mips_howto_table
+ r_type
;
2549 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2551 /* Mix in the change in GP address for a GP relative reloc. */
2552 if (r_type
!= R_MIPS_GPREL16
2553 && r_type
!= R_MIPS_LITERAL
2554 && r_type
!= R_MIPS_GPREL32
)
2558 if (elf_gp (output_bfd
) == 0)
2560 if (! ((*info
->callbacks
->reloc_dangerous
)
2562 "GP relative relocation when GP not defined",
2563 input_bfd
, input_section
,
2566 /* Only give the error once per link. */
2567 elf_gp (output_bfd
) = 4;
2570 if (r_symndx
< extsymoff
2571 || (elf_bad_symtab (input_bfd
)
2572 && local_sections
[r_symndx
] != NULL
))
2574 /* This is a relocation against a section. The current
2575 addend in the instruction is the difference between
2576 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
2577 must change this to be the difference between the
2578 final definition (which will end up in RELOCATION)
2579 and the GP value of OUTPUT_BFD (which is in GP). */
2580 addend
= elf_gp (input_bfd
) - elf_gp (output_bfd
);
2582 else if (! info
->relocateable
)
2584 /* We are doing a final link. The current addend in the
2585 instruction is simply the desired offset into the
2586 symbol (normally zero). We want the instruction to
2587 hold the difference between the final definition of
2588 the symbol (which will end up in RELOCATION) and the
2589 GP value of OUTPUT_BFD (which is in GP). */
2590 addend
= - elf_gp (output_bfd
);
2594 /* We are generating relocateable output, and we aren't
2595 going to define this symbol, so we just leave the
2596 instruction alone. */
2604 if (info
->relocateable
)
2606 /* This is a relocateable link. We don't have to change
2607 anything, unless the reloc is against a section symbol,
2608 in which case we have to adjust according to where the
2609 section symbol winds up in the output section. */
2610 if (r_symndx
>= locsymcount
2611 || (elf_bad_symtab (input_bfd
)
2612 && local_sections
[r_symndx
] == NULL
))
2616 sym
= local_syms
+ r_symndx
;
2617 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2621 sec
= local_sections
[r_symndx
];
2623 /* It would be logical to add sym->st_value here,
2624 but Irix 5 sometimes generates a garbage symbol
2626 addend
+= sec
->output_offset
;
2628 /* If this is HI16 with an associated LO16, adjust
2629 the addend accordingly. Otherwise, just
2631 if (r_type
!= R_MIPS_HI16
2632 || (rel
+ 1) >= relend
2633 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
2634 r
= _bfd_relocate_contents (howto
, input_bfd
,
2636 contents
+ rel
->r_offset
);
2639 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
2650 /* This is a final link. */
2652 if (r_symndx
< extsymoff
2653 || (elf_bad_symtab (input_bfd
)
2654 && local_sections
[r_symndx
] != NULL
))
2656 sym
= local_syms
+ r_symndx
;
2657 sec
= local_sections
[r_symndx
];
2658 relocation
= (sec
->output_section
->vma
2659 + sec
->output_offset
);
2661 /* It would be logical to always add sym->st_value here,
2662 but Irix 5 sometimes generates a garbage symbol
2664 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2665 relocation
+= sym
->st_value
;
2671 indx
= r_symndx
- extsymoff
;
2672 h
= elf_sym_hashes (input_bfd
)[indx
];
2673 if (h
->root
.type
== bfd_link_hash_defined
2674 || h
->root
.type
== bfd_link_hash_defweak
)
2676 sec
= h
->root
.u
.def
.section
;
2677 relocation
= (h
->root
.u
.def
.value
2678 + sec
->output_section
->vma
2679 + sec
->output_offset
);
2681 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2685 if (! ((*info
->callbacks
->undefined_symbol
)
2686 (info
, h
->root
.root
.string
, input_bfd
,
2687 input_section
, rel
->r_offset
)))
2693 if (r_type
!= R_MIPS_HI16
2694 || (rel
+ 1) >= relend
2695 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
2696 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2697 contents
, rel
->r_offset
,
2698 relocation
, addend
);
2701 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
2702 contents
, relocation
+ addend
);
2707 if (r
!= bfd_reloc_ok
)
2712 case bfd_reloc_outofrange
:
2714 case bfd_reloc_overflow
:
2719 name
= h
->root
.root
.string
;
2722 name
= elf_string_from_elf_section (input_bfd
,
2723 symtab_hdr
->sh_link
,
2728 name
= bfd_section_name (input_bfd
, sec
);
2730 if (! ((*info
->callbacks
->reloc_overflow
)
2731 (info
, name
, howto
->name
, (bfd_vma
) 0,
2732 input_bfd
, input_section
, rel
->r_offset
)))
2743 /* This is almost identical to bfd_generic_get_... except that some
2744 MIPS relocations need to be handled specially. Sigh. */
2746 elf32_mips_get_relocated_section_contents (abfd
, link_info
, link_order
, data
,
2747 relocateable
, symbols
)
2749 struct bfd_link_info
*link_info
;
2750 struct bfd_link_order
*link_order
;
2752 boolean relocateable
;
2755 /* Get enough memory to hold the stuff */
2756 bfd
*input_bfd
= link_order
->u
.indirect
.section
->owner
;
2757 asection
*input_section
= link_order
->u
.indirect
.section
;
2759 long reloc_size
= bfd_get_reloc_upper_bound (input_bfd
, input_section
);
2760 arelent
**reloc_vector
= NULL
;
2766 reloc_vector
= (arelent
**) malloc (reloc_size
);
2767 if (reloc_vector
== NULL
&& reloc_size
!= 0)
2769 bfd_set_error (bfd_error_no_memory
);
2773 /* read in the section */
2774 if (!bfd_get_section_contents (input_bfd
,
2778 input_section
->_raw_size
))
2781 /* We're not relaxing the section, so just copy the size info */
2782 input_section
->_cooked_size
= input_section
->_raw_size
;
2783 input_section
->reloc_done
= true;
2785 reloc_count
= bfd_canonicalize_reloc (input_bfd
,
2789 if (reloc_count
< 0)
2792 if (reloc_count
> 0)
2800 struct bfd_hash_entry
*h
;
2801 struct bfd_link_hash_entry
*lh
;
2802 /* Skip all this stuff if we aren't mixing formats. */
2803 if (abfd
&& input_bfd
2804 && abfd
->xvec
== input_bfd
->xvec
)
2808 h
= bfd_hash_lookup (link_info
->hash
, "_gp", false, false);
2809 lh
= (struct bfd_link_hash_entry
*) h
;
2816 case bfd_link_hash_undefined
:
2817 case bfd_link_hash_undefweak
:
2818 case bfd_link_hash_common
:
2821 case bfd_link_hash_defined
:
2822 case bfd_link_hash_defweak
:
2824 gp
= lh
->u
.def
.value
;
2826 case bfd_link_hash_indirect
:
2827 case bfd_link_hash_warning
:
2829 /* @@FIXME ignoring warning for now */
2831 case bfd_link_hash_new
:
2840 for (parent
= reloc_vector
; *parent
!= (arelent
*) NULL
;
2843 char *error_message
= (char *) NULL
;
2844 bfd_reloc_status_type r
;
2846 /* Specific to MIPS: Deal with relocation types that require
2847 knowing the gp of the output bfd. */
2848 asymbol
*sym
= *(*parent
)->sym_ptr_ptr
;
2849 if (bfd_is_abs_section (sym
->section
) && abfd
)
2851 /* The special_function wouldn't get called anyways. */
2855 /* The gp isn't there; let the special function code
2856 fall over on its own. */
2858 else if ((*parent
)->howto
->special_function
== mips_elf_gprel16_reloc
)
2860 /* bypass special_function call */
2861 r
= gprel16_with_gp (input_bfd
, sym
, *parent
, input_section
,
2862 relocateable
, (PTR
) data
, gp
);
2863 goto skip_bfd_perform_relocation
;
2865 /* end mips specific stuff */
2867 r
= bfd_perform_relocation (input_bfd
,
2871 relocateable
? abfd
: (bfd
*) NULL
,
2873 skip_bfd_perform_relocation
:
2877 asection
*os
= input_section
->output_section
;
2879 /* A partial link, so keep the relocs */
2880 os
->orelocation
[os
->reloc_count
] = *parent
;
2884 if (r
!= bfd_reloc_ok
)
2888 case bfd_reloc_undefined
:
2889 if (!((*link_info
->callbacks
->undefined_symbol
)
2890 (link_info
, bfd_asymbol_name (*(*parent
)->sym_ptr_ptr
),
2891 input_bfd
, input_section
, (*parent
)->address
)))
2894 case bfd_reloc_dangerous
:
2895 BFD_ASSERT (error_message
!= (char *) NULL
);
2896 if (!((*link_info
->callbacks
->reloc_dangerous
)
2897 (link_info
, error_message
, input_bfd
, input_section
,
2898 (*parent
)->address
)))
2901 case bfd_reloc_overflow
:
2902 if (!((*link_info
->callbacks
->reloc_overflow
)
2903 (link_info
, bfd_asymbol_name (*(*parent
)->sym_ptr_ptr
),
2904 (*parent
)->howto
->name
, (*parent
)->addend
,
2905 input_bfd
, input_section
, (*parent
)->address
)))
2908 case bfd_reloc_outofrange
:
2917 if (reloc_vector
!= NULL
)
2918 free (reloc_vector
);
2922 if (reloc_vector
!= NULL
)
2923 free (reloc_vector
);
2926 #define bfd_elf32_bfd_get_relocated_section_contents elf32_mips_get_relocated_section_contents
2928 /* ECOFF swapping routines. These are used when dealing with the
2929 .mdebug section, which is in the ECOFF debugging format. */
2930 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap
=
2932 /* Symbol table magic number. */
2934 /* Alignment of debugging information. E.g., 4. */
2936 /* Sizes of external symbolic information. */
2937 sizeof (struct hdr_ext
),
2938 sizeof (struct dnr_ext
),
2939 sizeof (struct pdr_ext
),
2940 sizeof (struct sym_ext
),
2941 sizeof (struct opt_ext
),
2942 sizeof (struct fdr_ext
),
2943 sizeof (struct rfd_ext
),
2944 sizeof (struct ext_ext
),
2945 /* Functions to swap in external symbolic data. */
2954 _bfd_ecoff_swap_tir_in
,
2955 _bfd_ecoff_swap_rndx_in
,
2956 /* Functions to swap out external symbolic data. */
2965 _bfd_ecoff_swap_tir_out
,
2966 _bfd_ecoff_swap_rndx_out
,
2967 /* Function to read in symbolic data. */
2968 mips_elf_read_ecoff_info
2971 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
2972 #define TARGET_LITTLE_NAME "elf32-littlemips"
2973 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
2974 #define TARGET_BIG_NAME "elf32-bigmips"
2975 #define ELF_ARCH bfd_arch_mips
2976 #define ELF_MACHINE_CODE EM_MIPS
2977 #define ELF_MAXPAGESIZE 0x10000
2978 #define elf_backend_collect true
2979 #define elf_info_to_howto 0
2980 #define elf_info_to_howto_rel mips_info_to_howto_rel
2981 #define elf_backend_sym_is_global mips_elf_sym_is_global
2982 #define elf_backend_object_p mips_elf_object_p
2983 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
2984 #define elf_backend_fake_sections mips_elf_fake_sections
2985 #define elf_backend_section_from_bfd_section \
2986 mips_elf_section_from_bfd_section
2987 #define elf_backend_section_processing mips_elf_section_processing
2988 #define elf_backend_symbol_processing mips_elf_symbol_processing
2989 #define elf_backend_final_write_processing \
2990 mips_elf_final_write_processing
2991 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
2993 #define bfd_elf32_find_nearest_line mips_elf_find_nearest_line
2995 #define bfd_elf32_bfd_link_hash_table_create \
2996 mips_elf_link_hash_table_create
2997 #define bfd_elf32_bfd_final_link mips_elf_final_link
2998 #define elf_backend_relocate_section mips_elf_relocate_section
2999 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
3001 #include "elf32-target.h"