1 /* CRIS-specific support for 32-bit ELF.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Axis Communications AB.
4 Written by Hans-Peter Nilsson, based on elf32-fr30.c
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 /* Forward declarations. */
29 static reloc_howto_type
* cris_reloc_type_lookup
30 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
32 static void cris_info_to_howto_rela
33 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
35 static boolean cris_elf_relocate_section
36 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
37 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
39 static bfd_reloc_status_type cris_final_link_relocate
40 PARAMS ((reloc_howto_type
*, bfd
*, asection
*, bfd_byte
*,
41 Elf_Internal_Rela
*, bfd_vma
));
43 static boolean cris_elf_gc_sweep_hook
44 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
45 const Elf_Internal_Rela
*));
47 static asection
* cris_elf_gc_mark_hook
48 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
49 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
51 static boolean cris_elf_object_p
PARAMS ((bfd
*));
53 static void cris_elf_final_write_processing
PARAMS ((bfd
*, boolean
));
55 static boolean cris_elf_print_private_bfd_data
PARAMS ((bfd
*, PTR
));
57 static boolean cris_elf_merge_private_bfd_data
PARAMS ((bfd
*, bfd
*));
59 static reloc_howto_type cris_elf_howto_table
[] =
61 /* This reloc does nothing. */
62 HOWTO (R_CRIS_NONE
, /* type */
64 2, /* size (0 = byte, 1 = short, 2 = long) */
66 false, /* pc_relative */
68 complain_overflow_bitfield
, /* complain_on_overflow */
69 bfd_elf_generic_reloc
, /* special_function */
70 "R_CRIS_NONE", /* name */
71 false, /* partial_inplace */
74 false), /* pcrel_offset */
76 /* An 8 bit absolute relocation. */
77 HOWTO (R_CRIS_8
, /* type */
79 0, /* size (0 = byte, 1 = short, 2 = long) */
81 false, /* pc_relative */
83 complain_overflow_bitfield
, /* complain_on_overflow */
84 bfd_elf_generic_reloc
, /* special_function */
85 "R_CRIS_8", /* name */
86 false, /* partial_inplace */
87 0x0000, /* src_mask */
88 0x00ff, /* dst_mask */
89 false), /* pcrel_offset */
91 /* A 16 bit absolute relocation. */
92 HOWTO (R_CRIS_16
, /* type */
94 1, /* size (0 = byte, 1 = short, 2 = long) */
96 false, /* pc_relative */
98 complain_overflow_bitfield
, /* complain_on_overflow */
99 bfd_elf_generic_reloc
, /* special_function */
100 "R_CRIS_16", /* name */
101 false, /* partial_inplace */
102 0x00000000, /* src_mask */
103 0x0000ffff, /* dst_mask */
104 false), /* pcrel_offset */
106 /* A 32 bit absolute relocation. */
107 HOWTO (R_CRIS_32
, /* type */
109 2, /* size (0 = byte, 1 = short, 2 = long) */
111 false, /* pc_relative */
113 complain_overflow_bitfield
, /* complain_on_overflow */
114 bfd_elf_generic_reloc
, /* special_function */
115 "R_CRIS_32", /* name */
116 false, /* partial_inplace */
117 0x00000000, /* src_mask */
118 0xffffffff, /* dst_mask */
119 false), /* pcrel_offset */
121 /* An 8 bit absolute relocation. */
122 HOWTO (R_CRIS_8_PCREL
, /* type */
124 0, /* size (0 = byte, 1 = short, 2 = long) */
126 true, /* pc_relative */
128 complain_overflow_bitfield
, /* complain_on_overflow */
129 bfd_elf_generic_reloc
, /* special_function */
130 "R_CRIS_8_PCREL", /* name */
131 false, /* partial_inplace */
132 0x0000, /* src_mask */
133 0x00ff, /* dst_mask */
134 false), /* pcrel_offset */
136 /* A 16 bit absolute relocation. */
137 HOWTO (R_CRIS_16_PCREL
, /* type */
139 1, /* size (0 = byte, 1 = short, 2 = long) */
141 true, /* pc_relative */
143 complain_overflow_bitfield
, /* complain_on_overflow */
144 bfd_elf_generic_reloc
, /* special_function */
145 "R_CRIS_16", /* name */
146 false, /* partial_inplace */
147 0x00000000, /* src_mask */
148 0x0000ffff, /* dst_mask */
149 false), /* pcrel_offset */
151 /* A 32 bit absolute relocation. */
152 HOWTO (R_CRIS_32_PCREL
, /* type */
154 2, /* size (0 = byte, 1 = short, 2 = long) */
156 true, /* pc_relative */
158 complain_overflow_bitfield
, /* complain_on_overflow */
159 bfd_elf_generic_reloc
, /* special_function */
160 "R_CRIS_32", /* name */
161 false, /* partial_inplace */
162 0x00000000, /* src_mask */
163 0xffffffff, /* dst_mask */
164 false), /* pcrel_offset */
166 /* GNU extension to record C++ vtable hierarchy */
167 HOWTO (R_CRIS_GNU_VTINHERIT
, /* type */
169 2, /* size (0 = byte, 1 = short, 2 = long) */
171 false, /* pc_relative */
173 complain_overflow_dont
, /* complain_on_overflow */
174 NULL
, /* special_function */
175 "R_CRIS_GNU_VTINHERIT", /* name */
176 false, /* partial_inplace */
179 false), /* pcrel_offset */
181 /* GNU extension to record C++ vtable member usage */
182 HOWTO (R_CRIS_GNU_VTENTRY
, /* type */
184 2, /* size (0 = byte, 1 = short, 2 = long) */
186 false, /* pc_relative */
188 complain_overflow_dont
, /* complain_on_overflow */
189 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
190 "R_CRIS_GNU_VTENTRY", /* name */
191 false, /* partial_inplace */
194 false) /* pcrel_offset */
197 /* Map BFD reloc types to CRIS ELF reloc types. */
199 struct cris_reloc_map
201 bfd_reloc_code_real_type bfd_reloc_val
;
202 unsigned int cris_reloc_val
;
205 static const struct cris_reloc_map cris_reloc_map
[] =
207 { BFD_RELOC_NONE
, R_CRIS_NONE
},
208 { BFD_RELOC_8
, R_CRIS_8
},
209 { BFD_RELOC_16
, R_CRIS_16
},
210 { BFD_RELOC_32
, R_CRIS_32
},
211 { BFD_RELOC_8_PCREL
, R_CRIS_8_PCREL
},
212 { BFD_RELOC_16_PCREL
, R_CRIS_16_PCREL
},
213 { BFD_RELOC_32_PCREL
, R_CRIS_32_PCREL
},
214 { BFD_RELOC_VTABLE_INHERIT
, R_CRIS_GNU_VTINHERIT
},
215 { BFD_RELOC_VTABLE_ENTRY
, R_CRIS_GNU_VTENTRY
}
218 static reloc_howto_type
*
219 cris_reloc_type_lookup (abfd
, code
)
220 bfd
* abfd ATTRIBUTE_UNUSED
;
221 bfd_reloc_code_real_type code
;
225 for (i
= sizeof (cris_reloc_map
) / sizeof (cris_reloc_map
[0]);
227 if (cris_reloc_map
[i
].bfd_reloc_val
== code
)
228 return & cris_elf_howto_table
[cris_reloc_map
[i
].cris_reloc_val
];
233 /* Set the howto pointer for an CRIS ELF reloc. */
236 cris_info_to_howto_rela (abfd
, cache_ptr
, dst
)
237 bfd
* abfd ATTRIBUTE_UNUSED
;
239 Elf32_Internal_Rela
* dst
;
243 r_type
= ELF32_R_TYPE (dst
->r_info
);
244 BFD_ASSERT (r_type
< (unsigned int) R_CRIS_max
);
245 cache_ptr
->howto
= & cris_elf_howto_table
[r_type
];
248 /* Perform a single relocation. By default we use the standard BFD
249 routines, but we might have to do a few relocs ourselves in the future. */
251 static bfd_reloc_status_type
252 cris_final_link_relocate (howto
, input_bfd
, input_section
, contents
, rel
,
254 reloc_howto_type
* howto
;
256 asection
* input_section
;
258 Elf_Internal_Rela
* rel
;
261 bfd_reloc_status_type r
262 = _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
263 contents
, rel
->r_offset
,
264 relocation
, rel
->r_addend
);
268 /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
269 copied, for further comments. */
272 cris_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
273 contents
, relocs
, local_syms
, local_sections
)
274 bfd
* output_bfd ATTRIBUTE_UNUSED
;
275 struct bfd_link_info
* info
;
277 asection
* input_section
;
279 Elf_Internal_Rela
* relocs
;
280 Elf_Internal_Sym
* local_syms
;
281 asection
** local_sections
;
283 Elf_Internal_Shdr
* symtab_hdr
;
284 struct elf_link_hash_entry
** sym_hashes
;
285 Elf_Internal_Rela
* rel
;
286 Elf_Internal_Rela
* relend
;
288 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
289 sym_hashes
= elf_sym_hashes (input_bfd
);
290 relend
= relocs
+ input_section
->reloc_count
;
292 /* It seems this can happen with erroneous or unsupported input (mixing
293 a.out and elf in an archive, for example.) */
294 if (sym_hashes
== NULL
)
297 for (rel
= relocs
; rel
< relend
; rel
++)
299 reloc_howto_type
* howto
;
300 unsigned long r_symndx
;
301 Elf_Internal_Sym
* sym
;
303 struct elf_link_hash_entry
* h
;
305 bfd_reloc_status_type r
;
306 const char * name
= NULL
;
309 r_type
= ELF32_R_TYPE (rel
->r_info
);
311 if ( r_type
== R_CRIS_GNU_VTINHERIT
312 || r_type
== R_CRIS_GNU_VTENTRY
)
315 r_symndx
= ELF32_R_SYM (rel
->r_info
);
317 if (info
->relocateable
)
319 /* This is a relocateable link. We don't have to change
320 anything, unless the reloc is against a section symbol,
321 in which case we have to adjust according to where the
322 section symbol winds up in the output section. */
323 if (r_symndx
< symtab_hdr
->sh_info
)
325 sym
= local_syms
+ r_symndx
;
327 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
329 sec
= local_sections
[r_symndx
];
330 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
337 /* This is a final link. */
338 howto
= cris_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
343 if (r_symndx
< symtab_hdr
->sh_info
)
345 sym
= local_syms
+ r_symndx
;
346 sec
= local_sections
[r_symndx
];
347 relocation
= (sec
->output_section
->vma
351 name
= bfd_elf_string_from_elf_section
352 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
353 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
355 fprintf (stderr
, "local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
356 sec
->name
, name
, sym
->st_name
,
357 sec
->output_section
->vma
, sec
->output_offset
,
358 sym
->st_value
, rel
->r_addend
);
363 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
365 while (h
->root
.type
== bfd_link_hash_indirect
366 || h
->root
.type
== bfd_link_hash_warning
)
367 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
369 name
= h
->root
.root
.string
;
371 if (h
->root
.type
== bfd_link_hash_defined
372 || h
->root
.type
== bfd_link_hash_defweak
)
374 sec
= h
->root
.u
.def
.section
;
375 relocation
= (h
->root
.u
.def
.value
376 + sec
->output_section
->vma
377 + sec
->output_offset
);
380 "defined: sec: %s, name: %s, value: %x + %x + %x gives: %x\n",
381 sec
->name
, name
, h
->root
.u
.def
.value
,
382 sec
->output_section
->vma
, sec
->output_offset
, relocation
);
385 else if (h
->root
.type
== bfd_link_hash_undefweak
)
388 fprintf (stderr
, "undefined: sec: %s, name: %s\n",
393 else if (info
->shared
394 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
398 if (! ((*info
->callbacks
->undefined_symbol
)
399 (info
, h
->root
.root
.string
, input_bfd
,
400 input_section
, rel
->r_offset
, true)))
403 fprintf (stderr
, "unknown: name: %s\n", name
);
409 r
= cris_final_link_relocate (howto
, input_bfd
, input_section
,
410 contents
, rel
, relocation
);
412 if (r
!= bfd_reloc_ok
)
414 const char * msg
= (const char *) NULL
;
418 case bfd_reloc_overflow
:
419 r
= info
->callbacks
->reloc_overflow
420 (info
, name
, howto
->name
, (bfd_vma
) 0,
421 input_bfd
, input_section
, rel
->r_offset
);
424 case bfd_reloc_undefined
:
425 r
= info
->callbacks
->undefined_symbol
426 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
430 case bfd_reloc_outofrange
:
431 msg
= _("internal error: out of range error");
434 case bfd_reloc_notsupported
:
435 msg
= _("internal error: unsupported relocation error");
438 case bfd_reloc_dangerous
:
439 msg
= _("internal error: dangerous relocation");
443 msg
= _("internal error: unknown error");
448 r
= info
->callbacks
->warning
449 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
459 /* Return the section that should be marked against GC for a given
463 cris_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
465 struct bfd_link_info
* info ATTRIBUTE_UNUSED
;
466 Elf_Internal_Rela
* rel
;
467 struct elf_link_hash_entry
* h
;
468 Elf_Internal_Sym
* sym
;
472 switch (ELF32_R_TYPE (rel
->r_info
))
474 case R_CRIS_GNU_VTINHERIT
:
475 case R_CRIS_GNU_VTENTRY
:
479 switch (h
->root
.type
)
481 case bfd_link_hash_defined
:
482 case bfd_link_hash_defweak
:
483 return h
->root
.u
.def
.section
;
485 case bfd_link_hash_common
:
486 return h
->root
.u
.c
.p
->section
;
495 if (!(elf_bad_symtab (abfd
)
496 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
497 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
498 && sym
->st_shndx
!= SHN_COMMON
))
500 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
507 /* Update the got entry reference counts for the section being removed. */
510 cris_elf_gc_sweep_hook (abfd
, info
, sec
, relocs
)
511 bfd
* abfd ATTRIBUTE_UNUSED
;
512 struct bfd_link_info
* info ATTRIBUTE_UNUSED
;
513 asection
* sec ATTRIBUTE_UNUSED
;
514 const Elf_Internal_Rela
* relocs ATTRIBUTE_UNUSED
;
519 /* Look through the relocs for a section during the first phase.
520 Since we don't do .gots or .plts, we just need to consider the
521 virtual table relocs for gc. */
524 cris_elf_check_relocs (abfd
, info
, sec
, relocs
)
526 struct bfd_link_info
*info
;
528 const Elf_Internal_Rela
*relocs
;
530 Elf_Internal_Shdr
*symtab_hdr
;
531 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
532 const Elf_Internal_Rela
*rel
;
533 const Elf_Internal_Rela
*rel_end
;
535 if (info
->relocateable
)
538 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
539 sym_hashes
= elf_sym_hashes (abfd
);
540 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
541 if (!elf_bad_symtab (abfd
))
542 sym_hashes_end
-= symtab_hdr
->sh_info
;
544 rel_end
= relocs
+ sec
->reloc_count
;
545 for (rel
= relocs
; rel
< rel_end
; rel
++)
547 struct elf_link_hash_entry
*h
;
548 unsigned long r_symndx
;
550 r_symndx
= ELF32_R_SYM (rel
->r_info
);
551 if (r_symndx
< symtab_hdr
->sh_info
)
554 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
556 switch (ELF32_R_TYPE (rel
->r_info
))
558 /* This relocation describes the C++ object vtable hierarchy.
559 Reconstruct it for later use during GC. */
560 case R_CRIS_GNU_VTINHERIT
:
561 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
565 /* This relocation describes which C++ vtable entries are actually
566 used. Record for later use during GC. */
567 case R_CRIS_GNU_VTENTRY
:
568 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
577 /* Reject a file depending on underscores on symbols. */
580 cris_elf_object_p (abfd
)
583 if ((elf_elfheader (abfd
)->e_flags
& EF_CRIS_UNDERSCORE
))
584 return (bfd_get_symbol_leading_char (abfd
) == '_');
586 return (bfd_get_symbol_leading_char (abfd
) == 0);
589 /* Mark presence or absence of leading underscore. */
592 cris_elf_final_write_processing (abfd
, linker
)
594 boolean linker ATTRIBUTE_UNUSED
;
596 if (bfd_get_symbol_leading_char (abfd
) == '_')
597 elf_elfheader (abfd
)->e_flags
|= EF_CRIS_UNDERSCORE
;
599 elf_elfheader (abfd
)->e_flags
&= ~EF_CRIS_UNDERSCORE
;
602 /* Display the flags field. */
605 cris_elf_print_private_bfd_data (abfd
, ptr
)
609 FILE *file
= (FILE *) ptr
;
611 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
)
613 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
615 fprintf (file
, _("private flags = %lx:"), elf_elfheader (abfd
)->e_flags
);
617 if (elf_elfheader (abfd
)->e_flags
& EF_CRIS_UNDERSCORE
)
618 fprintf (file
, _(" [symbols have a _ prefix]"));
624 /* Don't mix files with and without a leading underscore. */
627 cris_elf_merge_private_bfd_data (ibfd
, obfd
)
631 flagword old_flags
, new_flags
;
633 if (_bfd_generic_verify_endian_match (ibfd
, obfd
) == false)
636 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
637 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
640 if (! elf_flags_init (obfd
))
642 /* This happens when ld starts out with a 'blank' output file. */
643 elf_flags_init (obfd
) = true;
645 /* Set flags according to current bfd_target. */
646 cris_elf_final_write_processing (obfd
, false);
649 old_flags
= elf_elfheader (obfd
)->e_flags
;
650 new_flags
= elf_elfheader (ibfd
)->e_flags
;
652 /* Is this good or bad? We'll follow with other excluding flags. */
653 if ((old_flags
& EF_CRIS_UNDERSCORE
) != (new_flags
& EF_CRIS_UNDERSCORE
))
655 (*_bfd_error_handler
)
656 ((new_flags
& EF_CRIS_UNDERSCORE
)
657 ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols")
658 : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
659 bfd_get_filename (ibfd
));
660 bfd_set_error (bfd_error_bad_value
);
667 #define ELF_ARCH bfd_arch_cris
668 #define ELF_MACHINE_CODE EM_CRIS
669 #define ELF_MAXPAGESIZE 0x2000
671 #define TARGET_LITTLE_SYM bfd_elf32_cris_vec
672 #define TARGET_LITTLE_NAME "elf32-cris"
673 #define elf_symbol_leading_char 0
675 #define elf_info_to_howto_rel NULL
676 #define elf_info_to_howto cris_info_to_howto_rela
677 #define elf_backend_relocate_section cris_elf_relocate_section
678 #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
679 #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook
680 #define elf_backend_check_relocs cris_elf_check_relocs
682 #define elf_backend_can_gc_sections 1
684 #define elf_backend_object_p cris_elf_object_p
685 #define elf_backend_final_write_processing \
686 cris_elf_final_write_processing
687 #define bfd_elf32_bfd_print_private_bfd_data \
688 cris_elf_print_private_bfd_data
689 #define bfd_elf32_bfd_merge_private_bfd_data \
690 cris_elf_merge_private_bfd_data
692 #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
694 /* Later, we my want to optimize RELA entries into REL entries for dynamic
695 linking and libraries (if it's a win of any significance). Until then,
696 take the easy route. */
697 #define elf_backend_may_use_rel_p 0
698 #define elf_backend_may_use_rela_p 1
700 #include "elf32-target.h"
702 #define INCLUDED_TARGET_FILE
704 #undef TARGET_LITTLE_SYM
705 #undef TARGET_LITTLE_NAME
706 #undef elf_symbol_leading_char
708 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
709 #define TARGET_LITTLE_NAME "elf32-us-cris"
710 #define elf_symbol_leading_char '_'
712 #include "elf32-target.h"