1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
2 Copyright (C) 1993, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include "opcodes/sh-opc.h"
30 #include "struc-symbol.h"
36 const char comment_chars[] = "!";
37 const char line_separator_chars[] = ";";
38 const char line_comment_chars[] = "!#";
40 static void s_uses PARAMS ((int));
42 static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
43 static void sh_frob_section PARAMS ((bfd *, segT, PTR));
46 void s_align_bytes ();
47 static void s_uacons PARAMS ((int));
48 static sh_opcode_info *find_cooked_opcode PARAMS ((char **));
49 static void assemble_ppi PARAMS ((char *, sh_opcode_info *));
55 int ignore ATTRIBUTE_UNUSED;
58 target_big_endian = 0;
61 /* This table describes all the machine specific pseudo-ops the assembler
62 has to support. The fields are:
63 pseudo-op name without dot
64 function to call to execute this pseudo-op
65 Integer arg to pass to the function. */
67 const pseudo_typeS md_pseudo_table[] =
71 {"form", listing_psize, 0},
72 {"little", little, 0},
73 {"heading", listing_title, 0},
74 {"import", s_ignore, 0},
75 {"page", listing_eject, 0},
76 {"program", s_ignore, 0},
78 {"uaword", s_uacons, 2},
79 {"ualong", s_uacons, 4},
83 /*int md_reloc_size; */
85 int sh_relax; /* set if -relax seen */
87 /* Whether -small was seen. */
91 /* Whether -dsp was seen. */
95 /* The bit mask of architectures that could
96 accomodate the insns seen so far. */
97 static int valid_arch;
99 const char EXP_CHARS[] = "eE";
101 /* Chars that mean this number is a floating point constant. */
104 const char FLT_CHARS[] = "rRsSfFdDxXpP";
106 #define C(a,b) ENCODE_RELAX(a,b)
108 #define JREG 14 /* Register used as a temp when relaxing */
109 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
110 #define GET_WHAT(x) ((x>>4))
112 /* These are the three types of relaxable instrction. */
114 #define COND_JUMP_DELAY 2
115 #define UNCOND_JUMP 3
124 #define UNDEF_WORD_DISP 4
129 /* Branch displacements are from the address of the branch plus
130 four, thus all minimum and maximum values have 4 added to them. */
133 #define COND8_LENGTH 2
135 /* There is one extra instruction before the branch, so we must add
136 two more bytes to account for it. */
137 #define COND12_F 4100
138 #define COND12_M -4090
139 #define COND12_LENGTH 6
141 #define COND12_DELAY_LENGTH 4
143 /* ??? The minimum and maximum values are wrong, but this does not matter
144 since this relocation type is not supported yet. */
145 #define COND32_F (1<<30)
146 #define COND32_M -(1<<30)
147 #define COND32_LENGTH 14
149 #define UNCOND12_F 4098
150 #define UNCOND12_M -4092
151 #define UNCOND12_LENGTH 2
153 /* ??? The minimum and maximum values are wrong, but this does not matter
154 since this relocation type is not supported yet. */
155 #define UNCOND32_F (1<<30)
156 #define UNCOND32_M -(1<<30)
157 #define UNCOND32_LENGTH 14
159 #define EMPTY { 0, 0, 0, 0 }
161 const relax_typeS md_relax_table[C (END, 0)] = {
162 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
163 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
166 /* C (COND_JUMP, COND8) */
167 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
168 /* C (COND_JUMP, COND12) */
169 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
170 /* C (COND_JUMP, COND32) */
171 { COND32_F, COND32_M, COND32_LENGTH, 0, },
172 EMPTY, EMPTY, EMPTY, EMPTY,
173 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
176 /* C (COND_JUMP_DELAY, COND8) */
177 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
178 /* C (COND_JUMP_DELAY, COND12) */
179 { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
180 /* C (COND_JUMP_DELAY, COND32) */
181 { COND32_F, COND32_M, COND32_LENGTH, 0, },
182 EMPTY, EMPTY, EMPTY, EMPTY,
183 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
186 /* C (UNCOND_JUMP, UNCOND12) */
187 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
188 /* C (UNCOND_JUMP, UNCOND32) */
189 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
190 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
191 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
196 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
198 /* This function is called once, at assembler startup time. This should
199 set up all the tables, etc that the MD part of the assembler needs. */
204 sh_opcode_info *opcode;
205 char *prev_name = "";
209 /* The WinCE OS only supports little endian executables. */
210 target_big_endian = 0;
213 target_big_endian = 1;
216 target_arch = arch_sh1_up & ~(sh_dsp ? arch_sh3e_up : arch_sh_dsp_up);
217 valid_arch = target_arch;
219 opcode_hash_control = hash_new ();
221 /* Insert unique names into hash table. */
222 for (opcode = sh_table; opcode->name; opcode++)
224 if (strcmp (prev_name, opcode->name))
226 if (! (opcode->arch & target_arch))
228 prev_name = opcode->name;
229 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
233 /* Make all the opcodes with the same name point to the same
235 opcode->name = prev_name;
242 static int reg_x, reg_y;
250 expressionS immediate;
254 #define IDENT_CHAR(c) (isalnum (c) || (c) == '_')
256 /* Try to parse a reg name. Return the number of chars consumed. */
259 parse_reg (src, mode, reg)
264 /* We use ! IDENT_CHAR for the next character after the register name, to
265 make sure that we won't accidentally recognize a symbol name such as
266 'sram' or sr_ram as being a reference to the register 'sr'. */
272 if (src[2] >= '0' && src[2] <= '5'
273 && ! IDENT_CHAR ((unsigned char) src[3]))
276 *reg = 10 + src[2] - '0';
280 if (src[1] >= '0' && src[1] <= '9'
281 && ! IDENT_CHAR ((unsigned char) src[2]))
284 *reg = (src[1] - '0');
287 if (src[1] >= '0' && src[1] <= '7' && strncmp (&src[2], "_bank", 5) == 0
288 && ! IDENT_CHAR ((unsigned char) src[7]))
291 *reg = (src[1] - '0');
295 if (src[1] == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
300 if (src[1] == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
311 if (! IDENT_CHAR ((unsigned char) src[2]))
317 if (src[2] == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
326 if (! IDENT_CHAR ((unsigned char) src[2]))
332 if (src[2] == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
340 if (src[1] == 'x' && src[2] >= '0' && src[2] <= '1'
341 && ! IDENT_CHAR ((unsigned char) src[3]))
344 *reg = 4 + (src[1] - '0');
347 if (src[1] == 'y' && src[2] >= '0' && src[2] <= '1'
348 && ! IDENT_CHAR ((unsigned char) src[3]))
351 *reg = 6 + (src[1] - '0');
354 if (src[1] == 's' && src[2] >= '0' && src[2] <= '3'
355 && ! IDENT_CHAR ((unsigned char) src[3]))
357 int n = src[1] - '0';
360 *reg = n | ((~n & 2) << 1);
365 if (src[0] == 'i' && src[1] && ! IDENT_CHAR ((unsigned char) src[3]))
387 if (src[0] == 'x' && src[1] >= '0' && src[1] <= '1'
388 && ! IDENT_CHAR ((unsigned char) src[2]))
391 *reg = A_X0_NUM + src[1] - '0';
395 if (src[0] == 'y' && src[1] >= '0' && src[1] <= '1'
396 && ! IDENT_CHAR ((unsigned char) src[2]))
399 *reg = A_Y0_NUM + src[1] - '0';
403 if (src[0] == 'm' && src[1] >= '0' && src[1] <= '1'
404 && ! IDENT_CHAR ((unsigned char) src[2]))
407 *reg = src[1] == '0' ? A_M0_NUM : A_M1_NUM;
413 && src[2] == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
419 if (src[0] == 's' && src[1] == 'p' && src[2] == 'c'
420 && ! IDENT_CHAR ((unsigned char) src[3]))
426 if (src[0] == 's' && src[1] == 'g' && src[2] == 'r'
427 && ! IDENT_CHAR ((unsigned char) src[3]))
433 if (src[0] == 'd' && src[1] == 's' && src[2] == 'r'
434 && ! IDENT_CHAR ((unsigned char) src[3]))
440 if (src[0] == 'd' && src[1] == 'b' && src[2] == 'r'
441 && ! IDENT_CHAR ((unsigned char) src[3]))
447 if (src[0] == 's' && src[1] == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
453 if (src[0] == 's' && src[1] == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
460 if (src[0] == 'p' && src[1] == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
465 if (src[0] == 'p' && src[1] == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
467 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
468 and use an uninitialized immediate. */
472 if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r'
473 && ! IDENT_CHAR ((unsigned char) src[3]))
478 if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r'
479 && ! IDENT_CHAR ((unsigned char) src[3]))
485 if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c'
486 && ! IDENT_CHAR ((unsigned char) src[4]))
499 if (src[0] == 'm' && src[1] == 'o' && src[2] == 'd'
500 && ! IDENT_CHAR ((unsigned char) src[4]))
505 if (src[0] == 'f' && src[1] == 'r')
509 if (src[3] >= '0' && src[3] <= '5'
510 && ! IDENT_CHAR ((unsigned char) src[4]))
513 *reg = 10 + src[3] - '0';
517 if (src[2] >= '0' && src[2] <= '9'
518 && ! IDENT_CHAR ((unsigned char) src[3]))
521 *reg = (src[2] - '0');
525 if (src[0] == 'd' && src[1] == 'r')
529 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
530 && ! IDENT_CHAR ((unsigned char) src[4]))
533 *reg = 10 + src[3] - '0';
537 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
538 && ! IDENT_CHAR ((unsigned char) src[3]))
541 *reg = (src[2] - '0');
545 if (src[0] == 'x' && src[1] == 'd')
549 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
550 && ! IDENT_CHAR ((unsigned char) src[4]))
553 *reg = 11 + src[3] - '0';
557 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
558 && ! IDENT_CHAR ((unsigned char) src[3]))
561 *reg = (src[2] - '0') + 1;
565 if (src[0] == 'f' && src[1] == 'v')
567 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
573 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
574 && ! IDENT_CHAR ((unsigned char) src[3]))
577 *reg = (src[2] - '0');
581 if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l'
582 && ! IDENT_CHAR ((unsigned char) src[4]))
588 if (src[0] == 'f' && src[1] == 'p' && src[2] == 's' && src[3] == 'c'
589 && src[4] == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
595 if (src[0] == 'x' && src[1] == 'm' && src[2] == 't' && src[3] == 'r'
596 && src[4] == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
610 /* JF: '.' is pseudo symbol with value of current location
611 in current segment. */
612 fake = FAKE_LABEL_NAME;
613 return symbol_new (fake,
615 (valueT) frag_now_fix (),
627 save = input_line_pointer;
628 input_line_pointer = s;
629 expression (&op->immediate);
630 if (op->immediate.X_op == O_absent)
631 as_bad (_("missing operand"));
632 new = input_line_pointer;
633 input_line_pointer = save;
637 /* The many forms of operand:
640 @Rn Register indirect
653 pr, gbr, vbr, macl, mach
666 /* Must be predecrement. */
669 len = parse_reg (src, &mode, &(op->reg));
671 as_bad (_("illegal register after @-"));
676 else if (src[0] == '(')
678 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
681 len = parse_reg (src, &mode, &(op->reg));
682 if (len && mode == A_REG_N)
687 as_bad (_("must be @(r0,...)"));
691 /* Now can be rn or gbr */
692 len = parse_reg (src, &mode, &(op->reg));
697 else if (mode == A_REG_N)
699 op->type = A_IND_R0_REG_N;
703 as_bad (_("syntax error in @(r0,...)"));
708 /* Must be an @(disp,.. thing) */
709 src = parse_exp (src, op);
712 /* Now can be rn, gbr or pc */
713 len = parse_reg (src, &mode, &op->reg);
718 op->type = A_DISP_REG_N;
720 else if (mode == A_GBR)
722 op->type = A_DISP_GBR;
724 else if (mode == A_PC)
726 /* Turn a plain @(4,pc) into @(.+4,pc). */
727 if (op->immediate.X_op == O_constant)
729 op->immediate.X_add_symbol = dot();
730 op->immediate.X_op = O_symbol;
732 op->type = A_DISP_PC;
736 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
741 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
746 as_bad (_("expecting )"));
752 src += parse_reg (src, &mode, &(op->reg));
755 as_bad (_("illegal register after @"));
760 if ((src[0] == 'r' && src[1] == '8')
761 || (src[0] == 'i' && (src[1] == 'x' || src[1] == 's')))
766 if ((src[0] == 'r' && src[1] == '9')
767 || (src[0] == 'i' && src[1] == 'y'))
770 op->type = A_PMODY_N;
784 get_operand (ptr, op)
795 *ptr = parse_exp (src, op);
800 else if (src[0] == '@')
802 *ptr = parse_at (src, op);
805 len = parse_reg (src, &mode, &(op->reg));
814 /* Not a reg, the only thing left is a displacement. */
815 *ptr = parse_exp (src, op);
816 op->type = A_DISP_PC;
822 get_operands (info, args, operand)
823 sh_opcode_info *info;
825 sh_operand_info *operand;
830 /* The pre-processor will eliminate whitespace in front of '@'
831 after the first argument; we may be called multiple times
832 from assemble_ppi, so don't insist on finding whitespace here. */
836 get_operand (&ptr, operand + 0);
843 get_operand (&ptr, operand + 1);
844 /* ??? Hack: psha/pshl have a varying operand number depending on
845 the type of the first operand. We handle this by having the
846 three-operand version first and reducing the number of operands
847 parsed to two if we see that the first operand is an immediate.
848 This works because no insn with three operands has an immediate
850 if (info->arg[2] && operand[0].type != A_IMM)
856 get_operand (&ptr, operand + 2);
878 /* Passed a pointer to a list of opcodes which use different
879 addressing modes, return the opcode which matches the opcodes
882 static sh_opcode_info *
883 get_specific (opcode, operands)
884 sh_opcode_info *opcode;
885 sh_operand_info *operands;
887 sh_opcode_info *this_try = opcode;
888 char *name = opcode->name;
894 if (this_try->name != name)
896 /* We've looked so far down the table that we've run out of
897 opcodes with the same name. */
901 /* Look at both operands needed by the opcodes and provided by
902 the user - since an arg test will often fail on the same arg
903 again and again, we'll try and test the last failing arg the
904 first on each opcode try. */
905 for (n = 0; this_try->arg[n]; n++)
907 sh_operand_info *user = operands + n;
908 sh_arg_type arg = this_try->arg[n];
920 if (user->type != arg)
924 /* opcode needs r0 */
925 if (user->type != A_REG_N || user->reg != 0)
929 if (user->type != A_R0_GBR || user->reg != 0)
933 if (user->type != F_REG_N || user->reg != 0)
952 /* Opcode needs rn */
953 if (user->type != arg)
958 if (user->type != D_REG_N && user->type != X_REG_N)
973 if (user->type != arg)
978 if (user->type != arg)
990 /* Opcode needs rn */
991 if (user->type != arg - A_REG_M + A_REG_N)
997 if (user->type != DSP_REG_N)
1019 if (user->type != DSP_REG_N)
1041 if (user->type != DSP_REG_N)
1063 if (user->type != DSP_REG_N)
1085 if (user->type != DSP_REG_N)
1107 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1111 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1115 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1119 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1123 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1133 /* Opcode needs rn */
1134 if (user->type != arg - F_REG_M + F_REG_N)
1139 if (user->type != D_REG_N && user->type != X_REG_N)
1144 if (user->type != XMTRX_M4)
1150 printf (_("unhandled %d\n"), arg);
1154 if ( !(valid_arch & this_try->arch))
1156 valid_arch &= this_try->arch;
1166 check (operand, low, high)
1167 expressionS *operand;
1171 if (operand->X_op != O_constant
1172 || operand->X_add_number < low
1173 || operand->X_add_number > high)
1175 as_bad (_("operand must be absolute in range %d..%d"), low, high);
1177 return operand->X_add_number;
1181 insert (where, how, pcrel, op)
1185 sh_operand_info *op;
1187 fix_new_exp (frag_now,
1188 where - frag_now->fr_literal,
1196 build_relax (opcode, op)
1197 sh_opcode_info *opcode;
1198 sh_operand_info *op;
1200 int high_byte = target_big_endian ? 0 : 1;
1203 if (opcode->arg[0] == A_BDISP8)
1205 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
1206 p = frag_var (rs_machine_dependent,
1207 md_relax_table[C (what, COND32)].rlx_length,
1208 md_relax_table[C (what, COND8)].rlx_length,
1210 op->immediate.X_add_symbol,
1211 op->immediate.X_add_number,
1213 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
1215 else if (opcode->arg[0] == A_BDISP12)
1217 p = frag_var (rs_machine_dependent,
1218 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
1219 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
1221 op->immediate.X_add_symbol,
1222 op->immediate.X_add_number,
1224 p[high_byte] = (opcode->nibbles[0] << 4);
1229 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
1232 insert_loop_bounds (output, operand)
1234 sh_operand_info *operand;
1239 /* Since the low byte of the opcode will be overwritten by the reloc, we
1240 can just stash the high byte into both bytes and ignore endianness. */
1243 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1244 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1248 static int count = 0;
1250 /* If the last loop insn is a two-byte-insn, it is in danger of being
1251 swapped with the insn after it. To prevent this, create a new
1252 symbol - complete with SH_LABEL reloc - after the last loop insn.
1253 If the last loop insn is four bytes long, the symbol will be
1254 right in the middle, but four byte insns are not swapped anyways. */
1255 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
1256 Hence a 9 digit number should be enough to count all REPEATs. */
1258 sprintf (name, "_R%x", count++ & 0x3fffffff);
1259 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
1260 /* Make this a local symbol. */
1262 SF_SET_LOCAL (end_sym);
1263 #endif /* OBJ_COFF */
1264 symbol_table_insert (end_sym);
1265 end_sym->sy_value = operand[1].immediate;
1266 end_sym->sy_value.X_add_number += 2;
1267 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
1270 output = frag_more (2);
1273 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1274 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1276 return frag_more (2);
1279 /* Now we know what sort of opcodes it is, let's build the bytes. */
1282 build_Mytes (opcode, operand)
1283 sh_opcode_info *opcode;
1284 sh_operand_info *operand;
1289 char *output = frag_more (2);
1290 int low_byte = target_big_endian ? 1 : 0;
1296 for (index = 0; index < 4; index++)
1298 sh_nibble_type i = opcode->nibbles[index];
1308 nbuf[index] = reg_n;
1311 nbuf[index] = reg_m;
1314 if (reg_n < 2 || reg_n > 5)
1315 as_bad (_("Invalid register: 'r%d'"), reg_n);
1316 nbuf[index] = (reg_n & 3) | 4;
1319 nbuf[index] = reg_n | (reg_m >> 2);
1322 nbuf[index] = reg_b | 0x08;
1325 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
1328 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
1331 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
1334 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
1337 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
1340 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
1343 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
1346 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
1349 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
1352 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
1355 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
1358 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
1361 insert (output, BFD_RELOC_SH_PCRELIMM8BY4, 1, operand);
1364 insert (output, BFD_RELOC_SH_PCRELIMM8BY2, 1, operand);
1367 output = insert_loop_bounds (output, operand);
1368 nbuf[index] = opcode->nibbles[3];
1372 printf (_("failed for %d\n"), i);
1376 if (!target_big_endian)
1378 output[1] = (nbuf[0] << 4) | (nbuf[1]);
1379 output[0] = (nbuf[2] << 4) | (nbuf[3]);
1383 output[0] = (nbuf[0] << 4) | (nbuf[1]);
1384 output[1] = (nbuf[2] << 4) | (nbuf[3]);
1388 /* Find an opcode at the start of *STR_P in the hash table, and set
1389 *STR_P to the first character after the last one read. */
1391 static sh_opcode_info *
1392 find_cooked_opcode (str_p)
1396 unsigned char *op_start;
1397 unsigned char *op_end;
1401 /* Drop leading whitespace. */
1405 /* Find the op code end.
1406 The pre-processor will eliminate whitespace in front of
1407 any '@' after the first argument; we may be called from
1408 assemble_ppi, so the opcode might be terminated by an '@'. */
1409 for (op_start = op_end = (unsigned char *) (str);
1412 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
1415 unsigned char c = op_start[nlen];
1417 /* The machine independent code will convert CMP/EQ into cmp/EQ
1418 because it thinks the '/' is the end of the symbol. Moreover,
1419 all but the first sub-insn is a parallel processing insn won't
1420 be capitailzed. Instead of hacking up the machine independent
1421 code, we just deal with it here. */
1422 c = isupper (c) ? tolower (c) : c;
1431 as_bad (_("can't find opcode "));
1433 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
1436 /* Assemble a parallel processing insn. */
1437 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
1440 assemble_ppi (op_end, opcode)
1442 sh_opcode_info *opcode;
1451 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
1452 Make sure we encode a defined insn pattern. */
1458 sh_operand_info operand[3];
1460 if (opcode->arg[0] != A_END)
1461 op_end = get_operands (opcode, op_end, operand);
1462 opcode = get_specific (opcode, operand);
1465 /* Couldn't find an opcode which matched the operands. */
1466 char *where = frag_more (2);
1470 as_bad (_("invalid operands for opcode"));
1474 if (opcode->nibbles[0] != PPI)
1475 as_bad (_("insn can't be combined with parallel processing insn"));
1477 switch (opcode->nibbles[1])
1482 as_bad (_("multiple movx specifications"));
1487 as_bad (_("multiple movy specifications"));
1493 as_bad (_("multiple movx specifications"));
1494 if (reg_n < 4 || reg_n > 5)
1495 as_bad (_("invalid movx address register"));
1496 if (opcode->nibbles[2] & 8)
1498 if (reg_m == A_A1_NUM)
1500 else if (reg_m != A_A0_NUM)
1501 as_bad (_("invalid movx dsp register"));
1506 as_bad (_("invalid movx dsp register"));
1509 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
1514 as_bad (_("multiple movy specifications"));
1515 if (opcode->nibbles[2] & 8)
1517 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
1520 if (reg_m == A_A1_NUM)
1522 else if (reg_m != A_A0_NUM)
1523 as_bad (_("invalid movy dsp register"));
1528 as_bad (_("invalid movy dsp register"));
1531 if (reg_n < 6 || reg_n > 7)
1532 as_bad (_("invalid movy address register"));
1533 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
1537 if (operand[0].immediate.X_op != O_constant)
1538 as_bad (_("dsp immediate shift value not constant"));
1539 field_b = ((opcode->nibbles[2] << 12)
1540 | (operand[0].immediate.X_add_number & 127) << 4
1545 as_bad (_("multiple parallel processing specifications"));
1546 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
1547 + (reg_x << 6) + (reg_y << 4) + reg_n);
1551 as_bad (_("multiple condition specifications"));
1552 cond = opcode->nibbles[2] << 8;
1554 goto skip_cond_check;
1558 as_bad (_("multiple parallel processing specifications"));
1559 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
1560 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
1566 if ((field_b & 0xef00) != 0xa100)
1567 as_bad (_("insn cannot be combined with pmuls"));
1569 switch (field_b & 0xf)
1572 field_b += 0 - A_X0_NUM;
1575 field_b += 1 - A_Y0_NUM;
1578 field_b += 2 - A_A0_NUM;
1581 field_b += 3 - A_A1_NUM;
1584 as_bad (_("bad padd / psub pmuls output operand"));
1587 field_b += 0x4000 + reg_efg;
1594 as_bad (_("condition not followed by conditionalizable insn"));
1600 opcode = find_cooked_opcode (&op_end);
1604 (_("unrecognized characters at end of parallel processing insn")));
1609 move_code = movx | movy;
1612 /* Parallel processing insn. */
1613 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
1615 output = frag_more (4);
1616 if (! target_big_endian)
1618 output[3] = ppi_code >> 8;
1619 output[2] = ppi_code;
1623 output[2] = ppi_code >> 8;
1624 output[3] = ppi_code;
1626 move_code |= 0xf800;
1629 /* Just a double data transfer. */
1630 output = frag_more (2);
1631 if (! target_big_endian)
1633 output[1] = move_code >> 8;
1634 output[0] = move_code;
1638 output[0] = move_code >> 8;
1639 output[1] = move_code;
1643 /* This is the guts of the machine-dependent assembler. STR points to a
1644 machine dependent instruction. This function is supposed to emit
1645 the frags/bytes it assembles to. */
1651 unsigned char *op_end;
1652 sh_operand_info operand[3];
1653 sh_opcode_info *opcode;
1655 opcode = find_cooked_opcode (&str);
1660 as_bad (_("unknown opcode"));
1665 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
1667 /* Output a CODE reloc to tell the linker that the following
1668 bytes are instructions, not data. */
1669 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1671 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
1674 if (opcode->nibbles[0] == PPI)
1676 assemble_ppi (op_end, opcode);
1680 if (opcode->arg[0] == A_BDISP12
1681 || opcode->arg[0] == A_BDISP8)
1683 parse_exp (op_end + 1, &operand[0]);
1684 build_relax (opcode, &operand[0]);
1688 if (opcode->arg[0] == A_END)
1690 /* Ignore trailing whitespace. If there is any, it has already
1691 been compressed to a single space. */
1697 op_end = get_operands (opcode, op_end, operand);
1699 opcode = get_specific (opcode, operand);
1703 /* Couldn't find an opcode which matched the operands. */
1704 char *where = frag_more (2);
1708 as_bad (_("invalid operands for opcode"));
1713 as_bad (_("excess operands: '%s'"), op_end);
1715 build_Mytes (opcode, operand);
1720 /* This routine is called each time a label definition is seen. It
1721 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
1726 static fragS *last_label_frag;
1727 static int last_label_offset;
1730 && seg_info (now_seg)->tc_segment_info_data.in_code)
1734 offset = frag_now_fix ();
1735 if (frag_now != last_label_frag
1736 || offset != last_label_offset)
1738 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
1739 last_label_frag = frag_now;
1740 last_label_offset = offset;
1745 /* This routine is called when the assembler is about to output some
1746 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
1749 sh_flush_pending_output ()
1752 && seg_info (now_seg)->tc_segment_info_data.in_code)
1754 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1756 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
1761 md_undefined_symbol (name)
1768 #ifndef BFD_ASSEMBLER
1771 tc_crawl_symbol_chain (headers)
1772 object_headers *headers;
1774 printf (_("call to tc_crawl_symbol_chain \n"));
1778 tc_headers_hook (headers)
1779 object_headers *headers;
1781 printf (_("call to tc_headers_hook \n"));
1787 /* Various routines to kill one day. */
1788 /* Equal to MAX_PRECISION in atof-ieee.c. */
1789 #define MAX_LITTLENUMS 6
1791 /* Turn a string in input_line_pointer into a floating point constant
1792 of type TYPE, and store the appropriate bytes in *LITP. The number
1793 of LITTLENUMS emitted is stored in *SIZEP . An error message is
1794 returned, or NULL on OK. */
1797 md_atof (type, litP, sizeP)
1803 LITTLENUM_TYPE words[4];
1819 return _("bad call to md_atof");
1822 t = atof_ieee (input_line_pointer, type, words);
1824 input_line_pointer = t;
1828 if (! target_big_endian)
1830 for (i = prec - 1; i >= 0; i--)
1832 md_number_to_chars (litP, (valueT) words[i], 2);
1838 for (i = 0; i < prec; i++)
1840 md_number_to_chars (litP, (valueT) words[i], 2);
1848 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
1849 call instruction. It refers to a label of the instruction which
1850 loads the register which the call uses. We use it to generate a
1851 special reloc for the linker. */
1855 int ignore ATTRIBUTE_UNUSED;
1860 as_warn (_(".uses pseudo-op seen when not relaxing"));
1864 if (ex.X_op != O_symbol || ex.X_add_number != 0)
1866 as_bad (_("bad .uses format"));
1867 ignore_rest_of_line ();
1871 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
1873 demand_empty_rest_of_line ();
1876 CONST char *md_shortopts = "";
1877 struct option md_longopts[] =
1879 #define OPTION_RELAX (OPTION_MD_BASE)
1880 #define OPTION_LITTLE (OPTION_MD_BASE + 1)
1881 #define OPTION_SMALL (OPTION_LITTLE + 1)
1882 #define OPTION_DSP (OPTION_SMALL + 1)
1884 {"relax", no_argument, NULL, OPTION_RELAX},
1885 {"little", no_argument, NULL, OPTION_LITTLE},
1886 {"small", no_argument, NULL, OPTION_SMALL},
1887 {"dsp", no_argument, NULL, OPTION_DSP},
1888 {NULL, no_argument, NULL, 0}
1890 size_t md_longopts_size = sizeof (md_longopts);
1893 md_parse_option (c, arg)
1895 char *arg ATTRIBUTE_UNUSED;
1905 target_big_endian = 0;
1924 md_show_usage (stream)
1927 fprintf (stream, _("\
1929 -little generate little endian code\n\
1930 -relax alter jump instructions for long displacements\n\
1931 -small align sections to 4 byte boundaries, not 16\n\
1932 -dsp enable sh-dsp insns, and disable sh3e / sh4 insns.\n"));
1936 tc_Nout_fix_to_chars ()
1938 printf (_("call to tc_Nout_fix_to_chars \n"));
1942 /* This struct is used to pass arguments to sh_count_relocs through
1943 bfd_map_over_sections. */
1945 struct sh_count_relocs
1947 /* Symbol we are looking for. */
1949 /* Count of relocs found. */
1953 /* Count the number of fixups in a section which refer to a particular
1954 symbol. When using BFD_ASSEMBLER, this is called via
1955 bfd_map_over_sections. */
1958 sh_count_relocs (abfd, sec, data)
1959 bfd *abfd ATTRIBUTE_UNUSED;
1963 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
1964 segment_info_type *seginfo;
1968 seginfo = seg_info (sec);
1969 if (seginfo == NULL)
1973 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
1975 if (fix->fx_addsy == sym)
1983 /* Handle the count relocs for a particular section. When using
1984 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
1987 sh_frob_section (abfd, sec, ignore)
1988 bfd *abfd ATTRIBUTE_UNUSED;
1990 PTR ignore ATTRIBUTE_UNUSED;
1992 segment_info_type *seginfo;
1995 seginfo = seg_info (sec);
1996 if (seginfo == NULL)
1999 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2004 struct sh_count_relocs info;
2006 if (fix->fx_r_type != BFD_RELOC_SH_USES)
2009 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2010 symbol in the same section. */
2011 sym = fix->fx_addsy;
2013 || fix->fx_subsy != NULL
2014 || fix->fx_addnumber != 0
2015 || S_GET_SEGMENT (sym) != sec
2016 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2017 || S_GET_STORAGE_CLASS (sym) == C_EXT
2019 || S_IS_EXTERNAL (sym))
2021 as_warn_where (fix->fx_file, fix->fx_line,
2022 _(".uses does not refer to a local symbol in the same section"));
2026 /* Look through the fixups again, this time looking for one
2027 at the same location as sym. */
2028 val = S_GET_VALUE (sym);
2029 for (fscan = seginfo->fix_root;
2031 fscan = fscan->fx_next)
2032 if (val == fscan->fx_frag->fr_address + fscan->fx_where
2033 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
2034 && fscan->fx_r_type != BFD_RELOC_SH_CODE
2035 && fscan->fx_r_type != BFD_RELOC_SH_DATA
2036 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
2040 as_warn_where (fix->fx_file, fix->fx_line,
2041 _("can't find fixup pointed to by .uses"));
2045 if (fscan->fx_tcbit)
2047 /* We've already done this one. */
2051 /* The variable fscan should also be a fixup to a local symbol
2052 in the same section. */
2053 sym = fscan->fx_addsy;
2055 || fscan->fx_subsy != NULL
2056 || fscan->fx_addnumber != 0
2057 || S_GET_SEGMENT (sym) != sec
2058 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2059 || S_GET_STORAGE_CLASS (sym) == C_EXT
2061 || S_IS_EXTERNAL (sym))
2063 as_warn_where (fix->fx_file, fix->fx_line,
2064 _(".uses target does not refer to a local symbol in the same section"));
2068 /* Now we look through all the fixups of all the sections,
2069 counting the number of times we find a reference to sym. */
2072 #ifdef BFD_ASSEMBLER
2073 bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
2078 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
2079 sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
2086 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
2087 We have already adjusted the value of sym to include the
2088 fragment address, so we undo that adjustment here. */
2089 subseg_change (sec, 0);
2090 fix_new (symbol_get_frag (sym),
2091 S_GET_VALUE (sym) - symbol_get_frag (sym)->fr_address,
2092 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
2096 /* This function is called after the symbol table has been completed,
2097 but before the relocs or section contents have been written out.
2098 If we have seen any .uses pseudo-ops, they point to an instruction
2099 which loads a register with the address of a function. We look
2100 through the fixups to find where the function address is being
2101 loaded from. We then generate a COUNT reloc giving the number of
2102 times that function address is referred to. The linker uses this
2103 information when doing relaxing, to decide when it can eliminate
2104 the stored function address entirely. */
2112 #ifdef BFD_ASSEMBLER
2113 bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
2118 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
2119 sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
2124 /* Called after relaxing. Set the correct sizes of the fragments, and
2125 create relocs so that md_apply_fix will fill in the correct values. */
2128 md_convert_frag (headers, seg, fragP)
2129 #ifdef BFD_ASSEMBLER
2130 bfd *headers ATTRIBUTE_UNUSED;
2132 object_headers *headers;
2139 switch (fragP->fr_subtype)
2141 case C (COND_JUMP, COND8):
2142 case C (COND_JUMP_DELAY, COND8):
2143 subseg_change (seg, 0);
2144 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2145 1, BFD_RELOC_SH_PCDISP8BY2);
2150 case C (UNCOND_JUMP, UNCOND12):
2151 subseg_change (seg, 0);
2152 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2153 1, BFD_RELOC_SH_PCDISP12BY2);
2158 case C (UNCOND_JUMP, UNCOND32):
2159 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
2160 if (fragP->fr_symbol == NULL)
2161 as_bad (_("at 0x%lx, displacement overflows 12-bit field"),
2162 (unsigned long) fragP->fr_address);
2163 else if (S_IS_DEFINED (fragP->fr_symbol))
2164 as_bad (_("at 0x%lx, displacement to defined symbol %s overflows 12-bit field"),
2165 (unsigned long) fragP->fr_address,
2166 S_GET_NAME (fragP->fr_symbol));
2168 as_bad (_("at 0x%lx, displacement to undefined symbol %s overflows 12-bit field"),
2169 (unsigned long) fragP->fr_address,
2170 S_GET_NAME (fragP->fr_symbol));
2173 /* This code works, but generates poor code and the compiler
2174 should never produce a sequence that requires it to be used. */
2176 /* A jump wont fit in 12 bits, make code which looks like
2182 int t = buffer[0] & 0x10;
2184 buffer[highbyte ] = 0xa0; /* branch over move and disp */
2185 buffer[lowbyte ] = 3;
2186 buffer[highbyte + 2] = 0xd0 | JREG; /* Build mov insn */
2187 buffer[lowbyte + 2] = 0x00;
2189 buffer[highbyte + 4] = 0; /* space for 32 bit jump disp */
2190 buffer[lowbyte + 4] = 0;
2191 buffer[highbyte + 6] = 0;
2192 buffer[lowbyte + 6] = 0;
2194 buffer[highbyte + 8] = 0x40 | JREG; /* Build jmp @JREG */
2195 buffer[lowbyte + 8] = t ? 0xb : 0x2b;
2197 buffer[highbyte + 10] = 0x20; /* build nop */
2198 buffer[lowbyte + 10] = 0x0b;
2200 /* Make reloc for the long disp. */
2208 fragP->fr_fix += UNCOND32_LENGTH;
2215 case C (COND_JUMP, COND12):
2216 case C (COND_JUMP_DELAY, COND12):
2217 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
2218 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
2219 was due to gas incorrectly relaxing an out-of-range conditional
2220 branch with delay slot. It turned:
2221 bf.s L6 (slot mov.l r12,@(44,r0))
2224 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
2226 32: 10 cb mov.l r12,@(44,r0)
2227 Therefore, branches with delay slots have to be handled
2228 differently from ones without delay slots. */
2230 unsigned char *buffer =
2231 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
2232 int highbyte = target_big_endian ? 0 : 1;
2233 int lowbyte = target_big_endian ? 1 : 0;
2234 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
2236 /* Toggle the true/false bit of the bcond. */
2237 buffer[highbyte] ^= 0x2;
2239 /* If this is a dalayed branch, we may not put the the bra in the
2240 slot. So we change it to a non-delayed branch, like that:
2241 b! cond slot_label; bra disp; slot_label: slot_insn
2242 ??? We should try if swapping the conditional branch and
2243 its delay-slot insn already makes the branch reach. */
2245 /* Build a relocation to six / four bytes farther on. */
2246 subseg_change (seg, 0);
2247 fix_new (fragP, fragP->fr_fix, 2,
2248 #ifdef BFD_ASSEMBLER
2249 section_symbol (seg),
2251 seg_info (seg)->dot,
2253 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
2254 1, BFD_RELOC_SH_PCDISP8BY2);
2256 /* Set up a jump instruction. */
2257 buffer[highbyte + 2] = 0xa0;
2258 buffer[lowbyte + 2] = 0;
2259 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
2260 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
2264 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
2269 /* Fill in a NOP instruction. */
2270 buffer[highbyte + 4] = 0x0;
2271 buffer[lowbyte + 4] = 0x9;
2280 case C (COND_JUMP, COND32):
2281 case C (COND_JUMP_DELAY, COND32):
2282 case C (COND_JUMP, UNDEF_WORD_DISP):
2283 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
2284 if (fragP->fr_symbol == NULL)
2285 as_bad (_("at 0x%lx, displacement overflows 8-bit field"),
2286 (unsigned long) fragP->fr_address);
2287 else if (S_IS_DEFINED (fragP->fr_symbol))
2288 as_bad (_("at 0x%lx, displacement to defined symbol %s overflows 8-bit field "),
2289 (unsigned long) fragP->fr_address,
2290 S_GET_NAME (fragP->fr_symbol));
2292 as_bad (_("at 0x%lx, displacement to undefined symbol %s overflows 8-bit field "),
2293 (unsigned long) fragP->fr_address,
2294 S_GET_NAME (fragP->fr_symbol));
2297 /* This code works, but generates poor code, and the compiler
2298 should never produce a sequence that requires it to be used. */
2300 /* A bcond won't fit and it won't go into a 12 bit
2301 displacement either, the code sequence looks like:
2310 buffer[0] ^= 0x2; /* Toggle T/F bit */
2312 buffer[1] = 5; /* branch over mov, jump, nop and ptr */
2313 buffer[2] = 0xd0 | JREG; /* Build mov insn */
2315 buffer[4] = 0x40 | JREG; /* Build jmp @JREG */
2317 buffer[6] = 0x20; /* build nop */
2319 buffer[8] = 0; /* space for 32 bit jump disp */
2325 /* Make reloc for the long disp */
2333 fragP->fr_fix += COND32_LENGTH;
2344 if (donerelax && !sh_relax)
2345 as_warn_where (fragP->fr_file, fragP->fr_line,
2346 _("overflow in branch to %s; converted into longer instruction sequence"),
2347 (fragP->fr_symbol != NULL
2348 ? S_GET_NAME (fragP->fr_symbol)
2353 md_section_align (seg, size)
2357 #ifdef BFD_ASSEMBLER
2360 #else /* ! OBJ_ELF */
2361 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
2362 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
2363 #endif /* ! OBJ_ELF */
2364 #else /* ! BFD_ASSEMBLER */
2365 return ((size + (1 << section_alignment[(int) seg]) - 1)
2366 & (-1 << section_alignment[(int) seg]));
2367 #endif /* ! BFD_ASSEMBLER */
2370 /* This static variable is set by s_uacons to tell sh_cons_align that
2371 the expession does not need to be aligned. */
2373 static int sh_no_align_cons = 0;
2375 /* This handles the unaligned space allocation pseudo-ops, such as
2376 .uaword. .uaword is just like .word, but the value does not need
2383 /* Tell sh_cons_align not to align this value. */
2384 sh_no_align_cons = 1;
2388 /* If a .word, et. al., pseud-op is seen, warn if the value is not
2389 aligned correctly. Note that this can cause warnings to be issued
2390 when assembling initialized structured which were declared with the
2391 packed attribute. FIXME: Perhaps we should require an option to
2392 enable this warning? */
2395 sh_cons_align (nbytes)
2401 if (sh_no_align_cons)
2403 /* This is an unaligned pseudo-op. */
2404 sh_no_align_cons = 0;
2409 while ((nbytes & 1) == 0)
2418 if (now_seg == absolute_section)
2420 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
2421 as_warn (_("misaligned data"));
2425 p = frag_var (rs_align_code, 1, 1, (relax_substateT) 0,
2426 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
2428 record_alignment (now_seg, nalign);
2431 /* When relaxing, we need to output a reloc for any .align directive
2432 that requests alignment to a four byte boundary or larger. This is
2433 also where we check for misaligned data. */
2436 sh_handle_align (frag)
2440 && frag->fr_type == rs_align
2441 && frag->fr_address + frag->fr_fix > 0
2442 && frag->fr_offset > 1
2443 && now_seg != bss_section)
2444 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
2445 BFD_RELOC_SH_ALIGN);
2447 if (frag->fr_type == rs_align_code
2448 && frag->fr_next->fr_address - frag->fr_address - frag->fr_fix != 0)
2449 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
2452 /* This macro decides whether a particular reloc is an entry in a
2453 switch table. It is used when relaxing, because the linker needs
2454 to know about all such entries so that it can adjust them if
2457 #ifdef BFD_ASSEMBLER
2458 #define SWITCH_TABLE_CONS(fix) (0)
2460 #define SWITCH_TABLE_CONS(fix) \
2461 ((fix)->fx_r_type == 0 \
2462 && ((fix)->fx_size == 2 \
2463 || (fix)->fx_size == 1 \
2464 || (fix)->fx_size == 4))
2467 #define SWITCH_TABLE(fix) \
2468 ((fix)->fx_addsy != NULL \
2469 && (fix)->fx_subsy != NULL \
2470 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
2471 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
2472 && ((fix)->fx_r_type == BFD_RELOC_32 \
2473 || (fix)->fx_r_type == BFD_RELOC_16 \
2474 || (fix)->fx_r_type == BFD_RELOC_8 \
2475 || SWITCH_TABLE_CONS (fix)))
2477 /* See whether we need to force a relocation into the output file.
2478 This is used to force out switch and PC relative relocations when
2482 sh_force_relocation (fix)
2486 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2487 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2488 || fix->fx_r_type == BFD_RELOC_SH_LOOP_START
2489 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END)
2495 return (fix->fx_pcrel
2496 || SWITCH_TABLE (fix)
2497 || fix->fx_r_type == BFD_RELOC_SH_COUNT
2498 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
2499 || fix->fx_r_type == BFD_RELOC_SH_CODE
2500 || fix->fx_r_type == BFD_RELOC_SH_DATA
2501 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
2506 sh_fix_adjustable (fixP)
2510 if (fixP->fx_addsy == NULL)
2513 /* We need the symbol name for the VTABLE entries */
2514 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2515 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2522 sh_elf_final_processing ()
2526 /* Set file-specific flags to indicate if this code needs
2527 a processor with the sh-dsp / sh3e ISA to execute. */
2528 if (valid_arch & arch_sh1)
2530 else if (valid_arch & arch_sh2)
2532 else if (valid_arch & arch_sh_dsp)
2534 else if (valid_arch & arch_sh3)
2536 else if (valid_arch & arch_sh3_dsp)
2538 else if (valid_arch & arch_sh3e)
2540 else if (valid_arch & arch_sh4)
2545 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
2546 elf_elfheader (stdoutput)->e_flags |= val;
2550 /* Apply a fixup to the object file. */
2552 #ifdef BFD_ASSEMBLER
2554 md_apply_fix (fixP, valp)
2559 md_apply_fix (fixP, val)
2564 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2565 int lowbyte = target_big_endian ? 1 : 0;
2566 int highbyte = target_big_endian ? 0 : 1;
2567 #ifdef BFD_ASSEMBLER
2573 #ifdef BFD_ASSEMBLER
2574 /* The function adjust_reloc_syms won't convert a reloc against a weak
2575 symbol into a reloc against a section, but bfd_install_relocation
2576 will screw up if the symbol is defined, so we have to adjust val here
2577 to avoid the screw up later. */
2578 if (fixP->fx_addsy != NULL
2579 && S_IS_WEAK (fixP->fx_addsy))
2580 val -= S_GET_VALUE (fixP->fx_addsy);
2583 #ifndef BFD_ASSEMBLER
2584 if (fixP->fx_r_type == 0)
2586 if (fixP->fx_size == 2)
2587 fixP->fx_r_type = BFD_RELOC_16;
2588 else if (fixP->fx_size == 4)
2589 fixP->fx_r_type = BFD_RELOC_32;
2590 else if (fixP->fx_size == 1)
2591 fixP->fx_r_type = BFD_RELOC_8;
2599 switch (fixP->fx_r_type)
2601 case BFD_RELOC_SH_IMM4:
2603 *buf = (*buf & 0xf0) | (val & 0xf);
2606 case BFD_RELOC_SH_IMM4BY2:
2609 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
2612 case BFD_RELOC_SH_IMM4BY4:
2615 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
2618 case BFD_RELOC_SH_IMM8BY2:
2624 case BFD_RELOC_SH_IMM8BY4:
2631 case BFD_RELOC_SH_IMM8:
2632 /* Sometimes the 8 bit value is sign extended (e.g., add) and
2633 sometimes it is not (e.g., and). We permit any 8 bit value.
2634 Note that adding further restrictions may invalidate
2635 reasonable looking assembly code, such as ``and -0x1,r0''. */
2641 case BFD_RELOC_SH_PCRELIMM8BY4:
2642 /* The lower two bits of the PC are cleared before the
2643 displacement is added in. We can assume that the destination
2644 is on a 4 byte bounday. If this instruction is also on a 4
2645 byte boundary, then we want
2647 and target - here is a multiple of 4.
2648 Otherwise, we are on a 2 byte boundary, and we want
2649 (target - (here - 2)) / 4
2650 and target - here is not a multiple of 4. Computing
2651 (target - (here - 2)) / 4 == (target - here + 2) / 4
2652 works for both cases, since in the first case the addition of
2653 2 will be removed by the division. target - here is in the
2655 val = (val + 2) / 4;
2657 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2661 case BFD_RELOC_SH_PCRELIMM8BY2:
2664 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2668 case BFD_RELOC_SH_PCDISP8BY2:
2670 if (val < -0x80 || val > 0x7f)
2671 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2675 case BFD_RELOC_SH_PCDISP12BY2:
2677 if (val < -0x800 || val >= 0x7ff)
2678 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2679 buf[lowbyte] = val & 0xff;
2680 buf[highbyte] |= (val >> 8) & 0xf;
2684 md_number_to_chars (buf, val, 4);
2688 md_number_to_chars (buf, val, 2);
2691 case BFD_RELOC_SH_USES:
2692 /* Pass the value into sh_coff_reloc_mangle. */
2693 fixP->fx_addnumber = val;
2696 case BFD_RELOC_SH_COUNT:
2697 case BFD_RELOC_SH_ALIGN:
2698 case BFD_RELOC_SH_CODE:
2699 case BFD_RELOC_SH_DATA:
2700 case BFD_RELOC_SH_LABEL:
2701 /* Nothing to do here. */
2704 case BFD_RELOC_SH_LOOP_START:
2705 case BFD_RELOC_SH_LOOP_END:
2707 case BFD_RELOC_VTABLE_INHERIT:
2708 case BFD_RELOC_VTABLE_ENTRY:
2710 #ifdef BFD_ASSEMBLER
2722 if ((val & ((1 << shift) - 1)) != 0)
2723 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
2727 val = ((val >> shift)
2728 | ((long) -1 & ~ ((long) -1 >> shift)));
2730 if (max != 0 && (val < min || val > max))
2731 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
2733 #ifdef BFD_ASSEMBLER
2738 /* Called just before address relaxation. Return the length
2739 by which a fragment must grow to reach it's destination. */
2742 md_estimate_size_before_relax (fragP, segment_type)
2743 register fragS *fragP;
2744 register segT segment_type;
2746 switch (fragP->fr_subtype)
2748 case C (UNCOND_JUMP, UNDEF_DISP):
2749 /* Used to be a branch to somewhere which was unknown. */
2750 if (!fragP->fr_symbol)
2752 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
2753 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
2755 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2757 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
2758 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
2762 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
2763 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
2764 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
2770 case C (COND_JUMP, UNDEF_DISP):
2771 case C (COND_JUMP_DELAY, UNDEF_DISP):
2772 /* Used to be a branch to somewhere which was unknown. */
2773 if (fragP->fr_symbol
2774 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2776 int what = GET_WHAT (fragP->fr_subtype);
2777 /* Got a symbol and it's defined in this segment, become byte
2778 sized - maybe it will fix up. */
2779 fragP->fr_subtype = C (what, COND8);
2780 fragP->fr_var = md_relax_table[C (what, COND8)].rlx_length;
2782 else if (fragP->fr_symbol)
2784 int what = GET_WHAT (fragP->fr_subtype);
2785 /* Its got a segment, but its not ours, so it will always be long. */
2786 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
2787 fragP->fr_var = md_relax_table[C (what, COND32)].rlx_length;
2788 return md_relax_table[C (what, COND32)].rlx_length;
2792 int what = GET_WHAT (fragP->fr_subtype);
2793 /* We know the abs value. */
2794 fragP->fr_subtype = C (what, COND8);
2795 fragP->fr_var = md_relax_table[C (what, COND8)].rlx_length;
2800 return fragP->fr_var;
2803 /* Put number into target byte order. */
2806 md_number_to_chars (ptr, use, nbytes)
2811 if (! target_big_endian)
2812 number_to_chars_littleendian (ptr, use, nbytes);
2814 number_to_chars_bigendian (ptr, use, nbytes);
2818 md_pcrel_from (fixP)
2821 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
2827 tc_coff_sizemachdep (frag)
2830 return md_relax_table[frag->fr_subtype].rlx_length;
2833 #endif /* OBJ_COFF */
2835 /* When we align the .text section, insert the correct NOP pattern. */
2838 sh_do_align (n, fill, len, max)
2841 int len ATTRIBUTE_UNUSED;
2845 && subseg_text_p (now_seg)
2848 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
2849 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
2851 /* First align to a 2 byte boundary, in case there is an odd
2853 frag_align (1, 0, 0);
2854 if (target_big_endian)
2855 frag_align_pattern (n, big_nop_pattern, sizeof big_nop_pattern, max);
2857 frag_align_pattern (n, little_nop_pattern, sizeof little_nop_pattern,
2865 #ifndef BFD_ASSEMBLER
2868 /* Map BFD relocs to SH COFF relocs. */
2872 bfd_reloc_code_real_type bfd_reloc;
2876 static const struct reloc_map coff_reloc_map[] =
2878 { BFD_RELOC_32, R_SH_IMM32 },
2879 { BFD_RELOC_16, R_SH_IMM16 },
2880 { BFD_RELOC_8, R_SH_IMM8 },
2881 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
2882 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
2883 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
2884 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
2885 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
2886 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
2887 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
2888 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
2889 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
2890 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
2891 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
2892 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
2893 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
2894 { BFD_RELOC_SH_USES, R_SH_USES },
2895 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
2896 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
2897 { BFD_RELOC_SH_CODE, R_SH_CODE },
2898 { BFD_RELOC_SH_DATA, R_SH_DATA },
2899 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
2900 { BFD_RELOC_UNUSED, 0 }
2903 /* Adjust a reloc for the SH. This is similar to the generic code,
2904 but does some minor tweaking. */
2907 sh_coff_reloc_mangle (seg, fix, intr, paddr)
2908 segment_info_type *seg;
2910 struct internal_reloc *intr;
2913 symbolS *symbol_ptr = fix->fx_addsy;
2916 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
2918 if (! SWITCH_TABLE (fix))
2920 const struct reloc_map *rm;
2922 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
2923 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
2925 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
2926 as_bad_where (fix->fx_file, fix->fx_line,
2927 _("Can not represent %s relocation in this object file format"),
2928 bfd_get_reloc_code_name (fix->fx_r_type));
2929 intr->r_type = rm->sh_reloc;
2936 if (fix->fx_r_type == BFD_RELOC_16)
2937 intr->r_type = R_SH_SWITCH16;
2938 else if (fix->fx_r_type == BFD_RELOC_8)
2939 intr->r_type = R_SH_SWITCH8;
2940 else if (fix->fx_r_type == BFD_RELOC_32)
2941 intr->r_type = R_SH_SWITCH32;
2945 /* For a switch reloc, we set r_offset to the difference between
2946 the reloc address and the subtrahend. When the linker is
2947 doing relaxing, it can use the determine the starting and
2948 ending points of the switch difference expression. */
2949 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
2952 /* PC relative relocs are always against the current section. */
2953 if (symbol_ptr == NULL)
2955 switch (fix->fx_r_type)
2957 case BFD_RELOC_SH_PCRELIMM8BY2:
2958 case BFD_RELOC_SH_PCRELIMM8BY4:
2959 case BFD_RELOC_SH_PCDISP8BY2:
2960 case BFD_RELOC_SH_PCDISP12BY2:
2961 case BFD_RELOC_SH_USES:
2962 symbol_ptr = seg->dot;
2969 if (fix->fx_r_type == BFD_RELOC_SH_USES)
2971 /* We can't store the offset in the object file, since this
2972 reloc does not take up any space, so we store it in r_offset.
2973 The fx_addnumber field was set in md_apply_fix. */
2974 intr->r_offset = fix->fx_addnumber;
2976 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
2978 /* We can't store the count in the object file, since this reloc
2979 does not take up any space, so we store it in r_offset. The
2980 fx_offset field was set when the fixup was created in
2981 sh_coff_frob_file. */
2982 intr->r_offset = fix->fx_offset;
2983 /* This reloc is always absolute. */
2986 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
2988 /* Store the alignment in the r_offset field. */
2989 intr->r_offset = fix->fx_offset;
2990 /* This reloc is always absolute. */
2993 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
2994 || fix->fx_r_type == BFD_RELOC_SH_DATA
2995 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
2997 /* These relocs are always absolute. */
3001 /* Turn the segment of the symbol into an offset. */
3002 if (symbol_ptr != NULL)
3004 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
3006 intr->r_symndx = dot->sy_number;
3008 intr->r_symndx = symbol_ptr->sy_number;
3011 intr->r_symndx = -1;
3014 #endif /* OBJ_COFF */
3015 #endif /* ! BFD_ASSEMBLER */
3017 #ifdef BFD_ASSEMBLER
3019 /* Create a reloc. */
3022 tc_gen_reloc (section, fixp)
3023 asection *section ATTRIBUTE_UNUSED;
3027 bfd_reloc_code_real_type r_type;
3029 rel = (arelent *) xmalloc (sizeof (arelent));
3030 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3031 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3032 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3034 r_type = fixp->fx_r_type;
3036 if (SWITCH_TABLE (fixp))
3038 rel->addend = rel->address - S_GET_VALUE (fixp->fx_subsy);
3039 if (r_type == BFD_RELOC_16)
3040 r_type = BFD_RELOC_SH_SWITCH16;
3041 else if (r_type == BFD_RELOC_8)
3042 r_type = BFD_RELOC_8_PCREL;
3043 else if (r_type == BFD_RELOC_32)
3044 r_type = BFD_RELOC_SH_SWITCH32;
3048 else if (r_type == BFD_RELOC_SH_USES)
3049 rel->addend = fixp->fx_addnumber;
3050 else if (r_type == BFD_RELOC_SH_COUNT)
3051 rel->addend = fixp->fx_offset;
3052 else if (r_type == BFD_RELOC_SH_ALIGN)
3053 rel->addend = fixp->fx_offset;
3054 else if (r_type == BFD_RELOC_VTABLE_INHERIT
3055 || r_type == BFD_RELOC_VTABLE_ENTRY)
3056 rel->addend = fixp->fx_offset;
3057 else if (r_type == BFD_RELOC_SH_LOOP_START
3058 || r_type == BFD_RELOC_SH_LOOP_END)
3059 rel->addend = fixp->fx_offset;
3060 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
3063 rel->address = rel->addend = fixp->fx_offset;
3065 else if (fixp->fx_pcrel)
3066 rel->addend = fixp->fx_addnumber;
3070 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
3071 if (rel->howto == NULL)
3073 as_bad_where (fixp->fx_file, fixp->fx_line,
3074 _("Cannot represent relocation type %s"),
3075 bfd_get_reloc_code_name (r_type));
3076 /* Set howto to a garbage value so that we can keep going. */
3077 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3078 assert (rel->howto != NULL);
3084 #endif /* BFD_ASSEMBLER */