1 /* Morpho Technologies MT specific support for 32-bit ELF
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 static reloc_howto_type
* mt_reloc_type_lookup
29 (bfd
*, bfd_reloc_code_real_type
);
31 static void mt_info_to_howto_rela
32 (bfd
*, arelent
*, Elf_Internal_Rela
*);
34 static bfd_reloc_status_type mt_elf_relocate_hi16
35 (bfd
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_vma
);
37 static bfd_reloc_status_type mt_final_link_relocate
38 (reloc_howto_type
*, bfd
*, asection
*, bfd_byte
*,
39 Elf_Internal_Rela
*, bfd_vma
);
41 static bfd_boolean mt_elf_relocate_section
42 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
43 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
45 /* Relocation tables. */
46 static reloc_howto_type mt_elf_howto_table
[] =
48 /* This reloc does nothing. */
49 HOWTO (R_MT_NONE
, /* type */
51 2, /* size (0 = byte, 1 = short, 2 = long) */
53 FALSE
, /* pc_relative */
55 complain_overflow_dont
, /* complain_on_overflow */
56 bfd_elf_generic_reloc
, /* special_function */
57 "R_MT_NONE", /* name */
58 FALSE
, /* partial_inplace */
61 FALSE
), /* pcrel_offset */
63 /* A 16 bit absolute relocation. */
64 HOWTO (R_MT_16
, /* type */
66 2, /* size (0 = byte, 1 = short, 2 = long) */
68 FALSE
, /* pc_relative */
70 complain_overflow_dont
, /* complain_on_overflow */
71 bfd_elf_generic_reloc
, /* special_function */
73 FALSE
, /* partial_inplace */
75 0xffff, /* dst_mask */
76 FALSE
), /* pcrel_offset */
78 /* A 32 bit absolute relocation. */
79 HOWTO (R_MT_32
, /* type */
81 2, /* size (0 = byte, 1 = short, 2 = long) */
83 FALSE
, /* pc_relative */
85 complain_overflow_dont
, /* complain_on_overflow */
86 bfd_elf_generic_reloc
, /* special_function */
88 FALSE
, /* partial_inplace */
90 0xffffffff, /* dst_mask */
91 FALSE
), /* pcrel_offset */
93 /* A 32 bit pc-relative relocation. */
94 HOWTO (R_MT_32_PCREL
, /* type */
96 2, /* size (0 = byte, 1 = short, 2 = long) */
98 TRUE
, /* pc_relative */
100 complain_overflow_dont
, /* complain_on_overflow */
101 bfd_elf_generic_reloc
, /* special_function */
102 "R_MT_32_PCREL", /* name */
103 FALSE
, /* partial_inplace */
105 0xffffffff, /* dst_mask */
106 TRUE
), /* pcrel_offset */
108 /* A 16 bit pc-relative relocation. */
109 HOWTO (R_MT_PC16
, /* type */
111 2, /* size (0 = byte, 1 = short, 2 = long) */
113 TRUE
, /* pc_relative */
115 complain_overflow_signed
, /* complain_on_overflow */
116 bfd_elf_generic_reloc
, /* special_function */
117 "R_MT_PC16", /* name */
118 FALSE
, /* partial_inplace */
120 0xffff, /* dst_mask */
121 TRUE
), /* pcrel_offset */
123 /* high 16 bits of symbol value. */
124 HOWTO (R_MT_HI16
, /* type */
126 2, /* size (0 = byte, 1 = short, 2 = long) */
128 FALSE
, /* pc_relative */
130 complain_overflow_dont
, /* complain_on_overflow */
131 bfd_elf_generic_reloc
, /* special_function */
132 "R_MT_HI16", /* name */
133 FALSE
, /* partial_inplace */
134 0xffff0000, /* src_mask */
135 0xffff0000, /* dst_mask */
136 FALSE
), /* pcrel_offset */
138 /* Low 16 bits of symbol value. */
139 HOWTO (R_MT_LO16
, /* type */
141 2, /* size (0 = byte, 1 = short, 2 = long) */
143 FALSE
, /* pc_relative */
145 complain_overflow_dont
, /* complain_on_overflow */
146 bfd_elf_generic_reloc
, /* special_function */
147 "R_MT_LO16", /* name */
148 FALSE
, /* partial_inplace */
149 0xffff, /* src_mask */
150 0xffff, /* dst_mask */
151 FALSE
), /* pcrel_offset */
154 /* Map BFD reloc types to MT ELF reloc types. */
156 static reloc_howto_type
*
158 (bfd
* abfd ATTRIBUTE_UNUSED
,
159 bfd_reloc_code_real_type code
)
161 /* Note that the mt_elf_howto_table is indxed by the R_
162 constants. Thus, the order that the howto records appear in the
163 table *must* match the order of the relocation types defined in
169 return &mt_elf_howto_table
[ (int) R_MT_NONE
];
171 return &mt_elf_howto_table
[ (int) R_MT_16
];
173 return &mt_elf_howto_table
[ (int) R_MT_32
];
174 case BFD_RELOC_32_PCREL
:
175 return &mt_elf_howto_table
[ (int) R_MT_32_PCREL
];
176 case BFD_RELOC_16_PCREL
:
177 return &mt_elf_howto_table
[ (int) R_MT_PC16
];
179 return &mt_elf_howto_table
[ (int) R_MT_HI16
];
181 return &mt_elf_howto_table
[ (int) R_MT_LO16
];
184 /* Pacify gcc -Wall. */
190 static reloc_howto_type
*
191 mt_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
197 i
< sizeof (mt_elf_howto_table
) / sizeof (mt_elf_howto_table
[0]);
199 if (mt_elf_howto_table
[i
].name
!= NULL
200 && strcasecmp (mt_elf_howto_table
[i
].name
, r_name
) == 0)
201 return &mt_elf_howto_table
[i
];
206 bfd_reloc_status_type
209 Elf_Internal_Rela
* relhi
,
215 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
217 value
+= relhi
->r_addend
;
219 insn
= ((insn
& ~0xFFFF) | value
);
221 bfd_put_32 (input_bfd
, insn
, contents
+ relhi
->r_offset
);
225 /* XXX: The following code is the result of a cut&paste. This unfortunate
226 practice is very widespread in the various target back-end files. */
228 /* Set the howto pointer for a MT ELF reloc. */
231 mt_info_to_howto_rela
232 (bfd
* abfd ATTRIBUTE_UNUSED
,
234 Elf_Internal_Rela
* dst
)
238 r_type
= ELF32_R_TYPE (dst
->r_info
);
239 cache_ptr
->howto
= & mt_elf_howto_table
[r_type
];
242 /* Perform a single relocation. By default we use the standard BFD
245 static bfd_reloc_status_type
246 mt_final_link_relocate
247 (reloc_howto_type
* howto
,
249 asection
* input_section
,
251 Elf_Internal_Rela
* rel
,
254 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
255 contents
, rel
->r_offset
,
256 relocation
, rel
->r_addend
);
259 /* Relocate a MT ELF section.
260 There is some attempt to make this function usable for many architectures,
261 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
262 if only to serve as a learning tool.
264 The RELOCATE_SECTION function is called by the new ELF backend linker
265 to handle the relocations for a section.
267 The relocs are always passed as Rela structures; if the section
268 actually uses Rel structures, the r_addend field will always be
271 This function is responsible for adjusting the section contents as
272 necessary, and (if using Rela relocs and generating a relocatable
273 output file) adjusting the reloc addend as necessary.
275 This function does not have to worry about setting the reloc
276 address or the reloc symbol index.
278 LOCAL_SYMS is a pointer to the swapped in local symbols.
280 LOCAL_SECTIONS is an array giving the section in the input file
281 corresponding to the st_shndx field of each local symbol.
283 The global hash table entry for the global symbols can be found
284 via elf_sym_hashes (input_bfd).
286 When generating relocatable output, this function must handle
287 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
288 going to be the section symbol corresponding to the output
289 section, which means that the addend must be adjusted
293 mt_elf_relocate_section
294 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
295 struct bfd_link_info
* info
,
297 asection
* input_section
,
299 Elf_Internal_Rela
* relocs
,
300 Elf_Internal_Sym
* local_syms
,
301 asection
** local_sections
)
303 Elf_Internal_Shdr
* symtab_hdr
;
304 struct elf_link_hash_entry
** sym_hashes
;
305 Elf_Internal_Rela
* rel
;
306 Elf_Internal_Rela
* relend
;
308 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
309 sym_hashes
= elf_sym_hashes (input_bfd
);
310 relend
= relocs
+ input_section
->reloc_count
;
312 for (rel
= relocs
; rel
< relend
; rel
++)
314 reloc_howto_type
* howto
;
315 unsigned long r_symndx
;
316 Elf_Internal_Sym
* sym
;
318 struct elf_link_hash_entry
* h
;
320 bfd_reloc_status_type r
;
321 const char * name
= NULL
;
324 r_type
= ELF32_R_TYPE (rel
->r_info
);
326 r_symndx
= ELF32_R_SYM (rel
->r_info
);
328 howto
= mt_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
333 if (r_symndx
< symtab_hdr
->sh_info
)
335 sym
= local_syms
+ r_symndx
;
336 sec
= local_sections
[r_symndx
];
337 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
339 name
= bfd_elf_string_from_elf_section
340 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
341 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
345 bfd_boolean unresolved_reloc
;
348 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
349 r_symndx
, symtab_hdr
, sym_hashes
,
351 unresolved_reloc
, warned
);
353 name
= h
->root
.root
.string
;
356 if (sec
!= NULL
&& elf_discarded_section (sec
))
358 /* For relocs against symbols from removed linkonce sections,
359 or sections discarded by a linker script, we just want the
360 section contents zeroed. Avoid any special processing. */
361 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
367 if (info
->relocatable
)
370 /* Finally, the sole MT-specific part. */
374 r
= mt_elf_relocate_hi16 (input_bfd
, rel
, contents
, relocation
);
377 r
= mt_final_link_relocate (howto
, input_bfd
, input_section
,
378 contents
, rel
, relocation
);
383 if (r
!= bfd_reloc_ok
)
385 const char * msg
= (const char *) NULL
;
389 case bfd_reloc_overflow
:
390 r
= info
->callbacks
->reloc_overflow
391 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
392 input_bfd
, input_section
, rel
->r_offset
);
395 case bfd_reloc_undefined
:
396 r
= info
->callbacks
->undefined_symbol
397 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
400 case bfd_reloc_outofrange
:
401 msg
= _("internal error: out of range error");
404 case bfd_reloc_dangerous
:
405 msg
= _("internal error: dangerous relocation");
409 msg
= _("internal error: unknown error");
414 r
= info
->callbacks
->warning
415 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
425 /* Look through the relocs for a section during the first phase.
426 Since we don't do .gots or .plts, we just need to consider the
427 virtual table relocs for gc. */
432 struct bfd_link_info
* info
,
434 const Elf_Internal_Rela
* relocs
)
436 Elf_Internal_Shdr
* symtab_hdr
;
437 struct elf_link_hash_entry
** sym_hashes
;
438 struct elf_link_hash_entry
** sym_hashes_end
;
439 const Elf_Internal_Rela
* rel
;
440 const Elf_Internal_Rela
* rel_end
;
442 if (info
->relocatable
)
445 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
446 sym_hashes
= elf_sym_hashes (abfd
);
447 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
448 if (!elf_bad_symtab (abfd
))
449 sym_hashes_end
-= symtab_hdr
->sh_info
;
451 rel_end
= relocs
+ sec
->reloc_count
;
452 for (rel
= relocs
; rel
< rel_end
; rel
++)
454 struct elf_link_hash_entry
*h
;
455 unsigned long r_symndx
;
457 r_symndx
= ELF32_R_SYM (rel
->r_info
);
458 if (r_symndx
< symtab_hdr
->sh_info
)
462 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
463 while (h
->root
.type
== bfd_link_hash_indirect
464 || h
->root
.type
== bfd_link_hash_warning
)
465 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
472 /* Return the MACH for an e_flags value. */
475 elf32_mt_machine (bfd
*abfd
)
477 switch (elf_elfheader (abfd
)->e_flags
& EF_MT_CPU_MASK
)
479 case EF_MT_CPU_MRISC
: return bfd_mach_ms1
;
480 case EF_MT_CPU_MRISC2
: return bfd_mach_mrisc2
;
481 case EF_MT_CPU_MS2
: return bfd_mach_ms2
;
488 mt_elf_object_p (bfd
* abfd
)
490 bfd_default_set_arch_mach (abfd
, bfd_arch_mt
, elf32_mt_machine (abfd
));
495 /* Function to set the ELF flag bits. */
498 mt_elf_set_private_flags (bfd
* abfd
,
501 elf_elfheader (abfd
)->e_flags
= flags
;
502 elf_flags_init (abfd
) = TRUE
;
507 mt_elf_copy_private_bfd_data (bfd
* ibfd
, bfd
* obfd
)
509 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
510 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
513 BFD_ASSERT (!elf_flags_init (obfd
)
514 || elf_elfheader (obfd
)->e_flags
== elf_elfheader (ibfd
)->e_flags
);
516 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
517 elf_flags_init (obfd
) = TRUE
;
521 /* Merge backend specific data from an object file to the output
522 object file when linking. */
525 mt_elf_merge_private_bfd_data (bfd
* ibfd
, bfd
* obfd
)
527 flagword old_flags
, new_flags
;
528 bfd_boolean ok
= TRUE
;
530 /* Check if we have the same endianess. */
531 if (_bfd_generic_verify_endian_match (ibfd
, obfd
) == FALSE
)
534 /* If they're not both mt, then merging is meaningless, so just
536 if (strcmp (ibfd
->arch_info
->arch_name
, "mt") != 0)
538 if (strcmp (obfd
->arch_info
->arch_name
, "mt") != 0)
541 new_flags
= elf_elfheader (ibfd
)->e_flags
;
542 old_flags
= elf_elfheader (obfd
)->e_flags
;
545 _bfd_error_handler ("%B: old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s",
546 ibfd
, old_flags
, new_flags
, elf_flags_init (obfd
) ? "yes" : "no");
549 if (!elf_flags_init (obfd
))
551 old_flags
= new_flags
;
552 elf_flags_init (obfd
) = TRUE
;
554 else if ((new_flags
& EF_MT_CPU_MASK
) != (old_flags
& EF_MT_CPU_MASK
))
556 /* CPU has changed. This is invalid, because MRISC, MRISC2 and
557 MS2 are not subsets of each other. */
563 obfd
->arch_info
= ibfd
->arch_info
;
564 elf_elfheader (obfd
)->e_flags
= old_flags
;
571 mt_elf_print_private_bfd_data (bfd
* abfd
, void * ptr
)
573 FILE * file
= (FILE *) ptr
;
576 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
578 /* Print normal ELF private data. */
579 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
581 flags
= elf_elfheader (abfd
)->e_flags
;
582 fprintf (file
, _("private flags = 0x%lx:"), (long)flags
);
584 switch (flags
& EF_MT_CPU_MASK
)
587 case EF_MT_CPU_MRISC
: fprintf (file
, " ms1-16-002"); break;
588 case EF_MT_CPU_MRISC2
: fprintf (file
, " ms1-16-003"); break;
589 case EF_MT_CPU_MS2
: fprintf (file
, " ms2"); break;
598 #define TARGET_BIG_SYM bfd_elf32_mt_vec
599 #define TARGET_BIG_NAME "elf32-mt"
601 #define ELF_ARCH bfd_arch_mt
602 #define ELF_MACHINE_CODE EM_MT
603 #define ELF_MAXPAGESIZE 1 /* No pages on the MT. */
605 #define elf_info_to_howto_rel NULL
606 #define elf_info_to_howto mt_info_to_howto_rela
608 #define elf_backend_relocate_section mt_elf_relocate_section
610 #define bfd_elf32_bfd_reloc_type_lookup mt_reloc_type_lookup
611 #define bfd_elf32_bfd_reloc_name_lookup mt_reloc_name_lookup
613 #define elf_backend_check_relocs mt_elf_check_relocs
614 #define elf_backend_object_p mt_elf_object_p
615 #define elf_backend_rela_normal 1
617 #define elf_backend_can_gc_sections 1
619 #define bfd_elf32_bfd_set_private_flags mt_elf_set_private_flags
620 #define bfd_elf32_bfd_copy_private_bfd_data mt_elf_copy_private_bfd_data
621 #define bfd_elf32_bfd_merge_private_bfd_data mt_elf_merge_private_bfd_data
622 #define bfd_elf32_bfd_print_private_bfd_data mt_elf_print_private_bfd_data
624 #include "elf32-target.h"