1 /* BFD back-end for Hitachi Super-H COFF binaries.
2 Copyright 1993, 94, 95, 96, 97, 98, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4 Written by Steve Chamberlain, <sac@cygnus.com>.
5 Relaxing code written by Ian Lance Taylor, <ian@cygnus.com>.
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. */
28 #include "coff/internal.h"
36 /* Internal functions. */
37 static bfd_reloc_status_type sh_reloc
38 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
39 static long get_symbol_value PARAMS ((asymbol *));
40 static boolean sh_relax_section
41 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
42 static boolean sh_relax_delete_bytes
43 PARAMS ((bfd *, asection *, bfd_vma, int));
44 static const struct sh_opcode *sh_insn_info PARAMS ((unsigned int));
45 static boolean sh_align_loads
46 PARAMS ((bfd *, asection *, struct internal_reloc *, bfd_byte *, boolean *));
47 static boolean sh_swap_insns
48 PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
49 static boolean sh_relocate_section
50 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
51 struct internal_reloc *, struct internal_syment *, asection **));
52 static bfd_byte *sh_coff_get_relocated_section_contents
53 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
54 bfd_byte *, boolean, asymbol **));
57 /* Can't build import tables with 2**4 alignment. */
58 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
60 /* Default section alignment to 2**4. */
61 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 4
64 #ifdef COFF_IMAGE_WITH_PE
65 /* Align PE executables. */
66 #define COFF_PAGE_SIZE 0x1000
69 /* Generate long file names. */
70 #define COFF_LONG_FILENAMES
73 /* Return true if this relocation should
74 appear in the output .reloc section. */
75 static boolean in_reloc_p (abfd, howto)
76 bfd * abfd ATTRIBUTE_UNUSED;
77 reloc_howto_type * howto;
79 return ! howto->pc_relative && howto->type != R_SH_IMAGEBASE;
83 /* The supported relocations. There are a lot of relocations defined
84 in coff/internal.h which we do not expect to ever see. */
85 static reloc_howto_type sh_coff_howtos[] =
91 HOWTO (R_SH_IMM32CE, /* type */
93 2, /* size (0 = byte, 1 = short, 2 = long) */
95 false, /* pc_relative */
97 complain_overflow_bitfield, /* complain_on_overflow */
98 sh_reloc, /* special_function */
99 "r_imm32ce", /* name */
100 true, /* partial_inplace */
101 0xffffffff, /* src_mask */
102 0xffffffff, /* dst_mask */
103 false), /* pcrel_offset */
107 EMPTY_HOWTO (3), /* R_SH_PCREL8 */
108 EMPTY_HOWTO (4), /* R_SH_PCREL16 */
109 EMPTY_HOWTO (5), /* R_SH_HIGH8 */
110 EMPTY_HOWTO (6), /* R_SH_IMM24 */
111 EMPTY_HOWTO (7), /* R_SH_LOW16 */
113 EMPTY_HOWTO (9), /* R_SH_PCDISP8BY4 */
115 HOWTO (R_SH_PCDISP8BY2, /* type */
117 1, /* size (0 = byte, 1 = short, 2 = long) */
119 true, /* pc_relative */
121 complain_overflow_signed, /* complain_on_overflow */
122 sh_reloc, /* special_function */
123 "r_pcdisp8by2", /* name */
124 true, /* partial_inplace */
127 true), /* pcrel_offset */
129 EMPTY_HOWTO (11), /* R_SH_PCDISP8 */
131 HOWTO (R_SH_PCDISP, /* type */
133 1, /* size (0 = byte, 1 = short, 2 = long) */
135 true, /* pc_relative */
137 complain_overflow_signed, /* complain_on_overflow */
138 sh_reloc, /* special_function */
139 "r_pcdisp12by2", /* name */
140 true, /* partial_inplace */
141 0xfff, /* src_mask */
142 0xfff, /* dst_mask */
143 true), /* pcrel_offset */
147 HOWTO (R_SH_IMM32, /* type */
149 2, /* size (0 = byte, 1 = short, 2 = long) */
151 false, /* pc_relative */
153 complain_overflow_bitfield, /* complain_on_overflow */
154 sh_reloc, /* special_function */
155 "r_imm32", /* name */
156 true, /* partial_inplace */
157 0xffffffff, /* src_mask */
158 0xffffffff, /* dst_mask */
159 false), /* pcrel_offset */
163 HOWTO (R_SH_IMAGEBASE, /* type */
165 2, /* size (0 = byte, 1 = short, 2 = long) */
167 false, /* pc_relative */
169 complain_overflow_bitfield, /* complain_on_overflow */
170 sh_reloc, /* special_function */
172 true, /* partial_inplace */
173 0xffffffff, /* src_mask */
174 0xffffffff, /* dst_mask */
175 false), /* pcrel_offset */
177 EMPTY_HOWTO (16), /* R_SH_IMM8 */
179 EMPTY_HOWTO (17), /* R_SH_IMM8BY2 */
180 EMPTY_HOWTO (18), /* R_SH_IMM8BY4 */
181 EMPTY_HOWTO (19), /* R_SH_IMM4 */
182 EMPTY_HOWTO (20), /* R_SH_IMM4BY2 */
183 EMPTY_HOWTO (21), /* R_SH_IMM4BY4 */
185 HOWTO (R_SH_PCRELIMM8BY2, /* type */
187 1, /* size (0 = byte, 1 = short, 2 = long) */
189 true, /* pc_relative */
191 complain_overflow_unsigned, /* complain_on_overflow */
192 sh_reloc, /* special_function */
193 "r_pcrelimm8by2", /* name */
194 true, /* partial_inplace */
197 true), /* pcrel_offset */
199 HOWTO (R_SH_PCRELIMM8BY4, /* type */
201 1, /* size (0 = byte, 1 = short, 2 = long) */
203 true, /* pc_relative */
205 complain_overflow_unsigned, /* complain_on_overflow */
206 sh_reloc, /* special_function */
207 "r_pcrelimm8by4", /* name */
208 true, /* partial_inplace */
211 true), /* pcrel_offset */
213 HOWTO (R_SH_IMM16, /* type */
215 1, /* size (0 = byte, 1 = short, 2 = long) */
217 false, /* pc_relative */
219 complain_overflow_bitfield, /* complain_on_overflow */
220 sh_reloc, /* special_function */
221 "r_imm16", /* name */
222 true, /* partial_inplace */
223 0xffff, /* src_mask */
224 0xffff, /* dst_mask */
225 false), /* pcrel_offset */
227 HOWTO (R_SH_SWITCH16, /* type */
229 1, /* size (0 = byte, 1 = short, 2 = long) */
231 false, /* pc_relative */
233 complain_overflow_bitfield, /* complain_on_overflow */
234 sh_reloc, /* special_function */
235 "r_switch16", /* name */
236 true, /* partial_inplace */
237 0xffff, /* src_mask */
238 0xffff, /* dst_mask */
239 false), /* pcrel_offset */
241 HOWTO (R_SH_SWITCH32, /* type */
243 2, /* size (0 = byte, 1 = short, 2 = long) */
245 false, /* pc_relative */
247 complain_overflow_bitfield, /* complain_on_overflow */
248 sh_reloc, /* special_function */
249 "r_switch32", /* name */
250 true, /* partial_inplace */
251 0xffffffff, /* src_mask */
252 0xffffffff, /* dst_mask */
253 false), /* pcrel_offset */
255 HOWTO (R_SH_USES, /* type */
257 1, /* size (0 = byte, 1 = short, 2 = long) */
259 false, /* pc_relative */
261 complain_overflow_bitfield, /* complain_on_overflow */
262 sh_reloc, /* special_function */
264 true, /* partial_inplace */
265 0xffff, /* src_mask */
266 0xffff, /* dst_mask */
267 false), /* pcrel_offset */
269 HOWTO (R_SH_COUNT, /* type */
271 2, /* size (0 = byte, 1 = short, 2 = long) */
273 false, /* pc_relative */
275 complain_overflow_bitfield, /* complain_on_overflow */
276 sh_reloc, /* special_function */
277 "r_count", /* name */
278 true, /* partial_inplace */
279 0xffffffff, /* src_mask */
280 0xffffffff, /* dst_mask */
281 false), /* pcrel_offset */
283 HOWTO (R_SH_ALIGN, /* type */
285 2, /* size (0 = byte, 1 = short, 2 = long) */
287 false, /* pc_relative */
289 complain_overflow_bitfield, /* complain_on_overflow */
290 sh_reloc, /* special_function */
291 "r_align", /* name */
292 true, /* partial_inplace */
293 0xffffffff, /* src_mask */
294 0xffffffff, /* dst_mask */
295 false), /* pcrel_offset */
297 HOWTO (R_SH_CODE, /* type */
299 2, /* size (0 = byte, 1 = short, 2 = long) */
301 false, /* pc_relative */
303 complain_overflow_bitfield, /* complain_on_overflow */
304 sh_reloc, /* special_function */
306 true, /* partial_inplace */
307 0xffffffff, /* src_mask */
308 0xffffffff, /* dst_mask */
309 false), /* pcrel_offset */
311 HOWTO (R_SH_DATA, /* type */
313 2, /* size (0 = byte, 1 = short, 2 = long) */
315 false, /* pc_relative */
317 complain_overflow_bitfield, /* complain_on_overflow */
318 sh_reloc, /* special_function */
320 true, /* partial_inplace */
321 0xffffffff, /* src_mask */
322 0xffffffff, /* dst_mask */
323 false), /* pcrel_offset */
325 HOWTO (R_SH_LABEL, /* type */
327 2, /* size (0 = byte, 1 = short, 2 = long) */
329 false, /* pc_relative */
331 complain_overflow_bitfield, /* complain_on_overflow */
332 sh_reloc, /* special_function */
333 "r_label", /* name */
334 true, /* partial_inplace */
335 0xffffffff, /* src_mask */
336 0xffffffff, /* dst_mask */
337 false), /* pcrel_offset */
339 HOWTO (R_SH_SWITCH8, /* type */
341 0, /* size (0 = byte, 1 = short, 2 = long) */
343 false, /* pc_relative */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 sh_reloc, /* special_function */
347 "r_switch8", /* name */
348 true, /* partial_inplace */
351 false) /* pcrel_offset */
354 #define SH_COFF_HOWTO_COUNT (sizeof sh_coff_howtos / sizeof sh_coff_howtos[0])
356 /* Check for a bad magic number. */
357 #define BADMAG(x) SHBADMAG(x)
359 /* Customize coffcode.h (this is not currently used). */
362 /* FIXME: This should not be set here. */
363 #define __A_MAGIC_SET__
366 /* Swap the r_offset field in and out. */
367 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
368 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
370 /* Swap out extra information in the reloc structure. */
371 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
374 dst->r_stuff[0] = 'S'; \
375 dst->r_stuff[1] = 'C'; \
380 /* Get the value of a symbol, when performing a relocation. */
383 get_symbol_value (symbol)
388 if (bfd_is_com_section (symbol->section))
391 relocation = (symbol->value +
392 symbol->section->output_section->vma +
393 symbol->section->output_offset);
399 /* Convert an rtype to howto for the COFF backend linker.
400 Copied from coff-i386. */
401 #define coff_rtype_to_howto coff_sh_rtype_to_howto
403 static reloc_howto_type *
404 coff_sh_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
407 struct internal_reloc * rel;
408 struct coff_link_hash_entry * h;
409 struct internal_syment * sym;
412 reloc_howto_type * howto;
414 howto = sh_coff_howtos + rel->r_type;
418 if (howto->pc_relative)
419 *addendp += sec->vma;
421 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
423 /* This is a common symbol. The section contents include the
424 size (sym->n_value) as an addend. The relocate_section
425 function will be adding in the final value of the symbol. We
426 need to subtract out the current size in order to get the
428 BFD_ASSERT (h != NULL);
431 if (howto->pc_relative)
435 /* If the symbol is defined, then the generic code is going to
436 add back the symbol value in order to cancel out an
437 adjustment it made to the addend. However, we set the addend
438 to 0 at the start of this function. We need to adjust here,
439 to avoid the adjustment the generic code will make. FIXME:
440 This is getting a bit hackish. */
441 if (sym != NULL && sym->n_scnum != 0)
442 *addendp -= sym->n_value;
445 if (rel->r_type == R_SH_IMAGEBASE)
446 *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
451 /* This structure is used to map BFD reloc codes to SH PE relocs. */
452 struct shcoff_reloc_map
454 unsigned char bfd_reloc_val;
455 unsigned char shcoff_reloc_val;
458 /* An array mapping BFD reloc codes to SH PE relocs. */
459 static const struct shcoff_reloc_map sh_reloc_map[] =
461 { BFD_RELOC_32, R_SH_IMM32CE },
462 { BFD_RELOC_RVA, R_SH_IMAGEBASE },
463 { BFD_RELOC_CTOR, R_SH_IMM32CE },
466 /* Given a BFD reloc code, return the howto structure for the
467 corresponding SH PE reloc. */
468 #define coff_bfd_reloc_type_lookup sh_coff_reloc_type_lookup
470 static reloc_howto_type *
471 sh_coff_reloc_type_lookup (abfd, code)
472 bfd * abfd ATTRIBUTE_UNUSED;
473 bfd_reloc_code_real_type code;
477 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct shcoff_reloc_map); i++)
479 if (sh_reloc_map[i].bfd_reloc_val == code)
480 return &sh_coff_howtos[(int) sh_reloc_map[i].shcoff_reloc_val];
483 fprintf (stderr, "SH Error: unknown reloc type %d\n", code);
486 #endif /* COFF_WITH_PE */
488 /* This macro is used in coffcode.h to get the howto corresponding to
489 an internal reloc. */
491 #define RTYPE2HOWTO(relent, internal) \
493 ((internal)->r_type < SH_COFF_HOWTO_COUNT \
494 ? &sh_coff_howtos[(internal)->r_type] \
495 : (reloc_howto_type *) NULL))
497 /* This is the same as the macro in coffcode.h, except that it copies
498 r_offset into reloc_entry->addend for some relocs. */
499 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
501 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
502 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
503 coffsym = (obj_symbols (abfd) \
504 + (cache_ptr->sym_ptr_ptr - symbols)); \
506 coffsym = coff_symbol_from (abfd, ptr); \
507 if (coffsym != (coff_symbol_type *) NULL \
508 && coffsym->native->u.syment.n_scnum == 0) \
509 cache_ptr->addend = 0; \
510 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
511 && ptr->section != (asection *) NULL) \
512 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
514 cache_ptr->addend = 0; \
515 if ((reloc).r_type == R_SH_SWITCH8 \
516 || (reloc).r_type == R_SH_SWITCH16 \
517 || (reloc).r_type == R_SH_SWITCH32 \
518 || (reloc).r_type == R_SH_USES \
519 || (reloc).r_type == R_SH_COUNT \
520 || (reloc).r_type == R_SH_ALIGN) \
521 cache_ptr->addend = (reloc).r_offset; \
524 /* This is the howto function for the SH relocations. */
526 static bfd_reloc_status_type
527 sh_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
530 arelent *reloc_entry;
533 asection *input_section;
535 char **error_message ATTRIBUTE_UNUSED;
539 unsigned short r_type;
540 bfd_vma addr = reloc_entry->address;
541 bfd_byte *hit_data = addr + (bfd_byte *) data;
543 r_type = reloc_entry->howto->type;
545 if (output_bfd != NULL)
547 /* Partial linking--do nothing. */
548 reloc_entry->address += input_section->output_offset;
552 /* Almost all relocs have to do with relaxing. If any work must be
553 done for them, it has been done in sh_relax_section. */
554 if (r_type != R_SH_IMM32
556 && r_type != R_SH_IMM32CE
557 && r_type != R_SH_IMAGEBASE
559 && (r_type != R_SH_PCDISP
560 || (symbol_in->flags & BSF_LOCAL) != 0))
563 if (symbol_in != NULL
564 && bfd_is_und_section (symbol_in->section))
565 return bfd_reloc_undefined;
567 sym_value = get_symbol_value (symbol_in);
575 insn = bfd_get_32 (abfd, hit_data);
576 insn += sym_value + reloc_entry->addend;
577 bfd_put_32 (abfd, insn, hit_data);
581 insn = bfd_get_32 (abfd, hit_data);
582 insn += (sym_value + reloc_entry->addend
583 - pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase);
584 bfd_put_32 (abfd, insn, hit_data);
588 insn = bfd_get_16 (abfd, hit_data);
589 sym_value += reloc_entry->addend;
590 sym_value -= (input_section->output_section->vma
591 + input_section->output_offset
594 sym_value += (insn & 0xfff) << 1;
597 insn = (insn & 0xf000) | (sym_value & 0xfff);
598 bfd_put_16 (abfd, insn, hit_data);
599 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
600 return bfd_reloc_overflow;
610 #define coff_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
612 /* We can do relaxing. */
613 #define coff_bfd_relax_section sh_relax_section
615 /* We use the special COFF backend linker. */
616 #define coff_relocate_section sh_relocate_section
618 /* When relaxing, we need to use special code to get the relocated
620 #define coff_bfd_get_relocated_section_contents \
621 sh_coff_get_relocated_section_contents
623 #include "coffcode.h"
625 /* This function handles relaxing on the SH.
627 Function calls on the SH look like this:
636 The compiler and assembler will cooperate to create R_SH_USES
637 relocs on the jsr instructions. The r_offset field of the
638 R_SH_USES reloc is the PC relative offset to the instruction which
639 loads the register (the r_offset field is computed as though it
640 were a jump instruction, so the offset value is actually from four
641 bytes past the instruction). The linker can use this reloc to
642 determine just which function is being called, and thus decide
643 whether it is possible to replace the jsr with a bsr.
645 If multiple function calls are all based on a single register load
646 (i.e., the same function is called multiple times), the compiler
647 guarantees that each function call will have an R_SH_USES reloc.
648 Therefore, if the linker is able to convert each R_SH_USES reloc
649 which refers to that address, it can safely eliminate the register
652 When the assembler creates an R_SH_USES reloc, it examines it to
653 determine which address is being loaded (L1 in the above example).
654 It then counts the number of references to that address, and
655 creates an R_SH_COUNT reloc at that address. The r_offset field of
656 the R_SH_COUNT reloc will be the number of references. If the
657 linker is able to eliminate a register load, it can use the
658 R_SH_COUNT reloc to see whether it can also eliminate the function
661 SH relaxing also handles another, unrelated, matter. On the SH, if
662 a load or store instruction is not aligned on a four byte boundary,
663 the memory cycle interferes with the 32 bit instruction fetch,
664 causing a one cycle bubble in the pipeline. Therefore, we try to
665 align load and store instructions on four byte boundaries if we
666 can, by swapping them with one of the adjacent instructions. */
669 sh_relax_section (abfd, sec, link_info, again)
672 struct bfd_link_info *link_info;
675 struct internal_reloc *internal_relocs;
676 struct internal_reloc *free_relocs = NULL;
678 struct internal_reloc *irel, *irelend;
679 bfd_byte *contents = NULL;
680 bfd_byte *free_contents = NULL;
684 if (link_info->relocateable
685 || (sec->flags & SEC_RELOC) == 0
686 || sec->reloc_count == 0)
689 /* If this is the first time we have been called for this section,
690 initialize the cooked size. */
691 if (sec->_cooked_size == 0)
692 sec->_cooked_size = sec->_raw_size;
694 internal_relocs = (_bfd_coff_read_internal_relocs
695 (abfd, sec, link_info->keep_memory,
696 (bfd_byte *) NULL, false,
697 (struct internal_reloc *) NULL));
698 if (internal_relocs == NULL)
700 if (! link_info->keep_memory)
701 free_relocs = internal_relocs;
705 irelend = internal_relocs + sec->reloc_count;
706 for (irel = internal_relocs; irel < irelend; irel++)
708 bfd_vma laddr, paddr, symval;
710 struct internal_reloc *irelfn, *irelscan, *irelcount;
711 struct internal_syment sym;
714 if (irel->r_type == R_SH_CODE)
717 if (irel->r_type != R_SH_USES)
720 /* Get the section contents. */
721 if (contents == NULL)
723 if (coff_section_data (abfd, sec) != NULL
724 && coff_section_data (abfd, sec)->contents != NULL)
725 contents = coff_section_data (abfd, sec)->contents;
728 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
729 if (contents == NULL)
731 free_contents = contents;
733 if (! bfd_get_section_contents (abfd, sec, contents,
734 (file_ptr) 0, sec->_raw_size))
739 /* The r_offset field of the R_SH_USES reloc will point us to
740 the register load. The 4 is because the r_offset field is
741 computed as though it were a jump offset, which are based
742 from 4 bytes after the jump instruction. */
743 laddr = irel->r_vaddr - sec->vma + 4;
744 /* Careful to sign extend the 32-bit offset. */
745 laddr += ((irel->r_offset & 0xffffffff) ^ 0x80000000) - 0x80000000;
746 if (laddr >= sec->_raw_size)
748 (*_bfd_error_handler) ("%s: 0x%lx: warning: bad R_SH_USES offset",
749 bfd_get_filename (abfd),
750 (unsigned long) irel->r_vaddr);
753 insn = bfd_get_16 (abfd, contents + laddr);
755 /* If the instruction is not mov.l NN,rN, we don't know what to do. */
756 if ((insn & 0xf000) != 0xd000)
758 ((*_bfd_error_handler)
759 ("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x",
760 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr, insn));
764 /* Get the address from which the register is being loaded. The
765 displacement in the mov.l instruction is quadrupled. It is a
766 displacement from four bytes after the movl instruction, but,
767 before adding in the PC address, two least significant bits
768 of the PC are cleared. We assume that the section is aligned
769 on a four byte boundary. */
772 paddr += (laddr + 4) &~ 3;
773 if (paddr >= sec->_raw_size)
775 ((*_bfd_error_handler)
776 ("%s: 0x%lx: warning: bad R_SH_USES load offset",
777 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
781 /* Get the reloc for the address from which the register is
782 being loaded. This reloc will tell us which function is
783 actually being called. */
785 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
786 if (irelfn->r_vaddr == paddr
788 && (irelfn->r_type == R_SH_IMM32
789 || irelfn->r_type == R_SH_IMM32CE
790 || irelfn->r_type == R_SH_IMAGEBASE))
793 && irelfn->r_type == R_SH_IMM32)
796 if (irelfn >= irelend)
798 ((*_bfd_error_handler)
799 ("%s: 0x%lx: warning: could not find expected reloc",
800 bfd_get_filename (abfd), (unsigned long) paddr));
804 /* Get the value of the symbol referred to by the reloc. */
805 if (! _bfd_coff_get_external_symbols (abfd))
807 bfd_coff_swap_sym_in (abfd,
808 ((bfd_byte *) obj_coff_external_syms (abfd)
810 * bfd_coff_symesz (abfd))),
812 if (sym.n_scnum != 0 && sym.n_scnum != sec->target_index)
814 ((*_bfd_error_handler)
815 ("%s: 0x%lx: warning: symbol in unexpected section",
816 bfd_get_filename (abfd), (unsigned long) paddr));
820 if (sym.n_sclass != C_EXT)
822 symval = (sym.n_value
824 + sec->output_section->vma
825 + sec->output_offset);
829 struct coff_link_hash_entry *h;
831 h = obj_coff_sym_hashes (abfd)[irelfn->r_symndx];
832 BFD_ASSERT (h != NULL);
833 if (h->root.type != bfd_link_hash_defined
834 && h->root.type != bfd_link_hash_defweak)
836 /* This appears to be a reference to an undefined
837 symbol. Just ignore it--it will be caught by the
838 regular reloc processing. */
842 symval = (h->root.u.def.value
843 + h->root.u.def.section->output_section->vma
844 + h->root.u.def.section->output_offset);
847 symval += bfd_get_32 (abfd, contents + paddr - sec->vma);
849 /* See if this function call can be shortened. */
853 + sec->output_section->vma
856 if (foff < -0x1000 || foff >= 0x1000)
858 /* After all that work, we can't shorten this function call. */
862 /* Shorten the function call. */
864 /* For simplicity of coding, we are going to modify the section
865 contents, the section relocs, and the BFD symbol table. We
866 must tell the rest of the code not to free up this
867 information. It would be possible to instead create a table
868 of changes which have to be made, as is done in coff-mips.c;
869 that would be more work, but would require less memory when
870 the linker is run. */
872 if (coff_section_data (abfd, sec) == NULL)
875 ((PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
876 if (sec->used_by_bfd == NULL)
880 coff_section_data (abfd, sec)->relocs = internal_relocs;
881 coff_section_data (abfd, sec)->keep_relocs = true;
884 coff_section_data (abfd, sec)->contents = contents;
885 coff_section_data (abfd, sec)->keep_contents = true;
886 free_contents = NULL;
888 obj_coff_keep_syms (abfd) = true;
890 /* Replace the jsr with a bsr. */
892 /* Change the R_SH_USES reloc into an R_SH_PCDISP reloc, and
893 replace the jsr with a bsr. */
894 irel->r_type = R_SH_PCDISP;
895 irel->r_symndx = irelfn->r_symndx;
896 if (sym.n_sclass != C_EXT)
898 /* If this needs to be changed because of future relaxing,
899 it will be handled here like other internal PCDISP
902 0xb000 | ((foff >> 1) & 0xfff),
903 contents + irel->r_vaddr - sec->vma);
907 /* We can't fully resolve this yet, because the external
908 symbol value may be changed by future relaxing. We let
909 the final link phase handle it. */
910 bfd_put_16 (abfd, 0xb000, contents + irel->r_vaddr - sec->vma);
913 /* See if there is another R_SH_USES reloc referring to the same
915 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
916 if (irelscan->r_type == R_SH_USES
917 && laddr == irelscan->r_vaddr - sec->vma + 4 + irelscan->r_offset)
919 if (irelscan < irelend)
921 /* Some other function call depends upon this register load,
922 and we have not yet converted that function call.
923 Indeed, we may never be able to convert it. There is
924 nothing else we can do at this point. */
928 /* Look for a R_SH_COUNT reloc on the location where the
929 function address is stored. Do this before deleting any
930 bytes, to avoid confusion about the address. */
931 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
932 if (irelcount->r_vaddr == paddr
933 && irelcount->r_type == R_SH_COUNT)
936 /* Delete the register load. */
937 if (! sh_relax_delete_bytes (abfd, sec, laddr, 2))
940 /* That will change things, so, just in case it permits some
941 other function call to come within range, we should relax
942 again. Note that this is not required, and it may be slow. */
945 /* Now check whether we got a COUNT reloc. */
946 if (irelcount >= irelend)
948 ((*_bfd_error_handler)
949 ("%s: 0x%lx: warning: could not find expected COUNT reloc",
950 bfd_get_filename (abfd), (unsigned long) paddr));
954 /* The number of uses is stored in the r_offset field. We've
956 if (irelcount->r_offset == 0)
958 ((*_bfd_error_handler) ("%s: 0x%lx: warning: bad count",
959 bfd_get_filename (abfd),
960 (unsigned long) paddr));
964 --irelcount->r_offset;
966 /* If there are no more uses, we can delete the address. Reload
967 the address from irelfn, in case it was changed by the
968 previous call to sh_relax_delete_bytes. */
969 if (irelcount->r_offset == 0)
971 if (! sh_relax_delete_bytes (abfd, sec,
972 irelfn->r_vaddr - sec->vma, 4))
976 /* We've done all we can with that function call. */
979 /* Look for load and store instructions that we can align on four
985 /* Get the section contents. */
986 if (contents == NULL)
988 if (coff_section_data (abfd, sec) != NULL
989 && coff_section_data (abfd, sec)->contents != NULL)
990 contents = coff_section_data (abfd, sec)->contents;
993 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
994 if (contents == NULL)
996 free_contents = contents;
998 if (! bfd_get_section_contents (abfd, sec, contents,
999 (file_ptr) 0, sec->_raw_size))
1004 if (! sh_align_loads (abfd, sec, internal_relocs, contents, &swapped))
1009 if (coff_section_data (abfd, sec) == NULL)
1012 ((PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1013 if (sec->used_by_bfd == NULL)
1017 coff_section_data (abfd, sec)->relocs = internal_relocs;
1018 coff_section_data (abfd, sec)->keep_relocs = true;
1021 coff_section_data (abfd, sec)->contents = contents;
1022 coff_section_data (abfd, sec)->keep_contents = true;
1023 free_contents = NULL;
1025 obj_coff_keep_syms (abfd) = true;
1029 if (free_relocs != NULL)
1035 if (free_contents != NULL)
1037 if (! link_info->keep_memory)
1038 free (free_contents);
1041 /* Cache the section contents for coff_link_input_bfd. */
1042 if (coff_section_data (abfd, sec) == NULL)
1045 ((PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1046 if (sec->used_by_bfd == NULL)
1048 coff_section_data (abfd, sec)->relocs = NULL;
1050 coff_section_data (abfd, sec)->contents = contents;
1057 if (free_relocs != NULL)
1059 if (free_contents != NULL)
1060 free (free_contents);
1064 /* Delete some bytes from a section while relaxing. */
1067 sh_relax_delete_bytes (abfd, sec, addr, count)
1074 struct internal_reloc *irel, *irelend;
1075 struct internal_reloc *irelalign;
1077 bfd_byte *esym, *esymend;
1078 bfd_size_type symesz;
1079 struct coff_link_hash_entry **sym_hash;
1082 contents = coff_section_data (abfd, sec)->contents;
1084 /* The deletion must stop at the next ALIGN reloc for an aligment
1085 power larger than the number of bytes we are deleting. */
1088 toaddr = sec->_cooked_size;
1090 irel = coff_section_data (abfd, sec)->relocs;
1091 irelend = irel + sec->reloc_count;
1092 for (; irel < irelend; irel++)
1094 if (irel->r_type == R_SH_ALIGN
1095 && irel->r_vaddr - sec->vma > addr
1096 && count < (1 << irel->r_offset))
1099 toaddr = irel->r_vaddr - sec->vma;
1104 /* Actually delete the bytes. */
1105 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1106 if (irelalign == NULL)
1107 sec->_cooked_size -= count;
1112 #define NOP_OPCODE (0x0009)
1114 BFD_ASSERT ((count & 1) == 0);
1115 for (i = 0; i < count; i += 2)
1116 bfd_put_16 (abfd, NOP_OPCODE, contents + toaddr - count + i);
1119 /* Adjust all the relocs. */
1120 for (irel = coff_section_data (abfd, sec)->relocs; irel < irelend; irel++)
1122 bfd_vma nraddr, stop;
1125 struct internal_syment sym;
1126 int off, adjust, oinsn;
1127 bfd_signed_vma voff = 0;
1130 /* Get the new reloc address. */
1131 nraddr = irel->r_vaddr - sec->vma;
1132 if ((irel->r_vaddr - sec->vma > addr
1133 && irel->r_vaddr - sec->vma < toaddr)
1134 || (irel->r_type == R_SH_ALIGN
1135 && irel->r_vaddr - sec->vma == toaddr))
1138 /* See if this reloc was for the bytes we have deleted, in which
1139 case we no longer care about it. Don't delete relocs which
1140 represent addresses, though. */
1141 if (irel->r_vaddr - sec->vma >= addr
1142 && irel->r_vaddr - sec->vma < addr + count
1143 && irel->r_type != R_SH_ALIGN
1144 && irel->r_type != R_SH_CODE
1145 && irel->r_type != R_SH_DATA
1146 && irel->r_type != R_SH_LABEL)
1147 irel->r_type = R_SH_UNUSED;
1149 /* If this is a PC relative reloc, see if the range it covers
1150 includes the bytes we have deleted. */
1151 switch (irel->r_type)
1156 case R_SH_PCDISP8BY2:
1158 case R_SH_PCRELIMM8BY2:
1159 case R_SH_PCRELIMM8BY4:
1160 start = irel->r_vaddr - sec->vma;
1161 insn = bfd_get_16 (abfd, contents + nraddr);
1165 switch (irel->r_type)
1168 start = stop = addr;
1174 case R_SH_IMAGEBASE:
1176 /* If this reloc is against a symbol defined in this
1177 section, and the symbol will not be adjusted below, we
1178 must check the addend to see it will put the value in
1179 range to be adjusted, and hence must be changed. */
1180 bfd_coff_swap_sym_in (abfd,
1181 ((bfd_byte *) obj_coff_external_syms (abfd)
1183 * bfd_coff_symesz (abfd))),
1185 if (sym.n_sclass != C_EXT
1186 && sym.n_scnum == sec->target_index
1187 && ((bfd_vma) sym.n_value <= addr
1188 || (bfd_vma) sym.n_value >= toaddr))
1192 val = bfd_get_32 (abfd, contents + nraddr);
1194 if (val > addr && val < toaddr)
1195 bfd_put_32 (abfd, val - count, contents + nraddr);
1197 start = stop = addr;
1200 case R_SH_PCDISP8BY2:
1204 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1208 bfd_coff_swap_sym_in (abfd,
1209 ((bfd_byte *) obj_coff_external_syms (abfd)
1211 * bfd_coff_symesz (abfd))),
1213 if (sym.n_sclass == C_EXT)
1214 start = stop = addr;
1220 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1224 case R_SH_PCRELIMM8BY2:
1226 stop = start + 4 + off * 2;
1229 case R_SH_PCRELIMM8BY4:
1231 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1237 /* These relocs types represent
1239 The r_offset field holds the difference between the reloc
1240 address and L1. That is the start of the reloc, and
1241 adding in the contents gives us the top. We must adjust
1242 both the r_offset field and the section contents. */
1244 start = irel->r_vaddr - sec->vma;
1245 stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_offset);
1249 && (stop <= addr || stop >= toaddr))
1250 irel->r_offset += count;
1251 else if (stop > addr
1253 && (start <= addr || start >= toaddr))
1254 irel->r_offset -= count;
1258 if (irel->r_type == R_SH_SWITCH16)
1259 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1260 else if (irel->r_type == R_SH_SWITCH8)
1261 voff = bfd_get_8 (abfd, contents + nraddr);
1263 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1264 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1269 start = irel->r_vaddr - sec->vma;
1270 stop = (bfd_vma) ((bfd_signed_vma) start
1271 + (long) irel->r_offset
1278 && (stop <= addr || stop >= toaddr))
1280 else if (stop > addr
1282 && (start <= addr || start >= toaddr))
1291 switch (irel->r_type)
1297 case R_SH_PCDISP8BY2:
1298 case R_SH_PCRELIMM8BY2:
1300 if ((oinsn & 0xff00) != (insn & 0xff00))
1302 bfd_put_16 (abfd, insn, contents + nraddr);
1307 if ((oinsn & 0xf000) != (insn & 0xf000))
1309 bfd_put_16 (abfd, insn, contents + nraddr);
1312 case R_SH_PCRELIMM8BY4:
1313 BFD_ASSERT (adjust == count || count >= 4);
1318 if ((irel->r_vaddr & 3) == 0)
1321 if ((oinsn & 0xff00) != (insn & 0xff00))
1323 bfd_put_16 (abfd, insn, contents + nraddr);
1328 if (voff < 0 || voff >= 0xff)
1330 bfd_put_8 (abfd, voff, contents + nraddr);
1335 if (voff < - 0x8000 || voff >= 0x8000)
1337 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1342 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1346 irel->r_offset += adjust;
1352 ((*_bfd_error_handler)
1353 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1354 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
1355 bfd_set_error (bfd_error_bad_value);
1360 irel->r_vaddr = nraddr + sec->vma;
1363 /* Look through all the other sections. If there contain any IMM32
1364 relocs against internal symbols which we are not going to adjust
1365 below, we may need to adjust the addends. */
1366 for (o = abfd->sections; o != NULL; o = o->next)
1368 struct internal_reloc *internal_relocs;
1369 struct internal_reloc *irelscan, *irelscanend;
1370 bfd_byte *ocontents;
1373 || (o->flags & SEC_RELOC) == 0
1374 || o->reloc_count == 0)
1377 /* We always cache the relocs. Perhaps, if info->keep_memory is
1378 false, we should free them, if we are permitted to, when we
1379 leave sh_coff_relax_section. */
1380 internal_relocs = (_bfd_coff_read_internal_relocs
1381 (abfd, o, true, (bfd_byte *) NULL, false,
1382 (struct internal_reloc *) NULL));
1383 if (internal_relocs == NULL)
1387 irelscanend = internal_relocs + o->reloc_count;
1388 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1390 struct internal_syment sym;
1393 if (irelscan->r_type != R_SH_IMM32
1394 && irelscan->r_type != R_SH_IMAGEBASE
1395 && irelscan->r_type != R_SH_IMM32CE)
1397 if (irelscan->r_type != R_SH_IMM32)
1401 bfd_coff_swap_sym_in (abfd,
1402 ((bfd_byte *) obj_coff_external_syms (abfd)
1403 + (irelscan->r_symndx
1404 * bfd_coff_symesz (abfd))),
1406 if (sym.n_sclass != C_EXT
1407 && sym.n_scnum == sec->target_index
1408 && ((bfd_vma) sym.n_value <= addr
1409 || (bfd_vma) sym.n_value >= toaddr))
1413 if (ocontents == NULL)
1415 if (coff_section_data (abfd, o)->contents != NULL)
1416 ocontents = coff_section_data (abfd, o)->contents;
1419 /* We always cache the section contents.
1420 Perhaps, if info->keep_memory is false, we
1421 should free them, if we are permitted to,
1422 when we leave sh_coff_relax_section. */
1423 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1424 if (ocontents == NULL)
1426 if (! bfd_get_section_contents (abfd, o, ocontents,
1430 coff_section_data (abfd, o)->contents = ocontents;
1434 val = bfd_get_32 (abfd, ocontents + irelscan->r_vaddr - o->vma);
1436 if (val > addr && val < toaddr)
1437 bfd_put_32 (abfd, val - count,
1438 ocontents + irelscan->r_vaddr - o->vma);
1440 coff_section_data (abfd, o)->keep_contents = true;
1445 /* Adjusting the internal symbols will not work if something has
1446 already retrieved the generic symbols. It would be possible to
1447 make this work by adjusting the generic symbols at the same time.
1448 However, this case should not arise in normal usage. */
1449 if (obj_symbols (abfd) != NULL
1450 || obj_raw_syments (abfd) != NULL)
1452 ((*_bfd_error_handler)
1453 ("%s: fatal: generic symbols retrieved before relaxing",
1454 bfd_get_filename (abfd)));
1455 bfd_set_error (bfd_error_invalid_operation);
1459 /* Adjust all the symbols. */
1460 sym_hash = obj_coff_sym_hashes (abfd);
1461 symesz = bfd_coff_symesz (abfd);
1462 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1463 esymend = esym + obj_raw_syment_count (abfd) * symesz;
1464 while (esym < esymend)
1466 struct internal_syment isym;
1468 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &isym);
1470 if (isym.n_scnum == sec->target_index
1471 && (bfd_vma) isym.n_value > addr
1472 && (bfd_vma) isym.n_value < toaddr)
1474 isym.n_value -= count;
1476 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
1478 if (*sym_hash != NULL)
1480 BFD_ASSERT ((*sym_hash)->root.type == bfd_link_hash_defined
1481 || (*sym_hash)->root.type == bfd_link_hash_defweak);
1482 BFD_ASSERT ((*sym_hash)->root.u.def.value >= addr
1483 && (*sym_hash)->root.u.def.value < toaddr);
1484 (*sym_hash)->root.u.def.value -= count;
1488 esym += (isym.n_numaux + 1) * symesz;
1489 sym_hash += isym.n_numaux + 1;
1492 /* See if we can move the ALIGN reloc forward. We have adjusted
1493 r_vaddr for it already. */
1494 if (irelalign != NULL)
1496 bfd_vma alignto, alignaddr;
1498 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_offset);
1499 alignaddr = BFD_ALIGN (irelalign->r_vaddr - sec->vma,
1500 1 << irelalign->r_offset);
1501 if (alignto != alignaddr)
1503 /* Tail recursion. */
1504 return sh_relax_delete_bytes (abfd, sec, alignaddr,
1505 alignto - alignaddr);
1512 /* This is yet another version of the SH opcode table, used to rapidly
1513 get information about a particular instruction. */
1515 /* The opcode map is represented by an array of these structures. The
1516 array is indexed by the high order four bits in the instruction. */
1518 struct sh_major_opcode
1520 /* A pointer to the instruction list. This is an array which
1521 contains all the instructions with this major opcode. */
1522 const struct sh_minor_opcode *minor_opcodes;
1523 /* The number of elements in minor_opcodes. */
1524 unsigned short count;
1527 /* This structure holds information for a set of SH opcodes. The
1528 instruction code is anded with the mask value, and the resulting
1529 value is used to search the order opcode list. */
1531 struct sh_minor_opcode
1533 /* The sorted opcode list. */
1534 const struct sh_opcode *opcodes;
1535 /* The number of elements in opcodes. */
1536 unsigned short count;
1537 /* The mask value to use when searching the opcode list. */
1538 unsigned short mask;
1541 /* This structure holds information for an SH instruction. An array
1542 of these structures is sorted in order by opcode. */
1546 /* The code for this instruction, after it has been anded with the
1547 mask value in the sh_major_opcode structure. */
1548 unsigned short opcode;
1549 /* Flags for this instruction. */
1550 unsigned short flags;
1553 /* Flag which appear in the sh_opcode structure. */
1555 /* This instruction loads a value from memory. */
1558 /* This instruction stores a value to memory. */
1561 /* This instruction is a branch. */
1562 #define BRANCH (0x4)
1564 /* This instruction has a delay slot. */
1567 /* This instruction uses the value in the register in the field at
1568 mask 0x0f00 of the instruction. */
1569 #define USES1 (0x10)
1570 #define USES1_REG(x) ((x & 0x0f00) >> 8)
1572 /* This instruction uses the value in the register in the field at
1573 mask 0x00f0 of the instruction. */
1574 #define USES2 (0x20)
1575 #define USES2_REG(x) ((x & 0x00f0) >> 4)
1577 /* This instruction uses the value in register 0. */
1578 #define USESR0 (0x40)
1580 /* This instruction sets the value in the register in the field at
1581 mask 0x0f00 of the instruction. */
1582 #define SETS1 (0x80)
1583 #define SETS1_REG(x) ((x & 0x0f00) >> 8)
1585 /* This instruction sets the value in the register in the field at
1586 mask 0x00f0 of the instruction. */
1587 #define SETS2 (0x100)
1588 #define SETS2_REG(x) ((x & 0x00f0) >> 4)
1590 /* This instruction sets register 0. */
1591 #define SETSR0 (0x200)
1593 /* This instruction sets a special register. */
1594 #define SETSSP (0x400)
1596 /* This instruction uses a special register. */
1597 #define USESSP (0x800)
1599 /* This instruction uses the floating point register in the field at
1600 mask 0x0f00 of the instruction. */
1601 #define USESF1 (0x1000)
1602 #define USESF1_REG(x) ((x & 0x0f00) >> 8)
1604 /* This instruction uses the floating point register in the field at
1605 mask 0x00f0 of the instruction. */
1606 #define USESF2 (0x2000)
1607 #define USESF2_REG(x) ((x & 0x00f0) >> 4)
1609 /* This instruction uses floating point register 0. */
1610 #define USESF0 (0x4000)
1612 /* This instruction sets the floating point register in the field at
1613 mask 0x0f00 of the instruction. */
1614 #define SETSF1 (0x8000)
1615 #define SETSF1_REG(x) ((x & 0x0f00) >> 8)
1617 #define USESAS (0x10000)
1618 #define USESAS_REG(x) (((((x) >> 8) - 2) & 3) + 2)
1619 #define USESR8 (0x20000)
1620 #define SETSAS (0x40000)
1621 #define SETSAS_REG(x) USESAS_REG (x)
1623 static boolean sh_insn_uses_reg
1624 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1625 static boolean sh_insn_sets_reg
1626 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1627 static boolean sh_insn_uses_or_sets_reg
1628 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1629 static boolean sh_insn_uses_freg
1630 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1631 static boolean sh_insn_sets_freg
1632 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1633 static boolean sh_insn_uses_or_sets_freg
1634 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1635 static boolean sh_insns_conflict
1636 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int,
1637 const struct sh_opcode *));
1638 static boolean sh_load_use
1639 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int,
1640 const struct sh_opcode *));
1642 /* The opcode maps. */
1644 #define MAP(a) a, sizeof a / sizeof a[0]
1646 static const struct sh_opcode sh_opcode00[] =
1648 { 0x0008, SETSSP }, /* clrt */
1649 { 0x0009, 0 }, /* nop */
1650 { 0x000b, BRANCH | DELAY | USESSP }, /* rts */
1651 { 0x0018, SETSSP }, /* sett */
1652 { 0x0019, SETSSP }, /* div0u */
1653 { 0x001b, 0 }, /* sleep */
1654 { 0x0028, SETSSP }, /* clrmac */
1655 { 0x002b, BRANCH | DELAY | SETSSP }, /* rte */
1656 { 0x0038, USESSP | SETSSP }, /* ldtlb */
1657 { 0x0048, SETSSP }, /* clrs */
1658 { 0x0058, SETSSP } /* sets */
1661 static const struct sh_opcode sh_opcode01[] =
1663 { 0x0003, BRANCH | DELAY | USES1 | SETSSP }, /* bsrf rn */
1664 { 0x000a, SETS1 | USESSP }, /* sts mach,rn */
1665 { 0x001a, SETS1 | USESSP }, /* sts macl,rn */
1666 { 0x0023, BRANCH | DELAY | USES1 }, /* braf rn */
1667 { 0x0029, SETS1 | USESSP }, /* movt rn */
1668 { 0x002a, SETS1 | USESSP }, /* sts pr,rn */
1669 { 0x005a, SETS1 | USESSP }, /* sts fpul,rn */
1670 { 0x006a, SETS1 | USESSP }, /* sts fpscr,rn / sts dsr,rn */
1671 { 0x0083, LOAD | USES1 }, /* pref @rn */
1672 { 0x007a, SETS1 | USESSP }, /* sts a0,rn */
1673 { 0x008a, SETS1 | USESSP }, /* sts x0,rn */
1674 { 0x009a, SETS1 | USESSP }, /* sts x1,rn */
1675 { 0x00aa, SETS1 | USESSP }, /* sts y0,rn */
1676 { 0x00ba, SETS1 | USESSP } /* sts y1,rn */
1679 /* These sixteen instructions can be handled with one table entry below. */
1681 { 0x0002, SETS1 | USESSP }, /* stc sr,rn */
1682 { 0x0012, SETS1 | USESSP }, /* stc gbr,rn */
1683 { 0x0022, SETS1 | USESSP }, /* stc vbr,rn */
1684 { 0x0032, SETS1 | USESSP }, /* stc ssr,rn */
1685 { 0x0042, SETS1 | USESSP }, /* stc spc,rn */
1686 { 0x0052, SETS1 | USESSP }, /* stc mod,rn */
1687 { 0x0062, SETS1 | USESSP }, /* stc rs,rn */
1688 { 0x0072, SETS1 | USESSP }, /* stc re,rn */
1689 { 0x0082, SETS1 | USESSP }, /* stc r0_bank,rn */
1690 { 0x0092, SETS1 | USESSP }, /* stc r1_bank,rn */
1691 { 0x00a2, SETS1 | USESSP }, /* stc r2_bank,rn */
1692 { 0x00b2, SETS1 | USESSP }, /* stc r3_bank,rn */
1693 { 0x00c2, SETS1 | USESSP }, /* stc r4_bank,rn */
1694 { 0x00d2, SETS1 | USESSP }, /* stc r5_bank,rn */
1695 { 0x00e2, SETS1 | USESSP }, /* stc r6_bank,rn */
1696 { 0x00f2, SETS1 | USESSP } /* stc r7_bank,rn */
1699 static const struct sh_opcode sh_opcode02[] =
1701 { 0x0002, SETS1 | USESSP }, /* stc <special_reg>,rn */
1702 { 0x0004, STORE | USES1 | USES2 | USESR0 }, /* mov.b rm,@(r0,rn) */
1703 { 0x0005, STORE | USES1 | USES2 | USESR0 }, /* mov.w rm,@(r0,rn) */
1704 { 0x0006, STORE | USES1 | USES2 | USESR0 }, /* mov.l rm,@(r0,rn) */
1705 { 0x0007, SETSSP | USES1 | USES2 }, /* mul.l rm,rn */
1706 { 0x000c, LOAD | SETS1 | USES2 | USESR0 }, /* mov.b @(r0,rm),rn */
1707 { 0x000d, LOAD | SETS1 | USES2 | USESR0 }, /* mov.w @(r0,rm),rn */
1708 { 0x000e, LOAD | SETS1 | USES2 | USESR0 }, /* mov.l @(r0,rm),rn */
1709 { 0x000f, LOAD|SETS1|SETS2|SETSSP|USES1|USES2|USESSP }, /* mac.l @rm+,@rn+ */
1712 static const struct sh_minor_opcode sh_opcode0[] =
1714 { MAP (sh_opcode00), 0xffff },
1715 { MAP (sh_opcode01), 0xf0ff },
1716 { MAP (sh_opcode02), 0xf00f }
1719 static const struct sh_opcode sh_opcode10[] =
1721 { 0x1000, STORE | USES1 | USES2 } /* mov.l rm,@(disp,rn) */
1724 static const struct sh_minor_opcode sh_opcode1[] =
1726 { MAP (sh_opcode10), 0xf000 }
1729 static const struct sh_opcode sh_opcode20[] =
1731 { 0x2000, STORE | USES1 | USES2 }, /* mov.b rm,@rn */
1732 { 0x2001, STORE | USES1 | USES2 }, /* mov.w rm,@rn */
1733 { 0x2002, STORE | USES1 | USES2 }, /* mov.l rm,@rn */
1734 { 0x2004, STORE | SETS1 | USES1 | USES2 }, /* mov.b rm,@-rn */
1735 { 0x2005, STORE | SETS1 | USES1 | USES2 }, /* mov.w rm,@-rn */
1736 { 0x2006, STORE | SETS1 | USES1 | USES2 }, /* mov.l rm,@-rn */
1737 { 0x2007, SETSSP | USES1 | USES2 | USESSP }, /* div0s */
1738 { 0x2008, SETSSP | USES1 | USES2 }, /* tst rm,rn */
1739 { 0x2009, SETS1 | USES1 | USES2 }, /* and rm,rn */
1740 { 0x200a, SETS1 | USES1 | USES2 }, /* xor rm,rn */
1741 { 0x200b, SETS1 | USES1 | USES2 }, /* or rm,rn */
1742 { 0x200c, SETSSP | USES1 | USES2 }, /* cmp/str rm,rn */
1743 { 0x200d, SETS1 | USES1 | USES2 }, /* xtrct rm,rn */
1744 { 0x200e, SETSSP | USES1 | USES2 }, /* mulu.w rm,rn */
1745 { 0x200f, SETSSP | USES1 | USES2 } /* muls.w rm,rn */
1748 static const struct sh_minor_opcode sh_opcode2[] =
1750 { MAP (sh_opcode20), 0xf00f }
1753 static const struct sh_opcode sh_opcode30[] =
1755 { 0x3000, SETSSP | USES1 | USES2 }, /* cmp/eq rm,rn */
1756 { 0x3002, SETSSP | USES1 | USES2 }, /* cmp/hs rm,rn */
1757 { 0x3003, SETSSP | USES1 | USES2 }, /* cmp/ge rm,rn */
1758 { 0x3004, SETSSP | USESSP | USES1 | USES2 }, /* div1 rm,rn */
1759 { 0x3005, SETSSP | USES1 | USES2 }, /* dmulu.l rm,rn */
1760 { 0x3006, SETSSP | USES1 | USES2 }, /* cmp/hi rm,rn */
1761 { 0x3007, SETSSP | USES1 | USES2 }, /* cmp/gt rm,rn */
1762 { 0x3008, SETS1 | USES1 | USES2 }, /* sub rm,rn */
1763 { 0x300a, SETS1 | SETSSP | USES1 | USES2 | USESSP }, /* subc rm,rn */
1764 { 0x300b, SETS1 | SETSSP | USES1 | USES2 }, /* subv rm,rn */
1765 { 0x300c, SETS1 | USES1 | USES2 }, /* add rm,rn */
1766 { 0x300d, SETSSP | USES1 | USES2 }, /* dmuls.l rm,rn */
1767 { 0x300e, SETS1 | SETSSP | USES1 | USES2 | USESSP }, /* addc rm,rn */
1768 { 0x300f, SETS1 | SETSSP | USES1 | USES2 } /* addv rm,rn */
1771 static const struct sh_minor_opcode sh_opcode3[] =
1773 { MAP (sh_opcode30), 0xf00f }
1776 static const struct sh_opcode sh_opcode40[] =
1778 { 0x4000, SETS1 | SETSSP | USES1 }, /* shll rn */
1779 { 0x4001, SETS1 | SETSSP | USES1 }, /* shlr rn */
1780 { 0x4002, STORE | SETS1 | USES1 | USESSP }, /* sts.l mach,@-rn */
1781 { 0x4004, SETS1 | SETSSP | USES1 }, /* rotl rn */
1782 { 0x4005, SETS1 | SETSSP | USES1 }, /* rotr rn */
1783 { 0x4006, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,mach */
1784 { 0x4008, SETS1 | USES1 }, /* shll2 rn */
1785 { 0x4009, SETS1 | USES1 }, /* shlr2 rn */
1786 { 0x400a, SETSSP | USES1 }, /* lds rm,mach */
1787 { 0x400b, BRANCH | DELAY | USES1 }, /* jsr @rn */
1788 { 0x4010, SETS1 | SETSSP | USES1 }, /* dt rn */
1789 { 0x4011, SETSSP | USES1 }, /* cmp/pz rn */
1790 { 0x4012, STORE | SETS1 | USES1 | USESSP }, /* sts.l macl,@-rn */
1791 { 0x4014, SETSSP | USES1 }, /* setrc rm */
1792 { 0x4015, SETSSP | USES1 }, /* cmp/pl rn */
1793 { 0x4016, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,macl */
1794 { 0x4018, SETS1 | USES1 }, /* shll8 rn */
1795 { 0x4019, SETS1 | USES1 }, /* shlr8 rn */
1796 { 0x401a, SETSSP | USES1 }, /* lds rm,macl */
1797 { 0x401b, LOAD | SETSSP | USES1 }, /* tas.b @rn */
1798 { 0x4020, SETS1 | SETSSP | USES1 }, /* shal rn */
1799 { 0x4021, SETS1 | SETSSP | USES1 }, /* shar rn */
1800 { 0x4022, STORE | SETS1 | USES1 | USESSP }, /* sts.l pr,@-rn */
1801 { 0x4024, SETS1 | SETSSP | USES1 | USESSP }, /* rotcl rn */
1802 { 0x4025, SETS1 | SETSSP | USES1 | USESSP }, /* rotcr rn */
1803 { 0x4026, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,pr */
1804 { 0x4028, SETS1 | USES1 }, /* shll16 rn */
1805 { 0x4029, SETS1 | USES1 }, /* shlr16 rn */
1806 { 0x402a, SETSSP | USES1 }, /* lds rm,pr */
1807 { 0x402b, BRANCH | DELAY | USES1 }, /* jmp @rn */
1808 { 0x4052, STORE | SETS1 | USES1 | USESSP }, /* sts.l fpul,@-rn */
1809 { 0x4056, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,fpul */
1810 { 0x405a, SETSSP | USES1 }, /* lds.l rm,fpul */
1811 { 0x4062, STORE | SETS1 | USES1 | USESSP }, /* sts.l fpscr / dsr,@-rn */
1812 { 0x4066, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,fpscr / dsr */
1813 { 0x406a, SETSSP | USES1 }, /* lds rm,fpscr / lds rm,dsr */
1814 { 0x4072, STORE | SETS1 | USES1 | USESSP }, /* sts.l a0,@-rn */
1815 { 0x4076, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,a0 */
1816 { 0x407a, SETSSP | USES1 }, /* lds.l rm,a0 */
1817 { 0x4082, STORE | SETS1 | USES1 | USESSP }, /* sts.l x0,@-rn */
1818 { 0x4086, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,x0 */
1819 { 0x408a, SETSSP | USES1 }, /* lds.l rm,x0 */
1820 { 0x4092, STORE | SETS1 | USES1 | USESSP }, /* sts.l x1,@-rn */
1821 { 0x4096, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,x1 */
1822 { 0x409a, SETSSP | USES1 }, /* lds.l rm,x1 */
1823 { 0x40a2, STORE | SETS1 | USES1 | USESSP }, /* sts.l y0,@-rn */
1824 { 0x40a6, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,y0 */
1825 { 0x40aa, SETSSP | USES1 }, /* lds.l rm,y0 */
1826 { 0x40b2, STORE | SETS1 | USES1 | USESSP }, /* sts.l y1,@-rn */
1827 { 0x40b6, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,y1 */
1828 { 0x40ba, SETSSP | USES1 } /* lds.l rm,y1 */
1829 #if 0 /* These groups sixteen insns can be
1830 handled with one table entry each below. */
1831 { 0x4003, STORE | SETS1 | USES1 | USESSP }, /* stc.l sr,@-rn */
1832 { 0x4013, STORE | SETS1 | USES1 | USESSP }, /* stc.l gbr,@-rn */
1833 { 0x4023, STORE | SETS1 | USES1 | USESSP }, /* stc.l vbr,@-rn */
1834 { 0x4033, STORE | SETS1 | USES1 | USESSP }, /* stc.l ssr,@-rn */
1835 { 0x4043, STORE | SETS1 | USES1 | USESSP }, /* stc.l spc,@-rn */
1836 { 0x4053, STORE | SETS1 | USES1 | USESSP }, /* stc.l mod,@-rn */
1837 { 0x4063, STORE | SETS1 | USES1 | USESSP }, /* stc.l rs,@-rn */
1838 { 0x4073, STORE | SETS1 | USES1 | USESSP }, /* stc.l re,@-rn */
1839 { 0x4083, STORE | SETS1 | USES1 | USESSP }, /* stc.l r0_bank,@-rn */
1841 { 0x40f3, STORE | SETS1 | USES1 | USESSP }, /* stc.l r7_bank,@-rn */
1843 { 0x4007, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,sr */
1844 { 0x4017, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,gbr */
1845 { 0x4027, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,vbr */
1846 { 0x4037, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,ssr */
1847 { 0x4047, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,spc */
1848 { 0x4057, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,mod */
1849 { 0x4067, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,rs */
1850 { 0x4077, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,re */
1851 { 0x4087, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,r0_bank */
1853 { 0x40f7, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,r7_bank */
1855 { 0x400e, SETSSP | USES1 }, /* ldc rm,sr */
1856 { 0x401e, SETSSP | USES1 }, /* ldc rm,gbr */
1857 { 0x402e, SETSSP | USES1 }, /* ldc rm,vbr */
1858 { 0x403e, SETSSP | USES1 }, /* ldc rm,ssr */
1859 { 0x404e, SETSSP | USES1 }, /* ldc rm,spc */
1860 { 0x405e, SETSSP | USES1 }, /* ldc rm,mod */
1861 { 0x406e, SETSSP | USES1 }, /* ldc rm,rs */
1862 { 0x407e, SETSSP | USES1 } /* ldc rm,re */
1863 { 0x408e, SETSSP | USES1 } /* ldc rm,r0_bank */
1865 { 0x40fe, SETSSP | USES1 } /* ldc rm,r7_bank */
1869 static const struct sh_opcode sh_opcode41[] =
1871 { 0x4003, STORE | SETS1 | USES1 | USESSP }, /* stc.l <special_reg>,@-rn */
1872 { 0x4007, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,<special_reg> */
1873 { 0x400c, SETS1 | USES1 | USES2 }, /* shad rm,rn */
1874 { 0x400d, SETS1 | USES1 | USES2 }, /* shld rm,rn */
1875 { 0x400e, SETSSP | USES1 }, /* ldc rm,<special_reg> */
1876 { 0x400f, LOAD|SETS1|SETS2|SETSSP|USES1|USES2|USESSP }, /* mac.w @rm+,@rn+ */
1879 static const struct sh_minor_opcode sh_opcode4[] =
1881 { MAP (sh_opcode40), 0xf0ff },
1882 { MAP (sh_opcode41), 0xf00f }
1885 static const struct sh_opcode sh_opcode50[] =
1887 { 0x5000, LOAD | SETS1 | USES2 } /* mov.l @(disp,rm),rn */
1890 static const struct sh_minor_opcode sh_opcode5[] =
1892 { MAP (sh_opcode50), 0xf000 }
1895 static const struct sh_opcode sh_opcode60[] =
1897 { 0x6000, LOAD | SETS1 | USES2 }, /* mov.b @rm,rn */
1898 { 0x6001, LOAD | SETS1 | USES2 }, /* mov.w @rm,rn */
1899 { 0x6002, LOAD | SETS1 | USES2 }, /* mov.l @rm,rn */
1900 { 0x6003, SETS1 | USES2 }, /* mov rm,rn */
1901 { 0x6004, LOAD | SETS1 | SETS2 | USES2 }, /* mov.b @rm+,rn */
1902 { 0x6005, LOAD | SETS1 | SETS2 | USES2 }, /* mov.w @rm+,rn */
1903 { 0x6006, LOAD | SETS1 | SETS2 | USES2 }, /* mov.l @rm+,rn */
1904 { 0x6007, SETS1 | USES2 }, /* not rm,rn */
1905 { 0x6008, SETS1 | USES2 }, /* swap.b rm,rn */
1906 { 0x6009, SETS1 | USES2 }, /* swap.w rm,rn */
1907 { 0x600a, SETS1 | SETSSP | USES2 | USESSP }, /* negc rm,rn */
1908 { 0x600b, SETS1 | USES2 }, /* neg rm,rn */
1909 { 0x600c, SETS1 | USES2 }, /* extu.b rm,rn */
1910 { 0x600d, SETS1 | USES2 }, /* extu.w rm,rn */
1911 { 0x600e, SETS1 | USES2 }, /* exts.b rm,rn */
1912 { 0x600f, SETS1 | USES2 } /* exts.w rm,rn */
1915 static const struct sh_minor_opcode sh_opcode6[] =
1917 { MAP (sh_opcode60), 0xf00f }
1920 static const struct sh_opcode sh_opcode70[] =
1922 { 0x7000, SETS1 | USES1 } /* add #imm,rn */
1925 static const struct sh_minor_opcode sh_opcode7[] =
1927 { MAP (sh_opcode70), 0xf000 }
1930 static const struct sh_opcode sh_opcode80[] =
1932 { 0x8000, STORE | USES2 | USESR0 }, /* mov.b r0,@(disp,rn) */
1933 { 0x8100, STORE | USES2 | USESR0 }, /* mov.w r0,@(disp,rn) */
1934 { 0x8200, SETSSP }, /* setrc #imm */
1935 { 0x8400, LOAD | SETSR0 | USES2 }, /* mov.b @(disp,rm),r0 */
1936 { 0x8500, LOAD | SETSR0 | USES2 }, /* mov.w @(disp,rn),r0 */
1937 { 0x8800, SETSSP | USESR0 }, /* cmp/eq #imm,r0 */
1938 { 0x8900, BRANCH | USESSP }, /* bt label */
1939 { 0x8b00, BRANCH | USESSP }, /* bf label */
1940 { 0x8c00, SETSSP }, /* ldrs @(disp,pc) */
1941 { 0x8d00, BRANCH | DELAY | USESSP }, /* bt/s label */
1942 { 0x8e00, SETSSP }, /* ldre @(disp,pc) */
1943 { 0x8f00, BRANCH | DELAY | USESSP } /* bf/s label */
1946 static const struct sh_minor_opcode sh_opcode8[] =
1948 { MAP (sh_opcode80), 0xff00 }
1951 static const struct sh_opcode sh_opcode90[] =
1953 { 0x9000, LOAD | SETS1 } /* mov.w @(disp,pc),rn */
1956 static const struct sh_minor_opcode sh_opcode9[] =
1958 { MAP (sh_opcode90), 0xf000 }
1961 static const struct sh_opcode sh_opcodea0[] =
1963 { 0xa000, BRANCH | DELAY } /* bra label */
1966 static const struct sh_minor_opcode sh_opcodea[] =
1968 { MAP (sh_opcodea0), 0xf000 }
1971 static const struct sh_opcode sh_opcodeb0[] =
1973 { 0xb000, BRANCH | DELAY } /* bsr label */
1976 static const struct sh_minor_opcode sh_opcodeb[] =
1978 { MAP (sh_opcodeb0), 0xf000 }
1981 static const struct sh_opcode sh_opcodec0[] =
1983 { 0xc000, STORE | USESR0 | USESSP }, /* mov.b r0,@(disp,gbr) */
1984 { 0xc100, STORE | USESR0 | USESSP }, /* mov.w r0,@(disp,gbr) */
1985 { 0xc200, STORE | USESR0 | USESSP }, /* mov.l r0,@(disp,gbr) */
1986 { 0xc300, BRANCH | USESSP }, /* trapa #imm */
1987 { 0xc400, LOAD | SETSR0 | USESSP }, /* mov.b @(disp,gbr),r0 */
1988 { 0xc500, LOAD | SETSR0 | USESSP }, /* mov.w @(disp,gbr),r0 */
1989 { 0xc600, LOAD | SETSR0 | USESSP }, /* mov.l @(disp,gbr),r0 */
1990 { 0xc700, SETSR0 }, /* mova @(disp,pc),r0 */
1991 { 0xc800, SETSSP | USESR0 }, /* tst #imm,r0 */
1992 { 0xc900, SETSR0 | USESR0 }, /* and #imm,r0 */
1993 { 0xca00, SETSR0 | USESR0 }, /* xor #imm,r0 */
1994 { 0xcb00, SETSR0 | USESR0 }, /* or #imm,r0 */
1995 { 0xcc00, LOAD | SETSSP | USESR0 | USESSP }, /* tst.b #imm,@(r0,gbr) */
1996 { 0xcd00, LOAD | STORE | USESR0 | USESSP }, /* and.b #imm,@(r0,gbr) */
1997 { 0xce00, LOAD | STORE | USESR0 | USESSP }, /* xor.b #imm,@(r0,gbr) */
1998 { 0xcf00, LOAD | STORE | USESR0 | USESSP } /* or.b #imm,@(r0,gbr) */
2001 static const struct sh_minor_opcode sh_opcodec[] =
2003 { MAP (sh_opcodec0), 0xff00 }
2006 static const struct sh_opcode sh_opcoded0[] =
2008 { 0xd000, LOAD | SETS1 } /* mov.l @(disp,pc),rn */
2011 static const struct sh_minor_opcode sh_opcoded[] =
2013 { MAP (sh_opcoded0), 0xf000 }
2016 static const struct sh_opcode sh_opcodee0[] =
2018 { 0xe000, SETS1 } /* mov #imm,rn */
2021 static const struct sh_minor_opcode sh_opcodee[] =
2023 { MAP (sh_opcodee0), 0xf000 }
2026 static const struct sh_opcode sh_opcodef0[] =
2028 { 0xf000, SETSF1 | USESF1 | USESF2 }, /* fadd fm,fn */
2029 { 0xf001, SETSF1 | USESF1 | USESF2 }, /* fsub fm,fn */
2030 { 0xf002, SETSF1 | USESF1 | USESF2 }, /* fmul fm,fn */
2031 { 0xf003, SETSF1 | USESF1 | USESF2 }, /* fdiv fm,fn */
2032 { 0xf004, SETSSP | USESF1 | USESF2 }, /* fcmp/eq fm,fn */
2033 { 0xf005, SETSSP | USESF1 | USESF2 }, /* fcmp/gt fm,fn */
2034 { 0xf006, LOAD | SETSF1 | USES2 | USESR0 }, /* fmov.s @(r0,rm),fn */
2035 { 0xf007, STORE | USES1 | USESF2 | USESR0 }, /* fmov.s fm,@(r0,rn) */
2036 { 0xf008, LOAD | SETSF1 | USES2 }, /* fmov.s @rm,fn */
2037 { 0xf009, LOAD | SETS2 | SETSF1 | USES2 }, /* fmov.s @rm+,fn */
2038 { 0xf00a, STORE | USES1 | USESF2 }, /* fmov.s fm,@rn */
2039 { 0xf00b, STORE | SETS1 | USES1 | USESF2 }, /* fmov.s fm,@-rn */
2040 { 0xf00c, SETSF1 | USESF2 }, /* fmov fm,fn */
2041 { 0xf00e, SETSF1 | USESF1 | USESF2 | USESF0 } /* fmac f0,fm,fn */
2044 static const struct sh_opcode sh_opcodef1[] =
2046 { 0xf00d, SETSF1 | USESSP }, /* fsts fpul,fn */
2047 { 0xf01d, SETSSP | USESF1 }, /* flds fn,fpul */
2048 { 0xf02d, SETSF1 | USESSP }, /* float fpul,fn */
2049 { 0xf03d, SETSSP | USESF1 }, /* ftrc fn,fpul */
2050 { 0xf04d, SETSF1 | USESF1 }, /* fneg fn */
2051 { 0xf05d, SETSF1 | USESF1 }, /* fabs fn */
2052 { 0xf06d, SETSF1 | USESF1 }, /* fsqrt fn */
2053 { 0xf07d, SETSSP | USESF1 }, /* ftst/nan fn */
2054 { 0xf08d, SETSF1 }, /* fldi0 fn */
2055 { 0xf09d, SETSF1 } /* fldi1 fn */
2058 static const struct sh_minor_opcode sh_opcodef[] =
2060 { MAP (sh_opcodef0), 0xf00f },
2061 { MAP (sh_opcodef1), 0xf0ff }
2064 static struct sh_major_opcode sh_opcodes[] =
2066 { MAP (sh_opcode0) },
2067 { MAP (sh_opcode1) },
2068 { MAP (sh_opcode2) },
2069 { MAP (sh_opcode3) },
2070 { MAP (sh_opcode4) },
2071 { MAP (sh_opcode5) },
2072 { MAP (sh_opcode6) },
2073 { MAP (sh_opcode7) },
2074 { MAP (sh_opcode8) },
2075 { MAP (sh_opcode9) },
2076 { MAP (sh_opcodea) },
2077 { MAP (sh_opcodeb) },
2078 { MAP (sh_opcodec) },
2079 { MAP (sh_opcoded) },
2080 { MAP (sh_opcodee) },
2081 { MAP (sh_opcodef) }
2084 /* The double data transfer / parallel processing insns are not
2085 described here. This will cause sh_align_load_span to leave them alone. */
2087 static const struct sh_opcode sh_dsp_opcodef0[] =
2089 { 0xf400, USESAS | SETSAS | LOAD | SETSSP }, /* movs.x @-as,ds */
2090 { 0xf401, USESAS | SETSAS | STORE | USESSP }, /* movs.x ds,@-as */
2091 { 0xf404, USESAS | LOAD | SETSSP }, /* movs.x @as,ds */
2092 { 0xf405, USESAS | STORE | USESSP }, /* movs.x ds,@as */
2093 { 0xf408, USESAS | SETSAS | LOAD | SETSSP }, /* movs.x @as+,ds */
2094 { 0xf409, USESAS | SETSAS | STORE | USESSP }, /* movs.x ds,@as+ */
2095 { 0xf40c, USESAS | SETSAS | LOAD | SETSSP | USESR8 }, /* movs.x @as+r8,ds */
2096 { 0xf40d, USESAS | SETSAS | STORE | USESSP | USESR8 } /* movs.x ds,@as+r8 */
2099 static const struct sh_minor_opcode sh_dsp_opcodef[] =
2101 { MAP (sh_dsp_opcodef0), 0xfc0d }
2104 /* Given an instruction, return a pointer to the corresponding
2105 sh_opcode structure. Return NULL if the instruction is not
2108 static const struct sh_opcode *
2112 const struct sh_major_opcode *maj;
2113 const struct sh_minor_opcode *min, *minend;
2115 maj = &sh_opcodes[(insn & 0xf000) >> 12];
2116 min = maj->minor_opcodes;
2117 minend = min + maj->count;
2118 for (; min < minend; min++)
2121 const struct sh_opcode *op, *opend;
2123 l = insn & min->mask;
2125 opend = op + min->count;
2127 /* Since the opcodes tables are sorted, we could use a binary
2128 search here if the count were above some cutoff value. */
2129 for (; op < opend; op++)
2130 if (op->opcode == l)
2137 /* See whether an instruction uses or sets a general purpose register */
2140 sh_insn_uses_or_sets_reg (insn, op, reg)
2142 const struct sh_opcode *op;
2145 if (sh_insn_uses_reg (insn, op, reg))
2148 return sh_insn_sets_reg (insn, op, reg);
2151 /* See whether an instruction uses a general purpose register. */
2154 sh_insn_uses_reg (insn, op, reg)
2156 const struct sh_opcode *op;
2163 if ((f & USES1) != 0
2164 && USES1_REG (insn) == reg)
2166 if ((f & USES2) != 0
2167 && USES2_REG (insn) == reg)
2169 if ((f & USESR0) != 0
2172 if ((f & USESAS) && reg == USESAS_REG (insn))
2174 if ((f & USESR8) && reg == 8)
2180 /* See whether an instruction sets a general purpose register. */
2183 sh_insn_sets_reg (insn, op, reg)
2185 const struct sh_opcode *op;
2192 if ((f & SETS1) != 0
2193 && SETS1_REG (insn) == reg)
2195 if ((f & SETS2) != 0
2196 && SETS2_REG (insn) == reg)
2198 if ((f & SETSR0) != 0
2201 if ((f & SETSAS) && reg == SETSAS_REG (insn))
2207 /* See whether an instruction uses or sets a floating point register */
2210 sh_insn_uses_or_sets_freg (insn, op, reg)
2212 const struct sh_opcode *op;
2215 if (sh_insn_uses_freg (insn, op, reg))
2218 return sh_insn_sets_freg (insn, op, reg);
2221 /* See whether an instruction uses a floating point register. */
2224 sh_insn_uses_freg (insn, op, freg)
2226 const struct sh_opcode *op;
2233 /* We can't tell if this is a double-precision insn, so just play safe
2234 and assume that it might be. So not only have we test FREG against
2235 itself, but also even FREG against FREG+1 - if the using insn uses
2236 just the low part of a double precision value - but also an odd
2237 FREG against FREG-1 - if the setting insn sets just the low part
2238 of a double precision value.
2239 So what this all boils down to is that we have to ignore the lowest
2240 bit of the register number. */
2242 if ((f & USESF1) != 0
2243 && (USESF1_REG (insn) & 0xe) == (freg & 0xe))
2245 if ((f & USESF2) != 0
2246 && (USESF2_REG (insn) & 0xe) == (freg & 0xe))
2248 if ((f & USESF0) != 0
2255 /* See whether an instruction sets a floating point register. */
2258 sh_insn_sets_freg (insn, op, freg)
2260 const struct sh_opcode *op;
2267 /* We can't tell if this is a double-precision insn, so just play safe
2268 and assume that it might be. So not only have we test FREG against
2269 itself, but also even FREG against FREG+1 - if the using insn uses
2270 just the low part of a double precision value - but also an odd
2271 FREG against FREG-1 - if the setting insn sets just the low part
2272 of a double precision value.
2273 So what this all boils down to is that we have to ignore the lowest
2274 bit of the register number. */
2276 if ((f & SETSF1) != 0
2277 && (SETSF1_REG (insn) & 0xe) == (freg & 0xe))
2283 /* See whether instructions I1 and I2 conflict, assuming I1 comes
2284 before I2. OP1 and OP2 are the corresponding sh_opcode structures.
2285 This should return true if there is a conflict, or false if the
2286 instructions can be swapped safely. */
2289 sh_insns_conflict (i1, op1, i2, op2)
2291 const struct sh_opcode *op1;
2293 const struct sh_opcode *op2;
2295 unsigned int f1, f2;
2300 /* Load of fpscr conflicts with floating point operations.
2301 FIXME: shouldn't test raw opcodes here. */
2302 if (((i1 & 0xf0ff) == 0x4066 && (i2 & 0xf000) == 0xf000)
2303 || ((i2 & 0xf0ff) == 0x4066 && (i1 & 0xf000) == 0xf000))
2306 if ((f1 & (BRANCH | DELAY)) != 0
2307 || (f2 & (BRANCH | DELAY)) != 0)
2310 if (((f1 | f2) & SETSSP)
2311 && (f1 & (SETSSP | USESSP))
2312 && (f2 & (SETSSP | USESSP)))
2315 if ((f1 & SETS1) != 0
2316 && sh_insn_uses_or_sets_reg (i2, op2, SETS1_REG (i1)))
2318 if ((f1 & SETS2) != 0
2319 && sh_insn_uses_or_sets_reg (i2, op2, SETS2_REG (i1)))
2321 if ((f1 & SETSR0) != 0
2322 && sh_insn_uses_or_sets_reg (i2, op2, 0))
2325 && sh_insn_uses_or_sets_reg (i2, op2, SETSAS_REG (i1)))
2327 if ((f1 & SETSF1) != 0
2328 && sh_insn_uses_or_sets_freg (i2, op2, SETSF1_REG (i1)))
2331 if ((f2 & SETS1) != 0
2332 && sh_insn_uses_or_sets_reg (i1, op1, SETS1_REG (i2)))
2334 if ((f2 & SETS2) != 0
2335 && sh_insn_uses_or_sets_reg (i1, op1, SETS2_REG (i2)))
2337 if ((f2 & SETSR0) != 0
2338 && sh_insn_uses_or_sets_reg (i1, op1, 0))
2341 && sh_insn_uses_or_sets_reg (i1, op1, SETSAS_REG (i2)))
2343 if ((f2 & SETSF1) != 0
2344 && sh_insn_uses_or_sets_freg (i1, op1, SETSF1_REG (i2)))
2347 /* The instructions do not conflict. */
2351 /* I1 is a load instruction, and I2 is some other instruction. Return
2352 true if I1 loads a register which I2 uses. */
2355 sh_load_use (i1, op1, i2, op2)
2357 const struct sh_opcode *op1;
2359 const struct sh_opcode *op2;
2365 if ((f1 & LOAD) == 0)
2368 /* If both SETS1 and SETSSP are set, that means a load to a special
2369 register using postincrement addressing mode, which we don't care
2371 if ((f1 & SETS1) != 0
2372 && (f1 & SETSSP) == 0
2373 && sh_insn_uses_reg (i2, op2, (i1 & 0x0f00) >> 8))
2376 if ((f1 & SETSR0) != 0
2377 && sh_insn_uses_reg (i2, op2, 0))
2380 if ((f1 & SETSF1) != 0
2381 && sh_insn_uses_freg (i2, op2, (i1 & 0x0f00) >> 8))
2387 #ifndef COFF_IMAGE_WITH_PE
2388 /* Try to align loads and stores within a span of memory. This is
2389 called by both the ELF and the COFF sh targets. ABFD and SEC are
2390 the BFD and section we are examining. CONTENTS is the contents of
2391 the section. SWAP is the routine to call to swap two instructions.
2392 RELOCS is a pointer to the internal relocation information, to be
2393 passed to SWAP. PLABEL is a pointer to the current label in a
2394 sorted list of labels; LABEL_END is the end of the list. START and
2395 STOP are the range of memory to examine. If a swap is made,
2396 *PSWAPPED is set to true. */
2399 _bfd_sh_align_load_span (abfd, sec, contents, swap, relocs,
2400 plabel, label_end, start, stop, pswapped)
2404 boolean (*swap) PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
2412 int dsp = (abfd->arch_info->mach == bfd_mach_sh_dsp
2413 || abfd->arch_info->mach == bfd_mach_sh3_dsp);
2416 /* The SH4 has a Harvard architecture, hence aligning loads is not
2417 desirable. In fact, it is counter-productive, since it interferes
2418 with the schedules generated by the compiler. */
2419 if (abfd->arch_info->mach == bfd_mach_sh4)
2422 /* If we are linking sh[3]-dsp code, swap the FPU instructions for DSP
2426 sh_opcodes[0xf].minor_opcodes = sh_dsp_opcodef;
2427 sh_opcodes[0xf].count = sizeof sh_dsp_opcodef / sizeof sh_dsp_opcodef;
2430 /* Instructions should be aligned on 2 byte boundaries. */
2431 if ((start & 1) == 1)
2434 /* Now look through the unaligned addresses. */
2438 for (; i < stop; i += 4)
2441 const struct sh_opcode *op;
2442 unsigned int prev_insn = 0;
2443 const struct sh_opcode *prev_op = NULL;
2445 insn = bfd_get_16 (abfd, contents + i);
2446 op = sh_insn_info (insn);
2448 || (op->flags & (LOAD | STORE)) == 0)
2451 /* This is a load or store which is not on a four byte boundary. */
2453 while (*plabel < label_end && **plabel < i)
2458 prev_insn = bfd_get_16 (abfd, contents + i - 2);
2459 /* If INSN is the field b of a parallel processing insn, it is not
2460 a load / store after all. Note that the test here might mistake
2461 the field_b of a pcopy insn for the starting code of a parallel
2462 processing insn; this might miss a swapping opportunity, but at
2463 least we're on the safe side. */
2464 if (dsp && (prev_insn & 0xfc00) == 0xf800)
2467 /* Check if prev_insn is actually the field b of a parallel
2468 processing insn. Again, this can give a spurious match
2470 if (dsp && i - 2 > start)
2472 unsigned pprev_insn = bfd_get_16 (abfd, contents + i - 4);
2474 if ((pprev_insn & 0xfc00) == 0xf800)
2477 prev_op = sh_insn_info (prev_insn);
2480 prev_op = sh_insn_info (prev_insn);
2482 /* If the load/store instruction is in a delay slot, we
2485 || (prev_op->flags & DELAY) != 0)
2489 && (*plabel >= label_end || **plabel != i)
2491 && (prev_op->flags & (LOAD | STORE)) == 0
2492 && ! sh_insns_conflict (prev_insn, prev_op, insn, op))
2496 /* The load/store instruction does not have a label, and
2497 there is a previous instruction; PREV_INSN is not
2498 itself a load/store instruction, and PREV_INSN and
2499 INSN do not conflict. */
2505 unsigned int prev2_insn;
2506 const struct sh_opcode *prev2_op;
2508 prev2_insn = bfd_get_16 (abfd, contents + i - 4);
2509 prev2_op = sh_insn_info (prev2_insn);
2511 /* If the instruction before PREV_INSN has a delay
2512 slot--that is, PREV_INSN is in a delay slot--we
2514 if (prev2_op == NULL
2515 || (prev2_op->flags & DELAY) != 0)
2518 /* If the instruction before PREV_INSN is a load,
2519 and it sets a register which INSN uses, then
2520 putting INSN immediately after PREV_INSN will
2521 cause a pipeline bubble, so there is no point to
2524 && (prev2_op->flags & LOAD) != 0
2525 && sh_load_use (prev2_insn, prev2_op, insn, op))
2531 if (! (*swap) (abfd, sec, relocs, contents, i - 2))
2538 while (*plabel < label_end && **plabel < i + 2)
2542 && (*plabel >= label_end || **plabel != i + 2))
2544 unsigned int next_insn;
2545 const struct sh_opcode *next_op;
2547 /* There is an instruction after the load/store
2548 instruction, and it does not have a label. */
2549 next_insn = bfd_get_16 (abfd, contents + i + 2);
2550 next_op = sh_insn_info (next_insn);
2552 && (next_op->flags & (LOAD | STORE)) == 0
2553 && ! sh_insns_conflict (insn, op, next_insn, next_op))
2557 /* NEXT_INSN is not itself a load/store instruction,
2558 and it does not conflict with INSN. */
2562 /* If PREV_INSN is a load, and it sets a register
2563 which NEXT_INSN uses, then putting NEXT_INSN
2564 immediately after PREV_INSN will cause a pipeline
2565 bubble, so there is no reason to make this swap. */
2567 && (prev_op->flags & LOAD) != 0
2568 && sh_load_use (prev_insn, prev_op, next_insn, next_op))
2571 /* If INSN is a load, and it sets a register which
2572 the insn after NEXT_INSN uses, then doing the
2573 swap will cause a pipeline bubble, so there is no
2574 reason to make the swap. However, if the insn
2575 after NEXT_INSN is itself a load or store
2576 instruction, then it is misaligned, so
2577 optimistically hope that it will be swapped
2578 itself, and just live with the pipeline bubble if
2582 && (op->flags & LOAD) != 0)
2584 unsigned int next2_insn;
2585 const struct sh_opcode *next2_op;
2587 next2_insn = bfd_get_16 (abfd, contents + i + 4);
2588 next2_op = sh_insn_info (next2_insn);
2589 if ((next2_op->flags & (LOAD | STORE)) == 0
2590 && sh_load_use (insn, op, next2_insn, next2_op))
2596 if (! (*swap) (abfd, sec, relocs, contents, i))
2609 /* Look for loads and stores which we can align to four byte
2610 boundaries. See the longer comment above sh_relax_section for why
2611 this is desirable. This sets *PSWAPPED if some instruction was
2615 sh_align_loads (abfd, sec, internal_relocs, contents, pswapped)
2618 struct internal_reloc *internal_relocs;
2622 struct internal_reloc *irel, *irelend;
2623 bfd_vma *labels = NULL;
2624 bfd_vma *label, *label_end;
2628 irelend = internal_relocs + sec->reloc_count;
2630 /* Get all the addresses with labels on them. */
2631 labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma));
2635 for (irel = internal_relocs; irel < irelend; irel++)
2637 if (irel->r_type == R_SH_LABEL)
2639 *label_end = irel->r_vaddr - sec->vma;
2644 /* Note that the assembler currently always outputs relocs in
2645 address order. If that ever changes, this code will need to sort
2646 the label values and the relocs. */
2650 for (irel = internal_relocs; irel < irelend; irel++)
2652 bfd_vma start, stop;
2654 if (irel->r_type != R_SH_CODE)
2657 start = irel->r_vaddr - sec->vma;
2659 for (irel++; irel < irelend; irel++)
2660 if (irel->r_type == R_SH_DATA)
2663 stop = irel->r_vaddr - sec->vma;
2665 stop = sec->_cooked_size;
2667 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_swap_insns,
2668 (PTR) internal_relocs, &label,
2669 label_end, start, stop, pswapped))
2683 /* Swap two SH instructions. */
2686 sh_swap_insns (abfd, sec, relocs, contents, addr)
2693 struct internal_reloc *internal_relocs = (struct internal_reloc *) relocs;
2694 unsigned short i1, i2;
2695 struct internal_reloc *irel, *irelend;
2697 /* Swap the instructions themselves. */
2698 i1 = bfd_get_16 (abfd, contents + addr);
2699 i2 = bfd_get_16 (abfd, contents + addr + 2);
2700 bfd_put_16 (abfd, i2, contents + addr);
2701 bfd_put_16 (abfd, i1, contents + addr + 2);
2703 /* Adjust all reloc addresses. */
2704 irelend = internal_relocs + sec->reloc_count;
2705 for (irel = internal_relocs; irel < irelend; irel++)
2709 /* There are a few special types of relocs that we don't want to
2710 adjust. These relocs do not apply to the instruction itself,
2711 but are only associated with the address. */
2712 type = irel->r_type;
2713 if (type == R_SH_ALIGN
2714 || type == R_SH_CODE
2715 || type == R_SH_DATA
2716 || type == R_SH_LABEL)
2719 /* If an R_SH_USES reloc points to one of the addresses being
2720 swapped, we must adjust it. It would be incorrect to do this
2721 for a jump, though, since we want to execute both
2722 instructions after the jump. (We have avoided swapping
2723 around a label, so the jump will not wind up executing an
2724 instruction it shouldn't). */
2725 if (type == R_SH_USES)
2729 off = irel->r_vaddr - sec->vma + 4 + irel->r_offset;
2731 irel->r_offset += 2;
2732 else if (off == addr + 2)
2733 irel->r_offset -= 2;
2736 if (irel->r_vaddr - sec->vma == addr)
2741 else if (irel->r_vaddr - sec->vma == addr + 2)
2752 unsigned short insn, oinsn;
2755 loc = contents + irel->r_vaddr - sec->vma;
2762 case R_SH_PCDISP8BY2:
2763 case R_SH_PCRELIMM8BY2:
2764 insn = bfd_get_16 (abfd, loc);
2767 if ((oinsn & 0xff00) != (insn & 0xff00))
2769 bfd_put_16 (abfd, insn, loc);
2773 insn = bfd_get_16 (abfd, loc);
2776 if ((oinsn & 0xf000) != (insn & 0xf000))
2778 bfd_put_16 (abfd, insn, loc);
2781 case R_SH_PCRELIMM8BY4:
2782 /* This reloc ignores the least significant 3 bits of
2783 the program counter before adding in the offset.
2784 This means that if ADDR is at an even address, the
2785 swap will not affect the offset. If ADDR is an at an
2786 odd address, then the instruction will be crossing a
2787 four byte boundary, and must be adjusted. */
2788 if ((addr & 3) != 0)
2790 insn = bfd_get_16 (abfd, loc);
2793 if ((oinsn & 0xff00) != (insn & 0xff00))
2795 bfd_put_16 (abfd, insn, loc);
2803 ((*_bfd_error_handler)
2804 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
2805 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
2806 bfd_set_error (bfd_error_bad_value);
2815 /* This is a modification of _bfd_coff_generic_relocate_section, which
2816 will handle SH relaxing. */
2819 sh_relocate_section (output_bfd, info, input_bfd, input_section, contents,
2820 relocs, syms, sections)
2821 bfd *output_bfd ATTRIBUTE_UNUSED;
2822 struct bfd_link_info *info;
2824 asection *input_section;
2826 struct internal_reloc *relocs;
2827 struct internal_syment *syms;
2828 asection **sections;
2830 struct internal_reloc *rel;
2831 struct internal_reloc *relend;
2834 relend = rel + input_section->reloc_count;
2835 for (; rel < relend; rel++)
2838 struct coff_link_hash_entry *h;
2839 struct internal_syment *sym;
2842 reloc_howto_type *howto;
2843 bfd_reloc_status_type rstat;
2845 /* Almost all relocs have to do with relaxing. If any work must
2846 be done for them, it has been done in sh_relax_section. */
2847 if (rel->r_type != R_SH_IMM32
2849 && rel->r_type != R_SH_IMM32CE
2850 && rel->r_type != R_SH_IMAGEBASE
2852 && rel->r_type != R_SH_PCDISP)
2855 symndx = rel->r_symndx;
2865 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2867 (*_bfd_error_handler)
2868 ("%s: illegal symbol index %ld in relocs",
2869 bfd_get_filename (input_bfd), symndx);
2870 bfd_set_error (bfd_error_bad_value);
2873 h = obj_coff_sym_hashes (input_bfd)[symndx];
2874 sym = syms + symndx;
2877 if (sym != NULL && sym->n_scnum != 0)
2878 addend = - sym->n_value;
2882 if (rel->r_type == R_SH_PCDISP)
2885 if (rel->r_type >= SH_COFF_HOWTO_COUNT)
2888 howto = &sh_coff_howtos[rel->r_type];
2892 bfd_set_error (bfd_error_bad_value);
2897 if (rel->r_type == R_SH_IMAGEBASE)
2898 addend -= pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
2907 /* There is nothing to do for an internal PCDISP reloc. */
2908 if (rel->r_type == R_SH_PCDISP)
2913 sec = bfd_abs_section_ptr;
2918 sec = sections[symndx];
2919 val = (sec->output_section->vma
2920 + sec->output_offset
2927 if (h->root.type == bfd_link_hash_defined
2928 || h->root.type == bfd_link_hash_defweak)
2932 sec = h->root.u.def.section;
2933 val = (h->root.u.def.value
2934 + sec->output_section->vma
2935 + sec->output_offset);
2937 else if (! info->relocateable)
2939 if (! ((*info->callbacks->undefined_symbol)
2940 (info, h->root.root.string, input_bfd, input_section,
2941 rel->r_vaddr - input_section->vma, true)))
2946 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2948 rel->r_vaddr - input_section->vma,
2957 case bfd_reloc_overflow:
2960 char buf[SYMNMLEN + 1];
2965 name = h->root.root.string;
2966 else if (sym->_n._n_n._n_zeroes == 0
2967 && sym->_n._n_n._n_offset != 0)
2968 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
2971 strncpy (buf, sym->_n._n_name, SYMNMLEN);
2972 buf[SYMNMLEN] = '\0';
2976 if (! ((*info->callbacks->reloc_overflow)
2977 (info, name, howto->name, (bfd_vma) 0, input_bfd,
2978 input_section, rel->r_vaddr - input_section->vma)))
2987 /* This is a version of bfd_generic_get_relocated_section_contents
2988 which uses sh_relocate_section. */
2991 sh_coff_get_relocated_section_contents (output_bfd, link_info, link_order,
2992 data, relocateable, symbols)
2994 struct bfd_link_info *link_info;
2995 struct bfd_link_order *link_order;
2997 boolean relocateable;
3000 asection *input_section = link_order->u.indirect.section;
3001 bfd *input_bfd = input_section->owner;
3002 asection **sections = NULL;
3003 struct internal_reloc *internal_relocs = NULL;
3004 struct internal_syment *internal_syms = NULL;
3006 /* We only need to handle the case of relaxing, or of having a
3007 particular set of section contents, specially. */
3009 || coff_section_data (input_bfd, input_section) == NULL
3010 || coff_section_data (input_bfd, input_section)->contents == NULL)
3011 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3016 memcpy (data, coff_section_data (input_bfd, input_section)->contents,
3017 input_section->_raw_size);
3019 if ((input_section->flags & SEC_RELOC) != 0
3020 && input_section->reloc_count > 0)
3022 bfd_size_type symesz = bfd_coff_symesz (input_bfd);
3023 bfd_byte *esym, *esymend;
3024 struct internal_syment *isymp;
3027 if (! _bfd_coff_get_external_symbols (input_bfd))
3030 internal_relocs = (_bfd_coff_read_internal_relocs
3031 (input_bfd, input_section, false, (bfd_byte *) NULL,
3032 false, (struct internal_reloc *) NULL));
3033 if (internal_relocs == NULL)
3036 internal_syms = ((struct internal_syment *)
3037 bfd_malloc (obj_raw_syment_count (input_bfd)
3038 * sizeof (struct internal_syment)));
3039 if (internal_syms == NULL)
3042 sections = (asection **) bfd_malloc (obj_raw_syment_count (input_bfd)
3043 * sizeof (asection *));
3044 if (sections == NULL)
3047 isymp = internal_syms;
3049 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3050 esymend = esym + obj_raw_syment_count (input_bfd) * symesz;
3051 while (esym < esymend)
3053 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3055 if (isymp->n_scnum != 0)
3056 *secpp = coff_section_from_bfd_index (input_bfd, isymp->n_scnum);
3059 if (isymp->n_value == 0)
3060 *secpp = bfd_und_section_ptr;
3062 *secpp = bfd_com_section_ptr;
3065 esym += (isymp->n_numaux + 1) * symesz;
3066 secpp += isymp->n_numaux + 1;
3067 isymp += isymp->n_numaux + 1;
3070 if (! sh_relocate_section (output_bfd, link_info, input_bfd,
3071 input_section, data, internal_relocs,
3072 internal_syms, sections))
3077 free (internal_syms);
3078 internal_syms = NULL;
3079 free (internal_relocs);
3080 internal_relocs = NULL;
3086 if (internal_relocs != NULL)
3087 free (internal_relocs);
3088 if (internal_syms != NULL)
3089 free (internal_syms);
3090 if (sections != NULL)
3095 /* The target vectors. */
3097 #ifndef TARGET_SHL_SYM
3098 CREATE_BIG_COFF_TARGET_VEC (shcoff_vec, "coff-sh", BFD_IS_RELAXABLE, 0, '_', NULL)
3101 #ifdef TARGET_SHL_SYM
3102 #define TARGET_SYM TARGET_SHL_SYM
3104 #define TARGET_SYM shlcoff_vec
3107 #ifndef TARGET_SHL_NAME
3108 #define TARGET_SHL_NAME "coff-shl"
3112 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE,
3113 SEC_CODE | SEC_DATA, '_', NULL);
3115 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE, 0, '_', NULL)
3118 #ifndef TARGET_SHL_SYM
3119 /* Some people want versions of the SH COFF target which do not align
3120 to 16 byte boundaries. We implement that by adding a couple of new
3121 target vectors. These are just like the ones above, but they
3122 change the default section alignment. To generate them in the
3123 assembler, use -small. To use them in the linker, use -b
3124 coff-sh{l}-small and -oformat coff-sh{l}-small.
3126 Yes, this is a horrible hack. A general solution for setting
3127 section alignment in COFF is rather complex. ELF handles this
3130 /* Only recognize the small versions if the target was not defaulted.
3131 Otherwise we won't recognize the non default endianness. */
3133 static const bfd_target *
3134 coff_small_object_p (abfd)
3137 if (abfd->target_defaulted)
3139 bfd_set_error (bfd_error_wrong_format);
3142 return coff_object_p (abfd);
3145 /* Set the section alignment for the small versions. */
3148 coff_small_new_section_hook (abfd, section)
3152 if (! coff_new_section_hook (abfd, section))
3155 /* We must align to at least a four byte boundary, because longword
3156 accesses must be on a four byte boundary. */
3157 if (section->alignment_power == COFF_DEFAULT_SECTION_ALIGNMENT_POWER)
3158 section->alignment_power = 2;
3163 /* This is copied from bfd_coff_std_swap_table so that we can change
3164 the default section alignment power. */
3166 static const bfd_coff_backend_data bfd_coff_small_swap_table =
3168 coff_swap_aux_in, coff_swap_sym_in, coff_swap_lineno_in,
3169 coff_swap_aux_out, coff_swap_sym_out,
3170 coff_swap_lineno_out, coff_swap_reloc_out,
3171 coff_swap_filehdr_out, coff_swap_aouthdr_out,
3172 coff_swap_scnhdr_out,
3173 FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
3174 #ifdef COFF_LONG_FILENAMES
3179 #ifdef COFF_LONG_SECTION_NAMES
3185 coff_swap_filehdr_in, coff_swap_aouthdr_in, coff_swap_scnhdr_in,
3186 coff_swap_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
3187 coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
3188 coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
3189 coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
3190 coff_classify_symbol, coff_compute_section_file_positions,
3191 coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
3192 coff_adjust_symndx, coff_link_add_one_symbol,
3193 coff_link_output_has_begun, coff_final_link_postscript
3196 #define coff_small_close_and_cleanup \
3197 coff_close_and_cleanup
3198 #define coff_small_bfd_free_cached_info \
3199 coff_bfd_free_cached_info
3200 #define coff_small_get_section_contents \
3201 coff_get_section_contents
3202 #define coff_small_get_section_contents_in_window \
3203 coff_get_section_contents_in_window
3205 extern const bfd_target shlcoff_small_vec;
3207 const bfd_target shcoff_small_vec =
3209 "coff-sh-small", /* name */
3210 bfd_target_coff_flavour,
3211 BFD_ENDIAN_BIG, /* data byte order is big */
3212 BFD_ENDIAN_BIG, /* header byte order is big */
3214 (HAS_RELOC | EXEC_P | /* object flags */
3215 HAS_LINENO | HAS_DEBUG |
3216 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
3218 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
3219 '_', /* leading symbol underscore */
3220 '/', /* ar_pad_char */
3221 15, /* ar_max_namelen */
3222 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3223 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3224 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3225 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3226 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3227 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3229 {_bfd_dummy_target, coff_small_object_p, /* bfd_check_format */
3230 bfd_generic_archive_p, _bfd_dummy_target},
3231 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3233 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
3234 _bfd_write_archive_contents, bfd_false},
3236 BFD_JUMP_TABLE_GENERIC (coff_small),
3237 BFD_JUMP_TABLE_COPY (coff),
3238 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3239 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
3240 BFD_JUMP_TABLE_SYMBOLS (coff),
3241 BFD_JUMP_TABLE_RELOCS (coff),
3242 BFD_JUMP_TABLE_WRITE (coff),
3243 BFD_JUMP_TABLE_LINK (coff),
3244 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3246 & shlcoff_small_vec,
3248 (PTR) &bfd_coff_small_swap_table
3251 const bfd_target shlcoff_small_vec =
3253 "coff-shl-small", /* name */
3254 bfd_target_coff_flavour,
3255 BFD_ENDIAN_LITTLE, /* data byte order is little */
3256 BFD_ENDIAN_LITTLE, /* header byte order is little endian too*/
3258 (HAS_RELOC | EXEC_P | /* object flags */
3259 HAS_LINENO | HAS_DEBUG |
3260 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
3262 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
3263 '_', /* leading symbol underscore */
3264 '/', /* ar_pad_char */
3265 15, /* ar_max_namelen */
3266 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
3267 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
3268 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
3269 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
3270 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
3271 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
3273 {_bfd_dummy_target, coff_small_object_p, /* bfd_check_format */
3274 bfd_generic_archive_p, _bfd_dummy_target},
3275 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3277 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
3278 _bfd_write_archive_contents, bfd_false},
3280 BFD_JUMP_TABLE_GENERIC (coff_small),
3281 BFD_JUMP_TABLE_COPY (coff),
3282 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3283 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
3284 BFD_JUMP_TABLE_SYMBOLS (coff),
3285 BFD_JUMP_TABLE_RELOCS (coff),
3286 BFD_JUMP_TABLE_WRITE (coff),
3287 BFD_JUMP_TABLE_LINK (coff),
3288 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3292 (PTR) &bfd_coff_small_swap_table