1 /* BFD back-end for Hitachi Super-H COFF binaries.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
5 Written by Steve Chamberlain, <sac@cygnus.com>.
6 Relaxing code written by Ian Lance Taylor, <ian@cygnus.com>.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "libiberty.h"
30 #include "coff/internal.h"
35 #ifndef COFF_IMAGE_WITH_PE
36 static boolean sh_align_load_span
37 PARAMS ((bfd *, asection *, bfd_byte *,
38 boolean (*) (bfd *, asection *, PTR, bfd_byte *, bfd_vma),
39 PTR, bfd_vma **, bfd_vma *, bfd_vma, bfd_vma, boolean *));
41 #define _bfd_sh_align_load_span sh_align_load_span
47 /* Internal functions. */
48 static bfd_reloc_status_type sh_reloc
49 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
50 static long get_symbol_value PARAMS ((asymbol *));
51 static boolean sh_relax_section
52 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
53 static boolean sh_relax_delete_bytes
54 PARAMS ((bfd *, asection *, bfd_vma, int));
55 #ifndef COFF_IMAGE_WITH_PE
56 static const struct sh_opcode *sh_insn_info PARAMS ((unsigned int));
58 static boolean sh_align_loads
59 PARAMS ((bfd *, asection *, struct internal_reloc *, bfd_byte *, boolean *));
60 static boolean sh_swap_insns
61 PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
62 static boolean sh_relocate_section
63 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
64 struct internal_reloc *, struct internal_syment *, asection **));
65 static bfd_byte *sh_coff_get_relocated_section_contents
66 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
67 bfd_byte *, boolean, asymbol **));
68 static reloc_howto_type * sh_coff_reloc_type_lookup PARAMS ((bfd *, bfd_reloc_code_real_type));
71 /* Can't build import tables with 2**4 alignment. */
72 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
74 /* Default section alignment to 2**4. */
75 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 4
78 #ifdef COFF_IMAGE_WITH_PE
79 /* Align PE executables. */
80 #define COFF_PAGE_SIZE 0x1000
83 /* Generate long file names. */
84 #define COFF_LONG_FILENAMES
87 static boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
88 /* Return true if this relocation should
89 appear in the output .reloc section. */
90 static boolean in_reloc_p (abfd, howto)
91 bfd * abfd ATTRIBUTE_UNUSED;
92 reloc_howto_type * howto;
94 return ! howto->pc_relative && howto->type != R_SH_IMAGEBASE;
98 /* The supported relocations. There are a lot of relocations defined
99 in coff/internal.h which we do not expect to ever see. */
100 static reloc_howto_type sh_coff_howtos[] =
106 HOWTO (R_SH_IMM32CE, /* type */
108 2, /* size (0 = byte, 1 = short, 2 = long) */
110 false, /* pc_relative */
112 complain_overflow_bitfield, /* complain_on_overflow */
113 sh_reloc, /* special_function */
114 "r_imm32ce", /* name */
115 true, /* partial_inplace */
116 0xffffffff, /* src_mask */
117 0xffffffff, /* dst_mask */
118 false), /* pcrel_offset */
122 EMPTY_HOWTO (3), /* R_SH_PCREL8 */
123 EMPTY_HOWTO (4), /* R_SH_PCREL16 */
124 EMPTY_HOWTO (5), /* R_SH_HIGH8 */
125 EMPTY_HOWTO (6), /* R_SH_IMM24 */
126 EMPTY_HOWTO (7), /* R_SH_LOW16 */
128 EMPTY_HOWTO (9), /* R_SH_PCDISP8BY4 */
130 HOWTO (R_SH_PCDISP8BY2, /* type */
132 1, /* size (0 = byte, 1 = short, 2 = long) */
134 true, /* pc_relative */
136 complain_overflow_signed, /* complain_on_overflow */
137 sh_reloc, /* special_function */
138 "r_pcdisp8by2", /* name */
139 true, /* partial_inplace */
142 true), /* pcrel_offset */
144 EMPTY_HOWTO (11), /* R_SH_PCDISP8 */
146 HOWTO (R_SH_PCDISP, /* type */
148 1, /* size (0 = byte, 1 = short, 2 = long) */
150 true, /* pc_relative */
152 complain_overflow_signed, /* complain_on_overflow */
153 sh_reloc, /* special_function */
154 "r_pcdisp12by2", /* name */
155 true, /* partial_inplace */
156 0xfff, /* src_mask */
157 0xfff, /* dst_mask */
158 true), /* pcrel_offset */
162 HOWTO (R_SH_IMM32, /* type */
164 2, /* size (0 = byte, 1 = short, 2 = long) */
166 false, /* pc_relative */
168 complain_overflow_bitfield, /* complain_on_overflow */
169 sh_reloc, /* special_function */
170 "r_imm32", /* name */
171 true, /* partial_inplace */
172 0xffffffff, /* src_mask */
173 0xffffffff, /* dst_mask */
174 false), /* pcrel_offset */
178 HOWTO (R_SH_IMAGEBASE, /* type */
180 2, /* size (0 = byte, 1 = short, 2 = long) */
182 false, /* pc_relative */
184 complain_overflow_bitfield, /* complain_on_overflow */
185 sh_reloc, /* special_function */
187 true, /* partial_inplace */
188 0xffffffff, /* src_mask */
189 0xffffffff, /* dst_mask */
190 false), /* pcrel_offset */
192 EMPTY_HOWTO (16), /* R_SH_IMM8 */
194 EMPTY_HOWTO (17), /* R_SH_IMM8BY2 */
195 EMPTY_HOWTO (18), /* R_SH_IMM8BY4 */
196 EMPTY_HOWTO (19), /* R_SH_IMM4 */
197 EMPTY_HOWTO (20), /* R_SH_IMM4BY2 */
198 EMPTY_HOWTO (21), /* R_SH_IMM4BY4 */
200 HOWTO (R_SH_PCRELIMM8BY2, /* type */
202 1, /* size (0 = byte, 1 = short, 2 = long) */
204 true, /* pc_relative */
206 complain_overflow_unsigned, /* complain_on_overflow */
207 sh_reloc, /* special_function */
208 "r_pcrelimm8by2", /* name */
209 true, /* partial_inplace */
212 true), /* pcrel_offset */
214 HOWTO (R_SH_PCRELIMM8BY4, /* type */
216 1, /* size (0 = byte, 1 = short, 2 = long) */
218 true, /* pc_relative */
220 complain_overflow_unsigned, /* complain_on_overflow */
221 sh_reloc, /* special_function */
222 "r_pcrelimm8by4", /* name */
223 true, /* partial_inplace */
226 true), /* pcrel_offset */
228 HOWTO (R_SH_IMM16, /* type */
230 1, /* size (0 = byte, 1 = short, 2 = long) */
232 false, /* pc_relative */
234 complain_overflow_bitfield, /* complain_on_overflow */
235 sh_reloc, /* special_function */
236 "r_imm16", /* name */
237 true, /* partial_inplace */
238 0xffff, /* src_mask */
239 0xffff, /* dst_mask */
240 false), /* pcrel_offset */
242 HOWTO (R_SH_SWITCH16, /* type */
244 1, /* size (0 = byte, 1 = short, 2 = long) */
246 false, /* pc_relative */
248 complain_overflow_bitfield, /* complain_on_overflow */
249 sh_reloc, /* special_function */
250 "r_switch16", /* name */
251 true, /* partial_inplace */
252 0xffff, /* src_mask */
253 0xffff, /* dst_mask */
254 false), /* pcrel_offset */
256 HOWTO (R_SH_SWITCH32, /* type */
258 2, /* size (0 = byte, 1 = short, 2 = long) */
260 false, /* pc_relative */
262 complain_overflow_bitfield, /* complain_on_overflow */
263 sh_reloc, /* special_function */
264 "r_switch32", /* name */
265 true, /* partial_inplace */
266 0xffffffff, /* src_mask */
267 0xffffffff, /* dst_mask */
268 false), /* pcrel_offset */
270 HOWTO (R_SH_USES, /* type */
272 1, /* size (0 = byte, 1 = short, 2 = long) */
274 false, /* pc_relative */
276 complain_overflow_bitfield, /* complain_on_overflow */
277 sh_reloc, /* special_function */
279 true, /* partial_inplace */
280 0xffff, /* src_mask */
281 0xffff, /* dst_mask */
282 false), /* pcrel_offset */
284 HOWTO (R_SH_COUNT, /* type */
286 2, /* size (0 = byte, 1 = short, 2 = long) */
288 false, /* pc_relative */
290 complain_overflow_bitfield, /* complain_on_overflow */
291 sh_reloc, /* special_function */
292 "r_count", /* name */
293 true, /* partial_inplace */
294 0xffffffff, /* src_mask */
295 0xffffffff, /* dst_mask */
296 false), /* pcrel_offset */
298 HOWTO (R_SH_ALIGN, /* type */
300 2, /* size (0 = byte, 1 = short, 2 = long) */
302 false, /* pc_relative */
304 complain_overflow_bitfield, /* complain_on_overflow */
305 sh_reloc, /* special_function */
306 "r_align", /* name */
307 true, /* partial_inplace */
308 0xffffffff, /* src_mask */
309 0xffffffff, /* dst_mask */
310 false), /* pcrel_offset */
312 HOWTO (R_SH_CODE, /* type */
314 2, /* size (0 = byte, 1 = short, 2 = long) */
316 false, /* pc_relative */
318 complain_overflow_bitfield, /* complain_on_overflow */
319 sh_reloc, /* special_function */
321 true, /* partial_inplace */
322 0xffffffff, /* src_mask */
323 0xffffffff, /* dst_mask */
324 false), /* pcrel_offset */
326 HOWTO (R_SH_DATA, /* type */
328 2, /* size (0 = byte, 1 = short, 2 = long) */
330 false, /* pc_relative */
332 complain_overflow_bitfield, /* complain_on_overflow */
333 sh_reloc, /* special_function */
335 true, /* partial_inplace */
336 0xffffffff, /* src_mask */
337 0xffffffff, /* dst_mask */
338 false), /* pcrel_offset */
340 HOWTO (R_SH_LABEL, /* type */
342 2, /* size (0 = byte, 1 = short, 2 = long) */
344 false, /* pc_relative */
346 complain_overflow_bitfield, /* complain_on_overflow */
347 sh_reloc, /* special_function */
348 "r_label", /* name */
349 true, /* partial_inplace */
350 0xffffffff, /* src_mask */
351 0xffffffff, /* dst_mask */
352 false), /* pcrel_offset */
354 HOWTO (R_SH_SWITCH8, /* type */
356 0, /* size (0 = byte, 1 = short, 2 = long) */
358 false, /* pc_relative */
360 complain_overflow_bitfield, /* complain_on_overflow */
361 sh_reloc, /* special_function */
362 "r_switch8", /* name */
363 true, /* partial_inplace */
366 false) /* pcrel_offset */
369 #define SH_COFF_HOWTO_COUNT (sizeof sh_coff_howtos / sizeof sh_coff_howtos[0])
371 /* Check for a bad magic number. */
372 #define BADMAG(x) SHBADMAG(x)
374 /* Customize coffcode.h (this is not currently used). */
377 /* FIXME: This should not be set here. */
378 #define __A_MAGIC_SET__
381 /* Swap the r_offset field in and out. */
382 #define SWAP_IN_RELOC_OFFSET H_GET_32
383 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
385 /* Swap out extra information in the reloc structure. */
386 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
389 dst->r_stuff[0] = 'S'; \
390 dst->r_stuff[1] = 'C'; \
395 /* Get the value of a symbol, when performing a relocation. */
398 get_symbol_value (symbol)
403 if (bfd_is_com_section (symbol->section))
406 relocation = (symbol->value +
407 symbol->section->output_section->vma +
408 symbol->section->output_offset);
414 /* Convert an rtype to howto for the COFF backend linker.
415 Copied from coff-i386. */
416 #define coff_rtype_to_howto coff_sh_rtype_to_howto
417 static reloc_howto_type * coff_sh_rtype_to_howto PARAMS ((bfd *, asection *, struct internal_reloc *, struct coff_link_hash_entry *, struct internal_syment *, bfd_vma *));
419 static reloc_howto_type *
420 coff_sh_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
421 bfd * abfd ATTRIBUTE_UNUSED;
423 struct internal_reloc * rel;
424 struct coff_link_hash_entry * h;
425 struct internal_syment * sym;
428 reloc_howto_type * howto;
430 howto = sh_coff_howtos + rel->r_type;
434 if (howto->pc_relative)
435 *addendp += sec->vma;
437 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
439 /* This is a common symbol. The section contents include the
440 size (sym->n_value) as an addend. The relocate_section
441 function will be adding in the final value of the symbol. We
442 need to subtract out the current size in order to get the
444 BFD_ASSERT (h != NULL);
447 if (howto->pc_relative)
451 /* If the symbol is defined, then the generic code is going to
452 add back the symbol value in order to cancel out an
453 adjustment it made to the addend. However, we set the addend
454 to 0 at the start of this function. We need to adjust here,
455 to avoid the adjustment the generic code will make. FIXME:
456 This is getting a bit hackish. */
457 if (sym != NULL && sym->n_scnum != 0)
458 *addendp -= sym->n_value;
461 if (rel->r_type == R_SH_IMAGEBASE)
462 *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
467 #endif /* COFF_WITH_PE */
469 /* This structure is used to map BFD reloc codes to SH PE relocs. */
470 struct shcoff_reloc_map
472 bfd_reloc_code_real_type bfd_reloc_val;
473 unsigned char shcoff_reloc_val;
476 /* An array mapping BFD reloc codes to SH PE relocs. */
477 static const struct shcoff_reloc_map sh_reloc_map[] =
479 { BFD_RELOC_32, R_SH_IMM32CE },
480 { BFD_RELOC_RVA, R_SH_IMAGEBASE },
481 { BFD_RELOC_CTOR, R_SH_IMM32CE },
484 /* Given a BFD reloc code, return the howto structure for the
485 corresponding SH PE reloc. */
486 #define coff_bfd_reloc_type_lookup sh_coff_reloc_type_lookup
488 static reloc_howto_type *
489 sh_coff_reloc_type_lookup (abfd, code)
490 bfd * abfd ATTRIBUTE_UNUSED;
491 bfd_reloc_code_real_type code;
495 for (i = ARRAY_SIZE (sh_reloc_map); i--;)
496 if (sh_reloc_map[i].bfd_reloc_val == code)
497 return &sh_coff_howtos[(int) sh_reloc_map[i].shcoff_reloc_val];
499 fprintf (stderr, "SH Error: unknown reloc type %d\n", code);
503 /* This macro is used in coffcode.h to get the howto corresponding to
504 an internal reloc. */
506 #define RTYPE2HOWTO(relent, internal) \
508 ((internal)->r_type < SH_COFF_HOWTO_COUNT \
509 ? &sh_coff_howtos[(internal)->r_type] \
510 : (reloc_howto_type *) NULL))
512 /* This is the same as the macro in coffcode.h, except that it copies
513 r_offset into reloc_entry->addend for some relocs. */
514 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
516 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
517 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
518 coffsym = (obj_symbols (abfd) \
519 + (cache_ptr->sym_ptr_ptr - symbols)); \
521 coffsym = coff_symbol_from (abfd, ptr); \
522 if (coffsym != (coff_symbol_type *) NULL \
523 && coffsym->native->u.syment.n_scnum == 0) \
524 cache_ptr->addend = 0; \
525 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
526 && ptr->section != (asection *) NULL) \
527 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
529 cache_ptr->addend = 0; \
530 if ((reloc).r_type == R_SH_SWITCH8 \
531 || (reloc).r_type == R_SH_SWITCH16 \
532 || (reloc).r_type == R_SH_SWITCH32 \
533 || (reloc).r_type == R_SH_USES \
534 || (reloc).r_type == R_SH_COUNT \
535 || (reloc).r_type == R_SH_ALIGN) \
536 cache_ptr->addend = (reloc).r_offset; \
539 /* This is the howto function for the SH relocations. */
541 static bfd_reloc_status_type
542 sh_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
545 arelent *reloc_entry;
548 asection *input_section;
550 char **error_message ATTRIBUTE_UNUSED;
554 unsigned short r_type;
555 bfd_vma addr = reloc_entry->address;
556 bfd_byte *hit_data = addr + (bfd_byte *) data;
558 r_type = reloc_entry->howto->type;
560 if (output_bfd != NULL)
562 /* Partial linking--do nothing. */
563 reloc_entry->address += input_section->output_offset;
567 /* Almost all relocs have to do with relaxing. If any work must be
568 done for them, it has been done in sh_relax_section. */
569 if (r_type != R_SH_IMM32
571 && r_type != R_SH_IMM32CE
572 && r_type != R_SH_IMAGEBASE
574 && (r_type != R_SH_PCDISP
575 || (symbol_in->flags & BSF_LOCAL) != 0))
578 if (symbol_in != NULL
579 && bfd_is_und_section (symbol_in->section))
580 return bfd_reloc_undefined;
582 sym_value = get_symbol_value (symbol_in);
590 insn = bfd_get_32 (abfd, hit_data);
591 insn += sym_value + reloc_entry->addend;
592 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
596 insn = bfd_get_32 (abfd, hit_data);
597 insn += sym_value + reloc_entry->addend;
598 insn -= pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
599 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
603 insn = bfd_get_16 (abfd, hit_data);
604 sym_value += reloc_entry->addend;
605 sym_value -= (input_section->output_section->vma
606 + input_section->output_offset
609 sym_value += (insn & 0xfff) << 1;
612 insn = (insn & 0xf000) | (sym_value & 0xfff);
613 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
614 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
615 return bfd_reloc_overflow;
625 #define coff_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
627 /* We can do relaxing. */
628 #define coff_bfd_relax_section sh_relax_section
630 /* We use the special COFF backend linker. */
631 #define coff_relocate_section sh_relocate_section
633 /* When relaxing, we need to use special code to get the relocated
635 #define coff_bfd_get_relocated_section_contents \
636 sh_coff_get_relocated_section_contents
638 #include "coffcode.h"
640 /* This function handles relaxing on the SH.
642 Function calls on the SH look like this:
651 The compiler and assembler will cooperate to create R_SH_USES
652 relocs on the jsr instructions. The r_offset field of the
653 R_SH_USES reloc is the PC relative offset to the instruction which
654 loads the register (the r_offset field is computed as though it
655 were a jump instruction, so the offset value is actually from four
656 bytes past the instruction). The linker can use this reloc to
657 determine just which function is being called, and thus decide
658 whether it is possible to replace the jsr with a bsr.
660 If multiple function calls are all based on a single register load
661 (i.e., the same function is called multiple times), the compiler
662 guarantees that each function call will have an R_SH_USES reloc.
663 Therefore, if the linker is able to convert each R_SH_USES reloc
664 which refers to that address, it can safely eliminate the register
667 When the assembler creates an R_SH_USES reloc, it examines it to
668 determine which address is being loaded (L1 in the above example).
669 It then counts the number of references to that address, and
670 creates an R_SH_COUNT reloc at that address. The r_offset field of
671 the R_SH_COUNT reloc will be the number of references. If the
672 linker is able to eliminate a register load, it can use the
673 R_SH_COUNT reloc to see whether it can also eliminate the function
676 SH relaxing also handles another, unrelated, matter. On the SH, if
677 a load or store instruction is not aligned on a four byte boundary,
678 the memory cycle interferes with the 32 bit instruction fetch,
679 causing a one cycle bubble in the pipeline. Therefore, we try to
680 align load and store instructions on four byte boundaries if we
681 can, by swapping them with one of the adjacent instructions. */
684 sh_relax_section (abfd, sec, link_info, again)
687 struct bfd_link_info *link_info;
690 struct internal_reloc *internal_relocs;
691 struct internal_reloc *free_relocs = NULL;
693 struct internal_reloc *irel, *irelend;
694 bfd_byte *contents = NULL;
695 bfd_byte *free_contents = NULL;
699 if (link_info->relocateable
700 || (sec->flags & SEC_RELOC) == 0
701 || sec->reloc_count == 0)
704 /* If this is the first time we have been called for this section,
705 initialize the cooked size. */
706 if (sec->_cooked_size == 0)
707 sec->_cooked_size = sec->_raw_size;
709 internal_relocs = (_bfd_coff_read_internal_relocs
710 (abfd, sec, link_info->keep_memory,
711 (bfd_byte *) NULL, false,
712 (struct internal_reloc *) NULL));
713 if (internal_relocs == NULL)
715 if (! link_info->keep_memory)
716 free_relocs = internal_relocs;
720 irelend = internal_relocs + sec->reloc_count;
721 for (irel = internal_relocs; irel < irelend; irel++)
723 bfd_vma laddr, paddr, symval;
725 struct internal_reloc *irelfn, *irelscan, *irelcount;
726 struct internal_syment sym;
729 if (irel->r_type == R_SH_CODE)
732 if (irel->r_type != R_SH_USES)
735 /* Get the section contents. */
736 if (contents == NULL)
738 if (coff_section_data (abfd, sec) != NULL
739 && coff_section_data (abfd, sec)->contents != NULL)
740 contents = coff_section_data (abfd, sec)->contents;
743 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
744 if (contents == NULL)
746 free_contents = contents;
748 if (! bfd_get_section_contents (abfd, sec, contents,
749 (file_ptr) 0, sec->_raw_size))
754 /* The r_offset field of the R_SH_USES reloc will point us to
755 the register load. The 4 is because the r_offset field is
756 computed as though it were a jump offset, which are based
757 from 4 bytes after the jump instruction. */
758 laddr = irel->r_vaddr - sec->vma + 4;
759 /* Careful to sign extend the 32-bit offset. */
760 laddr += ((irel->r_offset & 0xffffffff) ^ 0x80000000) - 0x80000000;
761 if (laddr >= sec->_raw_size)
763 (*_bfd_error_handler) ("%s: 0x%lx: warning: bad R_SH_USES offset",
764 bfd_archive_filename (abfd),
765 (unsigned long) irel->r_vaddr);
768 insn = bfd_get_16 (abfd, contents + laddr);
770 /* If the instruction is not mov.l NN,rN, we don't know what to do. */
771 if ((insn & 0xf000) != 0xd000)
773 ((*_bfd_error_handler)
774 ("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x",
775 bfd_archive_filename (abfd), (unsigned long) irel->r_vaddr, insn));
779 /* Get the address from which the register is being loaded. The
780 displacement in the mov.l instruction is quadrupled. It is a
781 displacement from four bytes after the movl instruction, but,
782 before adding in the PC address, two least significant bits
783 of the PC are cleared. We assume that the section is aligned
784 on a four byte boundary. */
787 paddr += (laddr + 4) &~ (bfd_vma) 3;
788 if (paddr >= sec->_raw_size)
790 ((*_bfd_error_handler)
791 ("%s: 0x%lx: warning: bad R_SH_USES load offset",
792 bfd_archive_filename (abfd), (unsigned long) irel->r_vaddr));
796 /* Get the reloc for the address from which the register is
797 being loaded. This reloc will tell us which function is
798 actually being called. */
800 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
801 if (irelfn->r_vaddr == paddr
803 && (irelfn->r_type == R_SH_IMM32
804 || irelfn->r_type == R_SH_IMM32CE
805 || irelfn->r_type == R_SH_IMAGEBASE))
808 && irelfn->r_type == R_SH_IMM32)
811 if (irelfn >= irelend)
813 ((*_bfd_error_handler)
814 ("%s: 0x%lx: warning: could not find expected reloc",
815 bfd_archive_filename (abfd), (unsigned long) paddr));
819 /* Get the value of the symbol referred to by the reloc. */
820 if (! _bfd_coff_get_external_symbols (abfd))
822 bfd_coff_swap_sym_in (abfd,
823 ((bfd_byte *) obj_coff_external_syms (abfd)
825 * bfd_coff_symesz (abfd))),
827 if (sym.n_scnum != 0 && sym.n_scnum != sec->target_index)
829 ((*_bfd_error_handler)
830 ("%s: 0x%lx: warning: symbol in unexpected section",
831 bfd_archive_filename (abfd), (unsigned long) paddr));
835 if (sym.n_sclass != C_EXT)
837 symval = (sym.n_value
839 + sec->output_section->vma
840 + sec->output_offset);
844 struct coff_link_hash_entry *h;
846 h = obj_coff_sym_hashes (abfd)[irelfn->r_symndx];
847 BFD_ASSERT (h != NULL);
848 if (h->root.type != bfd_link_hash_defined
849 && h->root.type != bfd_link_hash_defweak)
851 /* This appears to be a reference to an undefined
852 symbol. Just ignore it--it will be caught by the
853 regular reloc processing. */
857 symval = (h->root.u.def.value
858 + h->root.u.def.section->output_section->vma
859 + h->root.u.def.section->output_offset);
862 symval += bfd_get_32 (abfd, contents + paddr - sec->vma);
864 /* See if this function call can be shortened. */
868 + sec->output_section->vma
871 if (foff < -0x1000 || foff >= 0x1000)
873 /* After all that work, we can't shorten this function call. */
877 /* Shorten the function call. */
879 /* For simplicity of coding, we are going to modify the section
880 contents, the section relocs, and the BFD symbol table. We
881 must tell the rest of the code not to free up this
882 information. It would be possible to instead create a table
883 of changes which have to be made, as is done in coff-mips.c;
884 that would be more work, but would require less memory when
885 the linker is run. */
887 if (coff_section_data (abfd, sec) == NULL)
889 bfd_size_type amt = sizeof (struct coff_section_tdata);
890 sec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
891 if (sec->used_by_bfd == NULL)
895 coff_section_data (abfd, sec)->relocs = internal_relocs;
896 coff_section_data (abfd, sec)->keep_relocs = true;
899 coff_section_data (abfd, sec)->contents = contents;
900 coff_section_data (abfd, sec)->keep_contents = true;
901 free_contents = NULL;
903 obj_coff_keep_syms (abfd) = true;
905 /* Replace the jsr with a bsr. */
907 /* Change the R_SH_USES reloc into an R_SH_PCDISP reloc, and
908 replace the jsr with a bsr. */
909 irel->r_type = R_SH_PCDISP;
910 irel->r_symndx = irelfn->r_symndx;
911 if (sym.n_sclass != C_EXT)
913 /* If this needs to be changed because of future relaxing,
914 it will be handled here like other internal PCDISP
917 (bfd_vma) 0xb000 | ((foff >> 1) & 0xfff),
918 contents + irel->r_vaddr - sec->vma);
922 /* We can't fully resolve this yet, because the external
923 symbol value may be changed by future relaxing. We let
924 the final link phase handle it. */
925 bfd_put_16 (abfd, (bfd_vma) 0xb000,
926 contents + irel->r_vaddr - sec->vma);
929 /* See if there is another R_SH_USES reloc referring to the same
931 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
932 if (irelscan->r_type == R_SH_USES
933 && laddr == irelscan->r_vaddr - sec->vma + 4 + irelscan->r_offset)
935 if (irelscan < irelend)
937 /* Some other function call depends upon this register load,
938 and we have not yet converted that function call.
939 Indeed, we may never be able to convert it. There is
940 nothing else we can do at this point. */
944 /* Look for a R_SH_COUNT reloc on the location where the
945 function address is stored. Do this before deleting any
946 bytes, to avoid confusion about the address. */
947 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
948 if (irelcount->r_vaddr == paddr
949 && irelcount->r_type == R_SH_COUNT)
952 /* Delete the register load. */
953 if (! sh_relax_delete_bytes (abfd, sec, laddr, 2))
956 /* That will change things, so, just in case it permits some
957 other function call to come within range, we should relax
958 again. Note that this is not required, and it may be slow. */
961 /* Now check whether we got a COUNT reloc. */
962 if (irelcount >= irelend)
964 ((*_bfd_error_handler)
965 ("%s: 0x%lx: warning: could not find expected COUNT reloc",
966 bfd_archive_filename (abfd), (unsigned long) paddr));
970 /* The number of uses is stored in the r_offset field. We've
972 if (irelcount->r_offset == 0)
974 ((*_bfd_error_handler) ("%s: 0x%lx: warning: bad count",
975 bfd_archive_filename (abfd),
976 (unsigned long) paddr));
980 --irelcount->r_offset;
982 /* If there are no more uses, we can delete the address. Reload
983 the address from irelfn, in case it was changed by the
984 previous call to sh_relax_delete_bytes. */
985 if (irelcount->r_offset == 0)
987 if (! sh_relax_delete_bytes (abfd, sec,
988 irelfn->r_vaddr - sec->vma, 4))
992 /* We've done all we can with that function call. */
995 /* Look for load and store instructions that we can align on four
1001 /* Get the section contents. */
1002 if (contents == NULL)
1004 if (coff_section_data (abfd, sec) != NULL
1005 && coff_section_data (abfd, sec)->contents != NULL)
1006 contents = coff_section_data (abfd, sec)->contents;
1009 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1010 if (contents == NULL)
1012 free_contents = contents;
1014 if (! bfd_get_section_contents (abfd, sec, contents,
1015 (file_ptr) 0, sec->_raw_size))
1020 if (! sh_align_loads (abfd, sec, internal_relocs, contents, &swapped))
1025 if (coff_section_data (abfd, sec) == NULL)
1027 bfd_size_type amt = sizeof (struct coff_section_tdata);
1028 sec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1029 if (sec->used_by_bfd == NULL)
1033 coff_section_data (abfd, sec)->relocs = internal_relocs;
1034 coff_section_data (abfd, sec)->keep_relocs = true;
1037 coff_section_data (abfd, sec)->contents = contents;
1038 coff_section_data (abfd, sec)->keep_contents = true;
1039 free_contents = NULL;
1041 obj_coff_keep_syms (abfd) = true;
1045 if (free_relocs != NULL)
1051 if (free_contents != NULL)
1053 if (! link_info->keep_memory)
1054 free (free_contents);
1057 /* Cache the section contents for coff_link_input_bfd. */
1058 if (coff_section_data (abfd, sec) == NULL)
1060 bfd_size_type amt = sizeof (struct coff_section_tdata);
1061 sec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1062 if (sec->used_by_bfd == NULL)
1064 coff_section_data (abfd, sec)->relocs = NULL;
1066 coff_section_data (abfd, sec)->contents = contents;
1073 if (free_relocs != NULL)
1075 if (free_contents != NULL)
1076 free (free_contents);
1080 /* Delete some bytes from a section while relaxing. */
1083 sh_relax_delete_bytes (abfd, sec, addr, count)
1090 struct internal_reloc *irel, *irelend;
1091 struct internal_reloc *irelalign;
1093 bfd_byte *esym, *esymend;
1094 bfd_size_type symesz;
1095 struct coff_link_hash_entry **sym_hash;
1098 contents = coff_section_data (abfd, sec)->contents;
1100 /* The deletion must stop at the next ALIGN reloc for an aligment
1101 power larger than the number of bytes we are deleting. */
1104 toaddr = sec->_cooked_size;
1106 irel = coff_section_data (abfd, sec)->relocs;
1107 irelend = irel + sec->reloc_count;
1108 for (; irel < irelend; irel++)
1110 if (irel->r_type == R_SH_ALIGN
1111 && irel->r_vaddr - sec->vma > addr
1112 && count < (1 << irel->r_offset))
1115 toaddr = irel->r_vaddr - sec->vma;
1120 /* Actually delete the bytes. */
1121 memmove (contents + addr, contents + addr + count,
1122 (size_t) (toaddr - addr - count));
1123 if (irelalign == NULL)
1124 sec->_cooked_size -= count;
1129 #define NOP_OPCODE (0x0009)
1131 BFD_ASSERT ((count & 1) == 0);
1132 for (i = 0; i < count; i += 2)
1133 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
1136 /* Adjust all the relocs. */
1137 for (irel = coff_section_data (abfd, sec)->relocs; irel < irelend; irel++)
1139 bfd_vma nraddr, stop;
1142 struct internal_syment sym;
1143 int off, adjust, oinsn;
1144 bfd_signed_vma voff = 0;
1147 /* Get the new reloc address. */
1148 nraddr = irel->r_vaddr - sec->vma;
1149 if ((irel->r_vaddr - sec->vma > addr
1150 && irel->r_vaddr - sec->vma < toaddr)
1151 || (irel->r_type == R_SH_ALIGN
1152 && irel->r_vaddr - sec->vma == toaddr))
1155 /* See if this reloc was for the bytes we have deleted, in which
1156 case we no longer care about it. Don't delete relocs which
1157 represent addresses, though. */
1158 if (irel->r_vaddr - sec->vma >= addr
1159 && irel->r_vaddr - sec->vma < addr + count
1160 && irel->r_type != R_SH_ALIGN
1161 && irel->r_type != R_SH_CODE
1162 && irel->r_type != R_SH_DATA
1163 && irel->r_type != R_SH_LABEL)
1164 irel->r_type = R_SH_UNUSED;
1166 /* If this is a PC relative reloc, see if the range it covers
1167 includes the bytes we have deleted. */
1168 switch (irel->r_type)
1173 case R_SH_PCDISP8BY2:
1175 case R_SH_PCRELIMM8BY2:
1176 case R_SH_PCRELIMM8BY4:
1177 start = irel->r_vaddr - sec->vma;
1178 insn = bfd_get_16 (abfd, contents + nraddr);
1182 switch (irel->r_type)
1185 start = stop = addr;
1191 case R_SH_IMAGEBASE:
1193 /* If this reloc is against a symbol defined in this
1194 section, and the symbol will not be adjusted below, we
1195 must check the addend to see it will put the value in
1196 range to be adjusted, and hence must be changed. */
1197 bfd_coff_swap_sym_in (abfd,
1198 ((bfd_byte *) obj_coff_external_syms (abfd)
1200 * bfd_coff_symesz (abfd))),
1202 if (sym.n_sclass != C_EXT
1203 && sym.n_scnum == sec->target_index
1204 && ((bfd_vma) sym.n_value <= addr
1205 || (bfd_vma) sym.n_value >= toaddr))
1209 val = bfd_get_32 (abfd, contents + nraddr);
1211 if (val > addr && val < toaddr)
1212 bfd_put_32 (abfd, val - count, contents + nraddr);
1214 start = stop = addr;
1217 case R_SH_PCDISP8BY2:
1221 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1225 bfd_coff_swap_sym_in (abfd,
1226 ((bfd_byte *) obj_coff_external_syms (abfd)
1228 * bfd_coff_symesz (abfd))),
1230 if (sym.n_sclass == C_EXT)
1231 start = stop = addr;
1237 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1241 case R_SH_PCRELIMM8BY2:
1243 stop = start + 4 + off * 2;
1246 case R_SH_PCRELIMM8BY4:
1248 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1254 /* These relocs types represent
1256 The r_offset field holds the difference between the reloc
1257 address and L1. That is the start of the reloc, and
1258 adding in the contents gives us the top. We must adjust
1259 both the r_offset field and the section contents. */
1261 start = irel->r_vaddr - sec->vma;
1262 stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_offset);
1266 && (stop <= addr || stop >= toaddr))
1267 irel->r_offset += count;
1268 else if (stop > addr
1270 && (start <= addr || start >= toaddr))
1271 irel->r_offset -= count;
1275 if (irel->r_type == R_SH_SWITCH16)
1276 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1277 else if (irel->r_type == R_SH_SWITCH8)
1278 voff = bfd_get_8 (abfd, contents + nraddr);
1280 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1281 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1286 start = irel->r_vaddr - sec->vma;
1287 stop = (bfd_vma) ((bfd_signed_vma) start
1288 + (long) irel->r_offset
1295 && (stop <= addr || stop >= toaddr))
1297 else if (stop > addr
1299 && (start <= addr || start >= toaddr))
1308 switch (irel->r_type)
1314 case R_SH_PCDISP8BY2:
1315 case R_SH_PCRELIMM8BY2:
1317 if ((oinsn & 0xff00) != (insn & 0xff00))
1319 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1324 if ((oinsn & 0xf000) != (insn & 0xf000))
1326 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1329 case R_SH_PCRELIMM8BY4:
1330 BFD_ASSERT (adjust == count || count >= 4);
1335 if ((irel->r_vaddr & 3) == 0)
1338 if ((oinsn & 0xff00) != (insn & 0xff00))
1340 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1345 if (voff < 0 || voff >= 0xff)
1347 bfd_put_8 (abfd, (bfd_vma) voff, contents + nraddr);
1352 if (voff < - 0x8000 || voff >= 0x8000)
1354 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1359 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1363 irel->r_offset += adjust;
1369 ((*_bfd_error_handler)
1370 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1371 bfd_archive_filename (abfd), (unsigned long) irel->r_vaddr));
1372 bfd_set_error (bfd_error_bad_value);
1377 irel->r_vaddr = nraddr + sec->vma;
1380 /* Look through all the other sections. If there contain any IMM32
1381 relocs against internal symbols which we are not going to adjust
1382 below, we may need to adjust the addends. */
1383 for (o = abfd->sections; o != NULL; o = o->next)
1385 struct internal_reloc *internal_relocs;
1386 struct internal_reloc *irelscan, *irelscanend;
1387 bfd_byte *ocontents;
1390 || (o->flags & SEC_RELOC) == 0
1391 || o->reloc_count == 0)
1394 /* We always cache the relocs. Perhaps, if info->keep_memory is
1395 false, we should free them, if we are permitted to, when we
1396 leave sh_coff_relax_section. */
1397 internal_relocs = (_bfd_coff_read_internal_relocs
1398 (abfd, o, true, (bfd_byte *) NULL, false,
1399 (struct internal_reloc *) NULL));
1400 if (internal_relocs == NULL)
1404 irelscanend = internal_relocs + o->reloc_count;
1405 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1407 struct internal_syment sym;
1410 if (irelscan->r_type != R_SH_IMM32
1411 && irelscan->r_type != R_SH_IMAGEBASE
1412 && irelscan->r_type != R_SH_IMM32CE)
1414 if (irelscan->r_type != R_SH_IMM32)
1418 bfd_coff_swap_sym_in (abfd,
1419 ((bfd_byte *) obj_coff_external_syms (abfd)
1420 + (irelscan->r_symndx
1421 * bfd_coff_symesz (abfd))),
1423 if (sym.n_sclass != C_EXT
1424 && sym.n_scnum == sec->target_index
1425 && ((bfd_vma) sym.n_value <= addr
1426 || (bfd_vma) sym.n_value >= toaddr))
1430 if (ocontents == NULL)
1432 if (coff_section_data (abfd, o)->contents != NULL)
1433 ocontents = coff_section_data (abfd, o)->contents;
1436 /* We always cache the section contents.
1437 Perhaps, if info->keep_memory is false, we
1438 should free them, if we are permitted to,
1439 when we leave sh_coff_relax_section. */
1440 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1441 if (ocontents == NULL)
1443 if (! bfd_get_section_contents (abfd, o, ocontents,
1447 coff_section_data (abfd, o)->contents = ocontents;
1451 val = bfd_get_32 (abfd, ocontents + irelscan->r_vaddr - o->vma);
1453 if (val > addr && val < toaddr)
1454 bfd_put_32 (abfd, val - count,
1455 ocontents + irelscan->r_vaddr - o->vma);
1457 coff_section_data (abfd, o)->keep_contents = true;
1462 /* Adjusting the internal symbols will not work if something has
1463 already retrieved the generic symbols. It would be possible to
1464 make this work by adjusting the generic symbols at the same time.
1465 However, this case should not arise in normal usage. */
1466 if (obj_symbols (abfd) != NULL
1467 || obj_raw_syments (abfd) != NULL)
1469 ((*_bfd_error_handler)
1470 ("%s: fatal: generic symbols retrieved before relaxing",
1471 bfd_archive_filename (abfd)));
1472 bfd_set_error (bfd_error_invalid_operation);
1476 /* Adjust all the symbols. */
1477 sym_hash = obj_coff_sym_hashes (abfd);
1478 symesz = bfd_coff_symesz (abfd);
1479 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1480 esymend = esym + obj_raw_syment_count (abfd) * symesz;
1481 while (esym < esymend)
1483 struct internal_syment isym;
1485 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &isym);
1487 if (isym.n_scnum == sec->target_index
1488 && (bfd_vma) isym.n_value > addr
1489 && (bfd_vma) isym.n_value < toaddr)
1491 isym.n_value -= count;
1493 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
1495 if (*sym_hash != NULL)
1497 BFD_ASSERT ((*sym_hash)->root.type == bfd_link_hash_defined
1498 || (*sym_hash)->root.type == bfd_link_hash_defweak);
1499 BFD_ASSERT ((*sym_hash)->root.u.def.value >= addr
1500 && (*sym_hash)->root.u.def.value < toaddr);
1501 (*sym_hash)->root.u.def.value -= count;
1505 esym += (isym.n_numaux + 1) * symesz;
1506 sym_hash += isym.n_numaux + 1;
1509 /* See if we can move the ALIGN reloc forward. We have adjusted
1510 r_vaddr for it already. */
1511 if (irelalign != NULL)
1513 bfd_vma alignto, alignaddr;
1515 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_offset);
1516 alignaddr = BFD_ALIGN (irelalign->r_vaddr - sec->vma,
1517 1 << irelalign->r_offset);
1518 if (alignto != alignaddr)
1520 /* Tail recursion. */
1521 return sh_relax_delete_bytes (abfd, sec, alignaddr,
1522 (int) (alignto - alignaddr));
1529 /* This is yet another version of the SH opcode table, used to rapidly
1530 get information about a particular instruction. */
1532 /* The opcode map is represented by an array of these structures. The
1533 array is indexed by the high order four bits in the instruction. */
1535 struct sh_major_opcode
1537 /* A pointer to the instruction list. This is an array which
1538 contains all the instructions with this major opcode. */
1539 const struct sh_minor_opcode *minor_opcodes;
1540 /* The number of elements in minor_opcodes. */
1541 unsigned short count;
1544 /* This structure holds information for a set of SH opcodes. The
1545 instruction code is anded with the mask value, and the resulting
1546 value is used to search the order opcode list. */
1548 struct sh_minor_opcode
1550 /* The sorted opcode list. */
1551 const struct sh_opcode *opcodes;
1552 /* The number of elements in opcodes. */
1553 unsigned short count;
1554 /* The mask value to use when searching the opcode list. */
1555 unsigned short mask;
1558 /* This structure holds information for an SH instruction. An array
1559 of these structures is sorted in order by opcode. */
1563 /* The code for this instruction, after it has been anded with the
1564 mask value in the sh_major_opcode structure. */
1565 unsigned short opcode;
1566 /* Flags for this instruction. */
1567 unsigned long flags;
1570 /* Flag which appear in the sh_opcode structure. */
1572 /* This instruction loads a value from memory. */
1575 /* This instruction stores a value to memory. */
1578 /* This instruction is a branch. */
1579 #define BRANCH (0x4)
1581 /* This instruction has a delay slot. */
1584 /* This instruction uses the value in the register in the field at
1585 mask 0x0f00 of the instruction. */
1586 #define USES1 (0x10)
1587 #define USES1_REG(x) ((x & 0x0f00) >> 8)
1589 /* This instruction uses the value in the register in the field at
1590 mask 0x00f0 of the instruction. */
1591 #define USES2 (0x20)
1592 #define USES2_REG(x) ((x & 0x00f0) >> 4)
1594 /* This instruction uses the value in register 0. */
1595 #define USESR0 (0x40)
1597 /* This instruction sets the value in the register in the field at
1598 mask 0x0f00 of the instruction. */
1599 #define SETS1 (0x80)
1600 #define SETS1_REG(x) ((x & 0x0f00) >> 8)
1602 /* This instruction sets the value in the register in the field at
1603 mask 0x00f0 of the instruction. */
1604 #define SETS2 (0x100)
1605 #define SETS2_REG(x) ((x & 0x00f0) >> 4)
1607 /* This instruction sets register 0. */
1608 #define SETSR0 (0x200)
1610 /* This instruction sets a special register. */
1611 #define SETSSP (0x400)
1613 /* This instruction uses a special register. */
1614 #define USESSP (0x800)
1616 /* This instruction uses the floating point register in the field at
1617 mask 0x0f00 of the instruction. */
1618 #define USESF1 (0x1000)
1619 #define USESF1_REG(x) ((x & 0x0f00) >> 8)
1621 /* This instruction uses the floating point register in the field at
1622 mask 0x00f0 of the instruction. */
1623 #define USESF2 (0x2000)
1624 #define USESF2_REG(x) ((x & 0x00f0) >> 4)
1626 /* This instruction uses floating point register 0. */
1627 #define USESF0 (0x4000)
1629 /* This instruction sets the floating point register in the field at
1630 mask 0x0f00 of the instruction. */
1631 #define SETSF1 (0x8000)
1632 #define SETSF1_REG(x) ((x & 0x0f00) >> 8)
1634 #define USESAS (0x10000)
1635 #define USESAS_REG(x) (((((x) >> 8) - 2) & 3) + 2)
1636 #define USESR8 (0x20000)
1637 #define SETSAS (0x40000)
1638 #define SETSAS_REG(x) USESAS_REG (x)
1640 #ifndef COFF_IMAGE_WITH_PE
1641 static boolean sh_insn_uses_reg
1642 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1643 static boolean sh_insn_sets_reg
1644 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1645 static boolean sh_insn_uses_or_sets_reg
1646 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1647 static boolean sh_insn_uses_freg
1648 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1649 static boolean sh_insn_sets_freg
1650 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1651 static boolean sh_insn_uses_or_sets_freg
1652 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int));
1653 static boolean sh_insns_conflict
1654 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int,
1655 const struct sh_opcode *));
1656 static boolean sh_load_use
1657 PARAMS ((unsigned int, const struct sh_opcode *, unsigned int,
1658 const struct sh_opcode *));
1660 /* The opcode maps. */
1662 #define MAP(a) a, sizeof a / sizeof a[0]
1664 static const struct sh_opcode sh_opcode00[] =
1666 { 0x0008, SETSSP }, /* clrt */
1667 { 0x0009, 0 }, /* nop */
1668 { 0x000b, BRANCH | DELAY | USESSP }, /* rts */
1669 { 0x0018, SETSSP }, /* sett */
1670 { 0x0019, SETSSP }, /* div0u */
1671 { 0x001b, 0 }, /* sleep */
1672 { 0x0028, SETSSP }, /* clrmac */
1673 { 0x002b, BRANCH | DELAY | SETSSP }, /* rte */
1674 { 0x0038, USESSP | SETSSP }, /* ldtlb */
1675 { 0x0048, SETSSP }, /* clrs */
1676 { 0x0058, SETSSP } /* sets */
1679 static const struct sh_opcode sh_opcode01[] =
1681 { 0x0003, BRANCH | DELAY | USES1 | SETSSP }, /* bsrf rn */
1682 { 0x000a, SETS1 | USESSP }, /* sts mach,rn */
1683 { 0x001a, SETS1 | USESSP }, /* sts macl,rn */
1684 { 0x0023, BRANCH | DELAY | USES1 }, /* braf rn */
1685 { 0x0029, SETS1 | USESSP }, /* movt rn */
1686 { 0x002a, SETS1 | USESSP }, /* sts pr,rn */
1687 { 0x005a, SETS1 | USESSP }, /* sts fpul,rn */
1688 { 0x006a, SETS1 | USESSP }, /* sts fpscr,rn / sts dsr,rn */
1689 { 0x0083, LOAD | USES1 }, /* pref @rn */
1690 { 0x007a, SETS1 | USESSP }, /* sts a0,rn */
1691 { 0x008a, SETS1 | USESSP }, /* sts x0,rn */
1692 { 0x009a, SETS1 | USESSP }, /* sts x1,rn */
1693 { 0x00aa, SETS1 | USESSP }, /* sts y0,rn */
1694 { 0x00ba, SETS1 | USESSP } /* sts y1,rn */
1697 /* These sixteen instructions can be handled with one table entry below. */
1699 { 0x0002, SETS1 | USESSP }, /* stc sr,rn */
1700 { 0x0012, SETS1 | USESSP }, /* stc gbr,rn */
1701 { 0x0022, SETS1 | USESSP }, /* stc vbr,rn */
1702 { 0x0032, SETS1 | USESSP }, /* stc ssr,rn */
1703 { 0x0042, SETS1 | USESSP }, /* stc spc,rn */
1704 { 0x0052, SETS1 | USESSP }, /* stc mod,rn */
1705 { 0x0062, SETS1 | USESSP }, /* stc rs,rn */
1706 { 0x0072, SETS1 | USESSP }, /* stc re,rn */
1707 { 0x0082, SETS1 | USESSP }, /* stc r0_bank,rn */
1708 { 0x0092, SETS1 | USESSP }, /* stc r1_bank,rn */
1709 { 0x00a2, SETS1 | USESSP }, /* stc r2_bank,rn */
1710 { 0x00b2, SETS1 | USESSP }, /* stc r3_bank,rn */
1711 { 0x00c2, SETS1 | USESSP }, /* stc r4_bank,rn */
1712 { 0x00d2, SETS1 | USESSP }, /* stc r5_bank,rn */
1713 { 0x00e2, SETS1 | USESSP }, /* stc r6_bank,rn */
1714 { 0x00f2, SETS1 | USESSP } /* stc r7_bank,rn */
1717 static const struct sh_opcode sh_opcode02[] =
1719 { 0x0002, SETS1 | USESSP }, /* stc <special_reg>,rn */
1720 { 0x0004, STORE | USES1 | USES2 | USESR0 }, /* mov.b rm,@(r0,rn) */
1721 { 0x0005, STORE | USES1 | USES2 | USESR0 }, /* mov.w rm,@(r0,rn) */
1722 { 0x0006, STORE | USES1 | USES2 | USESR0 }, /* mov.l rm,@(r0,rn) */
1723 { 0x0007, SETSSP | USES1 | USES2 }, /* mul.l rm,rn */
1724 { 0x000c, LOAD | SETS1 | USES2 | USESR0 }, /* mov.b @(r0,rm),rn */
1725 { 0x000d, LOAD | SETS1 | USES2 | USESR0 }, /* mov.w @(r0,rm),rn */
1726 { 0x000e, LOAD | SETS1 | USES2 | USESR0 }, /* mov.l @(r0,rm),rn */
1727 { 0x000f, LOAD|SETS1|SETS2|SETSSP|USES1|USES2|USESSP }, /* mac.l @rm+,@rn+ */
1730 static const struct sh_minor_opcode sh_opcode0[] =
1732 { MAP (sh_opcode00), 0xffff },
1733 { MAP (sh_opcode01), 0xf0ff },
1734 { MAP (sh_opcode02), 0xf00f }
1737 static const struct sh_opcode sh_opcode10[] =
1739 { 0x1000, STORE | USES1 | USES2 } /* mov.l rm,@(disp,rn) */
1742 static const struct sh_minor_opcode sh_opcode1[] =
1744 { MAP (sh_opcode10), 0xf000 }
1747 static const struct sh_opcode sh_opcode20[] =
1749 { 0x2000, STORE | USES1 | USES2 }, /* mov.b rm,@rn */
1750 { 0x2001, STORE | USES1 | USES2 }, /* mov.w rm,@rn */
1751 { 0x2002, STORE | USES1 | USES2 }, /* mov.l rm,@rn */
1752 { 0x2004, STORE | SETS1 | USES1 | USES2 }, /* mov.b rm,@-rn */
1753 { 0x2005, STORE | SETS1 | USES1 | USES2 }, /* mov.w rm,@-rn */
1754 { 0x2006, STORE | SETS1 | USES1 | USES2 }, /* mov.l rm,@-rn */
1755 { 0x2007, SETSSP | USES1 | USES2 | USESSP }, /* div0s */
1756 { 0x2008, SETSSP | USES1 | USES2 }, /* tst rm,rn */
1757 { 0x2009, SETS1 | USES1 | USES2 }, /* and rm,rn */
1758 { 0x200a, SETS1 | USES1 | USES2 }, /* xor rm,rn */
1759 { 0x200b, SETS1 | USES1 | USES2 }, /* or rm,rn */
1760 { 0x200c, SETSSP | USES1 | USES2 }, /* cmp/str rm,rn */
1761 { 0x200d, SETS1 | USES1 | USES2 }, /* xtrct rm,rn */
1762 { 0x200e, SETSSP | USES1 | USES2 }, /* mulu.w rm,rn */
1763 { 0x200f, SETSSP | USES1 | USES2 } /* muls.w rm,rn */
1766 static const struct sh_minor_opcode sh_opcode2[] =
1768 { MAP (sh_opcode20), 0xf00f }
1771 static const struct sh_opcode sh_opcode30[] =
1773 { 0x3000, SETSSP | USES1 | USES2 }, /* cmp/eq rm,rn */
1774 { 0x3002, SETSSP | USES1 | USES2 }, /* cmp/hs rm,rn */
1775 { 0x3003, SETSSP | USES1 | USES2 }, /* cmp/ge rm,rn */
1776 { 0x3004, SETSSP | USESSP | USES1 | USES2 }, /* div1 rm,rn */
1777 { 0x3005, SETSSP | USES1 | USES2 }, /* dmulu.l rm,rn */
1778 { 0x3006, SETSSP | USES1 | USES2 }, /* cmp/hi rm,rn */
1779 { 0x3007, SETSSP | USES1 | USES2 }, /* cmp/gt rm,rn */
1780 { 0x3008, SETS1 | USES1 | USES2 }, /* sub rm,rn */
1781 { 0x300a, SETS1 | SETSSP | USES1 | USES2 | USESSP }, /* subc rm,rn */
1782 { 0x300b, SETS1 | SETSSP | USES1 | USES2 }, /* subv rm,rn */
1783 { 0x300c, SETS1 | USES1 | USES2 }, /* add rm,rn */
1784 { 0x300d, SETSSP | USES1 | USES2 }, /* dmuls.l rm,rn */
1785 { 0x300e, SETS1 | SETSSP | USES1 | USES2 | USESSP }, /* addc rm,rn */
1786 { 0x300f, SETS1 | SETSSP | USES1 | USES2 } /* addv rm,rn */
1789 static const struct sh_minor_opcode sh_opcode3[] =
1791 { MAP (sh_opcode30), 0xf00f }
1794 static const struct sh_opcode sh_opcode40[] =
1796 { 0x4000, SETS1 | SETSSP | USES1 }, /* shll rn */
1797 { 0x4001, SETS1 | SETSSP | USES1 }, /* shlr rn */
1798 { 0x4002, STORE | SETS1 | USES1 | USESSP }, /* sts.l mach,@-rn */
1799 { 0x4004, SETS1 | SETSSP | USES1 }, /* rotl rn */
1800 { 0x4005, SETS1 | SETSSP | USES1 }, /* rotr rn */
1801 { 0x4006, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,mach */
1802 { 0x4008, SETS1 | USES1 }, /* shll2 rn */
1803 { 0x4009, SETS1 | USES1 }, /* shlr2 rn */
1804 { 0x400a, SETSSP | USES1 }, /* lds rm,mach */
1805 { 0x400b, BRANCH | DELAY | USES1 }, /* jsr @rn */
1806 { 0x4010, SETS1 | SETSSP | USES1 }, /* dt rn */
1807 { 0x4011, SETSSP | USES1 }, /* cmp/pz rn */
1808 { 0x4012, STORE | SETS1 | USES1 | USESSP }, /* sts.l macl,@-rn */
1809 { 0x4014, SETSSP | USES1 }, /* setrc rm */
1810 { 0x4015, SETSSP | USES1 }, /* cmp/pl rn */
1811 { 0x4016, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,macl */
1812 { 0x4018, SETS1 | USES1 }, /* shll8 rn */
1813 { 0x4019, SETS1 | USES1 }, /* shlr8 rn */
1814 { 0x401a, SETSSP | USES1 }, /* lds rm,macl */
1815 { 0x401b, LOAD | SETSSP | USES1 }, /* tas.b @rn */
1816 { 0x4020, SETS1 | SETSSP | USES1 }, /* shal rn */
1817 { 0x4021, SETS1 | SETSSP | USES1 }, /* shar rn */
1818 { 0x4022, STORE | SETS1 | USES1 | USESSP }, /* sts.l pr,@-rn */
1819 { 0x4024, SETS1 | SETSSP | USES1 | USESSP }, /* rotcl rn */
1820 { 0x4025, SETS1 | SETSSP | USES1 | USESSP }, /* rotcr rn */
1821 { 0x4026, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,pr */
1822 { 0x4028, SETS1 | USES1 }, /* shll16 rn */
1823 { 0x4029, SETS1 | USES1 }, /* shlr16 rn */
1824 { 0x402a, SETSSP | USES1 }, /* lds rm,pr */
1825 { 0x402b, BRANCH | DELAY | USES1 }, /* jmp @rn */
1826 { 0x4052, STORE | SETS1 | USES1 | USESSP }, /* sts.l fpul,@-rn */
1827 { 0x4056, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,fpul */
1828 { 0x405a, SETSSP | USES1 }, /* lds.l rm,fpul */
1829 { 0x4062, STORE | SETS1 | USES1 | USESSP }, /* sts.l fpscr / dsr,@-rn */
1830 { 0x4066, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,fpscr / dsr */
1831 { 0x406a, SETSSP | USES1 }, /* lds rm,fpscr / lds rm,dsr */
1832 { 0x4072, STORE | SETS1 | USES1 | USESSP }, /* sts.l a0,@-rn */
1833 { 0x4076, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,a0 */
1834 { 0x407a, SETSSP | USES1 }, /* lds.l rm,a0 */
1835 { 0x4082, STORE | SETS1 | USES1 | USESSP }, /* sts.l x0,@-rn */
1836 { 0x4086, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,x0 */
1837 { 0x408a, SETSSP | USES1 }, /* lds.l rm,x0 */
1838 { 0x4092, STORE | SETS1 | USES1 | USESSP }, /* sts.l x1,@-rn */
1839 { 0x4096, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,x1 */
1840 { 0x409a, SETSSP | USES1 }, /* lds.l rm,x1 */
1841 { 0x40a2, STORE | SETS1 | USES1 | USESSP }, /* sts.l y0,@-rn */
1842 { 0x40a6, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,y0 */
1843 { 0x40aa, SETSSP | USES1 }, /* lds.l rm,y0 */
1844 { 0x40b2, STORE | SETS1 | USES1 | USESSP }, /* sts.l y1,@-rn */
1845 { 0x40b6, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,y1 */
1846 { 0x40ba, SETSSP | USES1 } /* lds.l rm,y1 */
1847 #if 0 /* These groups sixteen insns can be
1848 handled with one table entry each below. */
1849 { 0x4003, STORE | SETS1 | USES1 | USESSP }, /* stc.l sr,@-rn */
1850 { 0x4013, STORE | SETS1 | USES1 | USESSP }, /* stc.l gbr,@-rn */
1851 { 0x4023, STORE | SETS1 | USES1 | USESSP }, /* stc.l vbr,@-rn */
1852 { 0x4033, STORE | SETS1 | USES1 | USESSP }, /* stc.l ssr,@-rn */
1853 { 0x4043, STORE | SETS1 | USES1 | USESSP }, /* stc.l spc,@-rn */
1854 { 0x4053, STORE | SETS1 | USES1 | USESSP }, /* stc.l mod,@-rn */
1855 { 0x4063, STORE | SETS1 | USES1 | USESSP }, /* stc.l rs,@-rn */
1856 { 0x4073, STORE | SETS1 | USES1 | USESSP }, /* stc.l re,@-rn */
1857 { 0x4083, STORE | SETS1 | USES1 | USESSP }, /* stc.l r0_bank,@-rn */
1859 { 0x40f3, STORE | SETS1 | USES1 | USESSP }, /* stc.l r7_bank,@-rn */
1861 { 0x4007, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,sr */
1862 { 0x4017, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,gbr */
1863 { 0x4027, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,vbr */
1864 { 0x4037, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,ssr */
1865 { 0x4047, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,spc */
1866 { 0x4057, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,mod */
1867 { 0x4067, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,rs */
1868 { 0x4077, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,re */
1869 { 0x4087, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,r0_bank */
1871 { 0x40f7, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,r7_bank */
1873 { 0x400e, SETSSP | USES1 }, /* ldc rm,sr */
1874 { 0x401e, SETSSP | USES1 }, /* ldc rm,gbr */
1875 { 0x402e, SETSSP | USES1 }, /* ldc rm,vbr */
1876 { 0x403e, SETSSP | USES1 }, /* ldc rm,ssr */
1877 { 0x404e, SETSSP | USES1 }, /* ldc rm,spc */
1878 { 0x405e, SETSSP | USES1 }, /* ldc rm,mod */
1879 { 0x406e, SETSSP | USES1 }, /* ldc rm,rs */
1880 { 0x407e, SETSSP | USES1 } /* ldc rm,re */
1881 { 0x408e, SETSSP | USES1 } /* ldc rm,r0_bank */
1883 { 0x40fe, SETSSP | USES1 } /* ldc rm,r7_bank */
1887 static const struct sh_opcode sh_opcode41[] =
1889 { 0x4003, STORE | SETS1 | USES1 | USESSP }, /* stc.l <special_reg>,@-rn */
1890 { 0x4007, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,<special_reg> */
1891 { 0x400c, SETS1 | USES1 | USES2 }, /* shad rm,rn */
1892 { 0x400d, SETS1 | USES1 | USES2 }, /* shld rm,rn */
1893 { 0x400e, SETSSP | USES1 }, /* ldc rm,<special_reg> */
1894 { 0x400f, LOAD|SETS1|SETS2|SETSSP|USES1|USES2|USESSP }, /* mac.w @rm+,@rn+ */
1897 static const struct sh_minor_opcode sh_opcode4[] =
1899 { MAP (sh_opcode40), 0xf0ff },
1900 { MAP (sh_opcode41), 0xf00f }
1903 static const struct sh_opcode sh_opcode50[] =
1905 { 0x5000, LOAD | SETS1 | USES2 } /* mov.l @(disp,rm),rn */
1908 static const struct sh_minor_opcode sh_opcode5[] =
1910 { MAP (sh_opcode50), 0xf000 }
1913 static const struct sh_opcode sh_opcode60[] =
1915 { 0x6000, LOAD | SETS1 | USES2 }, /* mov.b @rm,rn */
1916 { 0x6001, LOAD | SETS1 | USES2 }, /* mov.w @rm,rn */
1917 { 0x6002, LOAD | SETS1 | USES2 }, /* mov.l @rm,rn */
1918 { 0x6003, SETS1 | USES2 }, /* mov rm,rn */
1919 { 0x6004, LOAD | SETS1 | SETS2 | USES2 }, /* mov.b @rm+,rn */
1920 { 0x6005, LOAD | SETS1 | SETS2 | USES2 }, /* mov.w @rm+,rn */
1921 { 0x6006, LOAD | SETS1 | SETS2 | USES2 }, /* mov.l @rm+,rn */
1922 { 0x6007, SETS1 | USES2 }, /* not rm,rn */
1923 { 0x6008, SETS1 | USES2 }, /* swap.b rm,rn */
1924 { 0x6009, SETS1 | USES2 }, /* swap.w rm,rn */
1925 { 0x600a, SETS1 | SETSSP | USES2 | USESSP }, /* negc rm,rn */
1926 { 0x600b, SETS1 | USES2 }, /* neg rm,rn */
1927 { 0x600c, SETS1 | USES2 }, /* extu.b rm,rn */
1928 { 0x600d, SETS1 | USES2 }, /* extu.w rm,rn */
1929 { 0x600e, SETS1 | USES2 }, /* exts.b rm,rn */
1930 { 0x600f, SETS1 | USES2 } /* exts.w rm,rn */
1933 static const struct sh_minor_opcode sh_opcode6[] =
1935 { MAP (sh_opcode60), 0xf00f }
1938 static const struct sh_opcode sh_opcode70[] =
1940 { 0x7000, SETS1 | USES1 } /* add #imm,rn */
1943 static const struct sh_minor_opcode sh_opcode7[] =
1945 { MAP (sh_opcode70), 0xf000 }
1948 static const struct sh_opcode sh_opcode80[] =
1950 { 0x8000, STORE | USES2 | USESR0 }, /* mov.b r0,@(disp,rn) */
1951 { 0x8100, STORE | USES2 | USESR0 }, /* mov.w r0,@(disp,rn) */
1952 { 0x8200, SETSSP }, /* setrc #imm */
1953 { 0x8400, LOAD | SETSR0 | USES2 }, /* mov.b @(disp,rm),r0 */
1954 { 0x8500, LOAD | SETSR0 | USES2 }, /* mov.w @(disp,rn),r0 */
1955 { 0x8800, SETSSP | USESR0 }, /* cmp/eq #imm,r0 */
1956 { 0x8900, BRANCH | USESSP }, /* bt label */
1957 { 0x8b00, BRANCH | USESSP }, /* bf label */
1958 { 0x8c00, SETSSP }, /* ldrs @(disp,pc) */
1959 { 0x8d00, BRANCH | DELAY | USESSP }, /* bt/s label */
1960 { 0x8e00, SETSSP }, /* ldre @(disp,pc) */
1961 { 0x8f00, BRANCH | DELAY | USESSP } /* bf/s label */
1964 static const struct sh_minor_opcode sh_opcode8[] =
1966 { MAP (sh_opcode80), 0xff00 }
1969 static const struct sh_opcode sh_opcode90[] =
1971 { 0x9000, LOAD | SETS1 } /* mov.w @(disp,pc),rn */
1974 static const struct sh_minor_opcode sh_opcode9[] =
1976 { MAP (sh_opcode90), 0xf000 }
1979 static const struct sh_opcode sh_opcodea0[] =
1981 { 0xa000, BRANCH | DELAY } /* bra label */
1984 static const struct sh_minor_opcode sh_opcodea[] =
1986 { MAP (sh_opcodea0), 0xf000 }
1989 static const struct sh_opcode sh_opcodeb0[] =
1991 { 0xb000, BRANCH | DELAY } /* bsr label */
1994 static const struct sh_minor_opcode sh_opcodeb[] =
1996 { MAP (sh_opcodeb0), 0xf000 }
1999 static const struct sh_opcode sh_opcodec0[] =
2001 { 0xc000, STORE | USESR0 | USESSP }, /* mov.b r0,@(disp,gbr) */
2002 { 0xc100, STORE | USESR0 | USESSP }, /* mov.w r0,@(disp,gbr) */
2003 { 0xc200, STORE | USESR0 | USESSP }, /* mov.l r0,@(disp,gbr) */
2004 { 0xc300, BRANCH | USESSP }, /* trapa #imm */
2005 { 0xc400, LOAD | SETSR0 | USESSP }, /* mov.b @(disp,gbr),r0 */
2006 { 0xc500, LOAD | SETSR0 | USESSP }, /* mov.w @(disp,gbr),r0 */
2007 { 0xc600, LOAD | SETSR0 | USESSP }, /* mov.l @(disp,gbr),r0 */
2008 { 0xc700, SETSR0 }, /* mova @(disp,pc),r0 */
2009 { 0xc800, SETSSP | USESR0 }, /* tst #imm,r0 */
2010 { 0xc900, SETSR0 | USESR0 }, /* and #imm,r0 */
2011 { 0xca00, SETSR0 | USESR0 }, /* xor #imm,r0 */
2012 { 0xcb00, SETSR0 | USESR0 }, /* or #imm,r0 */
2013 { 0xcc00, LOAD | SETSSP | USESR0 | USESSP }, /* tst.b #imm,@(r0,gbr) */
2014 { 0xcd00, LOAD | STORE | USESR0 | USESSP }, /* and.b #imm,@(r0,gbr) */
2015 { 0xce00, LOAD | STORE | USESR0 | USESSP }, /* xor.b #imm,@(r0,gbr) */
2016 { 0xcf00, LOAD | STORE | USESR0 | USESSP } /* or.b #imm,@(r0,gbr) */
2019 static const struct sh_minor_opcode sh_opcodec[] =
2021 { MAP (sh_opcodec0), 0xff00 }
2024 static const struct sh_opcode sh_opcoded0[] =
2026 { 0xd000, LOAD | SETS1 } /* mov.l @(disp,pc),rn */
2029 static const struct sh_minor_opcode sh_opcoded[] =
2031 { MAP (sh_opcoded0), 0xf000 }
2034 static const struct sh_opcode sh_opcodee0[] =
2036 { 0xe000, SETS1 } /* mov #imm,rn */
2039 static const struct sh_minor_opcode sh_opcodee[] =
2041 { MAP (sh_opcodee0), 0xf000 }
2044 static const struct sh_opcode sh_opcodef0[] =
2046 { 0xf000, SETSF1 | USESF1 | USESF2 }, /* fadd fm,fn */
2047 { 0xf001, SETSF1 | USESF1 | USESF2 }, /* fsub fm,fn */
2048 { 0xf002, SETSF1 | USESF1 | USESF2 }, /* fmul fm,fn */
2049 { 0xf003, SETSF1 | USESF1 | USESF2 }, /* fdiv fm,fn */
2050 { 0xf004, SETSSP | USESF1 | USESF2 }, /* fcmp/eq fm,fn */
2051 { 0xf005, SETSSP | USESF1 | USESF2 }, /* fcmp/gt fm,fn */
2052 { 0xf006, LOAD | SETSF1 | USES2 | USESR0 }, /* fmov.s @(r0,rm),fn */
2053 { 0xf007, STORE | USES1 | USESF2 | USESR0 }, /* fmov.s fm,@(r0,rn) */
2054 { 0xf008, LOAD | SETSF1 | USES2 }, /* fmov.s @rm,fn */
2055 { 0xf009, LOAD | SETS2 | SETSF1 | USES2 }, /* fmov.s @rm+,fn */
2056 { 0xf00a, STORE | USES1 | USESF2 }, /* fmov.s fm,@rn */
2057 { 0xf00b, STORE | SETS1 | USES1 | USESF2 }, /* fmov.s fm,@-rn */
2058 { 0xf00c, SETSF1 | USESF2 }, /* fmov fm,fn */
2059 { 0xf00e, SETSF1 | USESF1 | USESF2 | USESF0 } /* fmac f0,fm,fn */
2062 static const struct sh_opcode sh_opcodef1[] =
2064 { 0xf00d, SETSF1 | USESSP }, /* fsts fpul,fn */
2065 { 0xf01d, SETSSP | USESF1 }, /* flds fn,fpul */
2066 { 0xf02d, SETSF1 | USESSP }, /* float fpul,fn */
2067 { 0xf03d, SETSSP | USESF1 }, /* ftrc fn,fpul */
2068 { 0xf04d, SETSF1 | USESF1 }, /* fneg fn */
2069 { 0xf05d, SETSF1 | USESF1 }, /* fabs fn */
2070 { 0xf06d, SETSF1 | USESF1 }, /* fsqrt fn */
2071 { 0xf07d, SETSSP | USESF1 }, /* ftst/nan fn */
2072 { 0xf08d, SETSF1 }, /* fldi0 fn */
2073 { 0xf09d, SETSF1 } /* fldi1 fn */
2076 static const struct sh_minor_opcode sh_opcodef[] =
2078 { MAP (sh_opcodef0), 0xf00f },
2079 { MAP (sh_opcodef1), 0xf0ff }
2082 #ifndef COFF_IMAGE_WITH_PE
2083 static struct sh_major_opcode sh_opcodes[] =
2085 { MAP (sh_opcode0) },
2086 { MAP (sh_opcode1) },
2087 { MAP (sh_opcode2) },
2088 { MAP (sh_opcode3) },
2089 { MAP (sh_opcode4) },
2090 { MAP (sh_opcode5) },
2091 { MAP (sh_opcode6) },
2092 { MAP (sh_opcode7) },
2093 { MAP (sh_opcode8) },
2094 { MAP (sh_opcode9) },
2095 { MAP (sh_opcodea) },
2096 { MAP (sh_opcodeb) },
2097 { MAP (sh_opcodec) },
2098 { MAP (sh_opcoded) },
2099 { MAP (sh_opcodee) },
2100 { MAP (sh_opcodef) }
2104 /* The double data transfer / parallel processing insns are not
2105 described here. This will cause sh_align_load_span to leave them alone. */
2107 static const struct sh_opcode sh_dsp_opcodef0[] =
2109 { 0xf400, USESAS | SETSAS | LOAD | SETSSP }, /* movs.x @-as,ds */
2110 { 0xf401, USESAS | SETSAS | STORE | USESSP }, /* movs.x ds,@-as */
2111 { 0xf404, USESAS | LOAD | SETSSP }, /* movs.x @as,ds */
2112 { 0xf405, USESAS | STORE | USESSP }, /* movs.x ds,@as */
2113 { 0xf408, USESAS | SETSAS | LOAD | SETSSP }, /* movs.x @as+,ds */
2114 { 0xf409, USESAS | SETSAS | STORE | USESSP }, /* movs.x ds,@as+ */
2115 { 0xf40c, USESAS | SETSAS | LOAD | SETSSP | USESR8 }, /* movs.x @as+r8,ds */
2116 { 0xf40d, USESAS | SETSAS | STORE | USESSP | USESR8 } /* movs.x ds,@as+r8 */
2119 static const struct sh_minor_opcode sh_dsp_opcodef[] =
2121 { MAP (sh_dsp_opcodef0), 0xfc0d }
2124 #ifndef COFF_IMAGE_WITH_PE
2125 /* Given an instruction, return a pointer to the corresponding
2126 sh_opcode structure. Return NULL if the instruction is not
2129 static const struct sh_opcode *
2133 const struct sh_major_opcode *maj;
2134 const struct sh_minor_opcode *min, *minend;
2136 maj = &sh_opcodes[(insn & 0xf000) >> 12];
2137 min = maj->minor_opcodes;
2138 minend = min + maj->count;
2139 for (; min < minend; min++)
2142 const struct sh_opcode *op, *opend;
2144 l = insn & min->mask;
2146 opend = op + min->count;
2148 /* Since the opcodes tables are sorted, we could use a binary
2149 search here if the count were above some cutoff value. */
2150 for (; op < opend; op++)
2151 if (op->opcode == l)
2158 /* See whether an instruction uses or sets a general purpose register */
2161 sh_insn_uses_or_sets_reg (insn, op, reg)
2163 const struct sh_opcode *op;
2166 if (sh_insn_uses_reg (insn, op, reg))
2169 return sh_insn_sets_reg (insn, op, reg);
2172 /* See whether an instruction uses a general purpose register. */
2175 sh_insn_uses_reg (insn, op, reg)
2177 const struct sh_opcode *op;
2184 if ((f & USES1) != 0
2185 && USES1_REG (insn) == reg)
2187 if ((f & USES2) != 0
2188 && USES2_REG (insn) == reg)
2190 if ((f & USESR0) != 0
2193 if ((f & USESAS) && reg == USESAS_REG (insn))
2195 if ((f & USESR8) && reg == 8)
2201 /* See whether an instruction sets a general purpose register. */
2204 sh_insn_sets_reg (insn, op, reg)
2206 const struct sh_opcode *op;
2213 if ((f & SETS1) != 0
2214 && SETS1_REG (insn) == reg)
2216 if ((f & SETS2) != 0
2217 && SETS2_REG (insn) == reg)
2219 if ((f & SETSR0) != 0
2222 if ((f & SETSAS) && reg == SETSAS_REG (insn))
2228 /* See whether an instruction uses or sets a floating point register */
2231 sh_insn_uses_or_sets_freg (insn, op, reg)
2233 const struct sh_opcode *op;
2236 if (sh_insn_uses_freg (insn, op, reg))
2239 return sh_insn_sets_freg (insn, op, reg);
2242 /* See whether an instruction uses a floating point register. */
2245 sh_insn_uses_freg (insn, op, freg)
2247 const struct sh_opcode *op;
2254 /* We can't tell if this is a double-precision insn, so just play safe
2255 and assume that it might be. So not only have we test FREG against
2256 itself, but also even FREG against FREG+1 - if the using insn uses
2257 just the low part of a double precision value - but also an odd
2258 FREG against FREG-1 - if the setting insn sets just the low part
2259 of a double precision value.
2260 So what this all boils down to is that we have to ignore the lowest
2261 bit of the register number. */
2263 if ((f & USESF1) != 0
2264 && (USESF1_REG (insn) & 0xe) == (freg & 0xe))
2266 if ((f & USESF2) != 0
2267 && (USESF2_REG (insn) & 0xe) == (freg & 0xe))
2269 if ((f & USESF0) != 0
2276 /* See whether an instruction sets a floating point register. */
2279 sh_insn_sets_freg (insn, op, freg)
2281 const struct sh_opcode *op;
2288 /* We can't tell if this is a double-precision insn, so just play safe
2289 and assume that it might be. So not only have we test FREG against
2290 itself, but also even FREG against FREG+1 - if the using insn uses
2291 just the low part of a double precision value - but also an odd
2292 FREG against FREG-1 - if the setting insn sets just the low part
2293 of a double precision value.
2294 So what this all boils down to is that we have to ignore the lowest
2295 bit of the register number. */
2297 if ((f & SETSF1) != 0
2298 && (SETSF1_REG (insn) & 0xe) == (freg & 0xe))
2304 /* See whether instructions I1 and I2 conflict, assuming I1 comes
2305 before I2. OP1 and OP2 are the corresponding sh_opcode structures.
2306 This should return true if there is a conflict, or false if the
2307 instructions can be swapped safely. */
2310 sh_insns_conflict (i1, op1, i2, op2)
2312 const struct sh_opcode *op1;
2314 const struct sh_opcode *op2;
2316 unsigned int f1, f2;
2321 /* Load of fpscr conflicts with floating point operations.
2322 FIXME: shouldn't test raw opcodes here. */
2323 if (((i1 & 0xf0ff) == 0x4066 && (i2 & 0xf000) == 0xf000)
2324 || ((i2 & 0xf0ff) == 0x4066 && (i1 & 0xf000) == 0xf000))
2327 if ((f1 & (BRANCH | DELAY)) != 0
2328 || (f2 & (BRANCH | DELAY)) != 0)
2331 if (((f1 | f2) & SETSSP)
2332 && (f1 & (SETSSP | USESSP))
2333 && (f2 & (SETSSP | USESSP)))
2336 if ((f1 & SETS1) != 0
2337 && sh_insn_uses_or_sets_reg (i2, op2, SETS1_REG (i1)))
2339 if ((f1 & SETS2) != 0
2340 && sh_insn_uses_or_sets_reg (i2, op2, SETS2_REG (i1)))
2342 if ((f1 & SETSR0) != 0
2343 && sh_insn_uses_or_sets_reg (i2, op2, 0))
2346 && sh_insn_uses_or_sets_reg (i2, op2, SETSAS_REG (i1)))
2348 if ((f1 & SETSF1) != 0
2349 && sh_insn_uses_or_sets_freg (i2, op2, SETSF1_REG (i1)))
2352 if ((f2 & SETS1) != 0
2353 && sh_insn_uses_or_sets_reg (i1, op1, SETS1_REG (i2)))
2355 if ((f2 & SETS2) != 0
2356 && sh_insn_uses_or_sets_reg (i1, op1, SETS2_REG (i2)))
2358 if ((f2 & SETSR0) != 0
2359 && sh_insn_uses_or_sets_reg (i1, op1, 0))
2362 && sh_insn_uses_or_sets_reg (i1, op1, SETSAS_REG (i2)))
2364 if ((f2 & SETSF1) != 0
2365 && sh_insn_uses_or_sets_freg (i1, op1, SETSF1_REG (i2)))
2368 /* The instructions do not conflict. */
2372 /* I1 is a load instruction, and I2 is some other instruction. Return
2373 true if I1 loads a register which I2 uses. */
2376 sh_load_use (i1, op1, i2, op2)
2378 const struct sh_opcode *op1;
2380 const struct sh_opcode *op2;
2386 if ((f1 & LOAD) == 0)
2389 /* If both SETS1 and SETSSP are set, that means a load to a special
2390 register using postincrement addressing mode, which we don't care
2392 if ((f1 & SETS1) != 0
2393 && (f1 & SETSSP) == 0
2394 && sh_insn_uses_reg (i2, op2, (i1 & 0x0f00) >> 8))
2397 if ((f1 & SETSR0) != 0
2398 && sh_insn_uses_reg (i2, op2, 0))
2401 if ((f1 & SETSF1) != 0
2402 && sh_insn_uses_freg (i2, op2, (i1 & 0x0f00) >> 8))
2408 /* Try to align loads and stores within a span of memory. This is
2409 called by both the ELF and the COFF sh targets. ABFD and SEC are
2410 the BFD and section we are examining. CONTENTS is the contents of
2411 the section. SWAP is the routine to call to swap two instructions.
2412 RELOCS is a pointer to the internal relocation information, to be
2413 passed to SWAP. PLABEL is a pointer to the current label in a
2414 sorted list of labels; LABEL_END is the end of the list. START and
2415 STOP are the range of memory to examine. If a swap is made,
2416 *PSWAPPED is set to true. */
2422 _bfd_sh_align_load_span (abfd, sec, contents, swap, relocs,
2423 plabel, label_end, start, stop, pswapped)
2427 boolean (*swap) PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
2435 int dsp = (abfd->arch_info->mach == bfd_mach_sh_dsp
2436 || abfd->arch_info->mach == bfd_mach_sh3_dsp);
2439 /* The SH4 has a Harvard architecture, hence aligning loads is not
2440 desirable. In fact, it is counter-productive, since it interferes
2441 with the schedules generated by the compiler. */
2442 if (abfd->arch_info->mach == bfd_mach_sh4)
2445 /* If we are linking sh[3]-dsp code, swap the FPU instructions for DSP
2449 sh_opcodes[0xf].minor_opcodes = sh_dsp_opcodef;
2450 sh_opcodes[0xf].count = sizeof sh_dsp_opcodef / sizeof sh_dsp_opcodef;
2453 /* Instructions should be aligned on 2 byte boundaries. */
2454 if ((start & 1) == 1)
2457 /* Now look through the unaligned addresses. */
2461 for (; i < stop; i += 4)
2464 const struct sh_opcode *op;
2465 unsigned int prev_insn = 0;
2466 const struct sh_opcode *prev_op = NULL;
2468 insn = bfd_get_16 (abfd, contents + i);
2469 op = sh_insn_info (insn);
2471 || (op->flags & (LOAD | STORE)) == 0)
2474 /* This is a load or store which is not on a four byte boundary. */
2476 while (*plabel < label_end && **plabel < i)
2481 prev_insn = bfd_get_16 (abfd, contents + i - 2);
2482 /* If INSN is the field b of a parallel processing insn, it is not
2483 a load / store after all. Note that the test here might mistake
2484 the field_b of a pcopy insn for the starting code of a parallel
2485 processing insn; this might miss a swapping opportunity, but at
2486 least we're on the safe side. */
2487 if (dsp && (prev_insn & 0xfc00) == 0xf800)
2490 /* Check if prev_insn is actually the field b of a parallel
2491 processing insn. Again, this can give a spurious match
2493 if (dsp && i - 2 > start)
2495 unsigned pprev_insn = bfd_get_16 (abfd, contents + i - 4);
2497 if ((pprev_insn & 0xfc00) == 0xf800)
2500 prev_op = sh_insn_info (prev_insn);
2503 prev_op = sh_insn_info (prev_insn);
2505 /* If the load/store instruction is in a delay slot, we
2508 || (prev_op->flags & DELAY) != 0)
2512 && (*plabel >= label_end || **plabel != i)
2514 && (prev_op->flags & (LOAD | STORE)) == 0
2515 && ! sh_insns_conflict (prev_insn, prev_op, insn, op))
2519 /* The load/store instruction does not have a label, and
2520 there is a previous instruction; PREV_INSN is not
2521 itself a load/store instruction, and PREV_INSN and
2522 INSN do not conflict. */
2528 unsigned int prev2_insn;
2529 const struct sh_opcode *prev2_op;
2531 prev2_insn = bfd_get_16 (abfd, contents + i - 4);
2532 prev2_op = sh_insn_info (prev2_insn);
2534 /* If the instruction before PREV_INSN has a delay
2535 slot--that is, PREV_INSN is in a delay slot--we
2537 if (prev2_op == NULL
2538 || (prev2_op->flags & DELAY) != 0)
2541 /* If the instruction before PREV_INSN is a load,
2542 and it sets a register which INSN uses, then
2543 putting INSN immediately after PREV_INSN will
2544 cause a pipeline bubble, so there is no point to
2547 && (prev2_op->flags & LOAD) != 0
2548 && sh_load_use (prev2_insn, prev2_op, insn, op))
2554 if (! (*swap) (abfd, sec, relocs, contents, i - 2))
2561 while (*plabel < label_end && **plabel < i + 2)
2565 && (*plabel >= label_end || **plabel != i + 2))
2567 unsigned int next_insn;
2568 const struct sh_opcode *next_op;
2570 /* There is an instruction after the load/store
2571 instruction, and it does not have a label. */
2572 next_insn = bfd_get_16 (abfd, contents + i + 2);
2573 next_op = sh_insn_info (next_insn);
2575 && (next_op->flags & (LOAD | STORE)) == 0
2576 && ! sh_insns_conflict (insn, op, next_insn, next_op))
2580 /* NEXT_INSN is not itself a load/store instruction,
2581 and it does not conflict with INSN. */
2585 /* If PREV_INSN is a load, and it sets a register
2586 which NEXT_INSN uses, then putting NEXT_INSN
2587 immediately after PREV_INSN will cause a pipeline
2588 bubble, so there is no reason to make this swap. */
2590 && (prev_op->flags & LOAD) != 0
2591 && sh_load_use (prev_insn, prev_op, next_insn, next_op))
2594 /* If INSN is a load, and it sets a register which
2595 the insn after NEXT_INSN uses, then doing the
2596 swap will cause a pipeline bubble, so there is no
2597 reason to make the swap. However, if the insn
2598 after NEXT_INSN is itself a load or store
2599 instruction, then it is misaligned, so
2600 optimistically hope that it will be swapped
2601 itself, and just live with the pipeline bubble if
2605 && (op->flags & LOAD) != 0)
2607 unsigned int next2_insn;
2608 const struct sh_opcode *next2_op;
2610 next2_insn = bfd_get_16 (abfd, contents + i + 4);
2611 next2_op = sh_insn_info (next2_insn);
2612 if ((next2_op->flags & (LOAD | STORE)) == 0
2613 && sh_load_use (insn, op, next2_insn, next2_op))
2619 if (! (*swap) (abfd, sec, relocs, contents, i))
2630 #endif /* not COFF_IMAGE_WITH_PE */
2632 /* Look for loads and stores which we can align to four byte
2633 boundaries. See the longer comment above sh_relax_section for why
2634 this is desirable. This sets *PSWAPPED if some instruction was
2638 sh_align_loads (abfd, sec, internal_relocs, contents, pswapped)
2641 struct internal_reloc *internal_relocs;
2645 struct internal_reloc *irel, *irelend;
2646 bfd_vma *labels = NULL;
2647 bfd_vma *label, *label_end;
2652 irelend = internal_relocs + sec->reloc_count;
2654 /* Get all the addresses with labels on them. */
2655 amt = (bfd_size_type) sec->reloc_count * sizeof (bfd_vma);
2656 labels = (bfd_vma *) bfd_malloc (amt);
2660 for (irel = internal_relocs; irel < irelend; irel++)
2662 if (irel->r_type == R_SH_LABEL)
2664 *label_end = irel->r_vaddr - sec->vma;
2669 /* Note that the assembler currently always outputs relocs in
2670 address order. If that ever changes, this code will need to sort
2671 the label values and the relocs. */
2675 for (irel = internal_relocs; irel < irelend; irel++)
2677 bfd_vma start, stop;
2679 if (irel->r_type != R_SH_CODE)
2682 start = irel->r_vaddr - sec->vma;
2684 for (irel++; irel < irelend; irel++)
2685 if (irel->r_type == R_SH_DATA)
2688 stop = irel->r_vaddr - sec->vma;
2690 stop = sec->_cooked_size;
2692 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_swap_insns,
2693 (PTR) internal_relocs, &label,
2694 label_end, start, stop, pswapped))
2708 /* Swap two SH instructions. */
2711 sh_swap_insns (abfd, sec, relocs, contents, addr)
2718 struct internal_reloc *internal_relocs = (struct internal_reloc *) relocs;
2719 unsigned short i1, i2;
2720 struct internal_reloc *irel, *irelend;
2722 /* Swap the instructions themselves. */
2723 i1 = bfd_get_16 (abfd, contents + addr);
2724 i2 = bfd_get_16 (abfd, contents + addr + 2);
2725 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
2726 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
2728 /* Adjust all reloc addresses. */
2729 irelend = internal_relocs + sec->reloc_count;
2730 for (irel = internal_relocs; irel < irelend; irel++)
2734 /* There are a few special types of relocs that we don't want to
2735 adjust. These relocs do not apply to the instruction itself,
2736 but are only associated with the address. */
2737 type = irel->r_type;
2738 if (type == R_SH_ALIGN
2739 || type == R_SH_CODE
2740 || type == R_SH_DATA
2741 || type == R_SH_LABEL)
2744 /* If an R_SH_USES reloc points to one of the addresses being
2745 swapped, we must adjust it. It would be incorrect to do this
2746 for a jump, though, since we want to execute both
2747 instructions after the jump. (We have avoided swapping
2748 around a label, so the jump will not wind up executing an
2749 instruction it shouldn't). */
2750 if (type == R_SH_USES)
2754 off = irel->r_vaddr - sec->vma + 4 + irel->r_offset;
2756 irel->r_offset += 2;
2757 else if (off == addr + 2)
2758 irel->r_offset -= 2;
2761 if (irel->r_vaddr - sec->vma == addr)
2766 else if (irel->r_vaddr - sec->vma == addr + 2)
2777 unsigned short insn, oinsn;
2780 loc = contents + irel->r_vaddr - sec->vma;
2787 case R_SH_PCDISP8BY2:
2788 case R_SH_PCRELIMM8BY2:
2789 insn = bfd_get_16 (abfd, loc);
2792 if ((oinsn & 0xff00) != (insn & 0xff00))
2794 bfd_put_16 (abfd, (bfd_vma) insn, loc);
2798 insn = bfd_get_16 (abfd, loc);
2801 if ((oinsn & 0xf000) != (insn & 0xf000))
2803 bfd_put_16 (abfd, (bfd_vma) insn, loc);
2806 case R_SH_PCRELIMM8BY4:
2807 /* This reloc ignores the least significant 3 bits of
2808 the program counter before adding in the offset.
2809 This means that if ADDR is at an even address, the
2810 swap will not affect the offset. If ADDR is an at an
2811 odd address, then the instruction will be crossing a
2812 four byte boundary, and must be adjusted. */
2813 if ((addr & 3) != 0)
2815 insn = bfd_get_16 (abfd, loc);
2818 if ((oinsn & 0xff00) != (insn & 0xff00))
2820 bfd_put_16 (abfd, (bfd_vma) insn, loc);
2828 ((*_bfd_error_handler)
2829 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
2830 bfd_archive_filename (abfd), (unsigned long) irel->r_vaddr));
2831 bfd_set_error (bfd_error_bad_value);
2840 /* This is a modification of _bfd_coff_generic_relocate_section, which
2841 will handle SH relaxing. */
2844 sh_relocate_section (output_bfd, info, input_bfd, input_section, contents,
2845 relocs, syms, sections)
2846 bfd *output_bfd ATTRIBUTE_UNUSED;
2847 struct bfd_link_info *info;
2849 asection *input_section;
2851 struct internal_reloc *relocs;
2852 struct internal_syment *syms;
2853 asection **sections;
2855 struct internal_reloc *rel;
2856 struct internal_reloc *relend;
2859 relend = rel + input_section->reloc_count;
2860 for (; rel < relend; rel++)
2863 struct coff_link_hash_entry *h;
2864 struct internal_syment *sym;
2867 reloc_howto_type *howto;
2868 bfd_reloc_status_type rstat;
2870 /* Almost all relocs have to do with relaxing. If any work must
2871 be done for them, it has been done in sh_relax_section. */
2872 if (rel->r_type != R_SH_IMM32
2874 && rel->r_type != R_SH_IMM32CE
2875 && rel->r_type != R_SH_IMAGEBASE
2877 && rel->r_type != R_SH_PCDISP)
2880 symndx = rel->r_symndx;
2890 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2892 (*_bfd_error_handler)
2893 ("%s: illegal symbol index %ld in relocs",
2894 bfd_archive_filename (input_bfd), symndx);
2895 bfd_set_error (bfd_error_bad_value);
2898 h = obj_coff_sym_hashes (input_bfd)[symndx];
2899 sym = syms + symndx;
2902 if (sym != NULL && sym->n_scnum != 0)
2903 addend = - sym->n_value;
2907 if (rel->r_type == R_SH_PCDISP)
2910 if (rel->r_type >= SH_COFF_HOWTO_COUNT)
2913 howto = &sh_coff_howtos[rel->r_type];
2917 bfd_set_error (bfd_error_bad_value);
2922 if (rel->r_type == R_SH_IMAGEBASE)
2923 addend -= pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
2932 /* There is nothing to do for an internal PCDISP reloc. */
2933 if (rel->r_type == R_SH_PCDISP)
2938 sec = bfd_abs_section_ptr;
2943 sec = sections[symndx];
2944 val = (sec->output_section->vma
2945 + sec->output_offset
2952 if (h->root.type == bfd_link_hash_defined
2953 || h->root.type == bfd_link_hash_defweak)
2957 sec = h->root.u.def.section;
2958 val = (h->root.u.def.value
2959 + sec->output_section->vma
2960 + sec->output_offset);
2962 else if (! info->relocateable)
2964 if (! ((*info->callbacks->undefined_symbol)
2965 (info, h->root.root.string, input_bfd, input_section,
2966 rel->r_vaddr - input_section->vma, true)))
2971 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2973 rel->r_vaddr - input_section->vma,
2982 case bfd_reloc_overflow:
2985 char buf[SYMNMLEN + 1];
2990 name = h->root.root.string;
2991 else if (sym->_n._n_n._n_zeroes == 0
2992 && sym->_n._n_n._n_offset != 0)
2993 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
2996 strncpy (buf, sym->_n._n_name, SYMNMLEN);
2997 buf[SYMNMLEN] = '\0';
3001 if (! ((*info->callbacks->reloc_overflow)
3002 (info, name, howto->name, (bfd_vma) 0, input_bfd,
3003 input_section, rel->r_vaddr - input_section->vma)))
3012 /* This is a version of bfd_generic_get_relocated_section_contents
3013 which uses sh_relocate_section. */
3016 sh_coff_get_relocated_section_contents (output_bfd, link_info, link_order,
3017 data, relocateable, symbols)
3019 struct bfd_link_info *link_info;
3020 struct bfd_link_order *link_order;
3022 boolean relocateable;
3025 asection *input_section = link_order->u.indirect.section;
3026 bfd *input_bfd = input_section->owner;
3027 asection **sections = NULL;
3028 struct internal_reloc *internal_relocs = NULL;
3029 struct internal_syment *internal_syms = NULL;
3031 /* We only need to handle the case of relaxing, or of having a
3032 particular set of section contents, specially. */
3034 || coff_section_data (input_bfd, input_section) == NULL
3035 || coff_section_data (input_bfd, input_section)->contents == NULL)
3036 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3041 memcpy (data, coff_section_data (input_bfd, input_section)->contents,
3042 (size_t) input_section->_raw_size);
3044 if ((input_section->flags & SEC_RELOC) != 0
3045 && input_section->reloc_count > 0)
3047 bfd_size_type symesz = bfd_coff_symesz (input_bfd);
3048 bfd_byte *esym, *esymend;
3049 struct internal_syment *isymp;
3053 if (! _bfd_coff_get_external_symbols (input_bfd))
3056 internal_relocs = (_bfd_coff_read_internal_relocs
3057 (input_bfd, input_section, false, (bfd_byte *) NULL,
3058 false, (struct internal_reloc *) NULL));
3059 if (internal_relocs == NULL)
3062 amt = obj_raw_syment_count (input_bfd);
3063 amt *= sizeof (struct internal_syment);
3064 internal_syms = (struct internal_syment *) bfd_malloc (amt);
3065 if (internal_syms == NULL)
3068 amt = obj_raw_syment_count (input_bfd);
3069 amt *= sizeof (asection *);
3070 sections = (asection **) bfd_malloc (amt);
3071 if (sections == NULL)
3074 isymp = internal_syms;
3076 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3077 esymend = esym + obj_raw_syment_count (input_bfd) * symesz;
3078 while (esym < esymend)
3080 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3082 if (isymp->n_scnum != 0)
3083 *secpp = coff_section_from_bfd_index (input_bfd, isymp->n_scnum);
3086 if (isymp->n_value == 0)
3087 *secpp = bfd_und_section_ptr;
3089 *secpp = bfd_com_section_ptr;
3092 esym += (isymp->n_numaux + 1) * symesz;
3093 secpp += isymp->n_numaux + 1;
3094 isymp += isymp->n_numaux + 1;
3097 if (! sh_relocate_section (output_bfd, link_info, input_bfd,
3098 input_section, data, internal_relocs,
3099 internal_syms, sections))
3104 free (internal_syms);
3105 internal_syms = NULL;
3106 free (internal_relocs);
3107 internal_relocs = NULL;
3113 if (internal_relocs != NULL)
3114 free (internal_relocs);
3115 if (internal_syms != NULL)
3116 free (internal_syms);
3117 if (sections != NULL)
3122 /* The target vectors. */
3124 #ifndef TARGET_SHL_SYM
3125 CREATE_BIG_COFF_TARGET_VEC (shcoff_vec, "coff-sh", BFD_IS_RELAXABLE, 0, '_', NULL)
3128 #ifdef TARGET_SHL_SYM
3129 #define TARGET_SYM TARGET_SHL_SYM
3131 #define TARGET_SYM shlcoff_vec
3134 #ifndef TARGET_SHL_NAME
3135 #define TARGET_SHL_NAME "coff-shl"
3139 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE,
3140 SEC_CODE | SEC_DATA, '_', NULL);
3142 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE,
3146 #ifndef TARGET_SHL_SYM
3147 static const bfd_target * coff_small_object_p PARAMS ((bfd *));
3148 static boolean coff_small_new_section_hook PARAMS ((bfd *, asection *));
3149 /* Some people want versions of the SH COFF target which do not align
3150 to 16 byte boundaries. We implement that by adding a couple of new
3151 target vectors. These are just like the ones above, but they
3152 change the default section alignment. To generate them in the
3153 assembler, use -small. To use them in the linker, use -b
3154 coff-sh{l}-small and -oformat coff-sh{l}-small.
3156 Yes, this is a horrible hack. A general solution for setting
3157 section alignment in COFF is rather complex. ELF handles this
3160 /* Only recognize the small versions if the target was not defaulted.
3161 Otherwise we won't recognize the non default endianness. */
3163 static const bfd_target *
3164 coff_small_object_p (abfd)
3167 if (abfd->target_defaulted)
3169 bfd_set_error (bfd_error_wrong_format);
3172 return coff_object_p (abfd);
3175 /* Set the section alignment for the small versions. */
3178 coff_small_new_section_hook (abfd, section)
3182 if (! coff_new_section_hook (abfd, section))
3185 /* We must align to at least a four byte boundary, because longword
3186 accesses must be on a four byte boundary. */
3187 if (section->alignment_power == COFF_DEFAULT_SECTION_ALIGNMENT_POWER)
3188 section->alignment_power = 2;
3193 /* This is copied from bfd_coff_std_swap_table so that we can change
3194 the default section alignment power. */
3196 static const bfd_coff_backend_data bfd_coff_small_swap_table =
3198 coff_swap_aux_in, coff_swap_sym_in, coff_swap_lineno_in,
3199 coff_swap_aux_out, coff_swap_sym_out,
3200 coff_swap_lineno_out, coff_swap_reloc_out,
3201 coff_swap_filehdr_out, coff_swap_aouthdr_out,
3202 coff_swap_scnhdr_out,
3203 FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
3204 #ifdef COFF_LONG_FILENAMES
3209 #ifdef COFF_LONG_SECTION_NAMES
3215 #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
3220 #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
3225 coff_swap_filehdr_in, coff_swap_aouthdr_in, coff_swap_scnhdr_in,
3226 coff_swap_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
3227 coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
3228 coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
3229 coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
3230 coff_classify_symbol, coff_compute_section_file_positions,
3231 coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
3232 coff_adjust_symndx, coff_link_add_one_symbol,
3233 coff_link_output_has_begun, coff_final_link_postscript
3236 #define coff_small_close_and_cleanup \
3237 coff_close_and_cleanup
3238 #define coff_small_bfd_free_cached_info \
3239 coff_bfd_free_cached_info
3240 #define coff_small_get_section_contents \
3241 coff_get_section_contents
3242 #define coff_small_get_section_contents_in_window \
3243 coff_get_section_contents_in_window
3245 extern const bfd_target shlcoff_small_vec;
3247 const bfd_target shcoff_small_vec =
3249 "coff-sh-small", /* name */
3250 bfd_target_coff_flavour,
3251 BFD_ENDIAN_BIG, /* data byte order is big */
3252 BFD_ENDIAN_BIG, /* header byte order is big */
3254 (HAS_RELOC | EXEC_P | /* object flags */
3255 HAS_LINENO | HAS_DEBUG |
3256 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
3258 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
3259 '_', /* leading symbol underscore */
3260 '/', /* ar_pad_char */
3261 15, /* ar_max_namelen */
3262 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3263 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3264 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3265 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3266 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3267 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3269 {_bfd_dummy_target, coff_small_object_p, /* bfd_check_format */
3270 bfd_generic_archive_p, _bfd_dummy_target},
3271 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3273 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
3274 _bfd_write_archive_contents, bfd_false},
3276 BFD_JUMP_TABLE_GENERIC (coff_small),
3277 BFD_JUMP_TABLE_COPY (coff),
3278 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3279 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
3280 BFD_JUMP_TABLE_SYMBOLS (coff),
3281 BFD_JUMP_TABLE_RELOCS (coff),
3282 BFD_JUMP_TABLE_WRITE (coff),
3283 BFD_JUMP_TABLE_LINK (coff),
3284 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3286 & shlcoff_small_vec,
3288 (PTR) &bfd_coff_small_swap_table
3291 const bfd_target shlcoff_small_vec =
3293 "coff-shl-small", /* name */
3294 bfd_target_coff_flavour,
3295 BFD_ENDIAN_LITTLE, /* data byte order is little */
3296 BFD_ENDIAN_LITTLE, /* header byte order is little endian too*/
3298 (HAS_RELOC | EXEC_P | /* object flags */
3299 HAS_LINENO | HAS_DEBUG |
3300 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
3302 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
3303 '_', /* leading symbol underscore */
3304 '/', /* ar_pad_char */
3305 15, /* ar_max_namelen */
3306 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
3307 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
3308 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
3309 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
3310 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
3311 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
3313 {_bfd_dummy_target, coff_small_object_p, /* bfd_check_format */
3314 bfd_generic_archive_p, _bfd_dummy_target},
3315 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3317 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
3318 _bfd_write_archive_contents, bfd_false},
3320 BFD_JUMP_TABLE_GENERIC (coff_small),
3321 BFD_JUMP_TABLE_COPY (coff),
3322 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3323 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
3324 BFD_JUMP_TABLE_SYMBOLS (coff),
3325 BFD_JUMP_TABLE_RELOCS (coff),
3326 BFD_JUMP_TABLE_WRITE (coff),
3327 BFD_JUMP_TABLE_LINK (coff),
3328 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3332 (PTR) &bfd_coff_small_swap_table