daily update
[binutils.git] / bfd / elf-m10200.c
blobd2611dd7bcd5647c162811249e8deb00056c2a33
1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
26 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
27 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
28 static void mn10200_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
30 static bfd_boolean mn10200_elf_relax_delete_bytes
31 PARAMS ((bfd *, asection *, bfd_vma, int));
32 static bfd_boolean mn10200_elf_symbol_address_p
33 PARAMS ((bfd *, asection *, Elf_Internal_Sym *, bfd_vma));
34 static bfd_reloc_status_type mn10200_elf_final_link_relocate
35 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *,
36 bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
37 struct bfd_link_info *, asection *, int));
38 static bfd_boolean mn10200_elf_relocate_section
39 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
40 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *,
41 asection **));
42 static bfd_boolean mn10200_elf_relax_section
43 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
44 static bfd_byte * mn10200_elf_get_relocated_section_contents
45 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
46 bfd_byte *, bfd_boolean, asymbol **));
48 enum reloc_type {
49 R_MN10200_NONE = 0,
50 R_MN10200_32,
51 R_MN10200_16,
52 R_MN10200_8,
53 R_MN10200_24,
54 R_MN10200_PCREL8,
55 R_MN10200_PCREL16,
56 R_MN10200_PCREL24,
57 R_MN10200_MAX
60 static reloc_howto_type elf_mn10200_howto_table[] = {
61 /* Dummy relocation. Does nothing. */
62 HOWTO (R_MN10200_NONE,
65 16,
66 FALSE,
68 complain_overflow_bitfield,
69 bfd_elf_generic_reloc,
70 "R_MN10200_NONE",
71 FALSE,
74 FALSE),
75 /* Standard 32 bit reloc. */
76 HOWTO (R_MN10200_32,
79 32,
80 FALSE,
82 complain_overflow_bitfield,
83 bfd_elf_generic_reloc,
84 "R_MN10200_32",
85 FALSE,
86 0xffffffff,
87 0xffffffff,
88 FALSE),
89 /* Standard 16 bit reloc. */
90 HOWTO (R_MN10200_16,
93 16,
94 FALSE,
96 complain_overflow_bitfield,
97 bfd_elf_generic_reloc,
98 "R_MN10200_16",
99 FALSE,
100 0xffff,
101 0xffff,
102 FALSE),
103 /* Standard 8 bit reloc. */
104 HOWTO (R_MN10200_8,
108 FALSE,
110 complain_overflow_bitfield,
111 bfd_elf_generic_reloc,
112 "R_MN10200_8",
113 FALSE,
114 0xff,
115 0xff,
116 FALSE),
117 /* Standard 24 bit reloc. */
118 HOWTO (R_MN10200_24,
122 FALSE,
124 complain_overflow_bitfield,
125 bfd_elf_generic_reloc,
126 "R_MN10200_24",
127 FALSE,
128 0xffffff,
129 0xffffff,
130 FALSE),
131 /* Simple 8 pc-relative reloc. */
132 HOWTO (R_MN10200_PCREL8,
136 TRUE,
138 complain_overflow_bitfield,
139 bfd_elf_generic_reloc,
140 "R_MN10200_PCREL8",
141 FALSE,
142 0xff,
143 0xff,
144 TRUE),
145 /* Simple 16 pc-relative reloc. */
146 HOWTO (R_MN10200_PCREL16,
150 TRUE,
152 complain_overflow_bitfield,
153 bfd_elf_generic_reloc,
154 "R_MN10200_PCREL16",
155 FALSE,
156 0xffff,
157 0xffff,
158 TRUE),
159 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
160 to get the pc-relative offset correct. */
161 HOWTO (R_MN10200_PCREL24,
165 TRUE,
167 complain_overflow_bitfield,
168 bfd_elf_generic_reloc,
169 "R_MN10200_PCREL24",
170 FALSE,
171 0xffffff,
172 0xffffff,
173 TRUE),
176 struct mn10200_reloc_map {
177 bfd_reloc_code_real_type bfd_reloc_val;
178 unsigned char elf_reloc_val;
181 static const struct mn10200_reloc_map mn10200_reloc_map[] = {
182 { BFD_RELOC_NONE , R_MN10200_NONE , },
183 { BFD_RELOC_32 , R_MN10200_32 , },
184 { BFD_RELOC_16 , R_MN10200_16 , },
185 { BFD_RELOC_8 , R_MN10200_8 , },
186 { BFD_RELOC_24 , R_MN10200_24 , },
187 { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , },
188 { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
189 { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
192 static reloc_howto_type *
193 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
194 bfd *abfd ATTRIBUTE_UNUSED;
195 bfd_reloc_code_real_type code;
197 unsigned int i;
199 for (i = 0;
200 i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
201 i++)
203 if (mn10200_reloc_map[i].bfd_reloc_val == code)
204 return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
207 return NULL;
210 static reloc_howto_type *
211 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
212 const char *r_name)
214 unsigned int i;
216 for (i = 0;
217 i < (sizeof (elf_mn10200_howto_table)
218 / sizeof (elf_mn10200_howto_table[0]));
219 i++)
220 if (elf_mn10200_howto_table[i].name != NULL
221 && strcasecmp (elf_mn10200_howto_table[i].name, r_name) == 0)
222 return &elf_mn10200_howto_table[i];
224 return NULL;
227 /* Set the howto pointer for an MN10200 ELF reloc. */
229 static void
230 mn10200_info_to_howto (abfd, cache_ptr, dst)
231 bfd *abfd ATTRIBUTE_UNUSED;
232 arelent *cache_ptr;
233 Elf_Internal_Rela *dst;
235 unsigned int r_type;
237 r_type = ELF32_R_TYPE (dst->r_info);
238 BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
239 cache_ptr->howto = &elf_mn10200_howto_table[r_type];
242 /* Perform a relocation as part of a final link. */
244 static bfd_reloc_status_type
245 mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
246 input_section, contents, offset, value,
247 addend, info, sym_sec, is_local)
248 reloc_howto_type *howto;
249 bfd *input_bfd;
250 bfd *output_bfd ATTRIBUTE_UNUSED;
251 asection *input_section;
252 bfd_byte *contents;
253 bfd_vma offset;
254 bfd_vma value;
255 bfd_vma addend;
256 struct bfd_link_info *info ATTRIBUTE_UNUSED;
257 asection *sym_sec ATTRIBUTE_UNUSED;
258 int is_local ATTRIBUTE_UNUSED;
260 unsigned long r_type = howto->type;
261 bfd_byte *hit_data = contents + offset;
263 switch (r_type)
266 case R_MN10200_NONE:
267 return bfd_reloc_ok;
269 case R_MN10200_32:
270 value += addend;
271 bfd_put_32 (input_bfd, value, hit_data);
272 return bfd_reloc_ok;
274 case R_MN10200_16:
275 value += addend;
277 if ((long) value > 0x7fff || (long) value < -0x8000)
278 return bfd_reloc_overflow;
280 bfd_put_16 (input_bfd, value, hit_data);
281 return bfd_reloc_ok;
283 case R_MN10200_8:
284 value += addend;
286 if ((long) value > 0x7f || (long) value < -0x80)
287 return bfd_reloc_overflow;
289 bfd_put_8 (input_bfd, value, hit_data);
290 return bfd_reloc_ok;
292 case R_MN10200_24:
293 value += addend;
295 if ((long) value > 0x7fffff || (long) value < -0x800000)
296 return bfd_reloc_overflow;
298 value &= 0xffffff;
299 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
300 bfd_put_32 (input_bfd, value, hit_data);
301 return bfd_reloc_ok;
303 case R_MN10200_PCREL8:
304 value -= (input_section->output_section->vma
305 + input_section->output_offset);
306 value -= (offset + 1);
307 value += addend;
309 if ((long) value > 0xff || (long) value < -0x100)
310 return bfd_reloc_overflow;
312 bfd_put_8 (input_bfd, value, hit_data);
313 return bfd_reloc_ok;
315 case R_MN10200_PCREL16:
316 value -= (input_section->output_section->vma
317 + input_section->output_offset);
318 value -= (offset + 2);
319 value += addend;
321 if ((long) value > 0xffff || (long) value < -0x10000)
322 return bfd_reloc_overflow;
324 bfd_put_16 (input_bfd, value, hit_data);
325 return bfd_reloc_ok;
327 case R_MN10200_PCREL24:
328 value -= (input_section->output_section->vma
329 + input_section->output_offset);
330 value -= (offset + 3);
331 value += addend;
333 if ((long) value > 0xffffff || (long) value < -0x1000000)
334 return bfd_reloc_overflow;
336 value &= 0xffffff;
337 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
338 bfd_put_32 (input_bfd, value, hit_data);
339 return bfd_reloc_ok;
341 default:
342 return bfd_reloc_notsupported;
346 /* Relocate an MN10200 ELF section. */
347 static bfd_boolean
348 mn10200_elf_relocate_section (output_bfd, info, input_bfd, input_section,
349 contents, relocs, local_syms, local_sections)
350 bfd *output_bfd;
351 struct bfd_link_info *info;
352 bfd *input_bfd;
353 asection *input_section;
354 bfd_byte *contents;
355 Elf_Internal_Rela *relocs;
356 Elf_Internal_Sym *local_syms;
357 asection **local_sections;
359 Elf_Internal_Shdr *symtab_hdr;
360 struct elf_link_hash_entry **sym_hashes;
361 Elf_Internal_Rela *rel, *relend;
363 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
364 sym_hashes = elf_sym_hashes (input_bfd);
366 rel = relocs;
367 relend = relocs + input_section->reloc_count;
368 for (; rel < relend; rel++)
370 int r_type;
371 reloc_howto_type *howto;
372 unsigned long r_symndx;
373 Elf_Internal_Sym *sym;
374 asection *sec;
375 struct elf_link_hash_entry *h;
376 bfd_vma relocation;
377 bfd_reloc_status_type r;
379 r_symndx = ELF32_R_SYM (rel->r_info);
380 r_type = ELF32_R_TYPE (rel->r_info);
381 howto = elf_mn10200_howto_table + r_type;
383 h = NULL;
384 sym = NULL;
385 sec = NULL;
386 if (r_symndx < symtab_hdr->sh_info)
388 sym = local_syms + r_symndx;
389 sec = local_sections[r_symndx];
390 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
392 else
394 bfd_boolean unresolved_reloc, warned;
396 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
397 r_symndx, symtab_hdr, sym_hashes,
398 h, sec, relocation,
399 unresolved_reloc, warned);
402 if (sec != NULL && elf_discarded_section (sec))
404 /* For relocs against symbols from removed linkonce sections,
405 or sections discarded by a linker script, we just want the
406 section contents zeroed. Avoid any special processing. */
407 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
408 rel->r_info = 0;
409 rel->r_addend = 0;
410 continue;
413 if (info->relocatable)
414 continue;
416 r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
417 input_section,
418 contents, rel->r_offset,
419 relocation, rel->r_addend,
420 info, sec, h == NULL);
422 if (r != bfd_reloc_ok)
424 const char *name;
425 const char *msg = (const char *) 0;
427 if (h != NULL)
428 name = h->root.root.string;
429 else
431 name = (bfd_elf_string_from_elf_section
432 (input_bfd, symtab_hdr->sh_link, sym->st_name));
433 if (name == NULL || *name == '\0')
434 name = bfd_section_name (input_bfd, sec);
437 switch (r)
439 case bfd_reloc_overflow:
440 if (! ((*info->callbacks->reloc_overflow)
441 (info, (h ? &h->root : NULL), name, howto->name,
442 (bfd_vma) 0, input_bfd, input_section,
443 rel->r_offset)))
444 return FALSE;
445 break;
447 case bfd_reloc_undefined:
448 if (! ((*info->callbacks->undefined_symbol)
449 (info, name, input_bfd, input_section,
450 rel->r_offset, TRUE)))
451 return FALSE;
452 break;
454 case bfd_reloc_outofrange:
455 msg = _("internal error: out of range error");
456 goto common_error;
458 case bfd_reloc_notsupported:
459 msg = _("internal error: unsupported relocation error");
460 goto common_error;
462 case bfd_reloc_dangerous:
463 msg = _("internal error: dangerous error");
464 goto common_error;
466 default:
467 msg = _("internal error: unknown error");
468 /* fall through */
470 common_error:
471 if (!((*info->callbacks->warning)
472 (info, msg, name, input_bfd, input_section,
473 rel->r_offset)))
474 return FALSE;
475 break;
480 return TRUE;
483 /* This function handles relaxing for the mn10200.
485 There are quite a few relaxing opportunities available on the mn10200:
487 * jsr:24 -> jsr:16 2 bytes
489 * jmp:24 -> jmp:16 2 bytes
490 * jmp:16 -> bra:8 1 byte
492 * If the previous instruction is a conditional branch
493 around the jump/bra, we may be able to reverse its condition
494 and change its target to the jump's target. The jump/bra
495 can then be deleted. 2 bytes
497 * mov abs24 -> mov abs16 2 byte savings
499 * Most instructions which accept imm24 can relax to imm16 2 bytes
500 - Most instructions which accept imm16 can relax to imm8 1 byte
502 * Most instructions which accept d24 can relax to d16 2 bytes
503 - Most instructions which accept d16 can relax to d8 1 byte
505 abs24, imm24, d24 all look the same at the reloc level. It
506 might make the code simpler if we had different relocs for
507 the various relaxable operand types.
509 We don't handle imm16->imm8 or d16->d8 as they're very rare
510 and somewhat more difficult to support. */
512 static bfd_boolean
513 mn10200_elf_relax_section (abfd, sec, link_info, again)
514 bfd *abfd;
515 asection *sec;
516 struct bfd_link_info *link_info;
517 bfd_boolean *again;
519 Elf_Internal_Shdr *symtab_hdr;
520 Elf_Internal_Rela *internal_relocs;
521 Elf_Internal_Rela *irel, *irelend;
522 bfd_byte *contents = NULL;
523 Elf_Internal_Sym *isymbuf = NULL;
525 /* Assume nothing changes. */
526 *again = FALSE;
528 /* We don't have to do anything for a relocatable link, if
529 this section does not have relocs, or if this is not a
530 code section. */
531 if (link_info->relocatable
532 || (sec->flags & SEC_RELOC) == 0
533 || sec->reloc_count == 0
534 || (sec->flags & SEC_CODE) == 0)
535 return TRUE;
537 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
539 /* Get a copy of the native relocations. */
540 internal_relocs = (_bfd_elf_link_read_relocs
541 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
542 link_info->keep_memory));
543 if (internal_relocs == NULL)
544 goto error_return;
546 /* Walk through them looking for relaxing opportunities. */
547 irelend = internal_relocs + sec->reloc_count;
548 for (irel = internal_relocs; irel < irelend; irel++)
550 bfd_vma symval;
552 /* If this isn't something that can be relaxed, then ignore
553 this reloc. */
554 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
555 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
556 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
557 continue;
559 /* Get the section contents if we haven't done so already. */
560 if (contents == NULL)
562 /* Get cached copy if it exists. */
563 if (elf_section_data (sec)->this_hdr.contents != NULL)
564 contents = elf_section_data (sec)->this_hdr.contents;
565 else
567 /* Go get them off disk. */
568 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
569 goto error_return;
573 /* Read this BFD's local symbols if we haven't done so already. */
574 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
576 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
577 if (isymbuf == NULL)
578 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
579 symtab_hdr->sh_info, 0,
580 NULL, NULL, NULL);
581 if (isymbuf == NULL)
582 goto error_return;
585 /* Get the value of the symbol referred to by the reloc. */
586 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
588 /* A local symbol. */
589 Elf_Internal_Sym *isym;
590 asection *sym_sec;
592 isym = isymbuf + ELF32_R_SYM (irel->r_info);
593 if (isym->st_shndx == SHN_UNDEF)
594 sym_sec = bfd_und_section_ptr;
595 else if (isym->st_shndx == SHN_ABS)
596 sym_sec = bfd_abs_section_ptr;
597 else if (isym->st_shndx == SHN_COMMON)
598 sym_sec = bfd_com_section_ptr;
599 else
600 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
601 symval = (isym->st_value
602 + sym_sec->output_section->vma
603 + sym_sec->output_offset);
605 else
607 unsigned long indx;
608 struct elf_link_hash_entry *h;
610 /* An external symbol. */
611 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
612 h = elf_sym_hashes (abfd)[indx];
613 BFD_ASSERT (h != NULL);
614 if (h->root.type != bfd_link_hash_defined
615 && h->root.type != bfd_link_hash_defweak)
617 /* This appears to be a reference to an undefined
618 symbol. Just ignore it--it will be caught by the
619 regular reloc processing. */
620 continue;
623 symval = (h->root.u.def.value
624 + h->root.u.def.section->output_section->vma
625 + h->root.u.def.section->output_offset);
628 /* For simplicity of coding, we are going to modify the section
629 contents, the section relocs, and the BFD symbol table. We
630 must tell the rest of the code not to free up this
631 information. It would be possible to instead create a table
632 of changes which have to be made, as is done in coff-mips.c;
633 that would be more work, but would require less memory when
634 the linker is run. */
636 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
637 branch/call. */
638 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
640 bfd_vma value = symval;
642 /* Deal with pc-relative gunk. */
643 value -= (sec->output_section->vma + sec->output_offset);
644 value -= (irel->r_offset + 3);
645 value += irel->r_addend;
647 /* See if the value will fit in 16 bits, note the high value is
648 0x7fff + 2 as the target will be two bytes closer if we are
649 able to relax. */
650 if ((long) value < 0x8001 && (long) value > -0x8000)
652 unsigned char code;
654 /* Get the opcode. */
655 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
657 if (code != 0xe0 && code != 0xe1)
658 continue;
660 /* Note that we've changed the relocs, section contents, etc. */
661 elf_section_data (sec)->relocs = internal_relocs;
662 elf_section_data (sec)->this_hdr.contents = contents;
663 symtab_hdr->contents = (unsigned char *) isymbuf;
665 /* Fix the opcode. */
666 if (code == 0xe0)
667 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
668 else if (code == 0xe1)
669 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
671 /* Fix the relocation's type. */
672 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
673 R_MN10200_PCREL16);
675 /* The opcode got shorter too, so we have to fix the offset. */
676 irel->r_offset -= 1;
678 /* Delete two bytes of data. */
679 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
680 irel->r_offset + 1, 2))
681 goto error_return;
683 /* That will change things, so, we should relax again.
684 Note that this is not required, and it may be slow. */
685 *again = TRUE;
689 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
690 branch. */
691 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
693 bfd_vma value = symval;
695 /* Deal with pc-relative gunk. */
696 value -= (sec->output_section->vma + sec->output_offset);
697 value -= (irel->r_offset + 2);
698 value += irel->r_addend;
700 /* See if the value will fit in 8 bits, note the high value is
701 0x7f + 1 as the target will be one bytes closer if we are
702 able to relax. */
703 if ((long) value < 0x80 && (long) value > -0x80)
705 unsigned char code;
707 /* Get the opcode. */
708 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
710 if (code != 0xfc)
711 continue;
713 /* Note that we've changed the relocs, section contents, etc. */
714 elf_section_data (sec)->relocs = internal_relocs;
715 elf_section_data (sec)->this_hdr.contents = contents;
716 symtab_hdr->contents = (unsigned char *) isymbuf;
718 /* Fix the opcode. */
719 bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
721 /* Fix the relocation's type. */
722 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
723 R_MN10200_PCREL8);
725 /* Delete one byte of data. */
726 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
727 irel->r_offset + 1, 1))
728 goto error_return;
730 /* That will change things, so, we should relax again.
731 Note that this is not required, and it may be slow. */
732 *again = TRUE;
736 /* Try to eliminate an unconditional 8 bit pc-relative branch
737 which immediately follows a conditional 8 bit pc-relative
738 branch around the unconditional branch.
740 original: new:
741 bCC lab1 bCC' lab2
742 bra lab2
743 lab1: lab1:
745 This happens when the bCC can't reach lab2 at assembly time,
746 but due to other relaxations it can reach at link time. */
747 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
749 Elf_Internal_Rela *nrel;
750 bfd_vma value = symval;
751 unsigned char code;
753 /* Deal with pc-relative gunk. */
754 value -= (sec->output_section->vma + sec->output_offset);
755 value -= (irel->r_offset + 1);
756 value += irel->r_addend;
758 /* Do nothing if this reloc is the last byte in the section. */
759 if (irel->r_offset == sec->size)
760 continue;
762 /* See if the next instruction is an unconditional pc-relative
763 branch, more often than not this test will fail, so we
764 test it first to speed things up. */
765 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
766 if (code != 0xea)
767 continue;
769 /* Also make sure the next relocation applies to the next
770 instruction and that it's a pc-relative 8 bit branch. */
771 nrel = irel + 1;
772 if (nrel == irelend
773 || irel->r_offset + 2 != nrel->r_offset
774 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
775 continue;
777 /* Make sure our destination immediately follows the
778 unconditional branch. */
779 if (symval != (sec->output_section->vma + sec->output_offset
780 + irel->r_offset + 3))
781 continue;
783 /* Now make sure we are a conditional branch. This may not
784 be necessary, but why take the chance.
786 Note these checks assume that R_MN10200_PCREL8 relocs
787 only occur on bCC and bCCx insns. If they occured
788 elsewhere, we'd need to know the start of this insn
789 for this check to be accurate. */
790 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
791 if (code != 0xe0 && code != 0xe1 && code != 0xe2
792 && code != 0xe3 && code != 0xe4 && code != 0xe5
793 && code != 0xe6 && code != 0xe7 && code != 0xe8
794 && code != 0xe9 && code != 0xec && code != 0xed
795 && code != 0xee && code != 0xef && code != 0xfc
796 && code != 0xfd && code != 0xfe && code != 0xff)
797 continue;
799 /* We also have to be sure there is no symbol/label
800 at the unconditional branch. */
801 if (mn10200_elf_symbol_address_p (abfd, sec, isymbuf,
802 irel->r_offset + 1))
803 continue;
805 /* Note that we've changed the relocs, section contents, etc. */
806 elf_section_data (sec)->relocs = internal_relocs;
807 elf_section_data (sec)->this_hdr.contents = contents;
808 symtab_hdr->contents = (unsigned char *) isymbuf;
810 /* Reverse the condition of the first branch. */
811 switch (code)
813 case 0xfc:
814 code = 0xfd;
815 break;
816 case 0xfd:
817 code = 0xfc;
818 break;
819 case 0xfe:
820 code = 0xff;
821 break;
822 case 0xff:
823 code = 0xfe;
824 break;
825 case 0xe8:
826 code = 0xe9;
827 break;
828 case 0xe9:
829 code = 0xe8;
830 break;
831 case 0xe0:
832 code = 0xe2;
833 break;
834 case 0xe2:
835 code = 0xe0;
836 break;
837 case 0xe3:
838 code = 0xe1;
839 break;
840 case 0xe1:
841 code = 0xe3;
842 break;
843 case 0xe4:
844 code = 0xe6;
845 break;
846 case 0xe6:
847 code = 0xe4;
848 break;
849 case 0xe7:
850 code = 0xe5;
851 break;
852 case 0xe5:
853 code = 0xe7;
854 break;
855 case 0xec:
856 code = 0xed;
857 break;
858 case 0xed:
859 code = 0xec;
860 break;
861 case 0xee:
862 code = 0xef;
863 break;
864 case 0xef:
865 code = 0xee;
866 break;
868 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
870 /* Set the reloc type and symbol for the first branch
871 from the second branch. */
872 irel->r_info = nrel->r_info;
874 /* Make the reloc for the second branch a null reloc. */
875 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
876 R_MN10200_NONE);
878 /* Delete two bytes of data. */
879 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
880 irel->r_offset + 1, 2))
881 goto error_return;
883 /* That will change things, so, we should relax again.
884 Note that this is not required, and it may be slow. */
885 *again = TRUE;
888 /* Try to turn a 24bit immediate, displacement or absolute address
889 into a 16bit immediate, displacement or absolute address. */
890 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
892 bfd_vma value = symval;
894 /* See if the value will fit in 16 bits.
895 We allow any 16bit match here. We prune those we can't
896 handle below. */
897 if ((long) value < 0x7fff && (long) value > -0x8000)
899 unsigned char code;
901 /* All insns which have 24bit operands are 5 bytes long,
902 the first byte will always be 0xf4, but we double check
903 it just in case. */
905 /* Get the first opcode. */
906 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
908 if (code != 0xf4)
909 continue;
911 /* Get the second opcode. */
912 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
914 switch (code & 0xfc)
916 /* mov imm24,dn -> mov imm16,dn */
917 case 0x70:
918 /* Not safe if the high bit is on as relaxing may
919 move the value out of high mem and thus not fit
920 in a signed 16bit value. */
921 if (value & 0x8000)
922 continue;
924 /* Note that we've changed the relocation contents, etc. */
925 elf_section_data (sec)->relocs = internal_relocs;
926 elf_section_data (sec)->this_hdr.contents = contents;
927 symtab_hdr->contents = (unsigned char *) isymbuf;
929 /* Fix the opcode. */
930 bfd_put_8 (abfd, 0xf8 + (code & 0x03),
931 contents + irel->r_offset - 2);
933 /* Fix the relocation's type. */
934 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
935 R_MN10200_16);
937 /* The opcode got shorter too, so we have to fix the
938 offset. */
939 irel->r_offset -= 1;
941 /* Delete two bytes of data. */
942 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
943 irel->r_offset + 1, 2))
944 goto error_return;
946 /* That will change things, so, we should relax again.
947 Note that this is not required, and it may be slow. */
948 *again = TRUE;
949 break;
951 /* mov imm24,an -> mov imm16,an
952 cmp imm24,an -> cmp imm16,an
953 mov (abs24),dn -> mov (abs16),dn
954 mov dn,(abs24) -> mov dn,(abs16)
955 movb dn,(abs24) -> movb dn,(abs16)
956 movbu (abs24),dn -> movbu (abs16),dn */
957 case 0x74:
958 case 0x7c:
959 case 0xc0:
960 case 0x40:
961 case 0x44:
962 case 0xc8:
963 /* Note that we've changed the relocation contents, etc. */
964 elf_section_data (sec)->relocs = internal_relocs;
965 elf_section_data (sec)->this_hdr.contents = contents;
966 symtab_hdr->contents = (unsigned char *) isymbuf;
968 if ((code & 0xfc) == 0x74)
969 code = 0xdc + (code & 0x03);
970 else if ((code & 0xfc) == 0x7c)
971 code = 0xec + (code & 0x03);
972 else if ((code & 0xfc) == 0xc0)
973 code = 0xc8 + (code & 0x03);
974 else if ((code & 0xfc) == 0x40)
975 code = 0xc0 + (code & 0x03);
976 else if ((code & 0xfc) == 0x44)
977 code = 0xc4 + (code & 0x03);
978 else if ((code & 0xfc) == 0xc8)
979 code = 0xcc + (code & 0x03);
981 /* Fix the opcode. */
982 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
984 /* Fix the relocation's type. */
985 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
986 R_MN10200_16);
988 /* The opcode got shorter too, so we have to fix the
989 offset. */
990 irel->r_offset -= 1;
992 /* Delete two bytes of data. */
993 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
994 irel->r_offset + 1, 2))
995 goto error_return;
997 /* That will change things, so, we should relax again.
998 Note that this is not required, and it may be slow. */
999 *again = TRUE;
1000 break;
1002 /* cmp imm24,dn -> cmp imm16,dn
1003 mov (abs24),an -> mov (abs16),an
1004 mov an,(abs24) -> mov an,(abs16)
1005 add imm24,dn -> add imm16,dn
1006 add imm24,an -> add imm16,an
1007 sub imm24,dn -> sub imm16,dn
1008 sub imm24,an -> sub imm16,an
1009 And all d24->d16 in memory ops. */
1010 case 0x78:
1011 case 0xd0:
1012 case 0x50:
1013 case 0x60:
1014 case 0x64:
1015 case 0x68:
1016 case 0x6c:
1017 case 0x80:
1018 case 0xf0:
1019 case 0x00:
1020 case 0x10:
1021 case 0xb0:
1022 case 0x30:
1023 case 0xa0:
1024 case 0x20:
1025 case 0x90:
1026 /* Not safe if the high bit is on as relaxing may
1027 move the value out of high mem and thus not fit
1028 in a signed 16bit value. */
1029 if (((code & 0xfc) == 0x78
1030 || (code & 0xfc) == 0x60
1031 || (code & 0xfc) == 0x64
1032 || (code & 0xfc) == 0x68
1033 || (code & 0xfc) == 0x6c
1034 || (code & 0xfc) == 0x80
1035 || (code & 0xfc) == 0xf0
1036 || (code & 0xfc) == 0x00
1037 || (code & 0xfc) == 0x10
1038 || (code & 0xfc) == 0xb0
1039 || (code & 0xfc) == 0x30
1040 || (code & 0xfc) == 0xa0
1041 || (code & 0xfc) == 0x20
1042 || (code & 0xfc) == 0x90)
1043 && (value & 0x8000) != 0)
1044 continue;
1046 /* Note that we've changed the relocation contents, etc. */
1047 elf_section_data (sec)->relocs = internal_relocs;
1048 elf_section_data (sec)->this_hdr.contents = contents;
1049 symtab_hdr->contents = (unsigned char *) isymbuf;
1051 /* Fix the opcode. */
1052 bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
1054 if ((code & 0xfc) == 0x78)
1055 code = 0x48 + (code & 0x03);
1056 else if ((code & 0xfc) == 0xd0)
1057 code = 0x30 + (code & 0x03);
1058 else if ((code & 0xfc) == 0x50)
1059 code = 0x20 + (code & 0x03);
1060 else if ((code & 0xfc) == 0x60)
1061 code = 0x18 + (code & 0x03);
1062 else if ((code & 0xfc) == 0x64)
1063 code = 0x08 + (code & 0x03);
1064 else if ((code & 0xfc) == 0x68)
1065 code = 0x1c + (code & 0x03);
1066 else if ((code & 0xfc) == 0x6c)
1067 code = 0x0c + (code & 0x03);
1068 else if ((code & 0xfc) == 0x80)
1069 code = 0xc0 + (code & 0x07);
1070 else if ((code & 0xfc) == 0xf0)
1071 code = 0xb0 + (code & 0x07);
1072 else if ((code & 0xfc) == 0x00)
1073 code = 0x80 + (code & 0x07);
1074 else if ((code & 0xfc) == 0x10)
1075 code = 0xa0 + (code & 0x07);
1076 else if ((code & 0xfc) == 0xb0)
1077 code = 0x70 + (code & 0x07);
1078 else if ((code & 0xfc) == 0x30)
1079 code = 0x60 + (code & 0x07);
1080 else if ((code & 0xfc) == 0xa0)
1081 code = 0xd0 + (code & 0x07);
1082 else if ((code & 0xfc) == 0x20)
1083 code = 0x90 + (code & 0x07);
1084 else if ((code & 0xfc) == 0x90)
1085 code = 0x50 + (code & 0x07);
1087 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1089 /* Fix the relocation's type. */
1090 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1091 R_MN10200_16);
1093 /* Delete one bytes of data. */
1094 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1095 irel->r_offset + 2, 1))
1096 goto error_return;
1098 /* That will change things, so, we should relax again.
1099 Note that this is not required, and it may be slow. */
1100 *again = TRUE;
1101 break;
1103 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1104 case 0xc4:
1105 /* Note that we've changed the reldection contents, etc. */
1106 elf_section_data (sec)->relocs = internal_relocs;
1107 elf_section_data (sec)->this_hdr.contents = contents;
1108 symtab_hdr->contents = (unsigned char *) isymbuf;
1110 bfd_put_8 (abfd, 0xcc + (code & 0x03),
1111 contents + irel->r_offset - 2);
1113 bfd_put_8 (abfd, 0xb8 + (code & 0x03),
1114 contents + irel->r_offset - 1);
1116 /* Fix the relocation's type. */
1117 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1118 R_MN10200_16);
1120 /* The reloc will be applied one byte in front of its
1121 current location. */
1122 irel->r_offset -= 1;
1124 /* Delete one bytes of data. */
1125 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1126 irel->r_offset + 2, 1))
1127 goto error_return;
1129 /* That will change things, so, we should relax again.
1130 Note that this is not required, and it may be slow. */
1131 *again = TRUE;
1132 break;
1138 if (isymbuf != NULL
1139 && symtab_hdr->contents != (unsigned char *) isymbuf)
1141 if (! link_info->keep_memory)
1142 free (isymbuf);
1143 else
1145 /* Cache the symbols for elf_link_input_bfd. */
1146 symtab_hdr->contents = (unsigned char *) isymbuf;
1150 if (contents != NULL
1151 && elf_section_data (sec)->this_hdr.contents != contents)
1153 if (! link_info->keep_memory)
1154 free (contents);
1155 else
1157 /* Cache the section contents for elf_link_input_bfd. */
1158 elf_section_data (sec)->this_hdr.contents = contents;
1162 if (internal_relocs != NULL
1163 && elf_section_data (sec)->relocs != internal_relocs)
1164 free (internal_relocs);
1166 return TRUE;
1168 error_return:
1169 if (isymbuf != NULL
1170 && symtab_hdr->contents != (unsigned char *) isymbuf)
1171 free (isymbuf);
1172 if (contents != NULL
1173 && elf_section_data (sec)->this_hdr.contents != contents)
1174 free (contents);
1175 if (internal_relocs != NULL
1176 && elf_section_data (sec)->relocs != internal_relocs)
1177 free (internal_relocs);
1179 return FALSE;
1182 /* Delete some bytes from a section while relaxing. */
1184 static bfd_boolean
1185 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
1186 bfd *abfd;
1187 asection *sec;
1188 bfd_vma addr;
1189 int count;
1191 Elf_Internal_Shdr *symtab_hdr;
1192 unsigned int sec_shndx;
1193 bfd_byte *contents;
1194 Elf_Internal_Rela *irel, *irelend;
1195 Elf_Internal_Rela *irelalign;
1196 bfd_vma toaddr;
1197 Elf_Internal_Sym *isym;
1198 Elf_Internal_Sym *isymend;
1199 struct elf_link_hash_entry **sym_hashes;
1200 struct elf_link_hash_entry **end_hashes;
1201 unsigned int symcount;
1203 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1205 contents = elf_section_data (sec)->this_hdr.contents;
1207 /* The deletion must stop at the next ALIGN reloc for an aligment
1208 power larger than the number of bytes we are deleting. */
1210 irelalign = NULL;
1211 toaddr = sec->size;
1213 irel = elf_section_data (sec)->relocs;
1214 irelend = irel + sec->reloc_count;
1216 /* Actually delete the bytes. */
1217 memmove (contents + addr, contents + addr + count,
1218 (size_t) (toaddr - addr - count));
1219 sec->size -= count;
1221 /* Adjust all the relocs. */
1222 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1224 /* Get the new reloc address. */
1225 if ((irel->r_offset > addr
1226 && irel->r_offset < toaddr))
1227 irel->r_offset -= count;
1230 /* Adjust the local symbols defined in this section. */
1231 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1232 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1233 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1235 if (isym->st_shndx == sec_shndx
1236 && isym->st_value > addr
1237 && isym->st_value < toaddr)
1238 isym->st_value -= count;
1241 /* Now adjust the global symbols defined in this section. */
1242 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1243 - symtab_hdr->sh_info);
1244 sym_hashes = elf_sym_hashes (abfd);
1245 end_hashes = sym_hashes + symcount;
1246 for (; sym_hashes < end_hashes; sym_hashes++)
1248 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1249 if ((sym_hash->root.type == bfd_link_hash_defined
1250 || sym_hash->root.type == bfd_link_hash_defweak)
1251 && sym_hash->root.u.def.section == sec
1252 && sym_hash->root.u.def.value > addr
1253 && sym_hash->root.u.def.value < toaddr)
1255 sym_hash->root.u.def.value -= count;
1259 return TRUE;
1262 /* Return TRUE if a symbol exists at the given address, else return
1263 FALSE. */
1264 static bfd_boolean
1265 mn10200_elf_symbol_address_p (abfd, sec, isym, addr)
1266 bfd *abfd;
1267 asection *sec;
1268 Elf_Internal_Sym *isym;
1269 bfd_vma addr;
1271 Elf_Internal_Shdr *symtab_hdr;
1272 unsigned int sec_shndx;
1273 Elf_Internal_Sym *isymend;
1274 struct elf_link_hash_entry **sym_hashes;
1275 struct elf_link_hash_entry **end_hashes;
1276 unsigned int symcount;
1278 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1280 /* Examine all the local symbols. */
1281 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1282 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1284 if (isym->st_shndx == sec_shndx
1285 && isym->st_value == addr)
1286 return TRUE;
1289 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1290 - symtab_hdr->sh_info);
1291 sym_hashes = elf_sym_hashes (abfd);
1292 end_hashes = sym_hashes + symcount;
1293 for (; sym_hashes < end_hashes; sym_hashes++)
1295 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1296 if ((sym_hash->root.type == bfd_link_hash_defined
1297 || sym_hash->root.type == bfd_link_hash_defweak)
1298 && sym_hash->root.u.def.section == sec
1299 && sym_hash->root.u.def.value == addr)
1300 return TRUE;
1303 return FALSE;
1306 /* This is a version of bfd_generic_get_relocated_section_contents
1307 which uses mn10200_elf_relocate_section. */
1309 static bfd_byte *
1310 mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1311 data, relocatable, symbols)
1312 bfd *output_bfd;
1313 struct bfd_link_info *link_info;
1314 struct bfd_link_order *link_order;
1315 bfd_byte *data;
1316 bfd_boolean relocatable;
1317 asymbol **symbols;
1319 Elf_Internal_Shdr *symtab_hdr;
1320 asection *input_section = link_order->u.indirect.section;
1321 bfd *input_bfd = input_section->owner;
1322 asection **sections = NULL;
1323 Elf_Internal_Rela *internal_relocs = NULL;
1324 Elf_Internal_Sym *isymbuf = NULL;
1326 /* We only need to handle the case of relaxing, or of having a
1327 particular set of section contents, specially. */
1328 if (relocatable
1329 || elf_section_data (input_section)->this_hdr.contents == NULL)
1330 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1331 link_order, data,
1332 relocatable,
1333 symbols);
1335 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1337 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1338 (size_t) input_section->size);
1340 if ((input_section->flags & SEC_RELOC) != 0
1341 && input_section->reloc_count > 0)
1343 Elf_Internal_Sym *isym;
1344 Elf_Internal_Sym *isymend;
1345 asection **secpp;
1346 bfd_size_type amt;
1348 internal_relocs = (_bfd_elf_link_read_relocs
1349 (input_bfd, input_section, (PTR) NULL,
1350 (Elf_Internal_Rela *) NULL, FALSE));
1351 if (internal_relocs == NULL)
1352 goto error_return;
1354 if (symtab_hdr->sh_info != 0)
1356 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1357 if (isymbuf == NULL)
1358 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1359 symtab_hdr->sh_info, 0,
1360 NULL, NULL, NULL);
1361 if (isymbuf == NULL)
1362 goto error_return;
1365 amt = symtab_hdr->sh_info;
1366 amt *= sizeof (asection *);
1367 sections = (asection **) bfd_malloc (amt);
1368 if (sections == NULL && amt != 0)
1369 goto error_return;
1371 isymend = isymbuf + symtab_hdr->sh_info;
1372 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
1374 asection *isec;
1376 if (isym->st_shndx == SHN_UNDEF)
1377 isec = bfd_und_section_ptr;
1378 else if (isym->st_shndx == SHN_ABS)
1379 isec = bfd_abs_section_ptr;
1380 else if (isym->st_shndx == SHN_COMMON)
1381 isec = bfd_com_section_ptr;
1382 else
1383 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1385 *secpp = isec;
1388 if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1389 input_section, data, internal_relocs,
1390 isymbuf, sections))
1391 goto error_return;
1393 if (sections != NULL)
1394 free (sections);
1395 if (isymbuf != NULL
1396 && symtab_hdr->contents != (unsigned char *) isymbuf)
1397 free (isymbuf);
1398 if (elf_section_data (input_section)->relocs != internal_relocs)
1399 free (internal_relocs);
1402 return data;
1404 error_return:
1405 if (sections != NULL)
1406 free (sections);
1407 if (isymbuf != NULL
1408 && symtab_hdr->contents != (unsigned char *) isymbuf)
1409 free (isymbuf);
1410 if (internal_relocs != NULL
1411 && elf_section_data (input_section)->relocs != internal_relocs)
1412 free (internal_relocs);
1413 return NULL;
1416 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1417 #define TARGET_LITTLE_NAME "elf32-mn10200"
1418 #define ELF_ARCH bfd_arch_mn10200
1419 #define ELF_MACHINE_CODE EM_MN10200
1420 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1421 #define ELF_MAXPAGESIZE 0x1000
1423 #define elf_backend_rela_normal 1
1424 #define elf_info_to_howto mn10200_info_to_howto
1425 #define elf_info_to_howto_rel 0
1426 #define elf_backend_relocate_section mn10200_elf_relocate_section
1427 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1428 #define bfd_elf32_bfd_get_relocated_section_contents \
1429 mn10200_elf_get_relocated_section_contents
1431 #define elf_symbol_leading_char '_'
1433 #include "elf32-target.h"