1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
24 different MIPS ELF from other targets. This matters when linking.
25 This file supports both, switching at runtime. */
35 /* Get the ECOFF swapping routines. */
37 #include "coff/symconst.h"
38 #include "coff/internal.h"
39 #include "coff/ecoff.h"
40 #include "coff/mips.h"
42 #include "ecoffswap.h"
44 static bfd_reloc_status_type mips_elf_hi16_reloc
PARAMS ((bfd
*abfd
,
51 static bfd_reloc_status_type mips_elf_got16_reloc
PARAMS ((bfd
*abfd
,
58 static bfd_reloc_status_type mips_elf_lo16_reloc
PARAMS ((bfd
*abfd
,
65 static bfd_reloc_status_type mips_elf_gprel16_reloc
PARAMS ((bfd
*abfd
,
72 static bfd_reloc_status_type mips_elf_gprel32_reloc
PARAMS ((bfd
*abfd
,
79 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
80 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
81 static void mips_info_to_howto_rel
82 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rel
*));
83 static boolean mips_elf_sym_is_global
PARAMS ((bfd
*, asymbol
*));
84 static boolean mips_elf_object_p
PARAMS ((bfd
*));
85 static boolean mips_elf_create_procedure_table
86 PARAMS ((PTR
, bfd
*, struct bfd_link_info
*, asection
*,
87 struct ecoff_debug_info
*));
88 static int mips_elf_additional_program_headers
PARAMS ((bfd
*));
89 static boolean mips_elf_modify_segment_map
PARAMS ((bfd
*));
90 static void mips_elf_final_write_processing
91 PARAMS ((bfd
*, boolean
));
92 static boolean mips_elf_section_from_shdr
93 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, char *));
94 static boolean mips_elf_fake_sections
95 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, asection
*));
96 static boolean mips_elf_section_from_bfd_section
97 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, asection
*, int *));
98 static boolean mips_elf_section_processing
99 PARAMS ((bfd
*, Elf32_Internal_Shdr
*));
100 static void mips_elf_symbol_processing
PARAMS ((bfd
*, asymbol
*));
101 static boolean mips_elf_read_ecoff_info
102 PARAMS ((bfd
*, asection
*, struct ecoff_debug_info
*));
103 static boolean mips_elf_is_local_label
104 PARAMS ((bfd
*, asymbol
*));
105 static boolean mips_elf_find_nearest_line
106 PARAMS ((bfd
*, asection
*, asymbol
**, bfd_vma
, const char **,
107 const char **, unsigned int *));
108 static struct bfd_hash_entry
*mips_elf_link_hash_newfunc
109 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
110 static struct bfd_link_hash_table
*mips_elf_link_hash_table_create
112 static int gptab_compare
PARAMS ((const void *, const void *));
113 static boolean mips_elf_final_link
114 PARAMS ((bfd
*, struct bfd_link_info
*));
115 static void mips_elf_relocate_hi16
116 PARAMS ((bfd
*, Elf_Internal_Rela
*, Elf_Internal_Rela
*, bfd_byte
*,
118 static void mips_elf_relocate_got_local
119 PARAMS ((bfd
*, bfd
*, asection
*, Elf_Internal_Rela
*,
120 Elf_Internal_Rela
*, bfd_byte
*, bfd_vma
));
121 static void mips_elf_relocate_global_got
122 PARAMS ((bfd
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_vma
));
123 static boolean mips_elf_adjust_dynindx
124 PARAMS ((struct elf_link_hash_entry
*, PTR
));
125 static boolean mips_elf_relocate_section
126 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
127 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
128 static boolean mips_elf_add_symbol_hook
129 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
130 const char **, flagword
*, asection
**, bfd_vma
*));
131 static bfd_reloc_status_type mips_elf_final_gp
132 PARAMS ((bfd
*, asymbol
*, boolean
, char **));
134 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
135 executables. FIXME: At the moment, we default to always generating
136 Irix 5 executables. */
138 #define SGI_COMPAT(abfd) (1)
140 /* This structure is used to hold .got information when linking. It
141 is stored in the tdata field of the bfd_elf_section_data structure. */
145 /* The symbol index of the first global .got symbol. */
146 unsigned long global_gotsym
;
147 /* The number of local .got entries. */
148 unsigned int local_gotno
;
151 /* The number of local .got entries we reserve. */
152 #define MIPS_RESERVED_GOTNO (2)
154 /* Instructions which appear in a stub. For some reason the stub is
155 slightly different on an SGI system. */
156 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
157 #define STUB_LW(abfd) \
159 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
160 : 0x8f998000) /* lw t9,0x8000(gp) */
161 #define STUB_MOVE 0x03e07825 /* move t7,ra */
162 #define STUB_JALR 0x0320f809 /* jal t9 */
163 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
164 #define MIPS_FUNCTION_STUB_SIZE (16)
166 /* Names of sections which appear in the .dynsym section in an Irix 5
169 static const char * const mips_elf_dynsym_sec_names
[] =
182 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
183 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
185 /* The number of entries in mips_elf_dynsym_sec_names which go in the
188 #define MIPS_TEXT_DYNSYM_SECNO (3)
190 /* The names of the runtime procedure table symbols used on Irix 5. */
192 static const char * const mips_elf_dynsym_rtproc_names
[] =
195 "_procedure_string_table",
196 "_procedure_table_size",
200 /* These structures are used to generate the .compact_rel section on
205 unsigned long id1
; /* Always one? */
206 unsigned long num
; /* Number of compact relocation entries. */
207 unsigned long id2
; /* Always two? */
208 unsigned long offset
; /* The file offset of the first relocation. */
209 unsigned long reserved0
; /* Zero? */
210 unsigned long reserved1
; /* Zero? */
219 bfd_byte reserved0
[4];
220 bfd_byte reserved1
[4];
221 } Elf32_External_compact_rel
;
227 unsigned long ctype
: 1, /* 1: long 0: short format. See below. */
228 rtype
: 4, /* Relocation types. See below. */
230 relvaddr
: 17; /* (VADDR - vaddr of theprevious entry) >> 2 */
234 unsigned long konst
; /* KONST field. See below. */
235 unsigned long vaddr
; /* VADDR to be relocated. */
242 unsigned long ctype
: 1, /* 1: long 0: short format. See below. */
243 rtype
: 4, /* Relocation types. See below. */
245 relvaddr
: 17; /* (VADDR - vaddr of the previous entry)/ 4 */
249 unsigned long konst
; /* KONST field. See below. */
257 } Elf32_External_crinfo
;
263 } Elf32_External_crinfo2
;
265 /* A compact relocation info has long (3 words) or short (2 words)
266 formats. A short format doesn't have VADDR field and relvaddr
267 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
268 #define CRF_MIPS_LONG 1
269 #define CRF_MIPS_SHORT 0
271 /* There are 4 types of compact relocation at least. The value KONST
272 has different meaning for each type:
275 CT_MIPS_REL32 Address in data
276 CT_MIPS_WORD Address in word (XXX)
277 CT_MIPS_GPHI_LO GP - vaddr
278 CT_MIPS_JMPAD Address to jump
281 #define CRT_MIPS_REL32 0xa
282 #define CRT_MIPS_WORD 0xb
283 #define CRT_MIPS_GPHI_LO 0xc
284 #define CRT_MIPS_JMPAD 0xd
286 #define mips_elf_set_cr_format(x,format) ((x).info.b.ctype = (format))
287 #define mips_elf_set_cr_type(x,type) ((x).info.b.rtype = (type))
288 #define mips_elf_set_cr_dist2to(x,v) ((x).info.b.dist2to = (v))
289 #define mips_elf_set_cr_relvaddr(x,d) ((x).info.b.relvaddr = (d)<<2)
293 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
298 R_MIPS_16
, R_MIPS_32
,
299 R_MIPS_REL32
, R_MIPS_26
,
300 R_MIPS_HI16
, R_MIPS_LO16
,
301 R_MIPS_GPREL16
, R_MIPS_LITERAL
,
302 R_MIPS_GOT16
, R_MIPS_PC16
,
303 R_MIPS_CALL16
, R_MIPS_GPREL32
,
304 /* The remaining relocs are defined on Irix, although they are not
305 in the MIPS ELF ABI. */
306 R_MIPS_UNUSED1
, R_MIPS_UNUSED2
,
308 R_MIPS_SHIFT5
, R_MIPS_SHIFT6
,
309 R_MIPS_64
, R_MIPS_GOT_DISP
,
310 R_MIPS_GOT_PAGE
, R_MIPS_GOT_OFST
,
311 R_MIPS_GOT_HI16
, R_MIPS_GOT_LO16
,
312 R_MIPS_SUB
, R_MIPS_INSERT_A
,
313 R_MIPS_INSERT_B
, R_MIPS_DELETE
,
314 R_MIPS_HIGHER
, R_MIPS_HIGHEST
,
315 R_MIPS_CALL_HI16
, R_MIPS_CALL_LO16
,
319 static reloc_howto_type elf_mips_howto_table
[] =
322 HOWTO (R_MIPS_NONE
, /* type */
324 0, /* size (0 = byte, 1 = short, 2 = long) */
326 false, /* pc_relative */
328 complain_overflow_dont
, /* complain_on_overflow */
329 bfd_elf_generic_reloc
, /* special_function */
330 "R_MIPS_NONE", /* name */
331 false, /* partial_inplace */
334 false), /* pcrel_offset */
336 /* 16 bit relocation. */
337 HOWTO (R_MIPS_16
, /* type */
339 1, /* size (0 = byte, 1 = short, 2 = long) */
341 false, /* pc_relative */
343 complain_overflow_bitfield
, /* complain_on_overflow */
344 bfd_elf_generic_reloc
, /* special_function */
345 "R_MIPS_16", /* name */
346 true, /* partial_inplace */
347 0xffff, /* src_mask */
348 0xffff, /* dst_mask */
349 false), /* pcrel_offset */
351 /* 32 bit relocation. */
352 HOWTO (R_MIPS_32
, /* type */
354 2, /* size (0 = byte, 1 = short, 2 = long) */
356 false, /* pc_relative */
358 complain_overflow_bitfield
, /* complain_on_overflow */
359 bfd_elf_generic_reloc
, /* special_function */
360 "R_MIPS_32", /* name */
361 true, /* partial_inplace */
362 0xffffffff, /* src_mask */
363 0xffffffff, /* dst_mask */
364 false), /* pcrel_offset */
366 /* 32 bit symbol relative relocation. */
367 HOWTO (R_MIPS_REL32
, /* type */
369 2, /* size (0 = byte, 1 = short, 2 = long) */
371 false, /* pc_relative */
373 complain_overflow_bitfield
, /* complain_on_overflow */
374 bfd_elf_generic_reloc
, /* special_function */
375 "R_MIPS_REL32", /* name */
376 true, /* partial_inplace */
377 0xffffffff, /* src_mask */
378 0xffffffff, /* dst_mask */
379 false), /* pcrel_offset */
381 /* 26 bit branch address. */
382 HOWTO (R_MIPS_26
, /* type */
384 2, /* size (0 = byte, 1 = short, 2 = long) */
386 false, /* pc_relative */
388 complain_overflow_dont
, /* complain_on_overflow */
389 /* This needs complex overflow
390 detection, because the upper four
391 bits must match the PC. */
392 bfd_elf_generic_reloc
, /* special_function */
393 "R_MIPS_26", /* name */
394 true, /* partial_inplace */
395 0x3ffffff, /* src_mask */
396 0x3ffffff, /* dst_mask */
397 false), /* pcrel_offset */
399 /* High 16 bits of symbol value. */
400 HOWTO (R_MIPS_HI16
, /* type */
402 2, /* size (0 = byte, 1 = short, 2 = long) */
404 false, /* pc_relative */
406 complain_overflow_dont
, /* complain_on_overflow */
407 mips_elf_hi16_reloc
, /* special_function */
408 "R_MIPS_HI16", /* name */
409 true, /* partial_inplace */
410 0xffff, /* src_mask */
411 0xffff, /* dst_mask */
412 false), /* pcrel_offset */
414 /* Low 16 bits of symbol value. */
415 HOWTO (R_MIPS_LO16
, /* type */
417 2, /* size (0 = byte, 1 = short, 2 = long) */
419 false, /* pc_relative */
421 complain_overflow_dont
, /* complain_on_overflow */
422 mips_elf_lo16_reloc
, /* special_function */
423 "R_MIPS_LO16", /* name */
424 true, /* partial_inplace */
425 0xffff, /* src_mask */
426 0xffff, /* dst_mask */
427 false), /* pcrel_offset */
429 /* GP relative reference. */
430 HOWTO (R_MIPS_GPREL16
, /* type */
432 2, /* size (0 = byte, 1 = short, 2 = long) */
434 false, /* pc_relative */
436 complain_overflow_signed
, /* complain_on_overflow */
437 mips_elf_gprel16_reloc
, /* special_function */
438 "R_MIPS_GPREL16", /* name */
439 true, /* partial_inplace */
440 0xffff, /* src_mask */
441 0xffff, /* dst_mask */
442 false), /* pcrel_offset */
444 /* Reference to literal section. */
445 HOWTO (R_MIPS_LITERAL
, /* type */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
449 false, /* pc_relative */
451 complain_overflow_signed
, /* complain_on_overflow */
452 mips_elf_gprel16_reloc
, /* special_function */
453 "R_MIPS_LITERAL", /* name */
454 true, /* partial_inplace */
455 0xffff, /* src_mask */
456 0xffff, /* dst_mask */
457 false), /* pcrel_offset */
459 /* Reference to global offset table. */
460 HOWTO (R_MIPS_GOT16
, /* type */
462 2, /* size (0 = byte, 1 = short, 2 = long) */
464 false, /* pc_relative */
466 complain_overflow_signed
, /* complain_on_overflow */
467 mips_elf_got16_reloc
, /* special_function */
468 "R_MIPS_GOT16", /* name */
469 false, /* partial_inplace */
471 0xffff, /* dst_mask */
472 false), /* pcrel_offset */
474 /* 16 bit PC relative reference. */
475 HOWTO (R_MIPS_PC16
, /* type */
477 2, /* size (0 = byte, 1 = short, 2 = long) */
479 true, /* pc_relative */
481 complain_overflow_signed
, /* complain_on_overflow */
482 bfd_elf_generic_reloc
, /* special_function */
483 "R_MIPS_PC16", /* name */
484 true, /* partial_inplace */
485 0xffff, /* src_mask */
486 0xffff, /* dst_mask */
487 false), /* pcrel_offset */
489 /* 16 bit call through global offset table. */
490 /* FIXME: This is not handled correctly. */
491 HOWTO (R_MIPS_CALL16
, /* type */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
495 false, /* pc_relative */
497 complain_overflow_signed
, /* complain_on_overflow */
498 bfd_elf_generic_reloc
, /* special_function */
499 "R_MIPS_CALL16", /* name */
500 false, /* partial_inplace */
502 0xffff, /* dst_mask */
503 false), /* pcrel_offset */
505 /* 32 bit GP relative reference. */
506 HOWTO (R_MIPS_GPREL32
, /* type */
508 2, /* size (0 = byte, 1 = short, 2 = long) */
510 false, /* pc_relative */
512 complain_overflow_bitfield
, /* complain_on_overflow */
513 mips_elf_gprel32_reloc
, /* special_function */
514 "R_MIPS_GPREL32", /* name */
515 true, /* partial_inplace */
516 0xffffffff, /* src_mask */
517 0xffffffff, /* dst_mask */
518 false), /* pcrel_offset */
520 /* The remaining relocs are defined on Irix 5, although they are
521 not defined by the ABI. */
526 /* A 5 bit shift field. */
527 HOWTO (R_MIPS_SHIFT5
, /* type */
529 2, /* size (0 = byte, 1 = short, 2 = long) */
531 false, /* pc_relative */
533 complain_overflow_bitfield
, /* complain_on_overflow */
534 bfd_elf_generic_reloc
, /* special_function */
535 "R_MIPS_SHIFT5", /* name */
536 true, /* partial_inplace */
537 0x000007c0, /* src_mask */
538 0x000007c0, /* dst_mask */
539 false), /* pcrel_offset */
541 /* A 6 bit shift field. */
542 /* FIXME: This is not handled correctly; a special function is
543 needed to put the most significant bit in the right place. */
544 HOWTO (R_MIPS_SHIFT6
, /* type */
546 2, /* size (0 = byte, 1 = short, 2 = long) */
548 false, /* pc_relative */
550 complain_overflow_bitfield
, /* complain_on_overflow */
551 bfd_elf_generic_reloc
, /* special_function */
552 "R_MIPS_SHIFT6", /* name */
553 true, /* partial_inplace */
554 0x000007c4, /* src_mask */
555 0x000007c4, /* dst_mask */
556 false), /* pcrel_offset */
558 /* A 64 bit relocation. Presumably not used in 32 bit ELF. */
561 /* Displacement in the global offset table. */
562 /* FIXME: Not handled correctly. */
563 HOWTO (R_MIPS_GOT_DISP
, /* type */
565 2, /* size (0 = byte, 1 = short, 2 = long) */
567 false, /* pc_relative */
569 complain_overflow_bitfield
, /* complain_on_overflow */
570 bfd_elf_generic_reloc
, /* special_function */
571 "R_MIPS_GOT_DISP", /* name */
572 true, /* partial_inplace */
573 0x0000ffff, /* src_mask */
574 0x0000ffff, /* dst_mask */
575 false), /* pcrel_offset */
577 /* Displacement to page pointer in the global offset table. */
578 /* FIXME: Not handled correctly. */
579 HOWTO (R_MIPS_GOT_PAGE
, /* type */
581 2, /* size (0 = byte, 1 = short, 2 = long) */
583 false, /* pc_relative */
585 complain_overflow_bitfield
, /* complain_on_overflow */
586 bfd_elf_generic_reloc
, /* special_function */
587 "R_MIPS_GOT_PAGE", /* name */
588 true, /* partial_inplace */
589 0x0000ffff, /* src_mask */
590 0x0000ffff, /* dst_mask */
591 false), /* pcrel_offset */
593 /* Offset from page pointer in the global offset table. */
594 /* FIXME: Not handled correctly. */
595 HOWTO (R_MIPS_GOT_OFST
, /* type */
597 2, /* size (0 = byte, 1 = short, 2 = long) */
599 false, /* pc_relative */
601 complain_overflow_bitfield
, /* complain_on_overflow */
602 bfd_elf_generic_reloc
, /* special_function */
603 "R_MIPS_GOT_OFST", /* name */
604 true, /* partial_inplace */
605 0x0000ffff, /* src_mask */
606 0x0000ffff, /* dst_mask */
607 false), /* pcrel_offset */
609 /* High 16 bits of displacement in global offset table. */
610 /* FIXME: Not handled correctly. */
611 HOWTO (R_MIPS_GOT_HI16
, /* type */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
615 false, /* pc_relative */
617 complain_overflow_dont
, /* complain_on_overflow */
618 bfd_elf_generic_reloc
, /* special_function */
619 "R_MIPS_GOT_HI16", /* name */
620 true, /* partial_inplace */
621 0x0000ffff, /* src_mask */
622 0x0000ffff, /* dst_mask */
623 false), /* pcrel_offset */
625 /* Low 16 bits of displacement in global offset table. */
626 /* FIXME: Not handled correctly. */
627 HOWTO (R_MIPS_GOT_LO16
, /* type */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
631 false, /* pc_relative */
633 complain_overflow_dont
, /* complain_on_overflow */
634 bfd_elf_generic_reloc
, /* special_function */
635 "R_MIPS_GOT_LO16", /* name */
636 true, /* partial_inplace */
637 0x0000ffff, /* src_mask */
638 0x0000ffff, /* dst_mask */
639 false), /* pcrel_offset */
641 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
644 /* Used to cause the linker to insert and delete instructions? */
649 /* Get the higher values of a 64 bit addend. Presumably not used in
654 /* High 16 bits of displacement in global offset table. */
655 /* FIXME: Not handled correctly. */
656 HOWTO (R_MIPS_CALL_HI16
, /* type */
658 2, /* size (0 = byte, 1 = short, 2 = long) */
660 false, /* pc_relative */
662 complain_overflow_dont
, /* complain_on_overflow */
663 bfd_elf_generic_reloc
, /* special_function */
664 "R_MIPS_CALL_HI16", /* name */
665 true, /* partial_inplace */
666 0x0000ffff, /* src_mask */
667 0x0000ffff, /* dst_mask */
668 false), /* pcrel_offset */
670 /* Low 16 bits of displacement in global offset table. */
671 /* FIXME: Not handled correctly. */
672 HOWTO (R_MIPS_CALL_LO16
, /* type */
674 2, /* size (0 = byte, 1 = short, 2 = long) */
676 false, /* pc_relative */
678 complain_overflow_dont
, /* complain_on_overflow */
679 bfd_elf_generic_reloc
, /* special_function */
680 "R_MIPS_CALL_LO16", /* name */
681 true, /* partial_inplace */
682 0x0000ffff, /* src_mask */
683 0x0000ffff, /* dst_mask */
684 false) /* pcrel_offset */
687 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
688 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
689 the HI16. Here we just save the information we need; we do the
690 actual relocation when we see the LO16. MIPS ELF requires that the
691 LO16 immediately follow the HI16, so this ought to work. */
693 static bfd_byte
*mips_hi16_addr
;
694 static bfd_vma mips_hi16_addend
;
696 static bfd_reloc_status_type
697 mips_elf_hi16_reloc (abfd
,
705 arelent
*reloc_entry
;
708 asection
*input_section
;
710 char **error_message
;
712 bfd_reloc_status_type ret
;
715 /* If we're relocating, and this an external symbol, we don't want
716 to change anything. */
717 if (output_bfd
!= (bfd
*) NULL
718 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
719 && reloc_entry
->addend
== 0)
721 reloc_entry
->address
+= input_section
->output_offset
;
727 if (strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
729 boolean relocateable
;
731 if (ret
== bfd_reloc_undefined
)
734 if (output_bfd
!= NULL
)
738 relocateable
= false;
739 output_bfd
= symbol
->section
->output_section
->owner
;
742 ret
= mips_elf_final_gp (output_bfd
, symbol
, relocateable
,
744 if (ret
!= bfd_reloc_ok
)
747 relocation
= elf_gp (output_bfd
) - reloc_entry
->address
;
751 if (bfd_is_und_section (symbol
->section
)
752 && output_bfd
== (bfd
*) NULL
)
753 ret
= bfd_reloc_undefined
;
755 if (bfd_is_com_section (symbol
->section
))
758 relocation
= symbol
->value
;
761 relocation
+= symbol
->section
->output_section
->vma
;
762 relocation
+= symbol
->section
->output_offset
;
763 relocation
+= reloc_entry
->addend
;
765 if (reloc_entry
->address
> input_section
->_cooked_size
)
766 return bfd_reloc_outofrange
;
768 /* Save the information, and let LO16 do the actual relocation. */
769 mips_hi16_addr
= (bfd_byte
*) data
+ reloc_entry
->address
;
770 mips_hi16_addend
= relocation
;
772 if (output_bfd
!= (bfd
*) NULL
)
773 reloc_entry
->address
+= input_section
->output_offset
;
778 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
779 inplace relocation; this function exists in order to do the
780 R_MIPS_HI16 relocation described above. */
782 static bfd_reloc_status_type
783 mips_elf_lo16_reloc (abfd
,
791 arelent
*reloc_entry
;
794 asection
*input_section
;
796 char **error_message
;
798 arelent gp_disp_relent
;
800 if (mips_hi16_addr
!= (bfd_byte
*) NULL
)
806 /* Do the HI16 relocation. Note that we actually don't need to
807 know anything about the LO16 itself, except where to find the
808 low 16 bits of the addend needed by the LO16. */
809 insn
= bfd_get_32 (abfd
, mips_hi16_addr
);
810 vallo
= (bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
)
812 val
= ((insn
& 0xffff) << 16) + vallo
;
813 val
+= mips_hi16_addend
;
815 /* The low order 16 bits are always treated as a signed value.
816 Therefore, a negative value in the low order bits requires an
817 adjustment in the high order bits. We need to make this
818 adjustment in two ways: once for the bits we took from the
819 data, and once for the bits we are putting back in to the
821 if ((vallo
& 0x8000) != 0)
823 if ((val
& 0x8000) != 0)
826 insn
= (insn
&~ 0xffff) | ((val
>> 16) & 0xffff);
827 bfd_put_32 (abfd
, insn
, mips_hi16_addr
);
829 mips_hi16_addr
= (bfd_byte
*) NULL
;
831 if (strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
833 gp_disp_relent
= *reloc_entry
;
834 reloc_entry
= &gp_disp_relent
;
835 reloc_entry
->addend
= mips_hi16_addend
;
838 else if (strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
840 bfd_reloc_status_type ret
;
843 /* FIXME: Does this case ever occur? */
845 ret
= mips_elf_final_gp (output_bfd
, symbol
, true, error_message
);
846 if (ret
!= bfd_reloc_ok
)
849 relocation
= elf_gp (output_bfd
) - reloc_entry
->address
;
850 relocation
+= symbol
->section
->output_section
->vma
;
851 relocation
+= symbol
->section
->output_offset
;
852 relocation
+= reloc_entry
->addend
;
854 if (reloc_entry
->address
> input_section
->_cooked_size
)
855 return bfd_reloc_outofrange
;
857 gp_disp_relent
= *reloc_entry
;
858 reloc_entry
= &gp_disp_relent
;
859 reloc_entry
->addend
= relocation
- 4;
862 /* Now do the LO16 reloc in the usual way. */
863 return bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
864 input_section
, output_bfd
, error_message
);
867 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
868 table used for PIC code. If the symbol is an external symbol, the
869 instruction is modified to contain the offset of the appropriate
870 entry in the global offset table. If the symbol is a section
871 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
872 addends are combined to form the real addend against the section
873 symbol; the GOT16 is modified to contain the offset of an entry in
874 the global offset table, and the LO16 is modified to offset it
875 appropriately. Thus an offset larger than 16 bits requires a
876 modified value in the global offset table.
878 This implementation suffices for the assembler, but the linker does
879 not yet know how to create global offset tables. */
881 static bfd_reloc_status_type
882 mips_elf_got16_reloc (abfd
,
890 arelent
*reloc_entry
;
893 asection
*input_section
;
895 char **error_message
;
897 /* If we're relocating, and this an external symbol, we don't want
898 to change anything. */
899 if (output_bfd
!= (bfd
*) NULL
900 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
901 && reloc_entry
->addend
== 0)
903 reloc_entry
->address
+= input_section
->output_offset
;
907 /* If we're relocating, and this is a local symbol, we can handle it
909 if (output_bfd
!= (bfd
*) NULL
910 && (symbol
->flags
& BSF_SECTION_SYM
) != 0)
911 return mips_elf_hi16_reloc (abfd
, reloc_entry
, symbol
, data
,
912 input_section
, output_bfd
, error_message
);
917 /* We have to figure out the gp value, so that we can adjust the
918 symbol value correctly. We look up the symbol _gp in the output
919 BFD. If we can't find it, we're stuck. We cache it in the ELF
920 target data. We don't need to adjust the symbol value for an
921 external symbol if we are producing relocateable output. */
923 static bfd_reloc_status_type
924 mips_elf_final_gp (output_bfd
, symbol
, relocateable
, error_message
)
927 boolean relocateable
;
928 char **error_message
;
930 if (bfd_is_und_section (symbol
->section
)
932 return bfd_reloc_undefined
;
934 /* This doesn't work if the BFD is not ELF. */
935 if (output_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
938 if (elf_gp (output_bfd
) == 0
940 || (symbol
->flags
& BSF_SECTION_SYM
) != 0))
944 /* Make up a value. */
945 elf_gp (output_bfd
) =
946 symbol
->section
->output_section
->vma
+ 0x4000;
954 count
= bfd_get_symcount (output_bfd
);
955 sym
= bfd_get_outsymbols (output_bfd
);
957 if (sym
== (asymbol
**) NULL
)
961 for (i
= 0; i
< count
; i
++, sym
++)
963 register CONST
char *name
;
965 name
= bfd_asymbol_name (*sym
);
966 if (*name
== '_' && strcmp (name
, "_gp") == 0)
968 elf_gp (output_bfd
) = bfd_asymbol_value (*sym
);
976 /* Only get the error once. */
977 elf_gp (output_bfd
) = 4;
979 (char *) "GP relative relocation when _gp not defined";
980 return bfd_reloc_dangerous
;
988 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
989 become the offset from the gp register. This function also handles
990 R_MIPS_LITERAL relocations, although those can be handled more
991 cleverly because the entries in the .lit8 and .lit4 sections can be
994 static bfd_reloc_status_type gprel16_with_gp
PARAMS ((bfd
*, asymbol
*,
995 arelent
*, asection
*,
996 boolean
, PTR
, bfd_vma
));
998 static bfd_reloc_status_type
999 mips_elf_gprel16_reloc (abfd
,
1007 arelent
*reloc_entry
;
1010 asection
*input_section
;
1012 char **error_message
;
1014 boolean relocateable
;
1015 bfd_reloc_status_type ret
;
1017 /* If we're relocating, and this is an external symbol with no
1018 addend, we don't want to change anything. We will only have an
1019 addend if this is a newly created reloc, not read from an ELF
1021 if (output_bfd
!= (bfd
*) NULL
1022 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1023 && reloc_entry
->addend
== 0)
1025 reloc_entry
->address
+= input_section
->output_offset
;
1026 return bfd_reloc_ok
;
1029 if (output_bfd
!= (bfd
*) NULL
)
1030 relocateable
= true;
1033 relocateable
= false;
1034 output_bfd
= symbol
->section
->output_section
->owner
;
1037 ret
= mips_elf_final_gp (output_bfd
, symbol
, relocateable
, error_message
);
1038 if (ret
!= bfd_reloc_ok
)
1041 return gprel16_with_gp (abfd
, symbol
, reloc_entry
, input_section
,
1042 relocateable
, data
, elf_gp (output_bfd
));
1045 static bfd_reloc_status_type
1046 gprel16_with_gp (abfd
, symbol
, reloc_entry
, input_section
, relocateable
, data
,
1050 arelent
*reloc_entry
;
1051 asection
*input_section
;
1052 boolean relocateable
;
1060 if (bfd_is_com_section (symbol
->section
))
1063 relocation
= symbol
->value
;
1065 relocation
+= symbol
->section
->output_section
->vma
;
1066 relocation
+= symbol
->section
->output_offset
;
1068 if (reloc_entry
->address
> input_section
->_cooked_size
)
1069 return bfd_reloc_outofrange
;
1071 insn
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
);
1073 /* Set val to the offset into the section or symbol. */
1074 val
= ((insn
& 0xffff) + reloc_entry
->addend
) & 0xffff;
1078 /* Adjust val for the final section location and GP value. If we
1079 are producing relocateable output, we don't want to do this for
1080 an external symbol. */
1082 || (symbol
->flags
& BSF_SECTION_SYM
) != 0)
1083 val
+= relocation
- gp
;
1085 insn
= (insn
&~ 0xffff) | (val
& 0xffff);
1086 bfd_put_32 (abfd
, insn
, (bfd_byte
*) data
+ reloc_entry
->address
);
1089 reloc_entry
->address
+= input_section
->output_offset
;
1091 /* Make sure it fit in 16 bits. */
1092 if (val
>= 0x8000 && val
< 0xffff8000)
1093 return bfd_reloc_overflow
;
1095 return bfd_reloc_ok
;
1098 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1099 from the gp register? XXX */
1101 static bfd_reloc_status_type gprel32_with_gp
PARAMS ((bfd
*, asymbol
*,
1102 arelent
*, asection
*,
1103 boolean
, PTR
, bfd_vma
));
1105 static bfd_reloc_status_type
1106 mips_elf_gprel32_reloc (abfd
,
1114 arelent
*reloc_entry
;
1117 asection
*input_section
;
1119 char **error_message
;
1121 boolean relocateable
;
1122 bfd_reloc_status_type ret
;
1124 /* If we're relocating, and this is an external symbol with no
1125 addend, we don't want to change anything. We will only have an
1126 addend if this is a newly created reloc, not read from an ELF
1128 if (output_bfd
!= (bfd
*) NULL
1129 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1130 && reloc_entry
->addend
== 0)
1132 *error_message
= (char *)
1133 "32bits gp relative relocation occurs for an external symbol";
1134 return bfd_reloc_outofrange
;
1137 if (output_bfd
!= (bfd
*) NULL
)
1138 relocateable
= true;
1141 relocateable
= false;
1142 output_bfd
= symbol
->section
->output_section
->owner
;
1144 ret
= mips_elf_final_gp (output_bfd
, symbol
, relocateable
,
1146 if (ret
!= bfd_reloc_ok
)
1150 return gprel32_with_gp (abfd
, symbol
, reloc_entry
, input_section
,
1151 relocateable
, data
, elf_gp (output_bfd
));
1154 static bfd_reloc_status_type
1155 gprel32_with_gp (abfd
, symbol
, reloc_entry
, input_section
, relocateable
, data
,
1159 arelent
*reloc_entry
;
1160 asection
*input_section
;
1161 boolean relocateable
;
1168 if (bfd_is_com_section (symbol
->section
))
1171 relocation
= symbol
->value
;
1173 relocation
+= symbol
->section
->output_section
->vma
;
1174 relocation
+= symbol
->section
->output_offset
;
1176 if (reloc_entry
->address
> input_section
->_cooked_size
)
1177 return bfd_reloc_outofrange
;
1179 val
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
);
1181 /* Set val to the offset into the section or symbol. */
1182 val
+= reloc_entry
->addend
;
1184 /* Adjust val for the final section location and GP value. If we
1185 are producing relocateable output, we don't want to do this for
1186 an external symbol. */
1188 || (symbol
->flags
& BSF_SECTION_SYM
) != 0)
1189 val
+= relocation
- gp
;
1191 bfd_put_32 (abfd
, val
, (bfd_byte
*) data
+ reloc_entry
->address
);
1194 reloc_entry
->address
+= input_section
->output_offset
;
1196 return bfd_reloc_ok
;
1199 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1201 struct elf_reloc_map
{
1202 bfd_reloc_code_real_type bfd_reloc_val
;
1203 enum reloc_type elf_reloc_val
;
1206 static CONST
struct elf_reloc_map mips_reloc_map
[] =
1208 { BFD_RELOC_NONE
, R_MIPS_NONE
, },
1209 { BFD_RELOC_16
, R_MIPS_16
},
1210 { BFD_RELOC_32
, R_MIPS_32
},
1211 { BFD_RELOC_CTOR
, R_MIPS_32
},
1212 { BFD_RELOC_32_PCREL
, R_MIPS_REL32
},
1213 { BFD_RELOC_MIPS_JMP
, R_MIPS_26
},
1214 { BFD_RELOC_HI16_S
, R_MIPS_HI16
},
1215 { BFD_RELOC_LO16
, R_MIPS_LO16
},
1216 { BFD_RELOC_MIPS_GPREL
, R_MIPS_GPREL16
},
1217 { BFD_RELOC_MIPS_LITERAL
, R_MIPS_LITERAL
},
1218 { BFD_RELOC_MIPS_GOT16
, R_MIPS_GOT16
},
1219 { BFD_RELOC_16_PCREL
, R_MIPS_PC16
},
1220 { BFD_RELOC_MIPS_CALL16
, R_MIPS_CALL16
},
1221 { BFD_RELOC_MIPS_GPREL32
, R_MIPS_GPREL32
}
1224 /* Given a BFD reloc type, return a howto structure. */
1226 static reloc_howto_type
*
1227 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
1229 bfd_reloc_code_real_type code
;
1233 for (i
= 0; i
< sizeof (mips_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
1235 if (mips_reloc_map
[i
].bfd_reloc_val
== code
)
1236 return &elf_mips_howto_table
[(int) mips_reloc_map
[i
].elf_reloc_val
];
1241 /* Given a MIPS reloc type, fill in an arelent structure. */
1244 mips_info_to_howto_rel (abfd
, cache_ptr
, dst
)
1247 Elf32_Internal_Rel
*dst
;
1249 unsigned int r_type
;
1251 r_type
= ELF32_R_TYPE (dst
->r_info
);
1252 BFD_ASSERT (r_type
< (unsigned int) R_MIPS_max
);
1253 cache_ptr
->howto
= &elf_mips_howto_table
[r_type
];
1255 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1256 value for the object file. We get the addend now, rather than
1257 when we do the relocation, because the symbol manipulations done
1258 by the linker may cause us to lose track of the input BFD. */
1259 if (((*cache_ptr
->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) != 0
1260 && (r_type
== (unsigned int) R_MIPS_GPREL16
1261 || r_type
== (unsigned int) R_MIPS_LITERAL
))
1262 cache_ptr
->addend
= elf_gp (abfd
);
1265 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1266 routines swap this structure in and out. They are used outside of
1267 BFD, so they are globally visible. */
1270 bfd_mips_elf32_swap_reginfo_in (abfd
, ex
, in
)
1272 const Elf32_External_RegInfo
*ex
;
1275 in
->ri_gprmask
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gprmask
);
1276 in
->ri_cprmask
[0] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[0]);
1277 in
->ri_cprmask
[1] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[1]);
1278 in
->ri_cprmask
[2] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[2]);
1279 in
->ri_cprmask
[3] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[3]);
1280 in
->ri_gp_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gp_value
);
1284 bfd_mips_elf32_swap_reginfo_out (abfd
, in
, ex
)
1286 const Elf32_RegInfo
*in
;
1287 Elf32_External_RegInfo
*ex
;
1289 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gprmask
,
1290 (bfd_byte
*) ex
->ri_gprmask
);
1291 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[0],
1292 (bfd_byte
*) ex
->ri_cprmask
[0]);
1293 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[1],
1294 (bfd_byte
*) ex
->ri_cprmask
[1]);
1295 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[2],
1296 (bfd_byte
*) ex
->ri_cprmask
[2]);
1297 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[3],
1298 (bfd_byte
*) ex
->ri_cprmask
[3]);
1299 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gp_value
,
1300 (bfd_byte
*) ex
->ri_gp_value
);
1303 /* Swap an entry in a .gptab section. Note that these routines rely
1304 on the equivalence of the two elements of the union. */
1307 bfd_mips_elf32_swap_gptab_in (abfd
, ex
, in
)
1309 const Elf32_External_gptab
*ex
;
1312 in
->gt_entry
.gt_g_value
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_g_value
);
1313 in
->gt_entry
.gt_bytes
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_bytes
);
1317 bfd_mips_elf32_swap_gptab_out (abfd
, in
, ex
)
1319 const Elf32_gptab
*in
;
1320 Elf32_External_gptab
*ex
;
1322 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_g_value
,
1323 ex
->gt_entry
.gt_g_value
);
1324 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_bytes
,
1325 ex
->gt_entry
.gt_bytes
);
1329 bfd_elf32_swap_compact_rel_out (abfd
, in
, ex
)
1331 Elf32_compact_rel
*in
;
1332 Elf32_External_compact_rel
*ex
;
1334 bfd_h_put_32 (abfd
, (bfd_vma
) in
->id1
, ex
->id1
);
1335 bfd_h_put_32 (abfd
, (bfd_vma
) in
->num
, ex
->num
);
1336 bfd_h_put_32 (abfd
, (bfd_vma
) in
->id2
, ex
->id2
);
1337 bfd_h_put_32 (abfd
, (bfd_vma
) in
->offset
, ex
->offset
);
1338 bfd_h_put_32 (abfd
, (bfd_vma
) in
->reserved0
, ex
->reserved0
);
1339 bfd_h_put_32 (abfd
, (bfd_vma
) in
->reserved1
, ex
->reserved1
);
1343 bfd_elf32_swap_crinfo_out (abfd
, in
, ex
)
1346 Elf32_External_crinfo
*ex
;
1348 bfd_h_put_32 (abfd
, (bfd_vma
) in
->info
.l
, ex
->info
);
1349 bfd_h_put_32 (abfd
, (bfd_vma
) in
->konst
, ex
->konst
);
1350 bfd_h_put_32 (abfd
, (bfd_vma
) in
->vaddr
, ex
->vaddr
);
1353 /* Determine whether a symbol is global for the purposes of splitting
1354 the symbol table into global symbols and local symbols. At least
1355 on Irix 5, this split must be between section symbols and all other
1356 symbols. On most ELF targets the split is between static symbols
1357 and externally visible symbols. */
1361 mips_elf_sym_is_global (abfd
, sym
)
1365 return (sym
->flags
& BSF_SECTION_SYM
) == 0 ? true : false;
1368 /* Set the right machine number for a MIPS ELF file. */
1371 mips_elf_object_p (abfd
)
1374 switch (elf_elfheader (abfd
)->e_flags
& EF_MIPS_ARCH
)
1378 /* Just use the default, which was set in elfcode.h. */
1382 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 6000);
1386 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 4000);
1390 /* Irix 5 is broken. Object file symbol tables are not always
1391 sorted correctly such that local symbols precede global symbols,
1392 and the sh_info field in the symbol table is not always right. */
1393 elf_bad_symtab (abfd
) = true;
1398 /* The final processing done just before writing out a MIPS ELF object
1399 file. This gets the MIPS architecture right based on the machine
1404 mips_elf_final_write_processing (abfd
, linker
)
1410 Elf_Internal_Shdr
**hdrpp
;
1412 switch (bfd_get_mach (abfd
))
1415 val
= E_MIPS_ARCH_1
;
1419 val
= E_MIPS_ARCH_2
;
1423 val
= E_MIPS_ARCH_3
;
1431 elf_elfheader (abfd
)->e_flags
&=~ EF_MIPS_ARCH
;
1432 elf_elfheader (abfd
)->e_flags
|= val
;
1434 /* Set the sh_info field for .gptab sections. */
1435 for (i
= 1, hdrpp
= elf_elfsections (abfd
) + 1;
1436 i
< elf_elfheader (abfd
)->e_shnum
;
1439 if ((*hdrpp
)->sh_type
== SHT_MIPS_GPTAB
)
1444 BFD_ASSERT ((*hdrpp
)->bfd_section
!= NULL
);
1445 name
= bfd_get_section_name (abfd
, (*hdrpp
)->bfd_section
);
1446 BFD_ASSERT (name
!= NULL
1447 && strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0);
1448 sec
= bfd_get_section_by_name (abfd
, name
+ sizeof ".gptab" - 1);
1449 BFD_ASSERT (sec
!= NULL
);
1450 (*hdrpp
)->sh_info
= elf_section_data (sec
)->this_idx
;
1455 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
1458 mips_elf_set_private_flags (abfd
, flags
)
1462 BFD_ASSERT (!elf_flags_init (abfd
)
1463 || elf_elfheader (abfd
)->e_flags
== flags
);
1465 elf_elfheader (abfd
)->e_flags
= flags
;
1466 elf_flags_init (abfd
) = true;
1470 /* Copy backend specific data from one object module to another */
1473 mips_elf_copy_private_bfd_data (ibfd
, obfd
)
1477 /* This function is selected based on the input vector. We only
1478 want to copy information over if the output BFD also uses Elf
1480 if (bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1483 BFD_ASSERT (!elf_flags_init (obfd
)
1484 || (elf_elfheader (obfd
)->e_flags
1485 == elf_elfheader (ibfd
)->e_flags
));
1487 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1488 elf_flags_init (obfd
) = true;
1492 /* Merge backend specific data from an object file to the output
1493 object file when linking. */
1496 mips_elf_merge_private_bfd_data (ibfd
, obfd
)
1503 /* Check if we have the same endianess */
1504 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1505 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1507 (*_bfd_error_handler
)
1508 ("%s: compiled for a %s endian system and target is %s endian",
1509 bfd_get_filename (ibfd
),
1510 bfd_big_endian (ibfd
) ? "big" : "little",
1511 bfd_big_endian (obfd
) ? "big" : "little");
1513 bfd_set_error (bfd_error_wrong_format
);
1517 /* This function is selected based on the input vector. We only
1518 want to copy information over if the output BFD also uses Elf
1520 if (bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1523 new_flags
= elf_elfheader (ibfd
)->e_flags
;
1524 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_MIPS_NOREORDER
;
1525 old_flags
= elf_elfheader (obfd
)->e_flags
;
1527 if (!elf_flags_init (obfd
)) /* First call, no flags set */
1529 elf_flags_init (obfd
) = true;
1530 elf_elfheader (obfd
)->e_flags
= new_flags
;
1532 else if (((new_flags
^ old_flags
) & ~EF_MIPS_NOREORDER
)
1533 == 0) /* Compatible flags are ok */
1535 else /* Incompatible flags */
1537 /* Warn about -fPIC mismatch */
1538 if ((new_flags
& EF_MIPS_PIC
) != (old_flags
& EF_MIPS_PIC
))
1540 new_flags
&= ~EF_MIPS_PIC
;
1541 (*_bfd_error_handler
)
1542 ("%s: needs all files compiled with -fPIC",
1543 bfd_get_filename (ibfd
));
1546 if ((new_flags
& EF_MIPS_CPIC
) != (old_flags
& EF_MIPS_CPIC
))
1548 new_flags
&= ~EF_MIPS_CPIC
;
1549 (*_bfd_error_handler
)
1550 ("%s: needs all files compiled with -mabicalls",
1551 bfd_get_filename (ibfd
));
1554 /* Warn about any other mismatches */
1555 if (new_flags
!= old_flags
)
1556 (*_bfd_error_handler
)
1557 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
1558 bfd_get_filename (ibfd
), (unsigned long) new_flags
,
1559 (unsigned long) old_flags
);
1561 bfd_set_error (bfd_error_bad_value
);
1568 /* Handle a MIPS specific section when reading an object file. This
1569 is called when elfcode.h finds a section with an unknown type.
1570 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1574 mips_elf_section_from_shdr (abfd
, hdr
, name
)
1576 Elf32_Internal_Shdr
*hdr
;
1581 /* There ought to be a place to keep ELF backend specific flags, but
1582 at the moment there isn't one. We just keep track of the
1583 sections by their name, instead. Fortunately, the ABI gives
1584 suggested names for all the MIPS specific sections, so we will
1585 probably get away with this. */
1586 switch (hdr
->sh_type
)
1588 case SHT_MIPS_LIBLIST
:
1589 if (strcmp (name
, ".liblist") != 0)
1593 if (strcmp (name
, ".msym") != 0)
1596 case SHT_MIPS_CONFLICT
:
1597 if (strcmp (name
, ".conflict") != 0)
1600 case SHT_MIPS_GPTAB
:
1601 if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) != 0)
1604 case SHT_MIPS_UCODE
:
1605 if (strcmp (name
, ".ucode") != 0)
1608 case SHT_MIPS_DEBUG
:
1609 if (strcmp (name
, ".mdebug") != 0)
1612 case SHT_MIPS_REGINFO
:
1613 if (strcmp (name
, ".reginfo") != 0
1614 || hdr
->sh_size
!= sizeof (Elf32_External_RegInfo
))
1617 case SHT_MIPS_OPTIONS
:
1618 if (strcmp (name
, ".options") != 0)
1621 case SHT_MIPS_DWARF
:
1622 if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) != 0)
1625 case SHT_MIPS_EVENTS
:
1626 if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1633 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1635 newsect
= hdr
->bfd_section
;
1637 if (hdr
->sh_type
== SHT_MIPS_DEBUG
)
1639 if (! bfd_set_section_flags (abfd
, newsect
,
1640 (bfd_get_section_flags (abfd
, newsect
)
1645 /* FIXME: We should record sh_info for a .gptab section. */
1647 /* For a .reginfo section, set the gp value in the tdata information
1648 from the contents of this section. We need the gp value while
1649 processing relocs, so we just get it now. */
1650 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1652 Elf32_External_RegInfo ext
;
1655 if (! bfd_get_section_contents (abfd
, newsect
, (PTR
) &ext
,
1656 (file_ptr
) 0, sizeof ext
))
1658 bfd_mips_elf32_swap_reginfo_in (abfd
, &ext
, &s
);
1659 elf_gp (abfd
) = s
.ri_gp_value
;
1665 /* Set the correct type for a MIPS ELF section. We do this by the
1666 section name, which is a hack, but ought to work. */
1669 mips_elf_fake_sections (abfd
, hdr
, sec
)
1671 Elf32_Internal_Shdr
*hdr
;
1674 register const char *name
;
1676 name
= bfd_get_section_name (abfd
, sec
);
1678 if (strcmp (name
, ".liblist") == 0)
1680 hdr
->sh_type
= SHT_MIPS_LIBLIST
;
1681 hdr
->sh_info
= sec
->_raw_size
/ sizeof (Elf32_Lib
);
1682 /* FIXME: Set the sh_link field. */
1684 else if (strcmp (name
, ".msym") == 0)
1686 hdr
->sh_type
= SHT_MIPS_MSYM
;
1687 hdr
->sh_entsize
= 8;
1688 /* FIXME: Set the sh_info field. */
1690 else if (strcmp (name
, ".conflict") == 0)
1691 hdr
->sh_type
= SHT_MIPS_CONFLICT
;
1692 else if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0)
1694 hdr
->sh_type
= SHT_MIPS_GPTAB
;
1695 hdr
->sh_entsize
= sizeof (Elf32_External_gptab
);
1696 /* The sh_info field is set in mips_elf_final_write_processing. */
1698 else if (strcmp (name
, ".ucode") == 0)
1699 hdr
->sh_type
= SHT_MIPS_UCODE
;
1700 else if (strcmp (name
, ".mdebug") == 0)
1702 hdr
->sh_type
= SHT_MIPS_DEBUG
;
1703 /* In a shared object on Irix 5.3, the .mdebug section has an
1704 entsize of 0. FIXME: Does this matter? */
1705 if (SGI_COMPAT (abfd
) && (abfd
->flags
& DYNAMIC
) != 0)
1706 hdr
->sh_entsize
= 0;
1708 hdr
->sh_entsize
= 1;
1710 else if (strcmp (name
, ".reginfo") == 0)
1712 hdr
->sh_type
= SHT_MIPS_REGINFO
;
1713 /* In a shared object on Irix 5.3, the .reginfo section has an
1714 entsize of 0x18. FIXME: Does this matter? */
1715 if (SGI_COMPAT (abfd
) && (abfd
->flags
& DYNAMIC
) != 0)
1716 hdr
->sh_entsize
= sizeof (Elf32_External_RegInfo
);
1718 hdr
->sh_entsize
= 1;
1720 /* Force the section size to the correct value, even if the
1721 linker thinks it is larger. The link routine below will only
1722 write out this much data for .reginfo. */
1723 hdr
->sh_size
= sec
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1725 else if (SGI_COMPAT (abfd
)
1726 && (strcmp (name
, ".hash") == 0
1727 || strcmp (name
, ".dynamic") == 0
1728 || strcmp (name
, ".dynstr") == 0))
1730 hdr
->sh_entsize
= 0;
1731 hdr
->sh_info
= SIZEOF_MIPS_DYNSYM_SECNAMES
;
1733 else if (strcmp (name
, ".got") == 0
1734 || strcmp (name
, ".sdata") == 0
1735 || strcmp (name
, ".sbss") == 0
1736 || strcmp (name
, ".lit4") == 0
1737 || strcmp (name
, ".lit8") == 0)
1738 hdr
->sh_flags
|= SHF_MIPS_GPREL
;
1739 else if (strcmp (name
, ".options") == 0)
1741 hdr
->sh_type
= SHT_MIPS_OPTIONS
;
1742 hdr
->sh_entsize
= 1;
1744 else if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) == 0)
1745 hdr
->sh_type
= SHT_MIPS_DWARF
;
1746 else if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1747 hdr
->sh_type
= SHT_MIPS_EVENTS
;
1752 /* Given a BFD section, try to locate the corresponding ELF section
1756 mips_elf_section_from_bfd_section (abfd
, hdr
, sec
, retval
)
1758 Elf32_Internal_Shdr
*hdr
;
1762 if (strcmp (bfd_get_section_name (abfd
, sec
), ".scommon") == 0)
1764 *retval
= SHN_MIPS_SCOMMON
;
1767 if (strcmp (bfd_get_section_name (abfd
, sec
), ".acommon") == 0)
1769 *retval
= SHN_MIPS_ACOMMON
;
1775 /* Work over a section just before writing it out. We update the GP
1776 value in the .reginfo section based on the value we are using.
1777 FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
1778 name; there has to be a better way. */
1781 mips_elf_section_processing (abfd
, hdr
)
1783 Elf32_Internal_Shdr
*hdr
;
1785 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1789 BFD_ASSERT (hdr
->sh_size
== sizeof (Elf32_External_RegInfo
));
1790 BFD_ASSERT (hdr
->contents
== NULL
);
1793 hdr
->sh_offset
+ sizeof (Elf32_External_RegInfo
) - 4,
1796 bfd_h_put_32 (abfd
, (bfd_vma
) elf_gp (abfd
), buf
);
1797 if (bfd_write (buf
, (bfd_size_type
) 1, (bfd_size_type
) 4, abfd
) != 4)
1801 if (hdr
->bfd_section
!= NULL
)
1803 const char *name
= bfd_get_section_name (abfd
, hdr
->bfd_section
);
1805 if (strcmp (name
, ".sdata") == 0)
1807 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1808 hdr
->sh_type
= SHT_PROGBITS
;
1810 else if (strcmp (name
, ".sbss") == 0)
1812 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1813 hdr
->sh_type
= SHT_NOBITS
;
1815 else if (strcmp (name
, ".lit8") == 0
1816 || strcmp (name
, ".lit4") == 0)
1818 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1819 hdr
->sh_type
= SHT_PROGBITS
;
1821 else if (strcmp (name
, ".compact_rel") == 0)
1824 hdr
->sh_type
= SHT_PROGBITS
;
1826 else if (strcmp (name
, ".rtproc") == 0)
1828 if (hdr
->sh_addralign
!= 0 && hdr
->sh_entsize
== 0)
1830 unsigned int adjust
;
1832 adjust
= hdr
->sh_size
% hdr
->sh_addralign
;
1834 hdr
->sh_size
+= hdr
->sh_addralign
- adjust
;
1842 /* MIPS ELF uses two common sections. One is the usual one, and the
1843 other is for small objects. All the small objects are kept
1844 together, and then referenced via the gp pointer, which yields
1845 faster assembler code. This is what we use for the small common
1846 section. This approach is copied from ecoff.c. */
1847 static asection mips_elf_scom_section
;
1848 static asymbol mips_elf_scom_symbol
;
1849 static asymbol
*mips_elf_scom_symbol_ptr
;
1851 /* MIPS ELF also uses an acommon section, which represents an
1852 allocated common symbol which may be overridden by a
1853 definition in a shared library. */
1854 static asection mips_elf_acom_section
;
1855 static asymbol mips_elf_acom_symbol
;
1856 static asymbol
*mips_elf_acom_symbol_ptr
;
1858 /* The Irix 5 support uses two virtual sections, which represent
1859 text/data symbols defined in dynamic objects. */
1860 static asection mips_elf_text_section
;
1861 static asection
*mips_elf_text_section_ptr
;
1862 static asymbol mips_elf_text_symbol
;
1863 static asymbol
*mips_elf_text_symbol_ptr
;
1865 static asection mips_elf_data_section
;
1866 static asection
*mips_elf_data_section_ptr
;
1867 static asymbol mips_elf_data_symbol
;
1868 static asymbol
*mips_elf_data_symbol_ptr
;
1870 /* Handle the special MIPS section numbers that a symbol may use. */
1873 mips_elf_symbol_processing (abfd
, asym
)
1877 elf_symbol_type
*elfsym
;
1879 elfsym
= (elf_symbol_type
*) asym
;
1880 switch (elfsym
->internal_elf_sym
.st_shndx
)
1882 case SHN_MIPS_ACOMMON
:
1883 /* This section is used in a dynamically linked executable file.
1884 It is an allocated common section. The dynamic linker can
1885 either resolve these symbols to something in a shared
1886 library, or it can just leave them here. For our purposes,
1887 we can consider these symbols to be in a new section. */
1888 if (mips_elf_acom_section
.name
== NULL
)
1890 /* Initialize the acommon section. */
1891 mips_elf_acom_section
.name
= ".acommon";
1892 mips_elf_acom_section
.flags
= SEC_ALLOC
;
1893 mips_elf_acom_section
.output_section
= &mips_elf_acom_section
;
1894 mips_elf_acom_section
.symbol
= &mips_elf_acom_symbol
;
1895 mips_elf_acom_section
.symbol_ptr_ptr
= &mips_elf_acom_symbol_ptr
;
1896 mips_elf_acom_symbol
.name
= ".acommon";
1897 mips_elf_acom_symbol
.flags
= BSF_SECTION_SYM
;
1898 mips_elf_acom_symbol
.section
= &mips_elf_acom_section
;
1899 mips_elf_acom_symbol_ptr
= &mips_elf_acom_symbol
;
1901 asym
->section
= &mips_elf_acom_section
;
1905 /* Common symbols less than the GP size are automatically
1906 treated as SHN_MIPS_SCOMMON symbols. */
1907 if (asym
->value
> elf_gp_size (abfd
))
1910 case SHN_MIPS_SCOMMON
:
1911 if (mips_elf_scom_section
.name
== NULL
)
1913 /* Initialize the small common section. */
1914 mips_elf_scom_section
.name
= ".scommon";
1915 mips_elf_scom_section
.flags
= SEC_IS_COMMON
;
1916 mips_elf_scom_section
.output_section
= &mips_elf_scom_section
;
1917 mips_elf_scom_section
.symbol
= &mips_elf_scom_symbol
;
1918 mips_elf_scom_section
.symbol_ptr_ptr
= &mips_elf_scom_symbol_ptr
;
1919 mips_elf_scom_symbol
.name
= ".scommon";
1920 mips_elf_scom_symbol
.flags
= BSF_SECTION_SYM
;
1921 mips_elf_scom_symbol
.section
= &mips_elf_scom_section
;
1922 mips_elf_scom_symbol_ptr
= &mips_elf_scom_symbol
;
1924 asym
->section
= &mips_elf_scom_section
;
1925 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
1928 case SHN_MIPS_SUNDEFINED
:
1929 asym
->section
= bfd_und_section_ptr
;
1932 #if 0 /* for SGI_COMPAT */
1934 asym
->section
= mips_elf_text_section_ptr
;
1938 asym
->section
= mips_elf_data_section_ptr
;
1944 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
1948 mips_elf_additional_program_headers (abfd
)
1956 if (! SGI_COMPAT (abfd
))
1959 s
= bfd_get_section_by_name (abfd
, ".reginfo");
1960 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1962 /* We need a PT_MIPS_REGINFO segment. */
1966 if (bfd_get_section_by_name (abfd
, ".interp") == NULL
1967 && bfd_get_section_by_name (abfd
, ".dynamic") != NULL
1968 && bfd_get_section_by_name (abfd
, ".mdebug") != NULL
)
1970 /* We need a PT_MIPS_RTPROC segment. */
1977 /* Modify the segment map for an Irix 5 executable. */
1980 mips_elf_modify_segment_map (abfd
)
1984 struct elf_segment_map
*m
, **pm
;
1986 if (! SGI_COMPAT (abfd
))
1989 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
1991 s
= bfd_get_section_by_name (abfd
, ".reginfo");
1992 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1994 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
1995 if (m
->p_type
== PT_MIPS_REGINFO
)
1999 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, sizeof *m
);
2003 m
->p_type
= PT_MIPS_REGINFO
;
2007 /* We want to put it after the PHDR and INTERP segments. */
2008 pm
= &elf_tdata (abfd
)->segment_map
;
2010 && ((*pm
)->p_type
== PT_PHDR
2011 || (*pm
)->p_type
== PT_INTERP
))
2019 /* If there are .dynamic and .mdebug sections, we make a room for
2020 the RTPROC header. FIXME: Rewrite without section names. */
2021 if (bfd_get_section_by_name (abfd
, ".interp") == NULL
2022 && bfd_get_section_by_name (abfd
, ".dynamic") != NULL
2023 && bfd_get_section_by_name (abfd
, ".mdebug") != NULL
)
2025 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2026 if (m
->p_type
== PT_MIPS_RTPROC
)
2030 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, sizeof *m
);
2034 m
->p_type
= PT_MIPS_RTPROC
;
2036 s
= bfd_get_section_by_name (abfd
, ".rtproc");
2041 m
->p_flags_valid
= 1;
2049 /* We want to put it after the DYNAMIC segment. */
2050 pm
= &elf_tdata (abfd
)->segment_map
;
2051 while (*pm
!= NULL
&& (*pm
)->p_type
!= PT_DYNAMIC
)
2061 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2062 .dynsym, and .hash sections, and everything in between. */
2063 for (pm
= &elf_tdata (abfd
)->segment_map
; *pm
!= NULL
; pm
= &(*pm
)->next
)
2064 if ((*pm
)->p_type
== PT_DYNAMIC
)
2069 && strcmp (m
->sections
[0]->name
, ".dynamic") == 0)
2071 static const char *sec_names
[] =
2072 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2075 struct elf_segment_map
*n
;
2079 for (i
= 0; i
< sizeof sec_names
/ sizeof sec_names
[0]; i
++)
2081 s
= bfd_get_section_by_name (abfd
, sec_names
[i
]);
2082 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2088 sz
= s
->_cooked_size
;
2091 if (high
< s
->vma
+ sz
)
2097 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2098 if ((s
->flags
& SEC_LOAD
) != 0
2101 + (s
->_cooked_size
!= 0 ? s
->_cooked_size
: s
->_raw_size
))
2105 n
= ((struct elf_segment_map
*)
2106 bfd_zalloc (abfd
, sizeof *n
+ (c
- 1) * sizeof (asection
*)));
2113 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2115 if ((s
->flags
& SEC_LOAD
) != 0
2118 + (s
->_cooked_size
!= 0 ? s
->_cooked_size
: s
->_raw_size
))
2132 /* The structure of the runtime procedure descriptor created by the
2133 loader for use by the static exception system. */
2135 typedef struct runtime_pdr
{
2136 bfd_vma adr
; /* memory address of start of procedure */
2137 long regmask
; /* save register mask */
2138 long regoffset
; /* save register offset */
2139 long fregmask
; /* save floating point register mask */
2140 long fregoffset
; /* save floating point register offset */
2141 long frameoffset
; /* frame size */
2142 short framereg
; /* frame pointer register */
2143 short pcreg
; /* offset or reg of return pc */
2144 long irpss
; /* index into the runtime string table */
2146 struct exception_info
*exception_info
;/* pointer to exception array */
2148 #define cbRPDR sizeof(RPDR)
2149 #define rpdNil ((pRPDR) 0)
2151 /* Swap RPDR (runtime procedure table entry) for output. */
2153 static void ecoff_swap_rpdr_out
2154 PARAMS ((bfd
*, const RPDR
*, struct rpdr_ext
*));
2157 ecoff_swap_rpdr_out (abfd
, in
, ex
)
2160 struct rpdr_ext
*ex
;
2162 /* ecoff_put_off was defined in ecoffswap.h. */
2163 ecoff_put_off (abfd
, in
->adr
, (bfd_byte
*) ex
->p_adr
);
2164 bfd_h_put_32 (abfd
, in
->regmask
, (bfd_byte
*) ex
->p_regmask
);
2165 bfd_h_put_32 (abfd
, in
->regoffset
, (bfd_byte
*) ex
->p_regoffset
);
2166 bfd_h_put_32 (abfd
, in
->fregmask
, (bfd_byte
*) ex
->p_fregmask
);
2167 bfd_h_put_32 (abfd
, in
->fregoffset
, (bfd_byte
*) ex
->p_fregoffset
);
2168 bfd_h_put_32 (abfd
, in
->frameoffset
, (bfd_byte
*) ex
->p_frameoffset
);
2170 bfd_h_put_16 (abfd
, in
->framereg
, (bfd_byte
*) ex
->p_framereg
);
2171 bfd_h_put_16 (abfd
, in
->pcreg
, (bfd_byte
*) ex
->p_pcreg
);
2173 bfd_h_put_32 (abfd
, in
->irpss
, (bfd_byte
*) ex
->p_irpss
);
2175 ecoff_put_off (abfd
, in
->exception_info
, (bfd_byte
*) ex
->p_exception_info
);
2179 /* Read ECOFF debugging information from a .mdebug section into a
2180 ecoff_debug_info structure. */
2183 mips_elf_read_ecoff_info (abfd
, section
, debug
)
2186 struct ecoff_debug_info
*debug
;
2189 const struct ecoff_debug_swap
*swap
;
2190 char *ext_hdr
= NULL
;
2192 swap
= get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2194 ext_hdr
= (char *) bfd_malloc ((size_t) swap
->external_hdr_size
);
2195 if (ext_hdr
== NULL
&& swap
->external_hdr_size
!= 0)
2198 if (bfd_get_section_contents (abfd
, section
, ext_hdr
, (file_ptr
) 0,
2199 swap
->external_hdr_size
)
2203 symhdr
= &debug
->symbolic_header
;
2204 (*swap
->swap_hdr_in
) (abfd
, ext_hdr
, symhdr
);
2206 /* The symbolic header contains absolute file offsets and sizes to
2208 #define READ(ptr, offset, count, size, type) \
2209 if (symhdr->count == 0) \
2210 debug->ptr = NULL; \
2213 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2214 if (debug->ptr == NULL) \
2215 goto error_return; \
2216 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2217 || (bfd_read (debug->ptr, size, symhdr->count, \
2218 abfd) != size * symhdr->count)) \
2219 goto error_return; \
2222 READ (line
, cbLineOffset
, cbLine
, sizeof (unsigned char), unsigned char *);
2223 READ (external_dnr
, cbDnOffset
, idnMax
, swap
->external_dnr_size
, PTR
);
2224 READ (external_pdr
, cbPdOffset
, ipdMax
, swap
->external_pdr_size
, PTR
);
2225 READ (external_sym
, cbSymOffset
, isymMax
, swap
->external_sym_size
, PTR
);
2226 READ (external_opt
, cbOptOffset
, ioptMax
, swap
->external_opt_size
, PTR
);
2227 READ (external_aux
, cbAuxOffset
, iauxMax
, sizeof (union aux_ext
),
2229 READ (ss
, cbSsOffset
, issMax
, sizeof (char), char *);
2230 READ (ssext
, cbSsExtOffset
, issExtMax
, sizeof (char), char *);
2231 READ (external_fdr
, cbFdOffset
, ifdMax
, swap
->external_fdr_size
, PTR
);
2232 READ (external_rfd
, cbRfdOffset
, crfd
, swap
->external_rfd_size
, PTR
);
2233 READ (external_ext
, cbExtOffset
, iextMax
, swap
->external_ext_size
, PTR
);
2237 debug
->adjust
= NULL
;
2242 if (ext_hdr
!= NULL
)
2244 if (debug
->line
!= NULL
)
2246 if (debug
->external_dnr
!= NULL
)
2247 free (debug
->external_dnr
);
2248 if (debug
->external_pdr
!= NULL
)
2249 free (debug
->external_pdr
);
2250 if (debug
->external_sym
!= NULL
)
2251 free (debug
->external_sym
);
2252 if (debug
->external_opt
!= NULL
)
2253 free (debug
->external_opt
);
2254 if (debug
->external_aux
!= NULL
)
2255 free (debug
->external_aux
);
2256 if (debug
->ss
!= NULL
)
2258 if (debug
->ssext
!= NULL
)
2259 free (debug
->ssext
);
2260 if (debug
->external_fdr
!= NULL
)
2261 free (debug
->external_fdr
);
2262 if (debug
->external_rfd
!= NULL
)
2263 free (debug
->external_rfd
);
2264 if (debug
->external_ext
!= NULL
)
2265 free (debug
->external_ext
);
2269 /* MIPS ELF local labels start with '$', not 'L'. */
2273 mips_elf_is_local_label (abfd
, symbol
)
2277 return symbol
->name
[0] == '$';
2280 /* MIPS ELF uses a special find_nearest_line routine in order the
2281 handle the ECOFF debugging information. */
2283 struct mips_elf_find_line
2285 struct ecoff_debug_info d
;
2286 struct ecoff_find_line i
;
2290 mips_elf_find_nearest_line (abfd
, section
, symbols
, offset
, filename_ptr
,
2291 functionname_ptr
, line_ptr
)
2296 const char **filename_ptr
;
2297 const char **functionname_ptr
;
2298 unsigned int *line_ptr
;
2302 msec
= bfd_get_section_by_name (abfd
, ".mdebug");
2306 struct mips_elf_find_line
*fi
;
2307 const struct ecoff_debug_swap
* const swap
=
2308 get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2310 /* If we are called during a link, mips_elf_final_link may have
2311 cleared the SEC_HAS_CONTENTS field. We force it back on here
2312 if appropriate (which it normally will be). */
2313 origflags
= msec
->flags
;
2314 if (elf_section_data (msec
)->this_hdr
.sh_type
!= SHT_NOBITS
)
2315 msec
->flags
|= SEC_HAS_CONTENTS
;
2317 fi
= elf_tdata (abfd
)->find_line_info
;
2320 bfd_size_type external_fdr_size
;
2323 struct fdr
*fdr_ptr
;
2325 fi
= ((struct mips_elf_find_line
*)
2326 bfd_alloc (abfd
, sizeof (struct mips_elf_find_line
)));
2329 msec
->flags
= origflags
;
2333 memset (fi
, 0, sizeof (struct mips_elf_find_line
));
2335 if (! mips_elf_read_ecoff_info (abfd
, msec
, &fi
->d
))
2337 msec
->flags
= origflags
;
2341 /* Swap in the FDR information. */
2342 fi
->d
.fdr
= ((struct fdr
*)
2344 (fi
->d
.symbolic_header
.ifdMax
*
2345 sizeof (struct fdr
))));
2346 if (fi
->d
.fdr
== NULL
)
2348 msec
->flags
= origflags
;
2351 external_fdr_size
= swap
->external_fdr_size
;
2352 fdr_ptr
= fi
->d
.fdr
;
2353 fraw_src
= (char *) fi
->d
.external_fdr
;
2354 fraw_end
= (fraw_src
2355 + fi
->d
.symbolic_header
.ifdMax
* external_fdr_size
);
2356 for (; fraw_src
< fraw_end
; fraw_src
+= external_fdr_size
, fdr_ptr
++)
2357 (*swap
->swap_fdr_in
) (abfd
, (PTR
) fraw_src
, fdr_ptr
);
2359 elf_tdata (abfd
)->find_line_info
= fi
;
2361 /* Note that we don't bother to ever free this information.
2362 find_nearest_line is either called all the time, as in
2363 objdump -l, so the information should be saved, or it is
2364 rarely called, as in ld error messages, so the memory
2365 wasted is unimportant. Still, it would probably be a
2366 good idea for free_cached_info to throw it away. */
2369 if (_bfd_ecoff_locate_line (abfd
, section
, offset
, &fi
->d
, swap
,
2370 &fi
->i
, filename_ptr
, functionname_ptr
,
2373 msec
->flags
= origflags
;
2377 msec
->flags
= origflags
;
2380 /* Fall back on the generic ELF find_nearest_line routine. */
2382 return _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
2383 filename_ptr
, functionname_ptr
,
2387 /* The MIPS ELF linker needs additional information for each symbol in
2388 the global hash table. */
2390 struct mips_elf_link_hash_entry
2392 struct elf_link_hash_entry root
;
2394 /* External symbol information. */
2398 /* MIPS ELF linker hash table. */
2400 struct mips_elf_link_hash_table
2402 struct elf_link_hash_table root
;
2403 /* String section indices for the dynamic section symbols. */
2404 bfd_size_type dynsym_sec_strindex
[SIZEOF_MIPS_DYNSYM_SECNAMES
];
2405 /* The number of .rtproc entries. */
2406 bfd_size_type procedure_count
;
2409 /* Look up an entry in a MIPS ELF linker hash table. */
2411 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
2412 ((struct mips_elf_link_hash_entry *) \
2413 elf_link_hash_lookup (&(table)->root, (string), (create), \
2416 /* Traverse a MIPS ELF linker hash table. */
2418 #define mips_elf_link_hash_traverse(table, func, info) \
2419 (elf_link_hash_traverse \
2421 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2424 /* Get the MIPS ELF linker hash table from a link_info structure. */
2426 #define mips_elf_hash_table(p) \
2427 ((struct mips_elf_link_hash_table *) ((p)->hash))
2429 static boolean mips_elf_output_extsym
2430 PARAMS ((struct mips_elf_link_hash_entry
*, PTR
));
2432 /* Create an entry in a MIPS ELF linker hash table. */
2434 static struct bfd_hash_entry
*
2435 mips_elf_link_hash_newfunc (entry
, table
, string
)
2436 struct bfd_hash_entry
*entry
;
2437 struct bfd_hash_table
*table
;
2440 struct mips_elf_link_hash_entry
*ret
=
2441 (struct mips_elf_link_hash_entry
*) entry
;
2443 /* Allocate the structure if it has not already been allocated by a
2445 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
2446 ret
= ((struct mips_elf_link_hash_entry
*)
2447 bfd_hash_allocate (table
,
2448 sizeof (struct mips_elf_link_hash_entry
)));
2449 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
2450 return (struct bfd_hash_entry
*) ret
;
2452 /* Call the allocation method of the superclass. */
2453 ret
= ((struct mips_elf_link_hash_entry
*)
2454 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2456 if (ret
!= (struct mips_elf_link_hash_entry
*) NULL
)
2458 /* Set local fields. */
2459 memset (&ret
->esym
, 0, sizeof (EXTR
));
2460 /* We use -2 as a marker to indicate that the information has
2461 not been set. -1 means there is no associated ifd. */
2465 return (struct bfd_hash_entry
*) ret
;
2468 /* Create a MIPS ELF linker hash table. */
2470 static struct bfd_link_hash_table
*
2471 mips_elf_link_hash_table_create (abfd
)
2474 struct mips_elf_link_hash_table
*ret
;
2477 ret
= ((struct mips_elf_link_hash_table
*)
2478 bfd_alloc (abfd
, sizeof (struct mips_elf_link_hash_table
)));
2479 if (ret
== (struct mips_elf_link_hash_table
*) NULL
)
2482 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2483 mips_elf_link_hash_newfunc
))
2485 bfd_release (abfd
, ret
);
2489 for (i
= 0; i
< SIZEOF_MIPS_DYNSYM_SECNAMES
; i
++)
2490 ret
->dynsym_sec_strindex
[i
] = (bfd_size_type
) -1;
2491 ret
->procedure_count
= 0;
2493 return &ret
->root
.root
;
2496 /* Hook called by the linker routine which adds symbols from an object
2497 file. We must handle the special MIPS section numbers here. */
2501 mips_elf_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
2503 struct bfd_link_info
*info
;
2504 const Elf_Internal_Sym
*sym
;
2510 switch (sym
->st_shndx
)
2513 /* Common symbols less than the GP size are automatically
2514 treated as SHN_MIPS_SCOMMON symbols. */
2515 if (sym
->st_size
> elf_gp_size (abfd
))
2518 case SHN_MIPS_SCOMMON
:
2519 *secp
= bfd_make_section_old_way (abfd
, ".scommon");
2520 (*secp
)->flags
|= SEC_IS_COMMON
;
2521 *valp
= sym
->st_size
;
2525 /* This section is used in a shared object. */
2526 if (mips_elf_text_section_ptr
== NULL
)
2528 /* Initialize the section. */
2529 mips_elf_text_section
.name
= ".text";
2530 mips_elf_text_section
.flags
= SEC_NO_FLAGS
;
2531 mips_elf_text_section
.output_section
= NULL
;
2532 mips_elf_text_section
.owner
= abfd
;
2533 mips_elf_text_section
.symbol
= &mips_elf_text_symbol
;
2534 mips_elf_text_section
.symbol_ptr_ptr
= &mips_elf_text_symbol_ptr
;
2535 mips_elf_text_symbol
.name
= ".text";
2536 mips_elf_text_symbol
.flags
= BSF_SECTION_SYM
;
2537 mips_elf_text_symbol
.section
= &mips_elf_text_section
;
2538 mips_elf_text_symbol_ptr
= &mips_elf_text_symbol
;
2539 mips_elf_text_section_ptr
= &mips_elf_text_section
;
2542 *secp
= bfd_und_section_ptr
;
2544 *secp
= mips_elf_text_section_ptr
;
2548 /* This section is used in a shared object. */
2549 if (mips_elf_data_section_ptr
== NULL
)
2551 /* Initialize the section. */
2552 mips_elf_data_section
.name
= ".data";
2553 mips_elf_data_section
.flags
= SEC_NO_FLAGS
;
2554 mips_elf_data_section
.output_section
= NULL
;
2555 mips_elf_data_section
.owner
= abfd
;
2556 mips_elf_data_section
.symbol
= &mips_elf_data_symbol
;
2557 mips_elf_data_section
.symbol_ptr_ptr
= &mips_elf_data_symbol_ptr
;
2558 mips_elf_data_symbol
.name
= ".data";
2559 mips_elf_data_symbol
.flags
= BSF_SECTION_SYM
;
2560 mips_elf_data_symbol
.section
= &mips_elf_data_section
;
2561 mips_elf_data_symbol_ptr
= &mips_elf_data_symbol
;
2562 mips_elf_data_section_ptr
= &mips_elf_data_section
;
2565 *secp
= bfd_und_section_ptr
;
2567 *secp
= mips_elf_data_section_ptr
;
2570 case SHN_MIPS_SUNDEFINED
:
2571 *secp
= bfd_und_section_ptr
;
2578 /* Structure used to pass information to mips_elf_output_extsym. */
2583 struct bfd_link_info
*info
;
2584 struct ecoff_debug_info
*debug
;
2585 const struct ecoff_debug_swap
*swap
;
2589 /* This routine is used to write out ECOFF debugging external symbol
2590 information. It is called via mips_elf_link_hash_traverse. The
2591 ECOFF external symbol information must match the ELF external
2592 symbol information. Unfortunately, at this point we don't know
2593 whether a symbol is required by reloc information, so the two
2594 tables may wind up being different. We must sort out the external
2595 symbol information before we can set the final size of the .mdebug
2596 section, and we must set the size of the .mdebug section before we
2597 can relocate any sections, and we can't know which symbols are
2598 required by relocation until we relocate the sections.
2599 Fortunately, it is relatively unlikely that any symbol will be
2600 stripped but required by a reloc. In particular, it can not happen
2601 when generating a final executable. */
2604 mips_elf_output_extsym (h
, data
)
2605 struct mips_elf_link_hash_entry
*h
;
2608 struct extsym_info
*einfo
= (struct extsym_info
*) data
;
2610 asection
*sec
, *output_section
;
2612 if (h
->root
.indx
== -2)
2614 else if (((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2615 || (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
2616 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2617 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
2619 else if (einfo
->info
->strip
== strip_all
2620 || (einfo
->info
->strip
== strip_some
2621 && bfd_hash_lookup (einfo
->info
->keep_hash
,
2622 h
->root
.root
.root
.string
,
2623 false, false) == NULL
))
2631 if (h
->esym
.ifd
== -2)
2634 h
->esym
.cobol_main
= 0;
2635 h
->esym
.weakext
= 0;
2636 h
->esym
.reserved
= 0;
2637 h
->esym
.ifd
= ifdNil
;
2638 h
->esym
.asym
.value
= 0;
2639 h
->esym
.asym
.st
= stGlobal
;
2641 if (SGI_COMPAT (einfo
->abfd
)
2642 && (h
->root
.root
.type
== bfd_link_hash_undefined
2643 || h
->root
.root
.type
== bfd_link_hash_undefweak
))
2647 /* Use undefined class. Also, set class and type for some
2649 name
= h
->root
.root
.root
.string
;
2650 if (strcmp (name
, mips_elf_dynsym_rtproc_names
[0]) == 0
2651 || strcmp (name
, mips_elf_dynsym_rtproc_names
[1]) == 0)
2653 h
->esym
.asym
.sc
= scData
;
2654 h
->esym
.asym
.st
= stLabel
;
2655 h
->esym
.asym
.value
= 0;
2657 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[2]) == 0)
2659 h
->esym
.asym
.sc
= scAbs
;
2660 h
->esym
.asym
.st
= stLabel
;
2661 h
->esym
.asym
.value
=
2662 mips_elf_hash_table (einfo
->info
)->procedure_count
;
2664 else if (strcmp (name
, "_gp_disp") == 0)
2666 h
->esym
.asym
.sc
= scAbs
;
2667 h
->esym
.asym
.st
= stLabel
;
2668 h
->esym
.asym
.value
= elf_gp (einfo
->abfd
);
2671 h
->esym
.asym
.sc
= scUndefined
;
2673 else if (h
->root
.root
.type
!= bfd_link_hash_defined
2674 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
2675 h
->esym
.asym
.sc
= scAbs
;
2680 sec
= h
->root
.root
.u
.def
.section
;
2681 output_section
= sec
->output_section
;
2683 /* When making a shared library and symbol h is the one from
2684 the another shared library, OUTPUT_SECTION may be null. */
2685 if (output_section
== NULL
)
2686 h
->esym
.asym
.sc
= scUndefined
;
2689 name
= bfd_section_name (output_section
->owner
, output_section
);
2691 if (strcmp (name
, ".text") == 0)
2692 h
->esym
.asym
.sc
= scText
;
2693 else if (strcmp (name
, ".data") == 0)
2694 h
->esym
.asym
.sc
= scData
;
2695 else if (strcmp (name
, ".sdata") == 0)
2696 h
->esym
.asym
.sc
= scSData
;
2697 else if (strcmp (name
, ".rodata") == 0
2698 || strcmp (name
, ".rdata") == 0)
2699 h
->esym
.asym
.sc
= scRData
;
2700 else if (strcmp (name
, ".bss") == 0)
2701 h
->esym
.asym
.sc
= scBss
;
2702 else if (strcmp (name
, ".sbss") == 0)
2703 h
->esym
.asym
.sc
= scSBss
;
2704 else if (strcmp (name
, ".init") == 0)
2705 h
->esym
.asym
.sc
= scInit
;
2706 else if (strcmp (name
, ".fini") == 0)
2707 h
->esym
.asym
.sc
= scFini
;
2709 h
->esym
.asym
.sc
= scAbs
;
2713 h
->esym
.asym
.reserved
= 0;
2714 h
->esym
.asym
.index
= indexNil
;
2717 if (h
->root
.root
.type
== bfd_link_hash_common
)
2718 h
->esym
.asym
.value
= h
->root
.root
.u
.c
.size
;
2719 else if (h
->root
.root
.type
== bfd_link_hash_defined
2720 || h
->root
.root
.type
== bfd_link_hash_defweak
)
2722 if (h
->esym
.asym
.sc
== scCommon
)
2723 h
->esym
.asym
.sc
= scBss
;
2724 else if (h
->esym
.asym
.sc
== scSCommon
)
2725 h
->esym
.asym
.sc
= scSBss
;
2727 sec
= h
->root
.root
.u
.def
.section
;
2728 output_section
= sec
->output_section
;
2729 if (output_section
!= NULL
)
2730 h
->esym
.asym
.value
= (h
->root
.root
.u
.def
.value
2731 + sec
->output_offset
2732 + output_section
->vma
);
2734 h
->esym
.asym
.value
= 0;
2736 else if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
2738 /* Set type and value for a symbol with a function stub. */
2739 h
->esym
.asym
.st
= stProc
;
2740 sec
= h
->root
.root
.u
.def
.section
;
2742 h
->esym
.asym
.value
= 0;
2745 output_section
= sec
->output_section
;
2746 if (output_section
!= NULL
)
2747 h
->esym
.asym
.value
= (h
->root
.plt_offset
2748 + sec
->output_offset
2749 + output_section
->vma
);
2751 h
->esym
.asym
.value
= 0;
2758 if (! bfd_ecoff_debug_one_external (einfo
->abfd
, einfo
->debug
, einfo
->swap
,
2759 h
->root
.root
.root
.string
,
2762 einfo
->failed
= true;
2769 /* Create a runtime procedure table from the .mdebug section. */
2772 mips_elf_create_procedure_table (handle
, abfd
, info
, s
, debug
)
2775 struct bfd_link_info
*info
;
2777 struct ecoff_debug_info
*debug
;
2779 const struct ecoff_debug_swap
*swap
;
2780 HDRR
*hdr
= &debug
->symbolic_header
;
2782 struct rpdr_ext
*erp
;
2784 struct pdr_ext
*epdr
;
2785 struct sym_ext
*esym
;
2788 unsigned long size
, count
;
2789 unsigned long sindex
;
2793 const char *no_name_func
= "static procedure (no name)";
2800 swap
= get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2802 sindex
= strlen (no_name_func
) + 1;
2803 count
= hdr
->ipdMax
;
2806 size
= swap
->external_pdr_size
;
2808 epdr
= (struct pdr_ext
*) bfd_malloc (size
* count
);
2812 if (! _bfd_ecoff_get_accumulated_pdr (handle
, (PTR
) epdr
))
2815 size
= sizeof (RPDR
);
2816 rp
= rpdr
= (RPDR
*) bfd_malloc (size
* count
);
2820 sv
= (char **) bfd_malloc (sizeof (char *) * count
);
2824 count
= hdr
->isymMax
;
2825 size
= swap
->external_sym_size
;
2826 esym
= (struct sym_ext
*) bfd_malloc (size
* count
);
2830 if (! _bfd_ecoff_get_accumulated_sym (handle
, (PTR
) esym
))
2833 count
= hdr
->issMax
;
2834 ss
= (char *) bfd_malloc (count
);
2837 if (! _bfd_ecoff_get_accumulated_ss (handle
, (PTR
) ss
))
2840 count
= hdr
->ipdMax
;
2841 for (i
= 0; i
< count
; i
++, rp
++, epdr
++)
2843 (*swap
->swap_pdr_in
) (abfd
, (PTR
) epdr
, &pdr
);
2844 (*swap
->swap_sym_in
) (abfd
, (PTR
) &esym
[pdr
.isym
], &sym
);
2845 rp
->adr
= sym
.value
;
2846 rp
->regmask
= pdr
.regmask
;
2847 rp
->regoffset
= pdr
.regoffset
;
2848 rp
->fregmask
= pdr
.fregmask
;
2849 rp
->fregoffset
= pdr
.fregoffset
;
2850 rp
->frameoffset
= pdr
.frameoffset
;
2851 rp
->framereg
= pdr
.framereg
;
2852 rp
->pcreg
= pdr
.pcreg
;
2854 sv
[i
] = ss
+ sym
.iss
;
2855 sindex
+= strlen (sv
[i
]) + 1;
2859 size
= sizeof (struct rpdr_ext
) * (count
+ 2) + sindex
;
2860 size
= BFD_ALIGN (size
, 16);
2861 rtproc
= (PTR
) bfd_alloc (abfd
, size
);
2864 mips_elf_hash_table (info
)->procedure_count
= 0;
2868 mips_elf_hash_table (info
)->procedure_count
= count
+ 2;
2870 erp
= (struct rpdr_ext
*) rtproc
;
2871 memset (rp
, 0, sizeof (struct rpdr_ext
));
2873 str
= (char *) rtproc
+ sizeof (struct rpdr_ext
) * (count
+ 2);
2874 strcpy (str
, no_name_func
);
2875 str
+= strlen (no_name_func
) + 1;
2876 for (i
= 0; i
< count
; i
++)
2878 ecoff_swap_rpdr_out (abfd
, rpdr
+ i
, erp
+ i
);
2879 strcpy (str
, sv
[i
]);
2880 str
+= strlen (sv
[i
]) + 1;
2882 ecoff_put_off (abfd
, (bfd_vma
) -1, (bfd_byte
*) (erp
+ count
)->p_adr
);
2884 /* Set the size and contents of .rtproc section. */
2885 s
->_raw_size
= size
;
2886 s
->contents
= rtproc
;
2888 /* Skip this section later on (I don't think this currently
2889 matters, but someday it might). */
2890 s
->link_order_head
= (struct bfd_link_order
*) NULL
;
2914 /* A comparison routine used to sort .gptab entries. */
2917 gptab_compare (p1
, p2
)
2921 const Elf32_gptab
*a1
= (const Elf32_gptab
*) p1
;
2922 const Elf32_gptab
*a2
= (const Elf32_gptab
*) p2
;
2924 return a1
->gt_entry
.gt_g_value
- a2
->gt_entry
.gt_g_value
;
2927 /* We need to use a special link routine to handle the .reginfo and
2928 the .mdebug sections. We need to merge all instances of these
2929 sections together, not write them all out sequentially. */
2932 mips_elf_final_link (abfd
, info
)
2934 struct bfd_link_info
*info
;
2938 struct bfd_link_order
*p
;
2939 asection
*reginfo_sec
, *mdebug_sec
, *gptab_data_sec
, *gptab_bss_sec
;
2940 asection
*rtproc_sec
;
2941 Elf32_RegInfo reginfo
;
2942 struct ecoff_debug_info debug
;
2943 const struct ecoff_debug_swap
*swap
2944 = get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2945 HDRR
*symhdr
= &debug
.symbolic_header
;
2946 PTR mdebug_handle
= NULL
;
2948 /* Drop the .options section, since it has special semantics which I
2949 haven't bothered to figure out. */
2950 for (secpp
= &abfd
->sections
; *secpp
!= NULL
; secpp
= &(*secpp
)->next
)
2952 if (strcmp ((*secpp
)->name
, ".options") == 0)
2954 for (p
= (*secpp
)->link_order_head
; p
!= NULL
; p
= p
->next
)
2955 if (p
->type
== bfd_indirect_link_order
)
2956 p
->u
.indirect
.section
->flags
&=~ SEC_HAS_CONTENTS
;
2957 (*secpp
)->link_order_head
= NULL
;
2958 *secpp
= (*secpp
)->next
;
2959 --abfd
->section_count
;
2964 /* Get a value for the GP register. */
2965 if (elf_gp (abfd
) == 0)
2967 struct bfd_link_hash_entry
*h
;
2969 h
= bfd_link_hash_lookup (info
->hash
, "_gp", false, false, true);
2970 if (h
!= (struct bfd_link_hash_entry
*) NULL
2971 && h
->type
== bfd_link_hash_defined
)
2972 elf_gp (abfd
) = (h
->u
.def
.value
2973 + h
->u
.def
.section
->output_section
->vma
2974 + h
->u
.def
.section
->output_offset
);
2975 else if (info
->relocateable
)
2979 /* Make up a value. */
2981 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
2984 && (strcmp (o
->name
, ".sbss") == 0
2985 || strcmp (o
->name
, ".sdata") == 0
2986 || strcmp (o
->name
, ".lit4") == 0
2987 || strcmp (o
->name
, ".lit8") == 0))
2990 elf_gp (abfd
) = lo
+ ELF_MIPS_GP_OFFSET (abfd
);
2994 /* If the relocate_section function needs to do a reloc
2995 involving the GP value, it should make a reloc_dangerous
2996 callback to warn that GP is not defined. */
3000 /* Go through the sections and collect the .reginfo and .mdebug
3004 gptab_data_sec
= NULL
;
3005 gptab_bss_sec
= NULL
;
3006 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3008 if (strcmp (o
->name
, ".reginfo") == 0)
3010 memset (®info
, 0, sizeof reginfo
);
3012 /* We have found the .reginfo section in the output file.
3013 Look through all the link_orders comprising it and merge
3014 the information together. */
3015 for (p
= o
->link_order_head
;
3016 p
!= (struct bfd_link_order
*) NULL
;
3019 asection
*input_section
;
3021 Elf32_External_RegInfo ext
;
3024 if (p
->type
!= bfd_indirect_link_order
)
3026 if (p
->type
== bfd_fill_link_order
)
3031 input_section
= p
->u
.indirect
.section
;
3032 input_bfd
= input_section
->owner
;
3034 /* The linker emulation code has probably clobbered the
3035 size to be zero bytes. */
3036 if (input_section
->_raw_size
== 0)
3037 input_section
->_raw_size
= sizeof (Elf32_External_RegInfo
);
3039 if (! bfd_get_section_contents (input_bfd
, input_section
,
3045 bfd_mips_elf32_swap_reginfo_in (input_bfd
, &ext
, &sub
);
3047 reginfo
.ri_gprmask
|= sub
.ri_gprmask
;
3048 reginfo
.ri_cprmask
[0] |= sub
.ri_cprmask
[0];
3049 reginfo
.ri_cprmask
[1] |= sub
.ri_cprmask
[1];
3050 reginfo
.ri_cprmask
[2] |= sub
.ri_cprmask
[2];
3051 reginfo
.ri_cprmask
[3] |= sub
.ri_cprmask
[3];
3053 /* ri_gp_value is set by the function
3054 mips_elf_section_processing when the section is
3055 finally written out. */
3057 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3058 elf_link_input_bfd ignores this section. */
3059 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3062 /* Force the section size to the value we want. */
3063 o
->_raw_size
= sizeof (Elf32_External_RegInfo
);
3065 /* Skip this section later on (I don't think this currently
3066 matters, but someday it might). */
3067 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3072 if (strcmp (o
->name
, ".mdebug") == 0)
3074 struct extsym_info einfo
;
3076 /* We have found the .mdebug section in the output file.
3077 Look through all the link_orders comprising it and merge
3078 the information together. */
3079 symhdr
->magic
= swap
->sym_magic
;
3080 /* FIXME: What should the version stamp be? */
3082 symhdr
->ilineMax
= 0;
3086 symhdr
->isymMax
= 0;
3087 symhdr
->ioptMax
= 0;
3088 symhdr
->iauxMax
= 0;
3090 symhdr
->issExtMax
= 0;
3093 symhdr
->iextMax
= 0;
3095 /* We accumulate the debugging information itself in the
3096 debug_info structure. */
3098 debug
.external_dnr
= NULL
;
3099 debug
.external_pdr
= NULL
;
3100 debug
.external_sym
= NULL
;
3101 debug
.external_opt
= NULL
;
3102 debug
.external_aux
= NULL
;
3104 debug
.ssext
= debug
.ssext_end
= NULL
;
3105 debug
.external_fdr
= NULL
;
3106 debug
.external_rfd
= NULL
;
3107 debug
.external_ext
= debug
.external_ext_end
= NULL
;
3109 mdebug_handle
= bfd_ecoff_debug_init (abfd
, &debug
, swap
, info
);
3110 if (mdebug_handle
== (PTR
) NULL
)
3113 if (SGI_COMPAT (abfd
))
3119 static const char * const name
[] =
3120 { ".text", ".init", ".fini", ".data",
3121 ".rodata", ".sdata", ".sbss", ".bss" };
3122 static const int sc
[] = { scText
, scInit
, scFini
, scData
,
3123 scRData
, scSData
, scSBss
, scBss
};
3126 esym
.cobol_main
= 0;
3130 esym
.asym
.iss
= issNil
;
3131 esym
.asym
.st
= stLocal
;
3132 esym
.asym
.reserved
= 0;
3133 esym
.asym
.index
= indexNil
;
3134 for (i
= 0; i
< 8; i
++)
3136 esym
.asym
.sc
= sc
[i
];
3137 s
= bfd_get_section_by_name (abfd
, name
[i
]);
3140 esym
.asym
.value
= s
->vma
;
3141 last
= s
->vma
+ s
->_raw_size
;
3144 esym
.asym
.value
= last
;
3146 if (! bfd_ecoff_debug_one_external (abfd
, &debug
, swap
,
3152 for (p
= o
->link_order_head
;
3153 p
!= (struct bfd_link_order
*) NULL
;
3156 asection
*input_section
;
3158 const struct ecoff_debug_swap
*input_swap
;
3159 struct ecoff_debug_info input_debug
;
3163 if (p
->type
!= bfd_indirect_link_order
)
3165 if (p
->type
== bfd_fill_link_order
)
3170 input_section
= p
->u
.indirect
.section
;
3171 input_bfd
= input_section
->owner
;
3173 if (bfd_get_flavour (input_bfd
) != bfd_target_elf_flavour
3174 || (get_elf_backend_data (input_bfd
)
3175 ->elf_backend_ecoff_debug_swap
) == NULL
)
3177 /* I don't know what a non MIPS ELF bfd would be
3178 doing with a .mdebug section, but I don't really
3179 want to deal with it. */
3183 input_swap
= (get_elf_backend_data (input_bfd
)
3184 ->elf_backend_ecoff_debug_swap
);
3186 BFD_ASSERT (p
->size
== input_section
->_raw_size
);
3188 /* The ECOFF linking code expects that we have already
3189 read in the debugging information and set up an
3190 ecoff_debug_info structure, so we do that now. */
3191 if (! mips_elf_read_ecoff_info (input_bfd
, input_section
,
3195 if (! (bfd_ecoff_debug_accumulate
3196 (mdebug_handle
, abfd
, &debug
, swap
, input_bfd
,
3197 &input_debug
, input_swap
, info
)))
3200 /* Loop through the external symbols. For each one with
3201 interesting information, try to find the symbol in
3202 the linker global hash table and save the information
3203 for the output external symbols. */
3204 eraw_src
= input_debug
.external_ext
;
3205 eraw_end
= (eraw_src
3206 + (input_debug
.symbolic_header
.iextMax
3207 * input_swap
->external_ext_size
));
3209 eraw_src
< eraw_end
;
3210 eraw_src
+= input_swap
->external_ext_size
)
3214 struct mips_elf_link_hash_entry
*h
;
3216 (*input_swap
->swap_ext_in
) (input_bfd
, (PTR
) eraw_src
, &ext
);
3217 if (ext
.asym
.sc
== scNil
3218 || ext
.asym
.sc
== scUndefined
3219 || ext
.asym
.sc
== scSUndefined
)
3222 name
= input_debug
.ssext
+ ext
.asym
.iss
;
3223 h
= mips_elf_link_hash_lookup (mips_elf_hash_table (info
),
3224 name
, false, false, true);
3225 if (h
== NULL
|| h
->esym
.ifd
!= -2)
3231 < input_debug
.symbolic_header
.ifdMax
);
3232 ext
.ifd
= input_debug
.ifdmap
[ext
.ifd
];
3238 /* Free up the information we just read. */
3239 free (input_debug
.line
);
3240 free (input_debug
.external_dnr
);
3241 free (input_debug
.external_pdr
);
3242 free (input_debug
.external_sym
);
3243 free (input_debug
.external_opt
);
3244 free (input_debug
.external_aux
);
3245 free (input_debug
.ss
);
3246 free (input_debug
.ssext
);
3247 free (input_debug
.external_fdr
);
3248 free (input_debug
.external_rfd
);
3249 free (input_debug
.external_ext
);
3251 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3252 elf_link_input_bfd ignores this section. */
3253 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3256 if (SGI_COMPAT (abfd
) && info
->shared
)
3258 /* Create .rtproc section. */
3259 rtproc_sec
= bfd_get_section_by_name (abfd
, ".rtproc");
3260 if (rtproc_sec
== NULL
)
3262 flagword flags
= (SEC_HAS_CONTENTS
| SEC_IN_MEMORY
3265 rtproc_sec
= bfd_make_section (abfd
, ".rtproc");
3266 if (rtproc_sec
== NULL
3267 || ! bfd_set_section_flags (abfd
, rtproc_sec
, flags
)
3268 || ! bfd_set_section_alignment (abfd
, rtproc_sec
, 12))
3272 if (! mips_elf_create_procedure_table (mdebug_handle
, abfd
,
3273 info
, rtproc_sec
, &debug
))
3277 /* Build the external symbol information. */
3280 einfo
.debug
= &debug
;
3282 einfo
.failed
= false;
3283 mips_elf_link_hash_traverse (mips_elf_hash_table (info
),
3284 mips_elf_output_extsym
,
3289 /* Set the size of the .mdebug section. */
3290 o
->_raw_size
= bfd_ecoff_debug_size (abfd
, &debug
, swap
);
3292 /* Skip this section later on (I don't think this currently
3293 matters, but someday it might). */
3294 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3299 if (strncmp (o
->name
, ".gptab.", sizeof ".gptab." - 1) == 0)
3301 const char *subname
;
3304 Elf32_External_gptab
*ext_tab
;
3307 /* The .gptab.sdata and .gptab.sbss sections hold
3308 information describing how the small data area would
3309 change depending upon the -G switch. These sections
3310 not used in executables files. */
3311 if (! info
->relocateable
)
3315 for (p
= o
->link_order_head
;
3316 p
!= (struct bfd_link_order
*) NULL
;
3319 asection
*input_section
;
3321 if (p
->type
!= bfd_indirect_link_order
)
3323 if (p
->type
== bfd_fill_link_order
)
3328 input_section
= p
->u
.indirect
.section
;
3330 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3331 elf_link_input_bfd ignores this section. */
3332 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3335 /* Skip this section later on (I don't think this
3336 currently matters, but someday it might). */
3337 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3339 /* Really remove the section. */
3340 for (secpp
= &abfd
->sections
;
3342 secpp
= &(*secpp
)->next
)
3344 *secpp
= (*secpp
)->next
;
3345 --abfd
->section_count
;
3350 /* There is one gptab for initialized data, and one for
3351 uninitialized data. */
3352 if (strcmp (o
->name
, ".gptab.sdata") == 0)
3354 else if (strcmp (o
->name
, ".gptab.sbss") == 0)
3358 (*_bfd_error_handler
)
3359 ("%s: illegal section name `%s'",
3360 bfd_get_filename (abfd
), o
->name
);
3361 bfd_set_error (bfd_error_nonrepresentable_section
);
3365 /* The linker script always combines .gptab.data and
3366 .gptab.sdata into .gptab.sdata, and likewise for
3367 .gptab.bss and .gptab.sbss. It is possible that there is
3368 no .sdata or .sbss section in the output file, in which
3369 case we must change the name of the output section. */
3370 subname
= o
->name
+ sizeof ".gptab" - 1;
3371 if (bfd_get_section_by_name (abfd
, subname
) == NULL
)
3373 if (o
== gptab_data_sec
)
3374 o
->name
= ".gptab.data";
3376 o
->name
= ".gptab.bss";
3377 subname
= o
->name
+ sizeof ".gptab" - 1;
3378 BFD_ASSERT (bfd_get_section_by_name (abfd
, subname
) != NULL
);
3381 /* Set up the first entry. */
3383 tab
= (Elf32_gptab
*) bfd_malloc (c
* sizeof (Elf32_gptab
));
3386 tab
[0].gt_header
.gt_current_g_value
= elf_gp_size (abfd
);
3387 tab
[0].gt_header
.gt_unused
= 0;
3389 /* Combine the input sections. */
3390 for (p
= o
->link_order_head
;
3391 p
!= (struct bfd_link_order
*) NULL
;
3394 asection
*input_section
;
3398 bfd_size_type gpentry
;
3400 if (p
->type
!= bfd_indirect_link_order
)
3402 if (p
->type
== bfd_fill_link_order
)
3407 input_section
= p
->u
.indirect
.section
;
3408 input_bfd
= input_section
->owner
;
3410 /* Combine the gptab entries for this input section one
3411 by one. We know that the input gptab entries are
3412 sorted by ascending -G value. */
3413 size
= bfd_section_size (input_bfd
, input_section
);
3415 for (gpentry
= sizeof (Elf32_External_gptab
);
3417 gpentry
+= sizeof (Elf32_External_gptab
))
3419 Elf32_External_gptab ext_gptab
;
3420 Elf32_gptab int_gptab
;
3426 if (! (bfd_get_section_contents
3427 (input_bfd
, input_section
, (PTR
) &ext_gptab
,
3428 gpentry
, sizeof (Elf32_External_gptab
))))
3434 bfd_mips_elf32_swap_gptab_in (input_bfd
, &ext_gptab
,
3436 val
= int_gptab
.gt_entry
.gt_g_value
;
3437 add
= int_gptab
.gt_entry
.gt_bytes
- last
;
3440 for (look
= 1; look
< c
; look
++)
3442 if (tab
[look
].gt_entry
.gt_g_value
>= val
)
3443 tab
[look
].gt_entry
.gt_bytes
+= add
;
3445 if (tab
[look
].gt_entry
.gt_g_value
== val
)
3451 Elf32_gptab
*new_tab
;
3454 /* We need a new table entry. */
3455 new_tab
= ((Elf32_gptab
*)
3456 bfd_realloc ((PTR
) tab
,
3457 (c
+ 1) * sizeof (Elf32_gptab
)));
3458 if (new_tab
== NULL
)
3464 tab
[c
].gt_entry
.gt_g_value
= val
;
3465 tab
[c
].gt_entry
.gt_bytes
= add
;
3467 /* Merge in the size for the next smallest -G
3468 value, since that will be implied by this new
3471 for (look
= 1; look
< c
; look
++)
3473 if (tab
[look
].gt_entry
.gt_g_value
< val
3475 || (tab
[look
].gt_entry
.gt_g_value
3476 > tab
[max
].gt_entry
.gt_g_value
)))
3480 tab
[c
].gt_entry
.gt_bytes
+=
3481 tab
[max
].gt_entry
.gt_bytes
;
3486 last
= int_gptab
.gt_entry
.gt_bytes
;
3489 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3490 elf_link_input_bfd ignores this section. */
3491 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3494 /* The table must be sorted by -G value. */
3496 qsort (tab
+ 1, c
- 1, sizeof (tab
[0]), gptab_compare
);
3498 /* Swap out the table. */
3499 ext_tab
= ((Elf32_External_gptab
*)
3500 bfd_alloc (abfd
, c
* sizeof (Elf32_External_gptab
)));
3501 if (ext_tab
== NULL
)
3507 for (i
= 0; i
< c
; i
++)
3508 bfd_mips_elf32_swap_gptab_out (abfd
, tab
+ i
, ext_tab
+ i
);
3511 o
->_raw_size
= c
* sizeof (Elf32_External_gptab
);
3512 o
->contents
= (bfd_byte
*) ext_tab
;
3514 /* Skip this section later on (I don't think this currently
3515 matters, but someday it might). */
3516 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3520 /* Invoke the regular ELF backend linker to do all the work. */
3521 if (! bfd_elf32_bfd_final_link (abfd
, info
))
3524 /* Now write out the computed sections. */
3526 if (reginfo_sec
!= (asection
*) NULL
)
3528 Elf32_External_RegInfo ext
;
3530 bfd_mips_elf32_swap_reginfo_out (abfd
, ®info
, &ext
);
3531 if (! bfd_set_section_contents (abfd
, reginfo_sec
, (PTR
) &ext
,
3532 (file_ptr
) 0, sizeof ext
))
3536 if (mdebug_sec
!= (asection
*) NULL
)
3538 BFD_ASSERT (abfd
->output_has_begun
);
3539 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle
, abfd
, &debug
,
3541 mdebug_sec
->filepos
))
3544 bfd_ecoff_debug_free (mdebug_handle
, abfd
, &debug
, swap
, info
);
3547 if (gptab_data_sec
!= (asection
*) NULL
)
3549 if (! bfd_set_section_contents (abfd
, gptab_data_sec
,
3550 gptab_data_sec
->contents
,
3552 gptab_data_sec
->_raw_size
))
3556 if (gptab_bss_sec
!= (asection
*) NULL
)
3558 if (! bfd_set_section_contents (abfd
, gptab_bss_sec
,
3559 gptab_bss_sec
->contents
,
3561 gptab_bss_sec
->_raw_size
))
3565 if (SGI_COMPAT (abfd
))
3567 rtproc_sec
= bfd_get_section_by_name (abfd
, ".rtproc");
3568 if (rtproc_sec
!= NULL
)
3570 if (! bfd_set_section_contents (abfd
, rtproc_sec
,
3571 rtproc_sec
->contents
,
3573 rtproc_sec
->_raw_size
))
3581 /* Handle a MIPS ELF HI16 reloc. */
3584 mips_elf_relocate_hi16 (input_bfd
, relhi
, rello
, contents
, addend
)
3586 Elf_Internal_Rela
*relhi
;
3587 Elf_Internal_Rela
*rello
;
3594 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
3596 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
3599 addend
+= ((insn
& 0xffff) << 16) + addlo
;
3601 if ((addlo
& 0x8000) != 0)
3603 if ((addend
& 0x8000) != 0)
3606 bfd_put_32 (input_bfd
,
3607 (insn
& 0xffff0000) | ((addend
>> 16) & 0xffff),
3608 contents
+ relhi
->r_offset
);
3611 /* Handle a MIPS ELF local GOT16 reloc. */
3614 mips_elf_relocate_got_local (output_bfd
, input_bfd
, sgot
, relhi
, rello
,
3619 Elf_Internal_Rela
*relhi
;
3620 Elf_Internal_Rela
*rello
;
3630 bfd_byte
*got_contents
;
3631 struct mips_got_info
*g
;
3633 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
3635 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
3638 addend
+= ((insn
& 0xffff) << 16) + addlo
;
3640 if ((addlo
& 0x8000) != 0)
3642 if ((addend
& 0x8000) != 0)
3645 /* Get a got entry representing requested hipage. */
3646 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
3647 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
3648 BFD_ASSERT (g
!= NULL
);
3650 local_gotno
= g
->local_gotno
;
3651 got_contents
= sgot
->contents
;
3652 hipage
= addend
& 0xffff0000;
3654 for (i
= MIPS_RESERVED_GOTNO
; i
< local_gotno
; i
++)
3656 address
= bfd_get_32 (input_bfd
, got_contents
+ i
* 4);
3657 if (hipage
== (address
& 0xffff0000))
3659 if (address
== (bfd_vma
) 0)
3661 bfd_put_32 (input_bfd
, hipage
, got_contents
+ i
* 4);
3666 BFD_ASSERT (i
< local_gotno
);
3668 if (i
== local_gotno
)
3669 (*_bfd_error_handler
)
3670 ("ELF MIPS linker: more got entries are needed for hipage: %x",
3674 i
= - ELF_MIPS_GP_OFFSET (output_bfd
) + i
* 4;
3675 bfd_put_32 (input_bfd
, (insn
& 0xffff0000) | (i
& 0xffff),
3676 contents
+ relhi
->r_offset
);
3679 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
3682 mips_elf_relocate_global_got (input_bfd
, rel
, contents
, offset
)
3684 Elf_Internal_Rela
*rel
;
3690 insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3691 bfd_put_32 (input_bfd
,
3692 (insn
& 0xffff0000) | (offset
& 0xffff),
3693 contents
+ rel
->r_offset
);
3696 /* Relocate a MIPS ELF section. */
3699 mips_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
3700 contents
, relocs
, local_syms
, local_sections
)
3702 struct bfd_link_info
*info
;
3704 asection
*input_section
;
3706 Elf_Internal_Rela
*relocs
;
3707 Elf_Internal_Sym
*local_syms
;
3708 asection
**local_sections
;
3710 Elf_Internal_Shdr
*symtab_hdr
;
3713 asection
*sgot
, *sreloc
, *scpt
;
3715 Elf_Internal_Rela
*rel
;
3716 Elf_Internal_Rela
*relend
;
3717 struct mips_got_info
*g
;
3719 dynobj
= elf_hash_table (info
)->dynobj
;
3720 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3724 if (dynobj
== NULL
|| ! SGI_COMPAT (output_bfd
))
3727 scpt
= bfd_get_section_by_name (dynobj
, ".compact_rel");
3730 if (elf_bad_symtab (input_bfd
))
3732 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
3737 locsymcount
= symtab_hdr
->sh_info
;
3738 extsymoff
= symtab_hdr
->sh_info
;
3742 relend
= relocs
+ input_section
->reloc_count
;
3743 for (; rel
< relend
; rel
++)
3746 reloc_howto_type
*howto
;
3747 unsigned long r_symndx
;
3749 struct elf_link_hash_entry
*h
;
3751 Elf_Internal_Sym
*sym
;
3752 bfd_reloc_status_type r
;
3754 r_type
= ELF32_R_TYPE (rel
->r_info
);
3755 if (r_type
< 0 || r_type
>= (int) R_MIPS_max
)
3757 bfd_set_error (bfd_error_bad_value
);
3760 howto
= elf_mips_howto_table
+ r_type
;
3763 && (r_type
== R_MIPS_CALL16
3764 || r_type
== R_MIPS_GOT16
3765 || r_type
== R_MIPS_CALL_HI16
3766 || r_type
== R_MIPS_CALL_LO16
3767 || r_type
== R_MIPS_GOT_HI16
3768 || r_type
== R_MIPS_GOT_LO16
))
3770 /* We need the .got section. */
3773 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3774 BFD_ASSERT (sgot
!= NULL
);
3775 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
3776 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
3777 BFD_ASSERT (g
!= NULL
);
3781 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3783 /* Mix in the change in GP address for a GP relative reloc. */
3784 if (r_type
!= R_MIPS_GPREL16
3785 && r_type
!= R_MIPS_LITERAL
3786 && r_type
!= R_MIPS_GPREL32
)
3790 if (elf_gp (output_bfd
) == 0)
3792 if (! ((*info
->callbacks
->reloc_dangerous
)
3794 "GP relative relocation when GP not defined",
3795 input_bfd
, input_section
,
3798 /* Only give the error once per link. */
3799 elf_gp (output_bfd
) = 4;
3802 if (r_symndx
< extsymoff
3803 || (elf_bad_symtab (input_bfd
)
3804 && local_sections
[r_symndx
] != NULL
))
3806 /* This is a relocation against a section. The current
3807 addend in the instruction is the difference between
3808 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
3809 must change this to be the difference between the
3810 final definition (which will end up in RELOCATION)
3811 and the GP value of OUTPUT_BFD (which is in GP). */
3812 addend
= elf_gp (input_bfd
) - elf_gp (output_bfd
);
3814 else if (! info
->relocateable
)
3816 /* We are doing a final link. The current addend in the
3817 instruction is simply the desired offset into the
3818 symbol (normally zero). We want the instruction to
3819 hold the difference between the final definition of
3820 the symbol (which will end up in RELOCATION) and the
3821 GP value of OUTPUT_BFD (which is in GP). */
3822 addend
= - elf_gp (output_bfd
);
3826 /* We are generating relocateable output, and we aren't
3827 going to define this symbol, so we just leave the
3828 instruction alone. */
3836 if (info
->relocateable
)
3838 /* This is a relocateable link. We don't have to change
3839 anything, unless the reloc is against a section symbol,
3840 in which case we have to adjust according to where the
3841 section symbol winds up in the output section. */
3842 if (r_symndx
>= locsymcount
3843 || (elf_bad_symtab (input_bfd
)
3844 && local_sections
[r_symndx
] == NULL
))
3848 sym
= local_syms
+ r_symndx
;
3849 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3853 sec
= local_sections
[r_symndx
];
3855 /* It would be logical to add sym->st_value here,
3856 but Irix 5 sometimes generates a garbage symbol
3858 addend
+= sec
->output_offset
;
3860 /* If this is HI16 or GOT16 with an associated LO16,
3861 adjust the addend accordingly. Otherwise, just
3863 if ((r_type
!= R_MIPS_HI16
|| r_type
== R_MIPS_GOT16
)
3864 || (rel
+ 1) >= relend
3865 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
3866 r
= _bfd_relocate_contents (howto
, input_bfd
,
3868 contents
+ rel
->r_offset
);
3871 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
3883 /* This is a final link. */
3885 if (r_symndx
< extsymoff
3886 || (elf_bad_symtab (input_bfd
)
3887 && local_sections
[r_symndx
] != NULL
))
3890 sym
= local_syms
+ r_symndx
;
3891 sec
= local_sections
[r_symndx
];
3892 relocation
= (sec
->output_section
->vma
3893 + sec
->output_offset
);
3895 /* It would be logical to always add sym->st_value here,
3896 but Irix 5 sometimes generates a garbage symbol
3898 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3899 relocation
+= sym
->st_value
;
3906 indx
= r_symndx
- extsymoff
;
3907 h
= elf_sym_hashes (input_bfd
)[indx
];
3908 if (strcmp (h
->root
.root
.string
, "_gp_disp") == 0)
3910 if (elf_gp (output_bfd
) == 0)
3912 if (! ((*info
->callbacks
->reloc_dangerous
)
3914 "_gp_disp used when GP not defined",
3915 input_bfd
, input_section
,
3918 /* Only give the error once per link. */
3919 elf_gp (output_bfd
) = 4;
3924 sec
= input_section
;
3925 if (sec
->output_section
!= NULL
)
3926 relocation
= (elf_gp (output_bfd
)
3928 + sec
->output_section
->vma
3929 + sec
->output_offset
));
3931 relocation
= elf_gp (output_bfd
) - rel
->r_offset
;
3932 if (r_type
== R_MIPS_LO16
)
3936 else if (h
->root
.type
== bfd_link_hash_defined
3937 || h
->root
.type
== bfd_link_hash_defweak
)
3939 sec
= h
->root
.u
.def
.section
;
3940 if (sec
->output_section
== NULL
)
3943 relocation
= (h
->root
.u
.def
.value
3944 + sec
->output_section
->vma
3945 + sec
->output_offset
);
3947 else if (h
->root
.type
== bfd_link_hash_undefweak
)
3949 else if (info
->shared
&& ! info
->symbolic
)
3953 if (! ((*info
->callbacks
->undefined_symbol
)
3954 (info
, h
->root
.root
.string
, input_bfd
,
3955 input_section
, rel
->r_offset
)))
3961 if (r_type
== R_MIPS_HI16
3962 && (rel
+ 1) < relend
3963 && ELF32_R_TYPE ((rel
+ 1)->r_info
) == R_MIPS_LO16
)
3965 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
3966 contents
, relocation
+ addend
);
3969 else if (r_type
== R_MIPS_GOT16
&& local
)
3971 /* GOT16 must be also with associated LO16 in the local
3972 case. In this case, the addend is extracted and the
3973 section in which the referenced object is determined.
3974 Then the final address of the object is computed and
3975 the GOT entry for the hipage (an aligned 64kb chunk)
3976 is added to .got section if needed. The offset field
3977 of the GOT16-relocated instruction is replaced by the
3978 index of this GOT entry for the hipage. */
3979 if ((rel
+ 1) < relend
3980 && ELF32_R_TYPE ((rel
+ 1)->r_info
) == R_MIPS_LO16
)
3982 mips_elf_relocate_got_local (output_bfd
, input_bfd
, sgot
,
3985 relocation
+ addend
);
3989 r
= bfd_reloc_outofrange
;
3991 else if (r_type
== R_MIPS_CALL16
3992 || r_type
== R_MIPS_GOT16
3993 || r_type
== R_MIPS_CALL_LO16
3994 || r_type
== R_MIPS_GOT_LO16
)
3998 /* This symbol must be registered as a global symbol
3999 having the corresponding got entry. */
4000 BFD_ASSERT (h
->got_offset
!= (bfd_vma
) -1);
4002 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
4003 BFD_ASSERT (g
->local_gotno
<= offset
4004 && offset
< sgot
->_raw_size
);
4005 bfd_put_32 (output_bfd
, relocation
+ addend
,
4006 sgot
->contents
+ offset
);
4007 offset
= (sgot
->output_section
->vma
+ sgot
->output_offset
4008 + offset
- elf_gp (output_bfd
));
4009 mips_elf_relocate_global_got (input_bfd
, rel
, contents
,
4013 else if (r_type
== R_MIPS_CALL_HI16
4014 || r_type
== R_MIPS_GOT_HI16
)
4018 /* This must be a global symbol with a got entry. The
4019 next reloc must be the corresponding LO16 reloc. */
4020 BFD_ASSERT (h
!= NULL
&& h
->got_offset
!= (bfd_vma
) -1);
4021 BFD_ASSERT ((rel
+ 1) < relend
);
4022 BFD_ASSERT (ELF32_R_TYPE ((rel
+ 1)->r_info
)
4023 == (r_type
== R_MIPS_CALL_HI16
4025 : R_MIPS_GOT_LO16
));
4027 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
4028 BFD_ASSERT (g
->local_gotno
<= offset
4029 && offset
< sgot
->_raw_size
);
4030 bfd_put_32 (output_bfd
, relocation
+ addend
,
4031 sgot
->contents
+ offset
);
4032 offset
= (sgot
->output_section
->vma
+ sgot
->output_offset
4033 + offset
- elf_gp (output_bfd
));
4034 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1, contents
,
4038 else if (r_type
== R_MIPS_REL32
4039 || r_type
== R_MIPS_32
)
4041 Elf_Internal_Rel outrel
;
4042 Elf32_crinfo cptrel
;
4046 && (input_section
->flags
& SEC_ALLOC
) != 0)
4048 /* When generating a shared object, these
4049 relocations are copied into the output file to be
4050 resolved at run time. */
4053 sreloc
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
4054 BFD_ASSERT (sreloc
!= NULL
);
4057 outrel
.r_offset
= (rel
->r_offset
4058 + input_section
->output_section
->vma
4059 + input_section
->output_offset
);
4061 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
4065 BFD_ASSERT (h
->dynindx
!= -1);
4066 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MIPS_REL32
);
4067 sec
= input_section
;
4073 sym
= local_syms
+ r_symndx
;
4075 BFD_ASSERT (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
);
4077 sec
= local_sections
[r_symndx
];
4078 if (sec
!= NULL
&& bfd_is_abs_section (sec
))
4080 else if (sec
== NULL
|| sec
->owner
== NULL
)
4082 bfd_set_error (bfd_error_bad_value
);
4089 osec
= sec
->output_section
;
4090 indx
= elf_section_data (osec
)->dynindx
;
4095 outrel
.r_info
= ELF32_R_INFO (indx
, R_MIPS_REL32
);
4096 addend
+= relocation
;
4099 bfd_put_32 (output_bfd
, addend
, contents
+ rel
->r_offset
);
4100 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
4101 (((Elf32_External_Rel
*)
4103 + sreloc
->reloc_count
));
4104 ++sreloc
->reloc_count
;
4106 if (SGI_COMPAT (output_bfd
))
4111 /* Make an entry of compact relocation info. */
4112 mips_elf_set_cr_format (cptrel
, CRF_MIPS_LONG
);
4113 cptrel
.vaddr
= (rel
->r_offset
4114 + input_section
->output_section
->vma
4115 + input_section
->output_offset
);
4116 if (r_type
== R_MIPS_REL32
)
4117 mips_elf_set_cr_type (cptrel
, CRT_MIPS_REL32
);
4119 mips_elf_set_cr_type (cptrel
, CRT_MIPS_WORD
);
4120 cptrel
.konst
= addend
;
4122 cr
= (scpt
->contents
4123 + sizeof (Elf32_External_compact_rel
));
4124 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4125 ((Elf32_External_crinfo
*) cr
4126 + scpt
->reloc_count
));
4127 ++scpt
->reloc_count
;
4130 /* This reloc will be computed at runtime, so
4131 there's no need to do anything now. */
4135 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4136 contents
, rel
->r_offset
,
4137 relocation
, addend
);
4140 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4141 contents
, rel
->r_offset
,
4142 relocation
, addend
);
4144 if (SGI_COMPAT (abfd
)
4146 && (input_section
->flags
& SEC_ALLOC
) != 0)
4148 Elf32_crinfo cptrel
;
4151 /* Make an entry of compact relocation info. */
4152 mips_elf_set_cr_format (cptrel
, CRF_MIPS_LONG
);
4153 cptrel
.vaddr
= (rel
->r_offset
4154 + input_section
->output_section
->vma
4155 + input_section
->output_offset
);
4160 mips_elf_set_cr_type (cptrel
, CRT_MIPS_JMPAD
);
4161 cptrel
.konst
= addend
;
4162 cr
= scpt
->contents
+ sizeof (Elf32_External_compact_rel
);
4163 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4164 ((Elf32_External_crinfo
*) cr
4165 + scpt
->reloc_count
));
4166 ++scpt
->reloc_count
;
4169 case R_MIPS_GPREL16
:
4170 case R_MIPS_LITERAL
:
4171 case R_MIPS_GPREL32
:
4172 mips_elf_set_cr_type (cptrel
, CRT_MIPS_GPHI_LO
);
4173 cptrel
.konst
= elf_gp (output_bfd
) - cptrel
.vaddr
;
4174 cr
= scpt
->contents
+ sizeof (Elf32_External_compact_rel
);
4175 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4176 ((Elf32_External_crinfo
*) cr
4177 + scpt
->reloc_count
));
4178 ++scpt
->reloc_count
;
4187 if (r
!= bfd_reloc_ok
)
4192 case bfd_reloc_outofrange
:
4194 case bfd_reloc_overflow
:
4199 name
= h
->root
.root
.string
;
4202 name
= bfd_elf_string_from_elf_section (input_bfd
,
4203 symtab_hdr
->sh_link
,
4208 name
= bfd_section_name (input_bfd
, sec
);
4210 if (! ((*info
->callbacks
->reloc_overflow
)
4211 (info
, name
, howto
->name
, (bfd_vma
) 0,
4212 input_bfd
, input_section
, rel
->r_offset
)))
4223 /* Functions for the dynamic linker. */
4225 /* The name of the dynamic interpreter. This is put in the .interp
4228 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
4230 /* Create dynamic sections when linking against a dynamic object. */
4231 static boolean
mips_elf_create_got_section (bfd
*, struct bfd_link_info
*);
4234 mips_elf_create_dynamic_sections (abfd
, info
)
4236 struct bfd_link_info
*info
;
4238 struct elf_link_hash_entry
*h
;
4240 register asection
*s
;
4241 const char * const *namep
;
4243 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4246 /* Mips ABI requests the .dynamic section to be read only. */
4247 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4250 if (! bfd_set_section_flags (abfd
, s
, flags
))
4254 /* We need to create .got section. */
4255 if (! mips_elf_create_got_section (abfd
, info
))
4258 /* Create .stub section. */
4259 if (bfd_get_section_by_name (abfd
, ".stub") == NULL
)
4261 s
= bfd_make_section (abfd
, ".stub");
4263 || ! bfd_set_section_flags (abfd
, s
, flags
)
4264 || ! bfd_set_section_alignment (abfd
, s
, 2))
4268 if (SGI_COMPAT (abfd
))
4270 for (namep
= mips_elf_dynsym_rtproc_names
; *namep
!= NULL
; namep
++)
4273 if (! (_bfd_generic_link_add_one_symbol
4274 (info
, abfd
, *namep
, BSF_GLOBAL
, bfd_und_section_ptr
,
4275 (bfd_vma
) 0, (const char *) NULL
, false,
4276 get_elf_backend_data (abfd
)->collect
,
4277 (struct bfd_link_hash_entry
**) &h
)))
4279 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4280 h
->type
= STT_SECTION
;
4283 && ! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4287 /* Create .compact_rel section. */
4288 if (bfd_get_section_by_name (abfd
, ".compact_rel") == NULL
)
4290 flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_READONLY
;
4292 s
= bfd_make_section (abfd
, ".compact_rel");
4294 || ! bfd_set_section_flags (abfd
, s
, flags
)
4295 || ! bfd_set_section_alignment (abfd
, s
, 2))
4299 s
->_raw_size
= sizeof (Elf32_compact_rel
);
4301 /* Change aligments of some sections. */
4302 s
= bfd_get_section_by_name (abfd
, ".hash");
4304 bfd_set_section_alignment (abfd
, s
, 4);
4305 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4307 bfd_set_section_alignment (abfd
, s
, 4);
4308 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4310 bfd_set_section_alignment (abfd
, s
, 4);
4311 s
= bfd_get_section_by_name (abfd
, ".reginfo");
4313 bfd_set_section_alignment (abfd
, s
, 4);
4314 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4316 bfd_set_section_alignment (abfd
, s
, 4);
4322 /* Create the .got section to hold the global offset table. */
4325 mips_elf_create_got_section (abfd
, info
)
4327 struct bfd_link_info
*info
;
4330 register asection
*s
;
4331 struct elf_link_hash_entry
*h
;
4332 struct mips_got_info
*g
;
4334 /* This function may be called more than once. */
4335 if (bfd_get_section_by_name (abfd
, ".got") != NULL
)
4338 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
4340 s
= bfd_make_section (abfd
, ".got");
4342 || ! bfd_set_section_flags (abfd
, s
, flags
)
4343 || ! bfd_set_section_alignment (abfd
, s
, 4))
4346 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4347 linker script because we don't want to define the symbol if we
4348 are not creating a global offset table. XXX Should this be
4349 defined at the start of the .got section, even if .got section is
4350 accessed by using 16 bit signed offset? */
4352 if (! (_bfd_generic_link_add_one_symbol
4353 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
4354 (bfd_vma
) 0, (const char *) NULL
, false,
4355 get_elf_backend_data (abfd
)->collect
,
4356 (struct bfd_link_hash_entry
**) &h
)))
4358 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4359 h
->type
= STT_OBJECT
;
4362 && ! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4365 /* The first several global offset table entries are reserved. */
4366 s
->_raw_size
= MIPS_RESERVED_GOTNO
* 4;
4368 g
= (struct mips_got_info
*) bfd_alloc (abfd
,
4369 sizeof (struct mips_got_info
));
4372 g
->global_gotsym
= 0;
4373 g
->local_gotno
= MIPS_RESERVED_GOTNO
;
4374 if (elf_section_data (s
) == NULL
)
4377 (PTR
) bfd_zalloc (abfd
, sizeof (struct bfd_elf_section_data
));
4378 if (elf_section_data (s
) == NULL
)
4381 elf_section_data (s
)->tdata
= (PTR
) g
;
4386 /* Look through the relocs for a section during the first phase, and
4387 allocate space in the global offset table. */
4390 mips_elf_check_relocs (abfd
, info
, sec
, relocs
)
4392 struct bfd_link_info
*info
;
4394 const Elf_Internal_Rela
*relocs
;
4397 Elf_Internal_Shdr
*symtab_hdr
;
4398 struct elf_link_hash_entry
**sym_hashes
;
4399 struct mips_got_info
*g
;
4401 const Elf_Internal_Rela
*rel
;
4402 const Elf_Internal_Rela
*rel_end
;
4407 if (info
->relocateable
)
4410 dynobj
= elf_hash_table (info
)->dynobj
;
4411 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4412 sym_hashes
= elf_sym_hashes (abfd
);
4413 extsymoff
= (elf_bad_symtab (abfd
)) ? 0 : symtab_hdr
->sh_info
;
4417 if (SGI_COMPAT (abfd
) && dynobj
!= NULL
)
4418 scpt
= bfd_get_section_by_name (dynobj
, ".compact_rel");
4422 rel_end
= relocs
+ sec
->reloc_count
;
4423 for (rel
= relocs
; rel
< rel_end
; rel
++)
4425 unsigned long r_symndx
;
4426 struct elf_link_hash_entry
*h
;
4428 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4430 if (r_symndx
< extsymoff
)
4433 h
= sym_hashes
[r_symndx
- extsymoff
];
4435 /* Some relocs require a global offset table. */
4438 switch (ELF32_R_TYPE (rel
->r_info
))
4442 case R_MIPS_CALL_HI16
:
4443 case R_MIPS_CALL_LO16
:
4444 case R_MIPS_GOT_HI16
:
4445 case R_MIPS_GOT_LO16
:
4446 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
4447 if (! mips_elf_create_got_section (dynobj
, info
))
4456 switch (ELF32_R_TYPE (rel
->r_info
))
4459 case R_MIPS_CALL_HI16
:
4460 case R_MIPS_CALL_LO16
:
4461 /* This symbol requires a global offset table entry. */
4464 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4465 BFD_ASSERT (sgot
!= NULL
);
4466 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4467 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4468 BFD_ASSERT (g
!= NULL
);
4471 BFD_ASSERT (h
!= NULL
);
4473 /* Make sure this symbol is output as a dynamic symbol. */
4474 if (h
->dynindx
== -1)
4476 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4480 if (h
->got_offset
!= (bfd_vma
) -1)
4482 /* We have already allocated space in the .got. */
4486 /* Note the index of the first global got symbol in .dynsym. */
4487 if (g
->global_gotsym
== 0
4488 || g
->global_gotsym
> (unsigned long) h
->dynindx
)
4489 g
->global_gotsym
= h
->dynindx
;
4491 /* Make this symbol to have the corresponding got entry. */
4494 /* We need a stub, not a plt entry for the undefined
4495 function. But we record it as if it needs plt. See
4496 elf_adjust_dynamic_symbol in elflink.h. */
4497 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
4503 case R_MIPS_GOT_HI16
:
4504 case R_MIPS_GOT_LO16
:
4505 /* This symbol requires a global offset table entry. */
4508 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4509 BFD_ASSERT (sgot
!= NULL
);
4510 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4511 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4512 BFD_ASSERT (g
!= NULL
);
4517 /* Make sure this symbol is output as a dynamic symbol. */
4518 if (h
->dynindx
== -1)
4520 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4524 if (h
->got_offset
!= (bfd_vma
) -1)
4526 /* We have already allocated space in the .got. */
4529 /* Note the index of the first global got symbol in
4531 if (g
->global_gotsym
== 0
4532 || g
->global_gotsym
> (unsigned long) h
->dynindx
)
4533 g
->global_gotsym
= h
->dynindx
;
4535 /* Make this symbol to be the global got symbol. */
4544 && (sec
->flags
& SEC_ALLOC
) != 0)
4546 /* When creating a shared object, we must copy these
4547 reloc types into the output file as R_MIPS_REL32
4548 relocs. We create the .rel.dyn reloc section in
4549 dynobj and make room for this reloc. */
4552 const char *name
= ".rel.dyn";
4554 sreloc
= bfd_get_section_by_name (dynobj
, name
);
4557 sreloc
= bfd_make_section (dynobj
, name
);
4559 || ! bfd_set_section_flags (dynobj
, sreloc
,
4565 || ! bfd_set_section_alignment (dynobj
, sreloc
, 4))
4568 /* Add a null element. */
4569 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
4570 ++sreloc
->reloc_count
;
4574 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
4577 scpt
->_raw_size
+= sizeof (Elf32_External_crinfo
);
4583 case R_MIPS_GPREL16
:
4584 case R_MIPS_LITERAL
:
4585 case R_MIPS_GPREL32
:
4587 scpt
->_raw_size
+= sizeof (Elf32_External_crinfo
);
4598 /* Adjust a symbol defined by a dynamic object and referenced by a
4599 regular object. The current definition is in some section of the
4600 dynamic object, but we're not including those sections. We have to
4601 change the definition to something the rest of the link can
4605 mips_elf_adjust_dynamic_symbol (info
, h
)
4606 struct bfd_link_info
*info
;
4607 struct elf_link_hash_entry
*h
;
4612 dynobj
= elf_hash_table (info
)->dynobj
;
4614 /* Make sure we know what is going on here. */
4615 BFD_ASSERT (dynobj
!= NULL
4616 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
4617 || h
->weakdef
!= NULL
4618 || ((h
->elf_link_hash_flags
4619 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4620 && (h
->elf_link_hash_flags
4621 & ELF_LINK_HASH_REF_REGULAR
) != 0
4622 && (h
->elf_link_hash_flags
4623 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
4625 /* For a function, create a stub, if needed. */
4626 if (h
->type
== STT_FUNC
4627 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
4629 if (! elf_hash_table (info
)->dynamic_sections_created
)
4632 /* If this symbol is not defined in a regular file, then set
4633 the symbol to the stub location. This is required to make
4634 function pointers compare as equal between the normal
4635 executable and the shared library. */
4636 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4638 /* We need .stub section. */
4639 s
= bfd_get_section_by_name (dynobj
, ".stub");
4640 BFD_ASSERT (s
!= NULL
);
4642 h
->root
.u
.def
.section
= s
;
4643 h
->root
.u
.def
.value
= s
->_raw_size
;
4645 /* XXX Write this stub address somewhere. */
4646 h
->plt_offset
= s
->_raw_size
;
4648 /* Make room for this stub code. */
4649 s
->_raw_size
+= MIPS_FUNCTION_STUB_SIZE
;
4651 /* The last half word of the stub will be filled with the index
4652 of this symbol in .dynsym section. */
4657 /* If this is a weak symbol, and there is a real definition, the
4658 processor independent code will have arranged for us to see the
4659 real definition first, and we can just use the same value. */
4660 if (h
->weakdef
!= NULL
)
4662 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
4663 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
4664 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
4665 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
4669 /* This is a reference to a symbol defined by a dynamic object which
4670 is not a function. */
4675 /* Set the sizes of the dynamic sections. */
4678 mips_elf_size_dynamic_sections (output_bfd
, info
)
4680 struct bfd_link_info
*info
;
4686 struct mips_got_info
*g
;
4688 dynobj
= elf_hash_table (info
)->dynobj
;
4689 BFD_ASSERT (dynobj
!= NULL
);
4691 if (elf_hash_table (info
)->dynamic_sections_created
)
4693 /* Set the contents of the .interp section to the interpreter. */
4696 s
= bfd_get_section_by_name (dynobj
, ".interp");
4697 BFD_ASSERT (s
!= NULL
);
4698 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4699 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4703 /* Recompute the size of .got for local entires (reserved and
4704 hipages) if needed. To estimate it, get the upper bound of total
4705 size of loadable sections. */
4706 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4710 bfd_size_type loadable_size
= 0;
4711 bfd_size_type local_gotno
;
4714 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4715 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4716 BFD_ASSERT (g
!= NULL
);
4718 for (sub
= info
->input_bfds
; sub
; sub
= sub
->link_next
)
4719 for (s
= sub
->sections
; s
!= NULL
; s
= s
->next
)
4721 if ((s
->flags
& SEC_ALLOC
) == 0)
4723 loadable_size
+= (s
->_raw_size
+ 0xf) & ~0xf;
4726 loadable_size
+= MIPS_FUNCTION_STUB_SIZE
;
4728 /* Assume there are two loadable segments consisting of
4729 contiguous sections. Is 5 enough? */
4730 local_gotno
= (loadable_size
>> 16) + 5 + MIPS_RESERVED_GOTNO
;
4731 g
->local_gotno
= local_gotno
;
4732 sgot
->_raw_size
+= local_gotno
* 4;
4735 /* The check_relocs and adjust_dynamic_symbol entry points have
4736 determined the sizes of the various dynamic sections. Allocate
4739 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4744 /* It's OK to base decisions on the section name, because none
4745 of the dynobj section names depend upon the input files. */
4746 name
= bfd_get_section_name (dynobj
, s
);
4748 if ((s
->flags
& SEC_IN_MEMORY
) == 0)
4753 if (strncmp (name
, ".rel", 4) == 0)
4755 if (s
->_raw_size
== 0)
4761 /* If this relocation section applies to a read only
4762 section, then we probably need a DT_TEXTREL entry.
4763 If the relocation section is .rel.dyn, we always
4764 assert a DT_TEXTREL entry rather than testing whether
4765 there exists a relocation to a read only section or
4767 target
= bfd_get_section_by_name (output_bfd
, name
+ 4);
4768 if ((target
!= NULL
&& (target
->flags
& SEC_READONLY
) != 0)
4769 || strcmp (name
, ".rel.dyn") == 0)
4772 /* We use the reloc_count field as a counter if we need
4773 to copy relocs into the output file. */
4774 if (strcmp (name
, ".rel.dyn") != 0)
4778 else if (strncmp (name
, ".got", 4) == 0)
4782 BFD_ASSERT (elf_section_data (s
) != NULL
);
4783 g
= (struct mips_got_info
*) elf_section_data (s
)->tdata
;
4784 BFD_ASSERT (g
!= NULL
);
4786 /* Fix the size of .got section for the correspondence of
4787 global symbols and got entries. This adds some useless
4788 got entries. Is this required by ABI really? */
4789 i
= elf_hash_table (info
)->dynsymcount
- g
->global_gotsym
;
4790 s
->_raw_size
+= i
* 4;
4792 else if (strncmp (name
, ".stub", 5) == 0)
4794 /* Irix rld assumes that the function stub isn't at the end
4795 of .text section. So put a dummy. XXX */
4796 s
->_raw_size
+= MIPS_FUNCTION_STUB_SIZE
;
4798 else if (strncmp (name
, ".init", 5) != 0
4799 && (! SGI_COMPAT (output_bfd
)
4800 || strncmp (name
, ".compact_rel", 12) != 0))
4802 /* It's not one of our sections, so don't allocate space. */
4810 for (spp
= &s
->output_section
->owner
->sections
;
4811 *spp
!= s
->output_section
;
4812 spp
= &(*spp
)->next
)
4814 *spp
= s
->output_section
->next
;
4815 --s
->output_section
->owner
->section_count
;
4820 /* Allocate memory for the section contents. */
4821 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
4822 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
4824 bfd_set_error (bfd_error_no_memory
);
4827 memset (s
->contents
, 0, s
->_raw_size
);
4830 if (elf_hash_table (info
)->dynamic_sections_created
)
4832 /* Add some entries to the .dynamic section. We fill in the
4833 values later, in elf_mips_finish_dynamic_sections, but we
4834 must add the entries now so that we get the correct size for
4835 the .dynamic section. The DT_DEBUG entry is filled in by the
4836 dynamic linker and used by the debugger. */
4839 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
4845 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
4849 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0))
4852 if (bfd_get_section_by_name (dynobj
, ".rel.dyn"))
4854 if (! bfd_elf32_add_dynamic_entry (info
, DT_REL
, 0))
4857 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELSZ
, 0))
4860 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELENT
, 0))
4864 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_CONFLICTNO
, 0))
4867 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LIBLISTNO
, 0))
4870 if (bfd_get_section_by_name (dynobj
, ".conflict"))
4872 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_CONFLICT
, 0))
4875 s
= bfd_get_section_by_name (dynobj
, ".liblist");
4876 BFD_ASSERT (s
!= NULL
);
4878 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LIBLIST
, 0))
4882 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_RLD_VERSION
, 0))
4885 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_FLAGS
, 0))
4888 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_TIME_STAMP
, 0))
4892 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_ICHECKSUM
, 0))
4897 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_IVERSION
, 0))
4901 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_BASE_ADDRESS
, 0))
4904 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LOCAL_GOTNO
, 0))
4907 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_SYMTABNO
, 0))
4910 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_UNREFEXTNO
, 0))
4913 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_GOTSYM
, 0))
4916 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_HIPAGENO
, 0))
4919 #if 0 /* (SGI_COMPAT) */
4920 if (! bfd_get_section_by_name (dynobj
, ".init"))
4921 if (! bfd_elf32_add_dynamic_entry (info
, DT_INIT
, 0))
4924 if (! bfd_get_section_by_name (dynobj
, ".fini"))
4925 if (! bfd_elf32_add_dynamic_entry (info
, DT_FINI
, 0))
4930 /* If we are generating a shared library, we generate a section
4931 symbol for each output section. These are local symbols, which
4932 means that they must come first in the dynamic symbol table.
4933 That means we must increment the dynamic symbol index of every
4934 other dynamic symbol. */
4937 const char * const *namep
;
4939 bfd_size_type strindex
;
4940 struct bfd_strtab_hash
*dynstr
;
4941 struct mips_got_info
*g
;
4943 if (SGI_COMPAT (output_bfd
))
4945 c
= SIZEOF_MIPS_DYNSYM_SECNAMES
- 1;
4946 elf_link_hash_traverse (elf_hash_table (info
),
4947 mips_elf_adjust_dynindx
,
4949 elf_hash_table (info
)->dynsymcount
+= c
;
4951 dynstr
= elf_hash_table (info
)->dynstr
;
4952 BFD_ASSERT (dynstr
!= NULL
);
4954 for (i
= 1, namep
= mips_elf_dynsym_sec_names
;
4958 s
= bfd_get_section_by_name (output_bfd
, *namep
);
4960 elf_section_data (s
)->dynindx
= i
;
4962 strindex
= _bfd_stringtab_add (dynstr
, *namep
, true, false);
4963 if (strindex
== (bfd_size_type
) -1)
4966 mips_elf_hash_table (info
)->dynsym_sec_strindex
[i
] = strindex
;
4971 c
= bfd_count_sections (output_bfd
);
4972 elf_link_hash_traverse (elf_hash_table (info
),
4973 mips_elf_adjust_dynindx
,
4975 elf_hash_table (info
)->dynsymcount
+= c
;
4977 for (i
= 1, s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
, i
++)
4979 elf_section_data (s
)->dynindx
= i
;
4980 /* These symbols will have no names, so we don't need to
4981 fiddle with dynstr_index. */
4985 s
= bfd_get_section_by_name (dynobj
, ".got");
4986 BFD_ASSERT (s
!= NULL
);
4987 BFD_ASSERT (elf_section_data (s
) != NULL
);
4988 g
= (struct mips_got_info
*) elf_section_data (s
)->tdata
;
4989 BFD_ASSERT (g
!= NULL
);
4991 /* If there are no global got symbols, fake the last symbol so for
4993 if (g
->global_gotsym
)
4994 g
->global_gotsym
+= c
;
4996 g
->global_gotsym
= elf_hash_table (info
)->dynsymcount
- 1;
5002 /* Increment the index of a dynamic symbol by a given amount. Called
5003 via elf_link_hash_traverse. */
5006 mips_elf_adjust_dynindx (h
, cparg
)
5007 struct elf_link_hash_entry
*h
;
5010 int *cp
= (int *) cparg
;
5012 if (h
->dynindx
!= -1)
5017 /* Finish up dynamic symbol handling. We set the contents of various
5018 dynamic sections here. */
5021 mips_elf_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
5023 struct bfd_link_info
*info
;
5024 struct elf_link_hash_entry
*h
;
5025 Elf_Internal_Sym
*sym
;
5030 struct mips_got_info
*g
;
5033 dynobj
= elf_hash_table (info
)->dynobj
;
5034 gval
= sym
->st_value
;
5036 if (h
->plt_offset
!= (bfd_vma
) -1)
5040 bfd_byte stub
[MIPS_FUNCTION_STUB_SIZE
];
5042 /* This symbol has a stub. Set it up. */
5044 BFD_ASSERT (h
->dynindx
!= -1);
5046 s
= bfd_get_section_by_name (dynobj
, ".stub");
5047 BFD_ASSERT (s
!= NULL
);
5049 /* Fill the stub. */
5051 bfd_put_32 (output_bfd
, STUB_LW(output_bfd
), p
);
5053 bfd_put_32 (output_bfd
, STUB_MOVE
, p
);
5056 /* FIXME: Can h->dynindex be more than 64K? */
5057 if (h
->dynindx
& 0xffff0000)
5060 bfd_put_32 (output_bfd
, STUB_JALR
, p
);
5062 bfd_put_32 (output_bfd
, STUB_LI16
+ h
->dynindx
, p
);
5064 BFD_ASSERT (h
->plt_offset
<= s
->_raw_size
);
5065 memcpy (s
->contents
+ h
->plt_offset
, stub
, MIPS_FUNCTION_STUB_SIZE
);
5067 /* Mark the symbol as undefined. plt_offset != -1 occurs
5068 only for the referenced symbol. */
5069 sym
->st_shndx
= SHN_UNDEF
;
5071 /* The run-time linker uses the st_value field of the symbol
5072 to reset the global offset table entry for this external
5073 to its stub address when unlinking a shared object. */
5074 gval
= s
->output_section
->vma
+ s
->output_offset
+ h
->plt_offset
;
5075 sym
->st_value
= gval
;
5078 BFD_ASSERT (h
->dynindx
!= -1);
5080 sgot
= bfd_get_section_by_name (dynobj
, ".got");
5081 BFD_ASSERT (sgot
!= NULL
);
5082 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
5083 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
5084 BFD_ASSERT (g
!= NULL
);
5086 if ((unsigned long) h
->dynindx
>= g
->global_gotsym
)
5088 bfd_size_type offset
;
5090 /* This symbol has an entry in the global offset table. Set its
5091 value to the corresponding got entry, if needed. */
5092 if (h
->got_offset
== (bfd_vma
) -1)
5094 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
5095 BFD_ASSERT (g
->local_gotno
* 4 <= offset
5096 && offset
< sgot
->_raw_size
);
5097 bfd_put_32 (output_bfd
, gval
, sgot
->contents
+ offset
);
5101 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5102 name
= h
->root
.root
.string
;
5103 if (strcmp (name
, "_DYNAMIC") == 0
5104 || strcmp (name
, "_GLOBAL_OFFSET_TABLE_") == 0)
5105 sym
->st_shndx
= SHN_ABS
;
5107 if (SGI_COMPAT (output_bfd
))
5109 if (strcmp (name
, "_gp_disp") == 0)
5111 sym
->st_shndx
= SHN_ABS
;
5112 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5113 sym
->st_value
= elf_gp (output_bfd
);
5115 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[0]) == 0
5116 || strcmp (name
, mips_elf_dynsym_rtproc_names
[1]) == 0)
5118 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5119 sym
->st_other
= STO_PROTECTED
;
5121 sym
->st_shndx
= SHN_MIPS_DATA
;
5123 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[2]) == 0)
5125 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5126 sym
->st_other
= STO_PROTECTED
;
5127 sym
->st_value
= mips_elf_hash_table (info
)->procedure_count
;
5128 sym
->st_shndx
= SHN_ABS
;
5131 if (sym
->st_shndx
!= SHN_UNDEF
)
5133 if (h
->type
== STT_FUNC
)
5134 sym
->st_shndx
= SHN_MIPS_TEXT
;
5135 else if (h
->type
== STT_OBJECT
)
5136 sym
->st_shndx
= SHN_MIPS_DATA
;
5143 /* Finish up the dynamic sections. */
5146 mips_elf_finish_dynamic_sections (output_bfd
, info
)
5148 struct bfd_link_info
*info
;
5153 struct mips_got_info
*g
;
5155 dynobj
= elf_hash_table (info
)->dynobj
;
5157 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
5159 sgot
= bfd_get_section_by_name (dynobj
, ".got");
5160 BFD_ASSERT (sgot
!= NULL
);
5162 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
5163 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
5164 BFD_ASSERT (g
!= NULL
);
5166 if (elf_hash_table (info
)->dynamic_sections_created
)
5168 Elf32_External_Dyn
*dyncon
, *dynconend
;
5170 BFD_ASSERT (sdyn
!= NULL
);
5172 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5173 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
5174 for (; dyncon
< dynconend
; dyncon
++)
5176 Elf_Internal_Dyn dyn
;
5181 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5189 s
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
5190 BFD_ASSERT (s
!= NULL
);
5191 dyn
.d_un
.d_val
= sizeof (Elf32_External_Rel
);
5192 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5196 /* Rewrite DT_STRSZ. */
5198 _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
5199 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5205 case DT_MIPS_CONFLICT
:
5208 case DT_MIPS_LIBLIST
:
5211 s
= bfd_get_section_by_name (output_bfd
, name
);
5212 BFD_ASSERT (s
!= NULL
);
5213 dyn
.d_un
.d_ptr
= s
->vma
;
5214 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5217 case DT_MIPS_RLD_VERSION
:
5218 dyn
.d_un
.d_val
= 1; /* XXX */
5219 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5223 dyn
.d_un
.d_val
= RHF_NOTPOT
; /* XXX */
5224 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5227 case DT_MIPS_CONFLICTNO
:
5229 elemsize
= sizeof (Elf32_Conflict
);
5232 case DT_MIPS_LIBLISTNO
:
5234 elemsize
= sizeof (Elf32_Lib
);
5236 s
= bfd_get_section_by_name (output_bfd
, name
);
5239 if (s
->_cooked_size
!= 0)
5240 dyn
.d_un
.d_val
= s
->_cooked_size
/ elemsize
;
5242 dyn
.d_un
.d_val
= s
->_raw_size
/ elemsize
;
5247 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5250 case DT_MIPS_TIME_STAMP
:
5251 time ((time_t *) &dyn
.d_un
.d_val
);
5252 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5255 case DT_MIPS_ICHECKSUM
:
5259 case DT_MIPS_IVERSION
:
5263 case DT_MIPS_BASE_ADDRESS
:
5264 s
= output_bfd
->sections
;
5265 BFD_ASSERT (s
!= NULL
);
5266 dyn
.d_un
.d_ptr
= s
->vma
& ~(0xffff);
5267 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5270 case DT_MIPS_LOCAL_GOTNO
:
5271 dyn
.d_un
.d_val
= g
->local_gotno
;
5272 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5275 case DT_MIPS_SYMTABNO
:
5277 elemsize
= sizeof (Elf32_External_Sym
);
5278 s
= bfd_get_section_by_name (output_bfd
, name
);
5279 BFD_ASSERT (s
!= NULL
);
5281 if (s
->_cooked_size
!= 0)
5282 dyn
.d_un
.d_val
= s
->_cooked_size
/ elemsize
;
5284 dyn
.d_un
.d_val
= s
->_raw_size
/ elemsize
;
5285 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5288 case DT_MIPS_UNREFEXTNO
:
5290 dyn
.d_un
.d_val
= SIZEOF_MIPS_DYNSYM_SECNAMES
;
5291 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5294 case DT_MIPS_GOTSYM
:
5295 dyn
.d_un
.d_val
= g
->global_gotsym
;
5296 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5299 case DT_MIPS_HIPAGENO
:
5300 dyn
.d_un
.d_val
= g
->local_gotno
- MIPS_RESERVED_GOTNO
;
5301 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5308 /* The first entry of the global offset table will be filled at
5309 runtime. The second entry will be used by some runtime loaders.
5310 This isn't the case of Irix rld. */
5311 if (sgot
->_raw_size
> 0)
5313 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
5314 bfd_put_32 (output_bfd
, (bfd_vma
) 0x80000000, sgot
->contents
+ 4);
5317 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
5325 Elf_Internal_Sym sym
;
5328 const char * const * namep
= mips_elf_dynsym_sec_names
;
5329 Elf32_compact_rel cpt
;
5331 /* Set up the section symbols for the output sections. SGI set
5332 STT_NOTYPE attribute for these symbols. Should we do so? */
5334 sdynsym
= bfd_get_section_by_name (dynobj
, ".dynsym");
5335 BFD_ASSERT (sdynsym
!= NULL
);
5337 if (SGI_COMPAT (output_bfd
))
5341 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_NOTYPE
);
5345 while ((name
= *namep
++) != NULL
)
5347 s
= bfd_get_section_by_name (output_bfd
, name
);
5350 sym
.st_value
= s
->vma
;
5351 dindx
= elf_section_data (s
)->dynindx
;
5352 last
= s
->vma
+ s
->_raw_size
;
5356 sym
.st_value
= last
;
5360 sym
.st_shndx
= (i
< MIPS_TEXT_DYNSYM_SECNO
5365 mips_elf_hash_table (info
)->dynsym_sec_strindex
[dindx
];
5367 bfd_elf32_swap_symbol_out (output_bfd
, &sym
,
5368 (((Elf32_External_Sym
*)
5373 /* Set the sh_info field of the output .dynsym section to
5374 the index of the first global symbol. */
5375 elf_section_data (sdynsym
->output_section
)->this_hdr
.sh_info
=
5376 SIZEOF_MIPS_DYNSYM_SECNAMES
;
5382 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5385 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5389 sym
.st_value
= s
->vma
;
5391 indx
= elf_section_data (s
)->this_idx
;
5392 BFD_ASSERT (indx
> 0);
5393 sym
.st_shndx
= indx
;
5395 bfd_elf32_swap_symbol_out (output_bfd
, &sym
,
5396 (((Elf32_External_Sym
*)
5398 + elf_section_data (s
)->dynindx
));
5401 /* Set the sh_info field of the output .dynsym section to
5402 the index of the first global symbol. */
5403 elf_section_data (sdynsym
->output_section
)->this_hdr
.sh_info
=
5404 bfd_count_sections (output_bfd
) + 1;
5407 if (SGI_COMPAT (output_bfd
))
5409 /* Write .compact_rel section out. */
5410 s
= bfd_get_section_by_name (dynobj
, ".compact_rel");
5411 BFD_ASSERT (s
!= NULL
);
5414 cpt
.num
= s
->reloc_count
;
5416 cpt
.offset
= (s
->output_section
->filepos
5417 + sizeof (Elf32_External_compact_rel
));
5420 bfd_elf32_swap_compact_rel_out (output_bfd
, &cpt
,
5421 ((Elf32_External_compact_rel
*)
5424 /* Clean up a dummy stub function entry in .text. */
5425 s
= bfd_get_section_by_name (dynobj
, ".stub");
5428 file_ptr dummy_offset
;
5430 BFD_ASSERT (s
->_raw_size
>= MIPS_FUNCTION_STUB_SIZE
);
5431 dummy_offset
= s
->_raw_size
- MIPS_FUNCTION_STUB_SIZE
;
5432 memset (s
->contents
+ dummy_offset
, 0, MIPS_FUNCTION_STUB_SIZE
);
5436 /* Clean up a first relocation in .rel.dyn. */
5437 s
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
5439 memset (s
->contents
, 0, sizeof (Elf32_External_Rel
));
5445 /* This is almost identical to bfd_generic_get_... except that some
5446 MIPS relocations need to be handled specially. Sigh. */
5449 elf32_mips_get_relocated_section_contents (abfd
, link_info
, link_order
, data
,
5450 relocateable
, symbols
)
5452 struct bfd_link_info
*link_info
;
5453 struct bfd_link_order
*link_order
;
5455 boolean relocateable
;
5458 /* Get enough memory to hold the stuff */
5459 bfd
*input_bfd
= link_order
->u
.indirect
.section
->owner
;
5460 asection
*input_section
= link_order
->u
.indirect
.section
;
5462 long reloc_size
= bfd_get_reloc_upper_bound (input_bfd
, input_section
);
5463 arelent
**reloc_vector
= NULL
;
5469 reloc_vector
= (arelent
**) bfd_malloc (reloc_size
);
5470 if (reloc_vector
== NULL
&& reloc_size
!= 0)
5473 /* read in the section */
5474 if (!bfd_get_section_contents (input_bfd
,
5478 input_section
->_raw_size
))
5481 /* We're not relaxing the section, so just copy the size info */
5482 input_section
->_cooked_size
= input_section
->_raw_size
;
5483 input_section
->reloc_done
= true;
5485 reloc_count
= bfd_canonicalize_reloc (input_bfd
,
5489 if (reloc_count
< 0)
5492 if (reloc_count
> 0)
5497 bfd_vma gp
= 0x12345678; /* initialize just to shut gcc up */
5500 struct bfd_hash_entry
*h
;
5501 struct bfd_link_hash_entry
*lh
;
5502 /* Skip all this stuff if we aren't mixing formats. */
5503 if (abfd
&& input_bfd
5504 && abfd
->xvec
== input_bfd
->xvec
)
5508 h
= bfd_hash_lookup (&link_info
->hash
->table
, "_gp", false, false);
5509 lh
= (struct bfd_link_hash_entry
*) h
;
5516 case bfd_link_hash_undefined
:
5517 case bfd_link_hash_undefweak
:
5518 case bfd_link_hash_common
:
5521 case bfd_link_hash_defined
:
5522 case bfd_link_hash_defweak
:
5524 gp
= lh
->u
.def
.value
;
5526 case bfd_link_hash_indirect
:
5527 case bfd_link_hash_warning
:
5529 /* @@FIXME ignoring warning for now */
5531 case bfd_link_hash_new
:
5540 for (parent
= reloc_vector
; *parent
!= (arelent
*) NULL
;
5543 char *error_message
= (char *) NULL
;
5544 bfd_reloc_status_type r
;
5546 /* Specific to MIPS: Deal with relocation types that require
5547 knowing the gp of the output bfd. */
5548 asymbol
*sym
= *(*parent
)->sym_ptr_ptr
;
5549 if (bfd_is_abs_section (sym
->section
) && abfd
)
5551 /* The special_function wouldn't get called anyways. */
5555 /* The gp isn't there; let the special function code
5556 fall over on its own. */
5558 else if ((*parent
)->howto
->special_function
5559 == mips_elf_gprel16_reloc
)
5561 /* bypass special_function call */
5562 r
= gprel16_with_gp (input_bfd
, sym
, *parent
, input_section
,
5563 relocateable
, (PTR
) data
, gp
);
5564 goto skip_bfd_perform_relocation
;
5566 /* end mips specific stuff */
5568 r
= bfd_perform_relocation (input_bfd
,
5572 relocateable
? abfd
: (bfd
*) NULL
,
5574 skip_bfd_perform_relocation
:
5578 asection
*os
= input_section
->output_section
;
5580 /* A partial link, so keep the relocs */
5581 os
->orelocation
[os
->reloc_count
] = *parent
;
5585 if (r
!= bfd_reloc_ok
)
5589 case bfd_reloc_undefined
:
5590 if (!((*link_info
->callbacks
->undefined_symbol
)
5591 (link_info
, bfd_asymbol_name (*(*parent
)->sym_ptr_ptr
),
5592 input_bfd
, input_section
, (*parent
)->address
)))
5595 case bfd_reloc_dangerous
:
5596 BFD_ASSERT (error_message
!= (char *) NULL
);
5597 if (!((*link_info
->callbacks
->reloc_dangerous
)
5598 (link_info
, error_message
, input_bfd
, input_section
,
5599 (*parent
)->address
)))
5602 case bfd_reloc_overflow
:
5603 if (!((*link_info
->callbacks
->reloc_overflow
)
5604 (link_info
, bfd_asymbol_name (*(*parent
)->sym_ptr_ptr
),
5605 (*parent
)->howto
->name
, (*parent
)->addend
,
5606 input_bfd
, input_section
, (*parent
)->address
)))
5609 case bfd_reloc_outofrange
:
5618 if (reloc_vector
!= NULL
)
5619 free (reloc_vector
);
5623 if (reloc_vector
!= NULL
)
5624 free (reloc_vector
);
5627 #define bfd_elf32_bfd_get_relocated_section_contents \
5628 elf32_mips_get_relocated_section_contents
5630 /* ECOFF swapping routines. These are used when dealing with the
5631 .mdebug section, which is in the ECOFF debugging format. */
5632 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap
=
5634 /* Symbol table magic number. */
5636 /* Alignment of debugging information. E.g., 4. */
5638 /* Sizes of external symbolic information. */
5639 sizeof (struct hdr_ext
),
5640 sizeof (struct dnr_ext
),
5641 sizeof (struct pdr_ext
),
5642 sizeof (struct sym_ext
),
5643 sizeof (struct opt_ext
),
5644 sizeof (struct fdr_ext
),
5645 sizeof (struct rfd_ext
),
5646 sizeof (struct ext_ext
),
5647 /* Functions to swap in external symbolic data. */
5656 _bfd_ecoff_swap_tir_in
,
5657 _bfd_ecoff_swap_rndx_in
,
5658 /* Functions to swap out external symbolic data. */
5667 _bfd_ecoff_swap_tir_out
,
5668 _bfd_ecoff_swap_rndx_out
,
5669 /* Function to read in symbolic data. */
5670 mips_elf_read_ecoff_info
5673 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
5674 #define TARGET_LITTLE_NAME "elf32-littlemips"
5675 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
5676 #define TARGET_BIG_NAME "elf32-bigmips"
5677 #define ELF_ARCH bfd_arch_mips
5678 #define ELF_MACHINE_CODE EM_MIPS
5679 #define ELF_MAXPAGESIZE 0x10000
5680 #define elf_backend_collect true
5681 #define elf_backend_type_change_ok true
5682 #define elf_info_to_howto 0
5683 #define elf_info_to_howto_rel mips_info_to_howto_rel
5684 #define elf_backend_sym_is_global mips_elf_sym_is_global
5685 #define elf_backend_object_p mips_elf_object_p
5686 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
5687 #define elf_backend_fake_sections mips_elf_fake_sections
5688 #define elf_backend_section_from_bfd_section \
5689 mips_elf_section_from_bfd_section
5690 #define elf_backend_section_processing mips_elf_section_processing
5691 #define elf_backend_symbol_processing mips_elf_symbol_processing
5692 #define elf_backend_additional_program_headers \
5693 mips_elf_additional_program_headers
5694 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
5695 #define elf_backend_final_write_processing \
5696 mips_elf_final_write_processing
5697 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
5699 #define bfd_elf32_bfd_is_local_label mips_elf_is_local_label
5700 #define bfd_elf32_find_nearest_line mips_elf_find_nearest_line
5702 #define bfd_elf32_bfd_link_hash_table_create \
5703 mips_elf_link_hash_table_create
5704 #define bfd_elf32_bfd_final_link mips_elf_final_link
5705 #define bfd_elf32_bfd_copy_private_bfd_data \
5706 mips_elf_copy_private_bfd_data
5707 #define bfd_elf32_bfd_merge_private_bfd_data \
5708 mips_elf_merge_private_bfd_data
5709 #define bfd_elf32_bfd_set_private_flags mips_elf_set_private_flags
5710 #define elf_backend_relocate_section mips_elf_relocate_section
5711 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
5712 #define elf_backend_create_dynamic_sections \
5713 mips_elf_create_dynamic_sections
5714 #define elf_backend_check_relocs mips_elf_check_relocs
5715 #define elf_backend_adjust_dynamic_symbol \
5716 mips_elf_adjust_dynamic_symbol
5717 #define elf_backend_size_dynamic_sections \
5718 mips_elf_size_dynamic_sections
5719 #define elf_backend_relocate_section mips_elf_relocate_section
5720 #define elf_backend_finish_dynamic_symbol \
5721 mips_elf_finish_dynamic_symbol
5722 #define elf_backend_finish_dynamic_sections \
5723 mips_elf_finish_dynamic_sections
5724 #define elf_backend_want_hdr_in_seg 1
5726 #include "elf32-target.h"