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 #include "dwarf2dbg.h"
38 const char comment_chars[] = "!";
39 const char line_separator_chars[] = ";";
40 const char line_comment_chars[] = "!#";
42 static void s_uses PARAMS ((int));
44 static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
45 static void sh_frob_section PARAMS ((bfd *, segT, PTR));
48 void s_align_bytes ();
49 static void s_uacons PARAMS ((int));
50 static sh_opcode_info *find_cooked_opcode PARAMS ((char **));
51 static unsigned int assemble_ppi PARAMS ((char *, sh_opcode_info *));
54 static void sh_elf_cons PARAMS ((int));
56 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
63 int ignore ATTRIBUTE_UNUSED;
66 target_big_endian = 0;
69 /* This table describes all the machine specific pseudo-ops the assembler
70 has to support. The fields are:
71 pseudo-op name without dot
72 function to call to execute this pseudo-op
73 Integer arg to pass to the function. */
75 const pseudo_typeS md_pseudo_table[] =
78 {"long", sh_elf_cons, 4},
79 {"int", sh_elf_cons, 4},
80 {"word", sh_elf_cons, 2},
81 {"short", sh_elf_cons, 2},
86 {"form", listing_psize, 0},
87 {"little", little, 0},
88 {"heading", listing_title, 0},
89 {"import", s_ignore, 0},
90 {"page", listing_eject, 0},
91 {"program", s_ignore, 0},
93 {"uaword", s_uacons, 2},
94 {"ualong", s_uacons, 4},
95 { "file", dwarf2_directive_file, 0 },
96 { "loc", dwarf2_directive_loc, 0 },
100 /*int md_reloc_size; */
102 int sh_relax; /* set if -relax seen */
104 /* Whether -small was seen. */
108 /* Whether -dsp was seen. */
112 /* The bit mask of architectures that could
113 accomodate the insns seen so far. */
114 static int valid_arch;
116 const char EXP_CHARS[] = "eE";
118 /* Chars that mean this number is a floating point constant. */
121 const char FLT_CHARS[] = "rRsSfFdDxXpP";
123 #define C(a,b) ENCODE_RELAX(a,b)
125 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
126 #define GET_WHAT(x) ((x>>4))
128 /* These are the three types of relaxable instrction. */
130 #define COND_JUMP_DELAY 2
131 #define UNCOND_JUMP 3
140 #define UNDEF_WORD_DISP 4
145 /* Branch displacements are from the address of the branch plus
146 four, thus all minimum and maximum values have 4 added to them. */
149 #define COND8_LENGTH 2
151 /* There is one extra instruction before the branch, so we must add
152 two more bytes to account for it. */
153 #define COND12_F 4100
154 #define COND12_M -4090
155 #define COND12_LENGTH 6
157 #define COND12_DELAY_LENGTH 4
159 /* ??? The minimum and maximum values are wrong, but this does not matter
160 since this relocation type is not supported yet. */
161 #define COND32_F (1<<30)
162 #define COND32_M -(1<<30)
163 #define COND32_LENGTH 14
165 #define UNCOND12_F 4098
166 #define UNCOND12_M -4092
167 #define UNCOND12_LENGTH 2
169 /* ??? The minimum and maximum values are wrong, but this does not matter
170 since this relocation type is not supported yet. */
171 #define UNCOND32_F (1<<30)
172 #define UNCOND32_M -(1<<30)
173 #define UNCOND32_LENGTH 14
175 #define EMPTY { 0, 0, 0, 0 }
177 const relax_typeS md_relax_table[C (END, 0)] = {
178 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
179 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
182 /* C (COND_JUMP, COND8) */
183 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
184 /* C (COND_JUMP, COND12) */
185 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
186 /* C (COND_JUMP, COND32) */
187 { COND32_F, COND32_M, COND32_LENGTH, 0, },
188 EMPTY, EMPTY, EMPTY, EMPTY,
189 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
192 /* C (COND_JUMP_DELAY, COND8) */
193 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
194 /* C (COND_JUMP_DELAY, COND12) */
195 { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
196 /* C (COND_JUMP_DELAY, COND32) */
197 { COND32_F, COND32_M, COND32_LENGTH, 0, },
198 EMPTY, EMPTY, EMPTY, EMPTY,
199 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
202 /* C (UNCOND_JUMP, UNCOND12) */
203 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
204 /* C (UNCOND_JUMP, UNCOND32) */
205 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
206 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
207 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
212 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
216 /* Parse @got, etc. and return the desired relocation.
217 If we have additional arithmetic expression, then we fill in new_exp_p. */
218 static bfd_reloc_code_real_type
219 sh_elf_suffix (str_p, exp_p, new_exp_p)
221 expressionS *exp_p, *new_exp_p;
226 bfd_reloc_code_real_type reloc;
236 #define MAP(str,reloc) { str, sizeof (str)-1, reloc }
238 static struct map_bfd mapping[] = {
239 MAP ("got", BFD_RELOC_32_GOT_PCREL),
240 MAP ("plt", BFD_RELOC_32_PLT_PCREL),
241 MAP ("gotoff", BFD_RELOC_32_GOTOFF),
242 { (char *)0, 0, BFD_RELOC_UNUSED }
246 return BFD_RELOC_UNUSED;
248 for (ch = *str, str2 = ident;
249 (str2 < ident + sizeof (ident) - 1
250 && (isalnum (ch) || ch == '@'));
253 *str2++ = (islower (ch)) ? ch : tolower (ch);
260 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
261 if (ch == ptr->string[0]
262 && len == ptr->length
263 && memcmp (ident, ptr->string, ptr->length) == 0)
265 /* Now check for identifier@suffix+constant */
266 if (*str == '-' || *str == '+')
268 char *orig_line = input_line_pointer;
270 input_line_pointer = str;
271 expression (new_exp_p);
272 if (new_exp_p->X_op == O_constant)
274 exp_p->X_add_number += new_exp_p->X_add_number;
275 str = input_line_pointer;
277 if (new_exp_p->X_op == O_subtract)
278 str = input_line_pointer;
280 if (&input_line_pointer != str_p)
281 input_line_pointer = orig_line;
288 return BFD_RELOC_UNUSED;
291 /* The regular cons() function, that reads constants, doesn't support
292 suffixes such as @GOT, @GOTOFF and @PLT, that generate
293 machine-specific relocation types. So we must define it here. */
294 /* Clobbers input_line_pointer, checks end-of-line. */
297 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
299 expressionS exp, new_exp;
300 bfd_reloc_code_real_type reloc;
303 if (is_it_end_of_statement ())
305 demand_empty_rest_of_line ();
312 new_exp.X_op = O_absent;
313 new_exp.X_add_symbol = new_exp.X_op_symbol = NULL;
314 /* If the _GLOBAL_OFFSET_TABLE_ symbol hasn't been found yet,
315 use the name of the symbol to tell whether it's the
316 _GLOBAL_OFFSET_TABLE_. If it has, comparing the symbols is
318 if (! GOT_symbol && exp.X_add_symbol)
319 name = S_GET_NAME (exp.X_add_symbol);
322 /* Check whether this expression involves the
323 _GLOBAL_OFFSET_TABLE_ symbol, by itself or added to a
324 difference of two other symbols. */
325 if (((GOT_symbol && GOT_symbol == exp.X_add_symbol)
326 || (! GOT_symbol && name
327 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0))
328 && (exp.X_op == O_symbol
329 || (exp.X_op == O_add
330 && ((symbol_get_value_expression (exp.X_op_symbol)->X_op)
333 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput,
335 int size = bfd_get_reloc_size (reloc_howto);
337 if (GOT_symbol == NULL)
338 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
341 as_bad (_("%s relocations do not fit in %d bytes\n"),
342 reloc_howto->name, nbytes);
345 register char *p = frag_more ((int) nbytes);
346 int offset = nbytes - size;
348 fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
349 size, &exp, 0, TC_RELOC_GLOBAL_OFFSET_TABLE);
352 /* Check if this symbol involves one of the magic suffixes, such
353 as @GOT, @GOTOFF or @PLT, and determine which relocation type
355 else if ((exp.X_op == O_symbol || (exp.X_op == O_add && exp.X_op_symbol))
356 && *input_line_pointer == '@'
357 && ((reloc = sh_elf_suffix (&input_line_pointer, &exp, &new_exp))
358 != BFD_RELOC_UNUSED))
360 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput,
362 int size = bfd_get_reloc_size (reloc_howto);
364 /* Force a GOT to be generated. */
365 if (GOT_symbol == NULL)
366 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
369 as_bad (_("%s relocations do not fit in %d bytes\n"),
370 reloc_howto->name, nbytes);
373 register char *p = frag_more ((int) nbytes);
374 int offset = nbytes - size;
376 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size,
378 if (new_exp.X_op != O_absent)
379 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size,
380 &new_exp, 0, BFD_RELOC_32);
384 emit_expr (&exp, (unsigned int) nbytes);
386 while (*input_line_pointer++ == ',');
388 input_line_pointer--; /* Put terminator back into stream. */
389 if (*input_line_pointer == '#' || *input_line_pointer == '!')
391 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
394 demand_empty_rest_of_line ();
399 /* This function is called once, at assembler startup time. This should
400 set up all the tables, etc that the MD part of the assembler needs. */
405 sh_opcode_info *opcode;
406 char *prev_name = "";
410 /* The WinCE OS only supports little endian executables. */
411 target_big_endian = 0;
414 target_big_endian = 1;
417 target_arch = arch_sh1_up & ~(sh_dsp ? arch_sh3e_up : arch_sh_dsp_up);
418 valid_arch = target_arch;
420 opcode_hash_control = hash_new ();
422 /* Insert unique names into hash table. */
423 for (opcode = sh_table; opcode->name; opcode++)
425 if (strcmp (prev_name, opcode->name))
427 if (! (opcode->arch & target_arch))
429 prev_name = opcode->name;
430 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
434 /* Make all the opcodes with the same name point to the same
436 opcode->name = prev_name;
443 static int reg_x, reg_y;
451 expressionS immediate;
455 #define IDENT_CHAR(c) (isalnum (c) || (c) == '_')
457 /* Try to parse a reg name. Return the number of chars consumed. */
460 parse_reg (src, mode, reg)
465 char l0 = tolower (src[0]);
466 char l1 = l0 ? tolower (src[1]) : 0;
468 /* We use ! IDENT_CHAR for the next character after the register name, to
469 make sure that we won't accidentally recognize a symbol name such as
470 'sram' or sr_ram as being a reference to the register 'sr'. */
476 if (src[2] >= '0' && src[2] <= '5'
477 && ! IDENT_CHAR ((unsigned char) src[3]))
480 *reg = 10 + src[2] - '0';
484 if (l1 >= '0' && l1 <= '9'
485 && ! IDENT_CHAR ((unsigned char) src[2]))
491 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
492 && ! IDENT_CHAR ((unsigned char) src[7]))
499 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
504 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
515 if (! IDENT_CHAR ((unsigned char) src[2]))
521 if (tolower (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
530 if (! IDENT_CHAR ((unsigned char) src[2]))
536 if (tolower (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
544 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
545 && ! IDENT_CHAR ((unsigned char) src[3]))
548 *reg = 4 + (l1 - '0');
551 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
552 && ! IDENT_CHAR ((unsigned char) src[3]))
555 *reg = 6 + (l1 - '0');
558 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
559 && ! IDENT_CHAR ((unsigned char) src[3]))
564 *reg = n | ((~n & 2) << 1);
569 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[3]))
591 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
592 && ! IDENT_CHAR ((unsigned char) src[2]))
595 *reg = A_X0_NUM + l1 - '0';
599 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
600 && ! IDENT_CHAR ((unsigned char) src[2]))
603 *reg = A_Y0_NUM + l1 - '0';
607 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
608 && ! IDENT_CHAR ((unsigned char) src[2]))
611 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
617 && tolower (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
623 if (l0 == 's' && l1 == 'p' && tolower (src[2]) == 'c'
624 && ! IDENT_CHAR ((unsigned char) src[3]))
630 if (l0 == 's' && l1 == 'g' && tolower (src[2]) == 'r'
631 && ! IDENT_CHAR ((unsigned char) src[3]))
637 if (l0 == 'd' && l1 == 's' && tolower (src[2]) == 'r'
638 && ! IDENT_CHAR ((unsigned char) src[3]))
644 if (l0 == 'd' && l1 == 'b' && tolower (src[2]) == 'r'
645 && ! IDENT_CHAR ((unsigned char) src[3]))
651 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
657 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
664 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
669 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
671 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
672 and use an uninitialized immediate. */
676 if (l0 == 'g' && l1 == 'b' && tolower (src[2]) == 'r'
677 && ! IDENT_CHAR ((unsigned char) src[3]))
682 if (l0 == 'v' && l1 == 'b' && tolower (src[2]) == 'r'
683 && ! IDENT_CHAR ((unsigned char) src[3]))
689 if (l0 == 'm' && l1 == 'a' && tolower (src[2]) == 'c'
690 && ! IDENT_CHAR ((unsigned char) src[4]))
703 if (l0 == 'm' && l1 == 'o' && tolower (src[2]) == 'd'
704 && ! IDENT_CHAR ((unsigned char) src[4]))
709 if (l0 == 'f' && l1 == 'r')
713 if (src[3] >= '0' && src[3] <= '5'
714 && ! IDENT_CHAR ((unsigned char) src[4]))
717 *reg = 10 + src[3] - '0';
721 if (src[2] >= '0' && src[2] <= '9'
722 && ! IDENT_CHAR ((unsigned char) src[3]))
725 *reg = (src[2] - '0');
729 if (l0 == 'd' && l1 == 'r')
733 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
734 && ! IDENT_CHAR ((unsigned char) src[4]))
737 *reg = 10 + src[3] - '0';
741 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
742 && ! IDENT_CHAR ((unsigned char) src[3]))
745 *reg = (src[2] - '0');
749 if (l0 == 'x' && l1 == 'd')
753 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
754 && ! IDENT_CHAR ((unsigned char) src[4]))
757 *reg = 11 + src[3] - '0';
761 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
762 && ! IDENT_CHAR ((unsigned char) src[3]))
765 *reg = (src[2] - '0') + 1;
769 if (l0 == 'f' && l1 == 'v')
771 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
777 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
778 && ! IDENT_CHAR ((unsigned char) src[3]))
781 *reg = (src[2] - '0');
785 if (l0 == 'f' && l1 == 'p' && tolower (src[2]) == 'u'
786 && tolower (src[3]) == 'l'
787 && ! IDENT_CHAR ((unsigned char) src[4]))
793 if (l0 == 'f' && l1 == 'p' && tolower (src[2]) == 's'
794 && tolower (src[3]) == 'c'
795 && tolower (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
801 if (l0 == 'x' && l1 == 'm' && tolower (src[2]) == 't'
802 && tolower (src[3]) == 'r'
803 && tolower (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
817 /* JF: '.' is pseudo symbol with value of current location
818 in current segment. */
819 fake = FAKE_LABEL_NAME;
820 return symbol_new (fake,
822 (valueT) frag_now_fix (),
834 save = input_line_pointer;
835 input_line_pointer = s;
836 expression (&op->immediate);
837 if (op->immediate.X_op == O_absent)
838 as_bad (_("missing operand"));
839 new = input_line_pointer;
840 input_line_pointer = save;
844 /* The many forms of operand:
847 @Rn Register indirect
860 pr, gbr, vbr, macl, mach
873 /* Must be predecrement. */
876 len = parse_reg (src, &mode, &(op->reg));
878 as_bad (_("illegal register after @-"));
883 else if (src[0] == '(')
885 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
888 len = parse_reg (src, &mode, &(op->reg));
889 if (len && mode == A_REG_N)
894 as_bad (_("must be @(r0,...)"));
898 /* Now can be rn or gbr */
899 len = parse_reg (src, &mode, &(op->reg));
904 else if (mode == A_REG_N)
906 op->type = A_IND_R0_REG_N;
910 as_bad (_("syntax error in @(r0,...)"));
915 /* Must be an @(disp,.. thing) */
916 src = parse_exp (src, op);
919 /* Now can be rn, gbr or pc */
920 len = parse_reg (src, &mode, &op->reg);
925 op->type = A_DISP_REG_N;
927 else if (mode == A_GBR)
929 op->type = A_DISP_GBR;
931 else if (mode == A_PC)
933 /* Turn a plain @(4,pc) into @(.+4,pc). */
934 if (op->immediate.X_op == O_constant)
936 op->immediate.X_add_symbol = dot();
937 op->immediate.X_op = O_symbol;
939 op->type = A_DISP_PC;
943 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
948 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
953 as_bad (_("expecting )"));
959 src += parse_reg (src, &mode, &(op->reg));
962 as_bad (_("illegal register after @"));
967 if ((src[0] == 'r' && src[1] == '8')
968 || (src[0] == 'i' && (src[1] == 'x' || src[1] == 's')))
973 if ((src[0] == 'r' && src[1] == '9')
974 || (src[0] == 'i' && src[1] == 'y'))
977 op->type = A_PMODY_N;
991 get_operand (ptr, op)
1002 *ptr = parse_exp (src, op);
1007 else if (src[0] == '@')
1009 *ptr = parse_at (src, op);
1012 len = parse_reg (src, &mode, &(op->reg));
1021 /* Not a reg, the only thing left is a displacement. */
1022 *ptr = parse_exp (src, op);
1023 op->type = A_DISP_PC;
1029 get_operands (info, args, operand)
1030 sh_opcode_info *info;
1032 sh_operand_info *operand;
1037 /* The pre-processor will eliminate whitespace in front of '@'
1038 after the first argument; we may be called multiple times
1039 from assemble_ppi, so don't insist on finding whitespace here. */
1043 get_operand (&ptr, operand + 0);
1050 get_operand (&ptr, operand + 1);
1051 /* ??? Hack: psha/pshl have a varying operand number depending on
1052 the type of the first operand. We handle this by having the
1053 three-operand version first and reducing the number of operands
1054 parsed to two if we see that the first operand is an immediate.
1055 This works because no insn with three operands has an immediate
1056 as first operand. */
1057 if (info->arg[2] && operand[0].type != A_IMM)
1063 get_operand (&ptr, operand + 2);
1067 operand[2].type = 0;
1072 operand[1].type = 0;
1073 operand[2].type = 0;
1078 operand[0].type = 0;
1079 operand[1].type = 0;
1080 operand[2].type = 0;
1085 /* Passed a pointer to a list of opcodes which use different
1086 addressing modes, return the opcode which matches the opcodes
1089 static sh_opcode_info *
1090 get_specific (opcode, operands)
1091 sh_opcode_info *opcode;
1092 sh_operand_info *operands;
1094 sh_opcode_info *this_try = opcode;
1095 char *name = opcode->name;
1098 while (opcode->name)
1100 this_try = opcode++;
1101 if (this_try->name != name)
1103 /* We've looked so far down the table that we've run out of
1104 opcodes with the same name. */
1108 /* Look at both operands needed by the opcodes and provided by
1109 the user - since an arg test will often fail on the same arg
1110 again and again, we'll try and test the last failing arg the
1111 first on each opcode try. */
1112 for (n = 0; this_try->arg[n]; n++)
1114 sh_operand_info *user = operands + n;
1115 sh_arg_type arg = this_try->arg[n];
1127 if (user->type != arg)
1131 /* opcode needs r0 */
1132 if (user->type != A_REG_N || user->reg != 0)
1136 if (user->type != A_R0_GBR || user->reg != 0)
1140 if (user->type != F_REG_N || user->reg != 0)
1148 case A_IND_R0_REG_N:
1159 /* Opcode needs rn */
1160 if (user->type != arg)
1165 if (user->type != D_REG_N && user->type != X_REG_N)
1180 if (user->type != arg)
1185 if (user->type != arg)
1194 case A_IND_R0_REG_M:
1197 /* Opcode needs rn */
1198 if (user->type != arg - A_REG_M + A_REG_N)
1204 if (user->type != DSP_REG_N)
1226 if (user->type != DSP_REG_N)
1248 if (user->type != DSP_REG_N)
1270 if (user->type != DSP_REG_N)
1292 if (user->type != DSP_REG_N)
1314 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1318 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1322 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1326 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1330 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1340 /* Opcode needs rn */
1341 if (user->type != arg - F_REG_M + F_REG_N)
1346 if (user->type != D_REG_N && user->type != X_REG_N)
1351 if (user->type != XMTRX_M4)
1357 printf (_("unhandled %d\n"), arg);
1361 if ( !(valid_arch & this_try->arch))
1363 valid_arch &= this_try->arch;
1373 check (operand, low, high)
1374 expressionS *operand;
1378 if (operand->X_op != O_constant
1379 || operand->X_add_number < low
1380 || operand->X_add_number > high)
1382 as_bad (_("operand must be absolute in range %d..%d"), low, high);
1384 return operand->X_add_number;
1388 insert (where, how, pcrel, op)
1392 sh_operand_info *op;
1394 fix_new_exp (frag_now,
1395 where - frag_now->fr_literal,
1403 build_relax (opcode, op)
1404 sh_opcode_info *opcode;
1405 sh_operand_info *op;
1407 int high_byte = target_big_endian ? 0 : 1;
1410 if (opcode->arg[0] == A_BDISP8)
1412 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
1413 p = frag_var (rs_machine_dependent,
1414 md_relax_table[C (what, COND32)].rlx_length,
1415 md_relax_table[C (what, COND8)].rlx_length,
1417 op->immediate.X_add_symbol,
1418 op->immediate.X_add_number,
1420 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
1422 else if (opcode->arg[0] == A_BDISP12)
1424 p = frag_var (rs_machine_dependent,
1425 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
1426 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
1428 op->immediate.X_add_symbol,
1429 op->immediate.X_add_number,
1431 p[high_byte] = (opcode->nibbles[0] << 4);
1436 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
1439 insert_loop_bounds (output, operand)
1441 sh_operand_info *operand;
1446 /* Since the low byte of the opcode will be overwritten by the reloc, we
1447 can just stash the high byte into both bytes and ignore endianness. */
1450 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1451 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1455 static int count = 0;
1457 /* If the last loop insn is a two-byte-insn, it is in danger of being
1458 swapped with the insn after it. To prevent this, create a new
1459 symbol - complete with SH_LABEL reloc - after the last loop insn.
1460 If the last loop insn is four bytes long, the symbol will be
1461 right in the middle, but four byte insns are not swapped anyways. */
1462 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
1463 Hence a 9 digit number should be enough to count all REPEATs. */
1465 sprintf (name, "_R%x", count++ & 0x3fffffff);
1466 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
1467 /* Make this a local symbol. */
1469 SF_SET_LOCAL (end_sym);
1470 #endif /* OBJ_COFF */
1471 symbol_table_insert (end_sym);
1472 end_sym->sy_value = operand[1].immediate;
1473 end_sym->sy_value.X_add_number += 2;
1474 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
1477 output = frag_more (2);
1480 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1481 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1483 return frag_more (2);
1486 /* Now we know what sort of opcodes it is, let's build the bytes. */
1489 build_Mytes (opcode, operand)
1490 sh_opcode_info *opcode;
1491 sh_operand_info *operand;
1496 char *output = frag_more (2);
1497 unsigned int size = 2;
1498 int low_byte = target_big_endian ? 1 : 0;
1504 for (index = 0; index < 4; index++)
1506 sh_nibble_type i = opcode->nibbles[index];
1516 nbuf[index] = reg_n;
1519 nbuf[index] = reg_m;
1522 if (reg_n < 2 || reg_n > 5)
1523 as_bad (_("Invalid register: 'r%d'"), reg_n);
1524 nbuf[index] = (reg_n & 3) | 4;
1527 nbuf[index] = reg_n | (reg_m >> 2);
1530 nbuf[index] = reg_b | 0x08;
1533 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
1536 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
1539 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
1542 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
1545 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
1548 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
1551 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
1554 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
1557 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
1560 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
1563 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
1566 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
1569 insert (output, BFD_RELOC_SH_PCRELIMM8BY4, 1, operand);
1572 insert (output, BFD_RELOC_SH_PCRELIMM8BY2, 1, operand);
1575 output = insert_loop_bounds (output, operand);
1576 nbuf[index] = opcode->nibbles[3];
1580 printf (_("failed for %d\n"), i);
1584 if (!target_big_endian)
1586 output[1] = (nbuf[0] << 4) | (nbuf[1]);
1587 output[0] = (nbuf[2] << 4) | (nbuf[3]);
1591 output[0] = (nbuf[0] << 4) | (nbuf[1]);
1592 output[1] = (nbuf[2] << 4) | (nbuf[3]);
1597 /* Find an opcode at the start of *STR_P in the hash table, and set
1598 *STR_P to the first character after the last one read. */
1600 static sh_opcode_info *
1601 find_cooked_opcode (str_p)
1605 unsigned char *op_start;
1606 unsigned char *op_end;
1610 /* Drop leading whitespace. */
1614 /* Find the op code end.
1615 The pre-processor will eliminate whitespace in front of
1616 any '@' after the first argument; we may be called from
1617 assemble_ppi, so the opcode might be terminated by an '@'. */
1618 for (op_start = op_end = (unsigned char *) (str);
1621 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
1624 unsigned char c = op_start[nlen];
1626 /* The machine independent code will convert CMP/EQ into cmp/EQ
1627 because it thinks the '/' is the end of the symbol. Moreover,
1628 all but the first sub-insn is a parallel processing insn won't
1629 be capitailzed. Instead of hacking up the machine independent
1630 code, we just deal with it here. */
1631 c = isupper (c) ? tolower (c) : c;
1640 as_bad (_("can't find opcode "));
1642 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
1645 /* Assemble a parallel processing insn. */
1646 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
1649 assemble_ppi (op_end, opcode)
1651 sh_opcode_info *opcode;
1661 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
1662 Make sure we encode a defined insn pattern. */
1668 sh_operand_info operand[3];
1670 if (opcode->arg[0] != A_END)
1671 op_end = get_operands (opcode, op_end, operand);
1672 opcode = get_specific (opcode, operand);
1675 /* Couldn't find an opcode which matched the operands. */
1676 char *where = frag_more (2);
1681 as_bad (_("invalid operands for opcode"));
1685 if (opcode->nibbles[0] != PPI)
1686 as_bad (_("insn can't be combined with parallel processing insn"));
1688 switch (opcode->nibbles[1])
1693 as_bad (_("multiple movx specifications"));
1698 as_bad (_("multiple movy specifications"));
1704 as_bad (_("multiple movx specifications"));
1705 if (reg_n < 4 || reg_n > 5)
1706 as_bad (_("invalid movx address register"));
1707 if (opcode->nibbles[2] & 8)
1709 if (reg_m == A_A1_NUM)
1711 else if (reg_m != A_A0_NUM)
1712 as_bad (_("invalid movx dsp register"));
1717 as_bad (_("invalid movx dsp register"));
1720 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
1725 as_bad (_("multiple movy specifications"));
1726 if (opcode->nibbles[2] & 8)
1728 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
1731 if (reg_m == A_A1_NUM)
1733 else if (reg_m != A_A0_NUM)
1734 as_bad (_("invalid movy dsp register"));
1739 as_bad (_("invalid movy dsp register"));
1742 if (reg_n < 6 || reg_n > 7)
1743 as_bad (_("invalid movy address register"));
1744 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
1748 if (operand[0].immediate.X_op != O_constant)
1749 as_bad (_("dsp immediate shift value not constant"));
1750 field_b = ((opcode->nibbles[2] << 12)
1751 | (operand[0].immediate.X_add_number & 127) << 4
1756 as_bad (_("multiple parallel processing specifications"));
1757 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
1758 + (reg_x << 6) + (reg_y << 4) + reg_n);
1762 as_bad (_("multiple condition specifications"));
1763 cond = opcode->nibbles[2] << 8;
1765 goto skip_cond_check;
1769 as_bad (_("multiple parallel processing specifications"));
1770 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
1771 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
1777 if ((field_b & 0xef00) != 0xa100)
1778 as_bad (_("insn cannot be combined with pmuls"));
1780 switch (field_b & 0xf)
1783 field_b += 0 - A_X0_NUM;
1786 field_b += 1 - A_Y0_NUM;
1789 field_b += 2 - A_A0_NUM;
1792 field_b += 3 - A_A1_NUM;
1795 as_bad (_("bad padd / psub pmuls output operand"));
1798 field_b += 0x4000 + reg_efg;
1805 as_bad (_("condition not followed by conditionalizable insn"));
1811 opcode = find_cooked_opcode (&op_end);
1815 (_("unrecognized characters at end of parallel processing insn")));
1820 move_code = movx | movy;
1823 /* Parallel processing insn. */
1824 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
1826 output = frag_more (4);
1828 if (! target_big_endian)
1830 output[3] = ppi_code >> 8;
1831 output[2] = ppi_code;
1835 output[2] = ppi_code >> 8;
1836 output[3] = ppi_code;
1838 move_code |= 0xf800;
1842 /* Just a double data transfer. */
1843 output = frag_more (2);
1846 if (! target_big_endian)
1848 output[1] = move_code >> 8;
1849 output[0] = move_code;
1853 output[0] = move_code >> 8;
1854 output[1] = move_code;
1859 /* This is the guts of the machine-dependent assembler. STR points to a
1860 machine dependent instruction. This function is supposed to emit
1861 the frags/bytes it assembles to. */
1867 unsigned char *op_end;
1868 sh_operand_info operand[3];
1869 sh_opcode_info *opcode;
1870 unsigned int size = 0;
1872 opcode = find_cooked_opcode (&str);
1877 as_bad (_("unknown opcode"));
1882 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
1884 /* Output a CODE reloc to tell the linker that the following
1885 bytes are instructions, not data. */
1886 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1888 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
1891 if (opcode->nibbles[0] == PPI)
1893 size = assemble_ppi (op_end, opcode);
1897 if (opcode->arg[0] == A_BDISP12
1898 || opcode->arg[0] == A_BDISP8)
1900 parse_exp (op_end + 1, &operand[0]);
1901 build_relax (opcode, &operand[0]);
1905 if (opcode->arg[0] == A_END)
1907 /* Ignore trailing whitespace. If there is any, it has already
1908 been compressed to a single space. */
1914 op_end = get_operands (opcode, op_end, operand);
1916 opcode = get_specific (opcode, operand);
1920 /* Couldn't find an opcode which matched the operands. */
1921 char *where = frag_more (2);
1926 as_bad (_("invalid operands for opcode"));
1931 as_bad (_("excess operands: '%s'"), op_end);
1933 size = build_Mytes (opcode, operand);
1938 dwarf2_emit_insn (size);
1941 /* This routine is called each time a label definition is seen. It
1942 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
1947 static fragS *last_label_frag;
1948 static int last_label_offset;
1951 && seg_info (now_seg)->tc_segment_info_data.in_code)
1955 offset = frag_now_fix ();
1956 if (frag_now != last_label_frag
1957 || offset != last_label_offset)
1959 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
1960 last_label_frag = frag_now;
1961 last_label_offset = offset;
1966 /* This routine is called when the assembler is about to output some
1967 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
1970 sh_flush_pending_output ()
1973 && seg_info (now_seg)->tc_segment_info_data.in_code)
1975 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1977 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
1982 md_undefined_symbol (name)
1986 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE. Otherwise we
1987 have no need to default values of symbols. */
1988 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
1992 if (symbol_find (name))
1993 as_bad ("GOT already in the symbol table");
1995 GOT_symbol = symbol_new (name, undefined_section,
1996 (valueT)0, & zero_address_frag);
2001 #endif /* OBJ_ELF */
2007 #ifndef BFD_ASSEMBLER
2010 tc_crawl_symbol_chain (headers)
2011 object_headers *headers;
2013 printf (_("call to tc_crawl_symbol_chain \n"));
2017 tc_headers_hook (headers)
2018 object_headers *headers;
2020 printf (_("call to tc_headers_hook \n"));
2026 /* Various routines to kill one day. */
2027 /* Equal to MAX_PRECISION in atof-ieee.c. */
2028 #define MAX_LITTLENUMS 6
2030 /* Turn a string in input_line_pointer into a floating point constant
2031 of type TYPE, and store the appropriate bytes in *LITP. The number
2032 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2033 returned, or NULL on OK. */
2036 md_atof (type, litP, sizeP)
2042 LITTLENUM_TYPE words[4];
2058 return _("bad call to md_atof");
2061 t = atof_ieee (input_line_pointer, type, words);
2063 input_line_pointer = t;
2067 if (! target_big_endian)
2069 for (i = prec - 1; i >= 0; i--)
2071 md_number_to_chars (litP, (valueT) words[i], 2);
2077 for (i = 0; i < prec; i++)
2079 md_number_to_chars (litP, (valueT) words[i], 2);
2087 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
2088 call instruction. It refers to a label of the instruction which
2089 loads the register which the call uses. We use it to generate a
2090 special reloc for the linker. */
2094 int ignore ATTRIBUTE_UNUSED;
2099 as_warn (_(".uses pseudo-op seen when not relaxing"));
2103 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2105 as_bad (_("bad .uses format"));
2106 ignore_rest_of_line ();
2110 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
2112 demand_empty_rest_of_line ();
2115 CONST char *md_shortopts = "";
2116 struct option md_longopts[] =
2118 #define OPTION_RELAX (OPTION_MD_BASE)
2119 #define OPTION_LITTLE (OPTION_MD_BASE + 1)
2120 #define OPTION_SMALL (OPTION_LITTLE + 1)
2121 #define OPTION_DSP (OPTION_SMALL + 1)
2123 {"relax", no_argument, NULL, OPTION_RELAX},
2124 {"little", no_argument, NULL, OPTION_LITTLE},
2125 {"small", no_argument, NULL, OPTION_SMALL},
2126 {"dsp", no_argument, NULL, OPTION_DSP},
2127 {NULL, no_argument, NULL, 0}
2129 size_t md_longopts_size = sizeof (md_longopts);
2132 md_parse_option (c, arg)
2134 char *arg ATTRIBUTE_UNUSED;
2144 target_big_endian = 0;
2163 md_show_usage (stream)
2166 fprintf (stream, _("\
2168 -little generate little endian code\n\
2169 -relax alter jump instructions for long displacements\n\
2170 -small align sections to 4 byte boundaries, not 16\n\
2171 -dsp enable sh-dsp insns, and disable sh3e / sh4 insns.\n"));
2175 tc_Nout_fix_to_chars ()
2177 printf (_("call to tc_Nout_fix_to_chars \n"));
2181 /* This struct is used to pass arguments to sh_count_relocs through
2182 bfd_map_over_sections. */
2184 struct sh_count_relocs
2186 /* Symbol we are looking for. */
2188 /* Count of relocs found. */
2192 /* Count the number of fixups in a section which refer to a particular
2193 symbol. When using BFD_ASSEMBLER, this is called via
2194 bfd_map_over_sections. */
2197 sh_count_relocs (abfd, sec, data)
2198 bfd *abfd ATTRIBUTE_UNUSED;
2202 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
2203 segment_info_type *seginfo;
2207 seginfo = seg_info (sec);
2208 if (seginfo == NULL)
2212 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2214 if (fix->fx_addsy == sym)
2222 /* Handle the count relocs for a particular section. When using
2223 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
2226 sh_frob_section (abfd, sec, ignore)
2227 bfd *abfd ATTRIBUTE_UNUSED;
2229 PTR ignore ATTRIBUTE_UNUSED;
2231 segment_info_type *seginfo;
2234 seginfo = seg_info (sec);
2235 if (seginfo == NULL)
2238 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2243 struct sh_count_relocs info;
2245 if (fix->fx_r_type != BFD_RELOC_SH_USES)
2248 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2249 symbol in the same section. */
2250 sym = fix->fx_addsy;
2252 || fix->fx_subsy != NULL
2253 || fix->fx_addnumber != 0
2254 || S_GET_SEGMENT (sym) != sec
2255 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2256 || S_GET_STORAGE_CLASS (sym) == C_EXT
2258 || S_IS_EXTERNAL (sym))
2260 as_warn_where (fix->fx_file, fix->fx_line,
2261 _(".uses does not refer to a local symbol in the same section"));
2265 /* Look through the fixups again, this time looking for one
2266 at the same location as sym. */
2267 val = S_GET_VALUE (sym);
2268 for (fscan = seginfo->fix_root;
2270 fscan = fscan->fx_next)
2271 if (val == fscan->fx_frag->fr_address + fscan->fx_where
2272 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
2273 && fscan->fx_r_type != BFD_RELOC_SH_CODE
2274 && fscan->fx_r_type != BFD_RELOC_SH_DATA
2275 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
2279 as_warn_where (fix->fx_file, fix->fx_line,
2280 _("can't find fixup pointed to by .uses"));
2284 if (fscan->fx_tcbit)
2286 /* We've already done this one. */
2290 /* The variable fscan should also be a fixup to a local symbol
2291 in the same section. */
2292 sym = fscan->fx_addsy;
2294 || fscan->fx_subsy != NULL
2295 || fscan->fx_addnumber != 0
2296 || S_GET_SEGMENT (sym) != sec
2297 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2298 || S_GET_STORAGE_CLASS (sym) == C_EXT
2300 || S_IS_EXTERNAL (sym))
2302 as_warn_where (fix->fx_file, fix->fx_line,
2303 _(".uses target does not refer to a local symbol in the same section"));
2307 /* Now we look through all the fixups of all the sections,
2308 counting the number of times we find a reference to sym. */
2311 #ifdef BFD_ASSEMBLER
2312 bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
2317 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
2318 sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
2325 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
2326 We have already adjusted the value of sym to include the
2327 fragment address, so we undo that adjustment here. */
2328 subseg_change (sec, 0);
2329 fix_new (symbol_get_frag (sym),
2330 S_GET_VALUE (sym) - symbol_get_frag (sym)->fr_address,
2331 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
2335 /* This function is called after the symbol table has been completed,
2336 but before the relocs or section contents have been written out.
2337 If we have seen any .uses pseudo-ops, they point to an instruction
2338 which loads a register with the address of a function. We look
2339 through the fixups to find where the function address is being
2340 loaded from. We then generate a COUNT reloc giving the number of
2341 times that function address is referred to. The linker uses this
2342 information when doing relaxing, to decide when it can eliminate
2343 the stored function address entirely. */
2351 #ifdef BFD_ASSEMBLER
2352 bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
2357 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
2358 sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
2363 /* Called after relaxing. Set the correct sizes of the fragments, and
2364 create relocs so that md_apply_fix will fill in the correct values. */
2367 md_convert_frag (headers, seg, fragP)
2368 #ifdef BFD_ASSEMBLER
2369 bfd *headers ATTRIBUTE_UNUSED;
2371 object_headers *headers;
2378 switch (fragP->fr_subtype)
2380 case C (COND_JUMP, COND8):
2381 case C (COND_JUMP_DELAY, COND8):
2382 subseg_change (seg, 0);
2383 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2384 1, BFD_RELOC_SH_PCDISP8BY2);
2389 case C (UNCOND_JUMP, UNCOND12):
2390 subseg_change (seg, 0);
2391 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2392 1, BFD_RELOC_SH_PCDISP12BY2);
2397 case C (UNCOND_JUMP, UNCOND32):
2398 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
2399 if (fragP->fr_symbol == NULL)
2400 as_bad_where (fragP->fr_file, fragP->fr_line,
2401 _("displacement overflows 12-bit field"));
2402 else if (S_IS_DEFINED (fragP->fr_symbol))
2403 as_bad_where (fragP->fr_file, fragP->fr_line,
2404 _("displacement to defined symbol %s overflows 12-bit field"),
2405 S_GET_NAME (fragP->fr_symbol));
2407 as_bad_where (fragP->fr_file, fragP->fr_line,
2408 _("displacement to undefined symbol %s overflows 12-bit field"),
2409 S_GET_NAME (fragP->fr_symbol));
2410 /* Stabilize this frag, so we don't trip an assert. */
2411 fragP->fr_fix += fragP->fr_var;
2415 case C (COND_JUMP, COND12):
2416 case C (COND_JUMP_DELAY, COND12):
2417 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
2418 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
2419 was due to gas incorrectly relaxing an out-of-range conditional
2420 branch with delay slot. It turned:
2421 bf.s L6 (slot mov.l r12,@(44,r0))
2424 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
2426 32: 10 cb mov.l r12,@(44,r0)
2427 Therefore, branches with delay slots have to be handled
2428 differently from ones without delay slots. */
2430 unsigned char *buffer =
2431 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
2432 int highbyte = target_big_endian ? 0 : 1;
2433 int lowbyte = target_big_endian ? 1 : 0;
2434 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
2436 /* Toggle the true/false bit of the bcond. */
2437 buffer[highbyte] ^= 0x2;
2439 /* If this is a dalayed branch, we may not put the the bra in the
2440 slot. So we change it to a non-delayed branch, like that:
2441 b! cond slot_label; bra disp; slot_label: slot_insn
2442 ??? We should try if swapping the conditional branch and
2443 its delay-slot insn already makes the branch reach. */
2445 /* Build a relocation to six / four bytes farther on. */
2446 subseg_change (seg, 0);
2447 fix_new (fragP, fragP->fr_fix, 2,
2448 #ifdef BFD_ASSEMBLER
2449 section_symbol (seg),
2451 seg_info (seg)->dot,
2453 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
2454 1, BFD_RELOC_SH_PCDISP8BY2);
2456 /* Set up a jump instruction. */
2457 buffer[highbyte + 2] = 0xa0;
2458 buffer[lowbyte + 2] = 0;
2459 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
2460 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
2464 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
2469 /* Fill in a NOP instruction. */
2470 buffer[highbyte + 4] = 0x0;
2471 buffer[lowbyte + 4] = 0x9;
2480 case C (COND_JUMP, COND32):
2481 case C (COND_JUMP_DELAY, COND32):
2482 case C (COND_JUMP, UNDEF_WORD_DISP):
2483 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
2484 if (fragP->fr_symbol == NULL)
2485 as_bad_where (fragP->fr_file, fragP->fr_line,
2486 _("displacement overflows 8-bit field"));
2487 else if (S_IS_DEFINED (fragP->fr_symbol))
2488 as_bad_where (fragP->fr_file, fragP->fr_line,
2489 _("displacement to defined symbol %s overflows 8-bit field"),
2490 S_GET_NAME (fragP->fr_symbol));
2492 as_bad_where (fragP->fr_file, fragP->fr_line,
2493 _("displacement to undefined symbol %s overflows 8-bit field "),
2494 S_GET_NAME (fragP->fr_symbol));
2495 /* Stabilize this frag, so we don't trip an assert. */
2496 fragP->fr_fix += fragP->fr_var;
2504 if (donerelax && !sh_relax)
2505 as_warn_where (fragP->fr_file, fragP->fr_line,
2506 _("overflow in branch to %s; converted into longer instruction sequence"),
2507 (fragP->fr_symbol != NULL
2508 ? S_GET_NAME (fragP->fr_symbol)
2513 md_section_align (seg, size)
2514 segT seg ATTRIBUTE_UNUSED;
2517 #ifdef BFD_ASSEMBLER
2520 #else /* ! OBJ_ELF */
2521 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
2522 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
2523 #endif /* ! OBJ_ELF */
2524 #else /* ! BFD_ASSEMBLER */
2525 return ((size + (1 << section_alignment[(int) seg]) - 1)
2526 & (-1 << section_alignment[(int) seg]));
2527 #endif /* ! BFD_ASSEMBLER */
2530 /* This static variable is set by s_uacons to tell sh_cons_align that
2531 the expession does not need to be aligned. */
2533 static int sh_no_align_cons = 0;
2535 /* This handles the unaligned space allocation pseudo-ops, such as
2536 .uaword. .uaword is just like .word, but the value does not need
2543 /* Tell sh_cons_align not to align this value. */
2544 sh_no_align_cons = 1;
2548 /* If a .word, et. al., pseud-op is seen, warn if the value is not
2549 aligned correctly. Note that this can cause warnings to be issued
2550 when assembling initialized structured which were declared with the
2551 packed attribute. FIXME: Perhaps we should require an option to
2552 enable this warning? */
2555 sh_cons_align (nbytes)
2561 if (sh_no_align_cons)
2563 /* This is an unaligned pseudo-op. */
2564 sh_no_align_cons = 0;
2569 while ((nbytes & 1) == 0)
2578 if (now_seg == absolute_section)
2580 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
2581 as_warn (_("misaligned data"));
2585 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
2586 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
2588 record_alignment (now_seg, nalign);
2591 /* When relaxing, we need to output a reloc for any .align directive
2592 that requests alignment to a four byte boundary or larger. This is
2593 also where we check for misaligned data. */
2596 sh_handle_align (frag)
2599 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
2601 if (frag->fr_type == rs_align_code)
2603 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
2604 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
2606 char *p = frag->fr_literal + frag->fr_fix;
2615 if (target_big_endian)
2617 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
2618 frag->fr_var = sizeof big_nop_pattern;
2622 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
2623 frag->fr_var = sizeof little_nop_pattern;
2626 else if (frag->fr_type == rs_align_test)
2629 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
2633 && (frag->fr_type == rs_align
2634 || frag->fr_type == rs_align_code)
2635 && frag->fr_address + frag->fr_fix > 0
2636 && frag->fr_offset > 1
2637 && now_seg != bss_section)
2638 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
2639 BFD_RELOC_SH_ALIGN);
2642 /* This macro decides whether a particular reloc is an entry in a
2643 switch table. It is used when relaxing, because the linker needs
2644 to know about all such entries so that it can adjust them if
2647 #ifdef BFD_ASSEMBLER
2648 #define SWITCH_TABLE_CONS(fix) (0)
2650 #define SWITCH_TABLE_CONS(fix) \
2651 ((fix)->fx_r_type == 0 \
2652 && ((fix)->fx_size == 2 \
2653 || (fix)->fx_size == 1 \
2654 || (fix)->fx_size == 4))
2657 #define SWITCH_TABLE(fix) \
2658 ((fix)->fx_addsy != NULL \
2659 && (fix)->fx_subsy != NULL \
2660 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
2661 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
2662 && ((fix)->fx_r_type == BFD_RELOC_32 \
2663 || (fix)->fx_r_type == BFD_RELOC_16 \
2664 || (fix)->fx_r_type == BFD_RELOC_8 \
2665 || SWITCH_TABLE_CONS (fix)))
2667 /* See whether we need to force a relocation into the output file.
2668 This is used to force out switch and PC relative relocations when
2672 sh_force_relocation (fix)
2676 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2677 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2678 || fix->fx_r_type == BFD_RELOC_SH_LOOP_START
2679 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END)
2685 return (fix->fx_pcrel
2686 || SWITCH_TABLE (fix)
2687 || fix->fx_r_type == BFD_RELOC_SH_COUNT
2688 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
2689 || fix->fx_r_type == BFD_RELOC_SH_CODE
2690 || fix->fx_r_type == BFD_RELOC_SH_DATA
2691 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
2696 sh_fix_adjustable (fixP)
2700 if (fixP->fx_addsy == NULL)
2703 if (fixP->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
2704 || fixP->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
2705 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
2706 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
2707 || fixP->fx_r_type == BFD_RELOC_8_PCREL
2708 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH16
2709 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH32)
2712 if (! TC_RELOC_RTSYM_LOC_FIXUP (fixP)
2713 || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
2714 || fixP->fx_r_type == BFD_RELOC_RVA)
2717 /* We need the symbol name for the VTABLE entries */
2718 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2719 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2726 sh_elf_final_processing ()
2730 /* Set file-specific flags to indicate if this code needs
2731 a processor with the sh-dsp / sh3e ISA to execute. */
2732 if (valid_arch & arch_sh1)
2734 else if (valid_arch & arch_sh2)
2736 else if (valid_arch & arch_sh_dsp)
2738 else if (valid_arch & arch_sh3)
2740 else if (valid_arch & arch_sh3_dsp)
2742 else if (valid_arch & arch_sh3e)
2744 else if (valid_arch & arch_sh4)
2749 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
2750 elf_elfheader (stdoutput)->e_flags |= val;
2754 /* Apply a fixup to the object file. */
2756 #ifdef BFD_ASSEMBLER
2758 md_apply_fix (fixP, valp)
2763 md_apply_fix (fixP, val)
2768 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2769 int lowbyte = target_big_endian ? 1 : 0;
2770 int highbyte = target_big_endian ? 0 : 1;
2771 #ifdef BFD_ASSEMBLER
2777 #ifdef BFD_ASSEMBLER
2778 /* A difference between two symbols, the second of which is in the
2779 current section, is transformed in a PC-relative relocation to
2780 the other symbol. We have to adjust the relocation type here. */
2783 switch (fixP->fx_r_type)
2789 fixP->fx_r_type = BFD_RELOC_32_PCREL;
2792 /* Currently, we only support 32-bit PCREL relocations.
2793 We'd need a new reloc type to handle 16_PCREL, and
2794 8_PCREL is already taken for R_SH_SWITCH8, which
2795 apparently does something completely different than what
2798 bfd_set_error (bfd_error_bad_value);
2802 bfd_set_error (bfd_error_bad_value);
2807 /* The function adjust_reloc_syms won't convert a reloc against a weak
2808 symbol into a reloc against a section, but bfd_install_relocation
2809 will screw up if the symbol is defined, so we have to adjust val here
2810 to avoid the screw up later.
2812 For ordinary relocs, this does not happen for ELF, since for ELF,
2813 bfd_install_relocation uses the "special function" field of the
2814 howto, and does not execute the code that needs to be undone, as long
2815 as the special function does not return bfd_reloc_continue.
2816 It can happen for GOT- and PLT-type relocs the way they are
2817 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
2818 doesn't matter here since those relocs don't use VAL; see below. */
2819 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2820 && fixP->fx_addsy != NULL
2821 && S_IS_WEAK (fixP->fx_addsy))
2822 val -= S_GET_VALUE (fixP->fx_addsy);
2825 #ifndef BFD_ASSEMBLER
2826 if (fixP->fx_r_type == 0)
2828 if (fixP->fx_size == 2)
2829 fixP->fx_r_type = BFD_RELOC_16;
2830 else if (fixP->fx_size == 4)
2831 fixP->fx_r_type = BFD_RELOC_32;
2832 else if (fixP->fx_size == 1)
2833 fixP->fx_r_type = BFD_RELOC_8;
2841 switch (fixP->fx_r_type)
2843 case BFD_RELOC_SH_IMM4:
2845 *buf = (*buf & 0xf0) | (val & 0xf);
2848 case BFD_RELOC_SH_IMM4BY2:
2851 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
2854 case BFD_RELOC_SH_IMM4BY4:
2857 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
2860 case BFD_RELOC_SH_IMM8BY2:
2866 case BFD_RELOC_SH_IMM8BY4:
2873 case BFD_RELOC_SH_IMM8:
2874 /* Sometimes the 8 bit value is sign extended (e.g., add) and
2875 sometimes it is not (e.g., and). We permit any 8 bit value.
2876 Note that adding further restrictions may invalidate
2877 reasonable looking assembly code, such as ``and -0x1,r0''. */
2883 case BFD_RELOC_SH_PCRELIMM8BY4:
2884 /* The lower two bits of the PC are cleared before the
2885 displacement is added in. We can assume that the destination
2886 is on a 4 byte bounday. If this instruction is also on a 4
2887 byte boundary, then we want
2889 and target - here is a multiple of 4.
2890 Otherwise, we are on a 2 byte boundary, and we want
2891 (target - (here - 2)) / 4
2892 and target - here is not a multiple of 4. Computing
2893 (target - (here - 2)) / 4 == (target - here + 2) / 4
2894 works for both cases, since in the first case the addition of
2895 2 will be removed by the division. target - here is in the
2897 val = (val + 2) / 4;
2899 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2903 case BFD_RELOC_SH_PCRELIMM8BY2:
2906 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2910 case BFD_RELOC_SH_PCDISP8BY2:
2912 if (val < -0x80 || val > 0x7f)
2913 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2917 case BFD_RELOC_SH_PCDISP12BY2:
2919 if (val < -0x800 || val > 0x7ff)
2920 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2921 buf[lowbyte] = val & 0xff;
2922 buf[highbyte] |= (val >> 8) & 0xf;
2926 case BFD_RELOC_32_PCREL:
2927 md_number_to_chars (buf, val, 4);
2931 md_number_to_chars (buf, val, 2);
2934 case BFD_RELOC_SH_USES:
2935 /* Pass the value into sh_coff_reloc_mangle. */
2936 fixP->fx_addnumber = val;
2939 case BFD_RELOC_SH_COUNT:
2940 case BFD_RELOC_SH_ALIGN:
2941 case BFD_RELOC_SH_CODE:
2942 case BFD_RELOC_SH_DATA:
2943 case BFD_RELOC_SH_LABEL:
2944 /* Nothing to do here. */
2947 case BFD_RELOC_SH_LOOP_START:
2948 case BFD_RELOC_SH_LOOP_END:
2950 case BFD_RELOC_VTABLE_INHERIT:
2951 case BFD_RELOC_VTABLE_ENTRY:
2953 #ifdef BFD_ASSEMBLER
2960 case BFD_RELOC_32_PLT_PCREL:
2961 /* Make the jump instruction point to the address of the operand. At
2962 runtime we merely add the offset to the actual PLT entry. */
2966 case BFD_RELOC_SH_GOTPC:
2967 /* This is tough to explain. We end up with this one if we have
2968 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
2969 The goal here is to obtain the absolute address of the GOT,
2970 and it is strongly preferable from a performance point of
2971 view to avoid using a runtime relocation for this. There are
2972 cases where you have something like:
2974 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
2976 and here no correction would be required. Internally in the
2977 assembler we treat operands of this form as not being pcrel
2978 since the '.' is explicitly mentioned, and I wonder whether
2979 it would simplify matters to do it this way. Who knows. In
2980 earlier versions of the PIC patches, the pcrel_adjust field
2981 was used to store the correction, but since the expression is
2982 not pcrel, I felt it would be confusing to do it this way. */
2984 md_number_to_chars (buf, val, 4);
2987 case BFD_RELOC_32_GOT_PCREL:
2988 *valp = 0; /* Fully resolved at runtime. No addend. */
2989 md_number_to_chars (buf, 0, 4);
2992 case BFD_RELOC_32_GOTOFF:
3002 if ((val & ((1 << shift) - 1)) != 0)
3003 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
3007 val = ((val >> shift)
3008 | ((long) -1 & ~ ((long) -1 >> shift)));
3010 if (max != 0 && (val < min || val > max))
3011 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
3013 #ifdef BFD_ASSEMBLER
3018 /* Called just before address relaxation. Return the length
3019 by which a fragment must grow to reach it's destination. */
3022 md_estimate_size_before_relax (fragP, segment_type)
3023 register fragS *fragP;
3024 register segT segment_type;
3026 switch (fragP->fr_subtype)
3028 case C (UNCOND_JUMP, UNDEF_DISP):
3029 /* Used to be a branch to somewhere which was unknown. */
3030 if (!fragP->fr_symbol)
3032 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3033 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
3035 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3037 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3038 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
3042 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
3043 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
3044 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
3050 case C (COND_JUMP, UNDEF_DISP):
3051 case C (COND_JUMP_DELAY, UNDEF_DISP):
3052 /* Used to be a branch to somewhere which was unknown. */
3053 if (fragP->fr_symbol
3054 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3056 int what = GET_WHAT (fragP->fr_subtype);
3057 /* Got a symbol and it's defined in this segment, become byte
3058 sized - maybe it will fix up. */
3059 fragP->fr_subtype = C (what, COND8);
3060 fragP->fr_var = md_relax_table[C (what, COND8)].rlx_length;
3062 else if (fragP->fr_symbol)
3064 int what = GET_WHAT (fragP->fr_subtype);
3065 /* Its got a segment, but its not ours, so it will always be long. */
3066 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
3067 fragP->fr_var = md_relax_table[C (what, COND32)].rlx_length;
3068 return md_relax_table[C (what, COND32)].rlx_length;
3072 int what = GET_WHAT (fragP->fr_subtype);
3073 /* We know the abs value. */
3074 fragP->fr_subtype = C (what, COND8);
3075 fragP->fr_var = md_relax_table[C (what, COND8)].rlx_length;
3080 return fragP->fr_var;
3083 /* Put number into target byte order. */
3086 md_number_to_chars (ptr, use, nbytes)
3091 if (! target_big_endian)
3092 number_to_chars_littleendian (ptr, use, nbytes);
3094 number_to_chars_bigendian (ptr, use, nbytes);
3098 md_pcrel_from (fixP)
3101 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
3107 tc_coff_sizemachdep (frag)
3110 return md_relax_table[frag->fr_subtype].rlx_length;
3113 #endif /* OBJ_COFF */
3115 #ifndef BFD_ASSEMBLER
3118 /* Map BFD relocs to SH COFF relocs. */
3122 bfd_reloc_code_real_type bfd_reloc;
3126 static const struct reloc_map coff_reloc_map[] =
3128 { BFD_RELOC_32, R_SH_IMM32 },
3129 { BFD_RELOC_16, R_SH_IMM16 },
3130 { BFD_RELOC_8, R_SH_IMM8 },
3131 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
3132 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
3133 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
3134 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
3135 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
3136 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
3137 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
3138 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
3139 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
3140 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
3141 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
3142 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
3143 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
3144 { BFD_RELOC_SH_USES, R_SH_USES },
3145 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
3146 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
3147 { BFD_RELOC_SH_CODE, R_SH_CODE },
3148 { BFD_RELOC_SH_DATA, R_SH_DATA },
3149 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
3150 { BFD_RELOC_UNUSED, 0 }
3153 /* Adjust a reloc for the SH. This is similar to the generic code,
3154 but does some minor tweaking. */
3157 sh_coff_reloc_mangle (seg, fix, intr, paddr)
3158 segment_info_type *seg;
3160 struct internal_reloc *intr;
3163 symbolS *symbol_ptr = fix->fx_addsy;
3166 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
3168 if (! SWITCH_TABLE (fix))
3170 const struct reloc_map *rm;
3172 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
3173 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
3175 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
3176 as_bad_where (fix->fx_file, fix->fx_line,
3177 _("Can not represent %s relocation in this object file format"),
3178 bfd_get_reloc_code_name (fix->fx_r_type));
3179 intr->r_type = rm->sh_reloc;
3186 if (fix->fx_r_type == BFD_RELOC_16)
3187 intr->r_type = R_SH_SWITCH16;
3188 else if (fix->fx_r_type == BFD_RELOC_8)
3189 intr->r_type = R_SH_SWITCH8;
3190 else if (fix->fx_r_type == BFD_RELOC_32)
3191 intr->r_type = R_SH_SWITCH32;
3195 /* For a switch reloc, we set r_offset to the difference between
3196 the reloc address and the subtrahend. When the linker is
3197 doing relaxing, it can use the determine the starting and
3198 ending points of the switch difference expression. */
3199 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
3202 /* PC relative relocs are always against the current section. */
3203 if (symbol_ptr == NULL)
3205 switch (fix->fx_r_type)
3207 case BFD_RELOC_SH_PCRELIMM8BY2:
3208 case BFD_RELOC_SH_PCRELIMM8BY4:
3209 case BFD_RELOC_SH_PCDISP8BY2:
3210 case BFD_RELOC_SH_PCDISP12BY2:
3211 case BFD_RELOC_SH_USES:
3212 symbol_ptr = seg->dot;
3219 if (fix->fx_r_type == BFD_RELOC_SH_USES)
3221 /* We can't store the offset in the object file, since this
3222 reloc does not take up any space, so we store it in r_offset.
3223 The fx_addnumber field was set in md_apply_fix. */
3224 intr->r_offset = fix->fx_addnumber;
3226 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
3228 /* We can't store the count in the object file, since this reloc
3229 does not take up any space, so we store it in r_offset. The
3230 fx_offset field was set when the fixup was created in
3231 sh_coff_frob_file. */
3232 intr->r_offset = fix->fx_offset;
3233 /* This reloc is always absolute. */
3236 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
3238 /* Store the alignment in the r_offset field. */
3239 intr->r_offset = fix->fx_offset;
3240 /* This reloc is always absolute. */
3243 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
3244 || fix->fx_r_type == BFD_RELOC_SH_DATA
3245 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
3247 /* These relocs are always absolute. */
3251 /* Turn the segment of the symbol into an offset. */
3252 if (symbol_ptr != NULL)
3254 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
3256 intr->r_symndx = dot->sy_number;
3258 intr->r_symndx = symbol_ptr->sy_number;
3261 intr->r_symndx = -1;
3264 #endif /* OBJ_COFF */
3265 #endif /* ! BFD_ASSEMBLER */
3267 #ifdef BFD_ASSEMBLER
3269 /* Create a reloc. */
3272 tc_gen_reloc (section, fixp)
3273 asection *section ATTRIBUTE_UNUSED;
3277 bfd_reloc_code_real_type r_type;
3279 rel = (arelent *) xmalloc (sizeof (arelent));
3280 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3281 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3282 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3284 r_type = fixp->fx_r_type;
3286 if (SWITCH_TABLE (fixp))
3288 rel->addend = rel->address - S_GET_VALUE (fixp->fx_subsy);
3289 if (r_type == BFD_RELOC_16)
3290 r_type = BFD_RELOC_SH_SWITCH16;
3291 else if (r_type == BFD_RELOC_8)
3292 r_type = BFD_RELOC_8_PCREL;
3293 else if (r_type == BFD_RELOC_32)
3294 r_type = BFD_RELOC_SH_SWITCH32;
3298 else if (r_type == BFD_RELOC_SH_USES)
3299 rel->addend = fixp->fx_addnumber;
3300 else if (r_type == BFD_RELOC_SH_COUNT)
3301 rel->addend = fixp->fx_offset;
3302 else if (r_type == BFD_RELOC_SH_ALIGN)
3303 rel->addend = fixp->fx_offset;
3304 else if (r_type == BFD_RELOC_VTABLE_INHERIT
3305 || r_type == BFD_RELOC_VTABLE_ENTRY)
3306 rel->addend = fixp->fx_offset;
3307 else if (r_type == BFD_RELOC_SH_LOOP_START
3308 || r_type == BFD_RELOC_SH_LOOP_END)
3309 rel->addend = fixp->fx_offset;
3310 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
3313 rel->address = rel->addend = fixp->fx_offset;
3315 else if (fixp->fx_pcrel)
3316 rel->addend = fixp->fx_addnumber;
3317 else if (r_type == BFD_RELOC_32 || r_type == BFD_RELOC_32_GOTOFF)
3318 rel->addend = fixp->fx_addnumber;
3322 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
3323 if (rel->howto == NULL)
3325 as_bad_where (fixp->fx_file, fixp->fx_line,
3326 _("Cannot represent relocation type %s"),
3327 bfd_get_reloc_code_name (r_type));
3328 /* Set howto to a garbage value so that we can keep going. */
3329 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3330 assert (rel->howto != NULL);
3336 #endif /* BFD_ASSEMBLER */