1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
3 Copyright (C) 1993, 94, 95, 1996 Free Software Foundation.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 Written By Steve Chamberlain
32 #include "opcodes/sh-opc.h"
34 const char comment_chars[] = "!";
35 const char line_separator_chars[] = ";";
36 const char line_comment_chars[] = "!#";
38 static void s_uses PARAMS ((int));
40 /* This table describes all the machine specific pseudo-ops the assembler
41 has to support. The fields are:
42 pseudo-op name without dot
43 function to call to execute this pseudo-op
44 Integer arg to pass to the function
48 void s_align_bytes ();
57 target_big_endian = 0;
60 const pseudo_typeS md_pseudo_table[] =
64 {"form", listing_psize, 0},
65 {"little", little, 0},
66 {"heading", listing_title, 0},
67 {"import", s_ignore, 0},
68 {"page", listing_eject, 0},
69 {"program", s_ignore, 0},
74 /*int md_reloc_size; */
76 int sh_relax; /* set if -relax seen */
78 const char EXP_CHARS[] = "eE";
80 /* Chars that mean this number is a floating point constant */
83 const char FLT_CHARS[] = "rRsSfFdDxXpP";
85 #define C(a,b) ENCODE_RELAX(a,b)
87 #define JREG 14 /* Register used as a temp when relaxing */
88 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
89 #define GET_WHAT(x) ((x>>4))
91 /* These are the two types of relaxable instrction */
101 #define UNDEF_WORD_DISP 4
107 /* Branch displacements are from the address of the branch plus
108 four, thus all minimum and maximum values have 4 added to them. */
111 #define COND8_LENGTH 2
113 /* There is one extra instruction before the branch, so we must add
114 two more bytes to account for it. */
115 #define COND12_F 4100
116 #define COND12_M -4090
117 #define COND12_LENGTH 6
119 /* ??? The minimum and maximum values are wrong, but this does not matter
120 since this relocation type is not supported yet. */
121 #define COND32_F (1<<30)
122 #define COND32_M -(1<<30)
123 #define COND32_LENGTH 14
125 #define UNCOND12_F 4098
126 #define UNCOND12_M -4092
127 #define UNCOND12_LENGTH 2
129 /* ??? The minimum and maximum values are wrong, but this does not matter
130 since this relocation type is not supported yet. */
131 #define UNCOND32_F (1<<30)
132 #define UNCOND32_M -(1<<30)
133 #define UNCOND32_LENGTH 14
135 const relax_typeS md_relax_table[C (END, 0)] = {
136 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
137 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
140 /* C (COND_JUMP, COND8) */
141 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
142 /* C (COND_JUMP, COND12) */
143 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
144 /* C (COND_JUMP, COND32) */
145 { COND32_F, COND32_M, COND32_LENGTH, 0, },
146 { 0 }, { 0 }, { 0 }, { 0 },
147 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
150 /* C (UNCOND_JUMP, UNCOND12) */
151 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
152 /* C (UNCOND_JUMP, UNCOND32) */
153 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
154 { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
155 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
158 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
161 This function is called once, at assembler startup time. This should
162 set up all the tables, etc that the MD part of the assembler needs
168 sh_opcode_info *opcode;
169 char *prev_name = "";
172 target_big_endian = 1;
174 opcode_hash_control = hash_new ();
176 /* Insert unique names into hash table */
177 for (opcode = sh_table; opcode->name; opcode++)
179 if (strcmp (prev_name, opcode->name))
181 prev_name = opcode->name;
182 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
186 /* Make all the opcodes with the same name point to the same
188 opcode->name = prev_name;
197 static expressionS immediate; /* absolute expression */
207 /* try and parse a reg name, returns number of chars consumed */
209 parse_reg (src, mode, reg)
214 /* We use !isalnum for the next character after the register name, to
215 make sure that we won't accidentally recognize a symbol name such as
216 'sram' as being a reference to the register 'sr'. */
220 if (src[1] >= '0' && src[1] <= '7' && strncmp(&src[2], "_bank", 5) == 0
221 && ! isalnum (src[7]))
224 *reg = (src[1] - '0');
233 if (src[2] >= '0' && src[2] <= '5' && ! isalnum (src[3]))
236 *reg = 10 + src[2] - '0';
240 if (src[1] >= '0' && src[1] <= '9' && ! isalnum (src[2]))
243 *reg = (src[1] - '0');
248 if (src[0] == 's' && src[1] == 's' && src[2] == 'r' && ! isalnum (src[3]))
254 if (src[0] == 's' && src[1] == 'p' && src[2] == 'c' && ! isalnum (src[3]))
260 if (src[0] == 's' && src[1] == 'r' && ! isalnum (src[2]))
266 if (src[0] == 's' && src[1] == 'p' && ! isalnum (src[2]))
273 if (src[0] == 'p' && src[1] == 'r' && ! isalnum (src[2]))
278 if (src[0] == 'p' && src[1] == 'c' && ! isalnum (src[2]))
283 if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r' && ! isalnum (src[3]))
288 if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r' && ! isalnum (src[3]))
294 if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c' && ! isalnum (src[4]))
307 if (src[0] == 'f' && src[1] == 'r')
311 if (src[3] >= '0' && src[3] <= '5' && ! isalnum (src[4]))
314 *reg = 10 + src[3] - '0';
318 if (src[2] >= '0' && src[2] <= '9' && ! isalnum (src[3]))
321 *reg = (src[2] - '0');
325 if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l'
326 && ! isalnum (src[4]))
332 if (src[0] == 'f' && src[1] == 'p' && src[2] == 's' && src[3] == 'c'
333 && src[4] == 'r' && ! isalnum (src[5]))
342 static symbolS *dot()
346 /* JF: '.' is pseudo symbol with value of current location
347 in current segment. */
348 fake = FAKE_LABEL_NAME;
349 return symbol_new (fake,
351 (valueT) frag_now_fix (),
365 save = input_line_pointer;
366 input_line_pointer = s;
367 expression (&immediate);
368 if (immediate.X_op == O_absent)
369 as_bad ("missing operand");
370 new = input_line_pointer;
371 input_line_pointer = save;
376 /* The many forms of operand:
379 @Rn Register indirect
392 pr, gbr, vbr, macl, mach
407 /* Must be predecrement */
410 len = parse_reg (src, &mode, &(op->reg));
412 as_bad ("illegal register after @-");
417 else if (src[0] == '(')
419 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
422 len = parse_reg (src, &mode, &(op->reg));
423 if (len && mode == A_REG_N)
428 as_bad ("must be @(r0,...)");
432 /* Now can be rn or gbr */
433 len = parse_reg (src, &mode, &(op->reg));
438 else if (mode == A_REG_N)
440 op->type = A_IND_R0_REG_N;
444 as_bad ("syntax error in @(r0,...)");
449 /* Must be an @(disp,.. thing) */
450 src = parse_exp (src);
453 /* Now can be rn, gbr or pc */
454 len = parse_reg (src, &mode, &op->reg);
459 op->type = A_DISP_REG_N;
461 else if (mode == A_GBR)
463 op->type = A_DISP_GBR;
465 else if (mode == A_DISP_PC)
467 /* Turn a plain @(4,pc) into @(.+4,pc) */
468 if (immediate.X_op == O_constant) {
469 immediate.X_add_symbol = dot();
470 immediate.X_op = O_symbol;
472 op->type = A_DISP_PC;
476 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
481 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
486 as_bad ("expecting )");
492 src += parse_reg (src, &mode, &(op->reg));
495 as_bad ("illegal register after @");
511 get_operand (ptr, op)
522 *ptr = parse_exp (src);
527 else if (src[0] == '@')
529 *ptr = parse_at (src, op);
532 len = parse_reg (src, &mode, &(op->reg));
541 /* Not a reg, the only thing left is a displacement */
542 *ptr = parse_exp (src);
543 op->type = A_DISP_PC;
550 get_operands (info, args, operand)
551 sh_opcode_info *info;
553 sh_operand_info *operand;
561 get_operand (&ptr, operand + 0);
568 get_operand (&ptr, operand + 1);
575 get_operand (&ptr, operand + 2);
597 /* Passed a pointer to a list of opcodes which use different
598 addressing modes, return the opcode which matches the opcodes
604 get_specific (opcode, operands)
605 sh_opcode_info *opcode;
606 sh_operand_info *operands;
608 sh_opcode_info *this_try = opcode;
609 char *name = opcode->name;
614 if (this_try->name != name)
616 /* We've looked so far down the table that we've run out of
617 opcodes with the same name */
620 /* look at both operands needed by the opcodes and provided by
621 the user - since an arg test will often fail on the same arg
622 again and again, we'll try and test the last failing arg the
623 first on each opcode try */
625 for (n = 0; this_try->arg[n]; n++)
627 sh_operand_info *user = operands + n;
628 sh_arg_type arg = this_try->arg[n];
639 if (user->type != arg)
643 /* opcode needs r0 */
644 if (user->type != A_REG_N || user->reg != 0)
648 if (user->type != A_R0_GBR || user->reg != 0)
652 if (user->type != F_REG_N || user->reg != 0)
665 /* Opcode needs rn */
666 if (user->type != arg)
675 if (user->type != arg)
680 if (user->type != arg)
691 /* Opcode needs rn */
692 if (user->type != arg - A_REG_M + A_REG_N)
700 /* Opcode needs rn */
701 if (user->type != arg - F_REG_M + F_REG_N)
707 printf ("unhandled %d\n", arg);
719 check (operand, low, high)
720 expressionS *operand;
724 if (operand->X_op != O_constant
725 || operand->X_add_number < low
726 || operand->X_add_number > high)
728 as_bad ("operand must be absolute in range %d..%d", low, high);
730 return operand->X_add_number;
735 insert (where, how, pcrel)
740 fix_new_exp (frag_now,
741 where - frag_now->fr_literal,
750 sh_opcode_info *opcode;
752 int high_byte = target_big_endian ? 0 : 1;
755 if (opcode->arg[0] == A_BDISP8)
757 p = frag_var (rs_machine_dependent,
758 md_relax_table[C (COND_JUMP, COND32)].rlx_length,
759 md_relax_table[C (COND_JUMP, COND8)].rlx_length,
761 immediate.X_add_symbol,
762 immediate.X_add_number,
764 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
766 else if (opcode->arg[0] == A_BDISP12)
768 p = frag_var (rs_machine_dependent,
769 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
770 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
772 immediate.X_add_symbol,
773 immediate.X_add_number,
775 p[high_byte] = (opcode->nibbles[0] << 4);
780 /* Now we know what sort of opcodes it is, lets build the bytes -
783 build_Mytes (opcode, operand)
784 sh_opcode_info *opcode;
785 sh_operand_info *operand;
790 char *output = frag_more (2);
791 int low_byte = target_big_endian ? 1 : 0;
797 for (index = 0; index < 4; index++)
799 sh_nibble_type i = opcode->nibbles[index];
815 nbuf[index] = reg_b | 0x08;
818 insert (output + low_byte, R_SH_IMM4, 0);
821 insert (output + low_byte, R_SH_IMM4BY4, 0);
824 insert (output + low_byte, R_SH_IMM4BY2, 0);
827 insert (output + low_byte, R_SH_IMM4, 0);
830 insert (output + low_byte, R_SH_IMM8BY4, 0);
833 insert (output + low_byte, R_SH_IMM8BY2, 0);
836 insert (output + low_byte, R_SH_IMM8, 0);
839 insert (output, R_SH_PCRELIMM8BY4, 1);
842 insert (output, R_SH_PCRELIMM8BY2, 1);
845 printf ("failed for %d\n", i);
849 if (! target_big_endian) {
850 output[1] = (nbuf[0] << 4) | (nbuf[1]);
851 output[0] = (nbuf[2] << 4) | (nbuf[3]);
854 output[0] = (nbuf[0] << 4) | (nbuf[1]);
855 output[1] = (nbuf[2] << 4) | (nbuf[3]);
859 /* This is the guts of the machine-dependent assembler. STR points to a
860 machine dependent instruction. This function is supposed to emit
861 the frags/bytes it assembles to.
868 unsigned char *op_start;
869 unsigned char *op_end;
870 sh_operand_info operand[3];
871 sh_opcode_info *opcode;
874 /* Drop leading whitespace */
878 /* find the op code end */
879 for (op_start = op_end = (unsigned char *) (str);
882 && !is_end_of_line[*op_end] && *op_end != ' ';
885 name[nlen] = op_start[nlen];
892 as_bad ("can't find opcode ");
895 opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
899 as_bad ("unknown opcode");
903 if (opcode->arg[0] == A_BDISP12
904 || opcode->arg[0] == A_BDISP8)
906 parse_exp (op_end + 1);
907 build_relax (opcode);
911 if (opcode->arg[0] != A_END)
913 get_operands (opcode, op_end, operand);
915 opcode = get_specific (opcode, operand);
919 /* Couldn't find an opcode which matched the operands */
920 char *where = frag_more (2);
924 as_bad ("invalid operands for opcode");
928 build_Mytes (opcode, operand);
934 DEFUN (tc_crawl_symbol_chain, (headers),
935 object_headers * headers)
937 printf ("call to tc_crawl_symbol_chain \n");
941 DEFUN (md_undefined_symbol, (name),
948 DEFUN (tc_headers_hook, (headers),
949 object_headers * headers)
951 printf ("call to tc_headers_hook \n");
954 /* Various routines to kill one day */
955 /* Equal to MAX_PRECISION in atof-ieee.c */
956 #define MAX_LITTLENUMS 6
958 /* Turn a string in input_line_pointer into a floating point constant of type
959 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
960 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
963 md_atof (type, litP, sizeP)
969 LITTLENUM_TYPE words[4];
985 return "bad call to md_atof";
988 t = atof_ieee (input_line_pointer, type, words);
990 input_line_pointer = t;
994 if (! target_big_endian)
996 for (i = prec - 1; i >= 0; i--)
998 md_number_to_chars (litP, (valueT) words[i], 2);
1004 for (i = 0; i < prec; i++)
1006 md_number_to_chars (litP, (valueT) words[i], 2);
1014 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
1015 call instruction. It refers to a label of the instruction which
1016 loads the register which the call uses. We use it to generate a
1017 special reloc for the linker. */
1026 as_warn (".uses pseudo-op seen when not relaxing");
1030 if (ex.X_op != O_symbol || ex.X_add_number != 0)
1032 as_bad ("bad .uses format");
1033 ignore_rest_of_line ();
1037 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, R_SH_USES);
1039 demand_empty_rest_of_line ();
1042 CONST char *md_shortopts = "";
1043 struct option md_longopts[] = {
1045 #define OPTION_RELAX (OPTION_MD_BASE)
1046 #define OPTION_LITTLE (OPTION_MD_BASE+1)
1048 {"relax", no_argument, NULL, OPTION_RELAX},
1049 {"little", no_argument, NULL, OPTION_LITTLE},
1050 {NULL, no_argument, NULL, 0}
1052 size_t md_longopts_size = sizeof(md_longopts);
1055 md_parse_option (c, arg)
1066 target_big_endian = 0;
1077 md_show_usage (stream)
1082 -little generate little endian code\n\
1083 -relax alter jump instructions for long displacements\n");
1086 int md_short_jump_size;
1089 tc_Nout_fix_to_chars ()
1091 printf ("call to tc_Nout_fix_to_chars \n");
1096 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1103 as_fatal ("failed sanity check.");
1107 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1109 addressT from_Nddr, to_Nddr;
1113 as_fatal ("failed sanity check.");
1116 /* This is function is called after the symbol table has been
1117 completed, but before md_convert_frag has been called. If we have
1118 seen any .uses pseudo-ops, they point to an instruction which loads
1119 a register with the address of a function. We look through the
1120 fixups to find where the function address is being loaded from. We
1121 then generate a COUNT reloc giving the number of times that
1122 function address is referred to. The linker uses this information
1123 when doing relaxing, to decide when it can eliminate the stored
1124 function address entirely. */
1127 sh_coff_frob_file ()
1134 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
1138 for (fix = segment_info[iseg].fix_root; fix != NULL; fix = fix->fx_next)
1147 if (fix->fx_r_type != R_SH_USES)
1150 /* The R_SH_USES reloc should refer to a defined local
1151 symbol in the same section. */
1152 sym = fix->fx_addsy;
1154 || fix->fx_subsy != NULL
1155 || fix->fx_addnumber != 0
1156 || S_GET_SEGMENT (sym) != iseg
1157 || S_GET_STORAGE_CLASS (sym) == C_EXT)
1159 as_warn_where (fix->fx_file, fix->fx_line,
1160 ".uses does not refer to a local symbol in the same section");
1164 /* Look through the fixups again, this time looking for one
1165 at the same location as sym. */
1166 val = S_GET_VALUE (sym);
1167 paddr = segment_info[iseg].scnhdr.s_paddr;
1168 for (fscan = segment_info[iseg].fix_root;
1170 fscan = fscan->fx_next)
1171 if (val == paddr + fscan->fx_frag->fr_address + fscan->fx_where)
1175 as_warn_where (fix->fx_file, fix->fx_line,
1176 "can't find fixup pointed to by .uses");
1180 if (fscan->fx_tcbit)
1182 /* We've already done this one. */
1186 /* fscan should also be a fixup to a local symbol in the same
1188 sym = fscan->fx_addsy;
1190 || fscan->fx_subsy != NULL
1191 || fscan->fx_addnumber != 0
1192 || S_GET_SEGMENT (sym) != iseg
1193 || S_GET_STORAGE_CLASS (sym) == C_EXT)
1195 as_warn_where (fix->fx_file, fix->fx_line,
1196 ".uses target does not refer to a local symbol in the same section");
1200 /* Now we look through all the fixups of all the sections,
1201 counting the number of times we find a reference to sym. */
1203 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
1205 paddr = segment_info[iscan].scnhdr.s_paddr;
1206 for (fscan = segment_info[iscan].fix_root;
1208 fscan = fscan->fx_next)
1210 if (fscan->fx_addsy == sym)
1213 fscan->fx_tcbit = 1;
1221 /* Generate a R_SH_COUNT fixup at the location of sym. We
1222 have already adjusted the value of sym to include the
1223 fragment address, so we undo that adjustment here. */
1224 subseg_change (iseg, 0);
1225 fix_new (sym->sy_frag, S_GET_VALUE (sym) - sym->sy_frag->fr_address,
1226 4, &abs_symbol, count, 0, R_SH_COUNT);
1231 /* Called after relaxing. Set the correct sizes of the fragments, and
1232 create relocs so that md_apply_fix will fill in the correct values. */
1235 md_convert_frag (headers, seg, fragP)
1236 object_headers *headers;
1242 switch (fragP->fr_subtype)
1244 case C (COND_JUMP, COND8):
1245 subseg_change (seg, 0);
1246 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1247 1, R_SH_PCDISP8BY2);
1252 case C (UNCOND_JUMP, UNCOND12):
1253 subseg_change (seg, 0);
1254 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1260 case C (UNCOND_JUMP, UNCOND32):
1261 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
1262 if (fragP->fr_symbol == NULL)
1263 as_bad ("at 0x%lx, displacement overflows 12-bit field",
1264 (unsigned long) fragP->fr_address);
1266 as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 12-bit field",
1267 (unsigned long) fragP->fr_address,
1268 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1269 S_GET_NAME (fragP->fr_symbol));
1271 #if 0 /* This code works, but generates poor code and the compiler
1272 should never produce a sequence that requires it to be used. */
1274 /* A jump wont fit in 12 bits, make code which looks like
1280 int t = buffer[0] & 0x10;
1282 buffer[highbyte] = 0xa0; /* branch over move and disp */
1283 buffer[lowbyte] = 3;
1284 buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
1285 buffer[lowbyte+2] = 0x00;
1287 buffer[highbyte+4] = 0; /* space for 32 bit jump disp */
1288 buffer[lowbyte+4] = 0;
1289 buffer[highbyte+6] = 0;
1290 buffer[lowbyte+6] = 0;
1292 buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
1293 buffer[lowbyte+8] = t ? 0xb : 0x2b;
1295 buffer[highbyte+10] = 0x20; /* build nop */
1296 buffer[lowbyte+10] = 0x0b;
1298 /* Make reloc for the long disp */
1306 fragP->fr_fix += UNCOND32_LENGTH;
1313 case C (COND_JUMP, COND12):
1314 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1316 unsigned char *buffer =
1317 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1318 int highbyte = target_big_endian ? 0 : 1;
1319 int lowbyte = target_big_endian ? 1 : 0;
1321 /* Toggle the true/false bit of the bcond. */
1322 buffer[highbyte] ^= 0x2;
1324 /* Build a relocation to six bytes farther on. */
1325 subseg_change (seg, 0);
1326 fix_new (fragP, fragP->fr_fix, 2,
1327 segment_info[seg].dot,
1328 fragP->fr_address + fragP->fr_fix + 6,
1329 1, R_SH_PCDISP8BY2);
1331 /* Set up a jump instruction. */
1332 buffer[highbyte + 2] = 0xa0;
1333 buffer[lowbyte + 2] = 0;
1334 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
1335 fragP->fr_offset, 1, R_SH_PCDISP);
1337 /* Fill in a NOP instruction. */
1338 buffer[highbyte + 4] = 0x0;
1339 buffer[lowbyte + 4] = 0x9;
1347 case C (COND_JUMP, COND32):
1348 case C (COND_JUMP, UNDEF_WORD_DISP):
1349 if (fragP->fr_symbol == NULL)
1350 as_bad ("at %0xlx, displacement overflows 8-bit field",
1351 (unsigned long) fragP->fr_address);
1353 as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 8-bit field ",
1354 (unsigned long) fragP->fr_address,
1355 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1356 S_GET_NAME (fragP->fr_symbol));
1358 #if 0 /* This code works, but generates poor code, and the compiler
1359 should never produce a sequence that requires it to be used. */
1361 /* A bcond won't fit and it won't go into a 12 bit
1362 displacement either, the code sequence looks like:
1371 buffer[0] ^= 0x2; /* Toggle T/F bit */
1373 buffer[1] = 5; /* branch over mov, jump, nop and ptr */
1374 buffer[2] = 0xd0 | JREG; /* Build mov insn */
1376 buffer[4] = 0x40 | JREG; /* Build jmp @JREG */
1378 buffer[6] = 0x20; /* build nop */
1380 buffer[8] = 0; /* space for 32 bit jump disp */
1386 /* Make reloc for the long disp */
1394 fragP->fr_fix += COND32_LENGTH;
1405 if (donerelax && !sh_relax)
1406 as_warn ("Offset doesn't fit at 0x%lx, trying to get to %s+0x%lx",
1407 (unsigned long) fragP->fr_address,
1408 fragP->fr_symbol ? S_GET_NAME(fragP->fr_symbol): "",
1409 (unsigned long) fragP->fr_offset);
1413 DEFUN (md_section_align, (seg, size),
1417 return ((size + (1 << section_alignment[(int) seg]) - 1)
1418 & (-1 << section_alignment[(int) seg]));
1422 /* When relaxing, we need to output a reloc for any .align directive
1423 that requests alignment to a four byte boundary or larger. */
1426 sh_handle_align (frag)
1430 && frag->fr_type == rs_align
1431 && frag->fr_address + frag->fr_fix > 0
1432 && frag->fr_offset > 1
1433 && now_seg != bss_section)
1434 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
1438 /* This macro decides whether a particular reloc is an entry in a
1439 switch table. It is used when relaxing, because the linker needs
1440 to know about all such entries so that it can adjust them if
1443 #define SWITCH_TABLE(fix) \
1444 ((fix)->fx_addsy != NULL \
1445 && (fix)->fx_subsy != NULL \
1446 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
1447 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
1448 && ((fix)->fx_r_type == R_SH_IMM32 \
1449 || (fix)->fx_r_type == R_SH_IMM16 \
1450 || ((fix)->fx_r_type == 0 \
1451 && ((fix)->fx_size == 2 \
1452 || (fix)->fx_size == 4))))
1454 /* See whether we need to force a relocation into the output file.
1455 This is used to force out switch and PC relative relocations when
1459 sh_force_relocation (fix)
1465 return (fix->fx_pcrel
1466 || SWITCH_TABLE (fix)
1467 || fix->fx_r_type == R_SH_COUNT
1468 || fix->fx_r_type == R_SH_ALIGN);
1471 /* Apply a fixup to the object file. */
1474 md_apply_fix (fixP, val)
1478 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1479 int lowbyte = target_big_endian ? 1 : 0;
1480 int highbyte = target_big_endian ? 0 : 1;
1482 if (fixP->fx_r_type == 0)
1484 if (fixP->fx_size == 2)
1485 fixP->fx_r_type = R_SH_IMM16;
1486 else if (fixP->fx_size == 4)
1487 fixP->fx_r_type = R_SH_IMM32;
1488 else if (fixP->fx_size == 1)
1489 fixP->fx_r_type = R_SH_IMM8;
1494 switch (fixP->fx_r_type)
1497 *buf = (*buf & 0xf0) | (val & 0xf);
1501 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1505 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1520 case R_SH_PCRELIMM8BY4:
1521 /* The lower two bits of the PC are cleared before the
1522 displacement is added in. We can assume that the destination
1523 is on a 4 byte bounday. If this instruction is also on a 4
1524 byte boundary, then we want
1526 and target - here is a multiple of 4.
1527 Otherwise, we are on a 2 byte boundary, and we want
1528 (target - (here - 2)) / 4
1529 and target - here is not a multiple of 4. Computing
1530 (target - (here - 2)) / 4 == (target - here + 2) / 4
1531 works for both cases, since in the first case the addition of
1532 2 will be removed by the division. target - here is in the
1534 val = (val + 2) / 4;
1536 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1540 case R_SH_PCRELIMM8BY2:
1543 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1547 case R_SH_PCDISP8BY2:
1549 if (val < -0x80 || val > 0x7f)
1550 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1556 if (val < -0x800 || val >= 0x7ff)
1557 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1558 buf[lowbyte] = val & 0xff;
1559 buf[highbyte] |= (val >> 8) & 0xf;
1563 if (! target_big_endian)
1580 if (! target_big_endian)
1593 /* Pass the value into sh_coff_reloc_mangle. */
1594 fixP->fx_addnumber = val;
1599 /* Nothing to do here. */
1607 int md_long_jump_size;
1609 /* Called just before address relaxation. Return the length
1610 by which a fragment must grow to reach it's destination. */
1613 md_estimate_size_before_relax (fragP, segment_type)
1614 register fragS *fragP;
1615 register segT segment_type;
1617 switch (fragP->fr_subtype)
1619 case C (UNCOND_JUMP, UNDEF_DISP):
1620 /* used to be a branch to somewhere which was unknown */
1621 if (!fragP->fr_symbol)
1623 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1624 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1626 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1628 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1629 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1633 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
1634 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1635 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1641 case C (COND_JUMP, UNDEF_DISP):
1642 /* used to be a branch to somewhere which was unknown */
1643 if (fragP->fr_symbol
1644 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1646 /* Got a symbol and it's defined in this segment, become byte
1647 sized - maybe it will fix up */
1648 fragP->fr_subtype = C (COND_JUMP, COND8);
1649 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1651 else if (fragP->fr_symbol)
1653 /* Its got a segment, but its not ours, so it will always be long */
1654 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
1655 fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1656 return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1660 /* We know the abs value */
1661 fragP->fr_subtype = C (COND_JUMP, COND8);
1662 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1667 return fragP->fr_var;
1670 /* Put number into target byte order */
1673 md_number_to_chars (ptr, use, nbytes)
1678 if (! target_big_endian)
1679 number_to_chars_littleendian (ptr, use, nbytes);
1681 number_to_chars_bigendian (ptr, use, nbytes);
1685 md_pcrel_from (fixP)
1688 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
1692 tc_coff_sizemachdep (frag)
1695 return md_relax_table[frag->fr_subtype].rlx_length;
1698 /* When we align the .text section, insert the correct NOP pattern. */
1701 sh_do_align (n, fill, len)
1706 if ((fill == NULL || (*fill == 0 && len == 1))
1707 && (now_seg == text_section
1708 #ifdef BFD_ASSEMBLER
1709 || (now_seg->flags & SEC_CODE) != 0
1711 || strcmp (obj_segment_name (now_seg), ".init") == 0))
1713 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
1714 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
1716 /* First align to a 2 byte boundary, in case there is an odd
1719 if (target_big_endian)
1720 frag_align_pattern (n, big_nop_pattern, sizeof big_nop_pattern);
1722 frag_align_pattern (n, little_nop_pattern, sizeof little_nop_pattern);
1731 /* Adjust a reloc for the SH. This is similar to the generic code,
1732 but does some minor tweaking. */
1735 sh_coff_reloc_mangle (seg, fix, intr, paddr)
1736 segment_info_type *seg;
1738 struct internal_reloc *intr;
1741 symbolS *symbol_ptr = fix->fx_addsy;
1744 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
1746 if (! SWITCH_TABLE (fix))
1748 intr->r_type = fix->fx_r_type;
1755 if (fix->fx_r_type == R_SH_IMM16)
1756 intr->r_type = R_SH_SWITCH16;
1757 else if (fix->fx_r_type == R_SH_IMM32)
1758 intr->r_type = R_SH_SWITCH32;
1762 /* For a switch reloc, we set r_offset to the difference between
1763 the reloc address and the subtrahend. When the linker is
1764 doing relaxing, it can use the determine the starting and
1765 ending points of the switch difference expression. */
1766 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
1769 /* PC relative relocs are always against the current section. */
1770 if (symbol_ptr == NULL)
1772 switch (fix->fx_r_type)
1774 case R_SH_PCRELIMM8BY2:
1775 case R_SH_PCRELIMM8BY4:
1776 case R_SH_PCDISP8BY2:
1779 symbol_ptr = seg->dot;
1786 if (fix->fx_r_type == R_SH_USES)
1788 /* We can't store the offset in the object file, since this
1789 reloc does not take up any space, so we store it in r_offset.
1790 The fx_addnumber field was set in md_apply_fix. */
1791 intr->r_offset = fix->fx_addnumber;
1793 else if (fix->fx_r_type == R_SH_COUNT)
1795 /* We can't store the count in the object file, since this reloc
1796 does not take up any space, so we store it in r_offset. The
1797 fx_offset field was set when the fixup was created in
1798 sh_coff_frob_file. */
1799 intr->r_offset = fix->fx_offset;
1800 /* This reloc is always absolute. */
1803 else if (fix->fx_r_type == R_SH_ALIGN)
1805 /* Store the alignment in the r_offset field. */
1806 intr->r_offset = fix->fx_offset;
1807 /* This reloc is always absolute. */
1811 /* Turn the segment of the symbol into an offset. */
1812 if (symbol_ptr != NULL)
1814 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1816 intr->r_symndx = dot->sy_number;
1818 intr->r_symndx = symbol_ptr->sy_number;
1821 intr->r_symndx = -1;