file config.h was initially added on branch binutils-2_11-branch.
[binutils.git] / gas / config / tc-sh.c
blob69dbf751672fcae88e2bd0c6258874ab92014928
1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
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)
10 any later version.
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. */
22 /* Written By Steve Chamberlain <sac@cygnus.com> */
24 #include <stdio.h>
25 #include "as.h"
26 #include "bfd.h"
27 #include "subsegs.h"
28 #define DEFINE_TABLE
29 #include "opcodes/sh-opc.h"
30 #include <ctype.h>
31 #include "struc-symbol.h"
33 #ifdef OBJ_ELF
34 #include "elf/sh.h"
35 #endif
37 #include "dwarf2dbg.h"
39 const char comment_chars[] = "!";
40 const char line_separator_chars[] = ";";
41 const char line_comment_chars[] = "!#";
43 static void s_uses PARAMS ((int));
45 static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
46 static void sh_frob_section PARAMS ((bfd *, segT, PTR));
48 void cons ();
49 void s_align_bytes ();
50 static void s_uacons PARAMS ((int));
51 static sh_opcode_info *find_cooked_opcode PARAMS ((char **));
52 static unsigned int assemble_ppi PARAMS ((char *, sh_opcode_info *));
54 #ifdef OBJ_ELF
55 static void sh_elf_cons PARAMS ((int));
57 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
58 #endif
60 int shl = 0;
62 static void
63 little (ignore)
64 int ignore ATTRIBUTE_UNUSED;
66 shl = 1;
67 target_big_endian = 0;
70 /* This table describes all the machine specific pseudo-ops the assembler
71 has to support. The fields are:
72 pseudo-op name without dot
73 function to call to execute this pseudo-op
74 Integer arg to pass to the function. */
76 const pseudo_typeS md_pseudo_table[] =
78 #ifdef OBJ_ELF
79 {"long", sh_elf_cons, 4},
80 {"int", sh_elf_cons, 4},
81 {"word", sh_elf_cons, 2},
82 {"short", sh_elf_cons, 2},
83 #else
84 {"int", cons, 4},
85 {"word", cons, 2},
86 #endif /* OBJ_ELF */
87 {"form", listing_psize, 0},
88 {"little", little, 0},
89 {"heading", listing_title, 0},
90 {"import", s_ignore, 0},
91 {"page", listing_eject, 0},
92 {"program", s_ignore, 0},
93 {"uses", s_uses, 0},
94 {"uaword", s_uacons, 2},
95 {"ualong", s_uacons, 4},
96 {"uaquad", s_uacons, 8},
97 {"2byte", s_uacons, 2},
98 {"4byte", s_uacons, 4},
99 {"8byte", s_uacons, 8},
100 {"file", dwarf2_directive_file, 0 },
101 {"loc", dwarf2_directive_loc, 0 },
102 {0, 0, 0}
105 /*int md_reloc_size; */
107 int sh_relax; /* set if -relax seen */
109 /* Whether -small was seen. */
111 int sh_small;
113 /* Whether -dsp was seen. */
115 static int sh_dsp;
117 /* The bit mask of architectures that could
118 accomodate the insns seen so far. */
119 static int valid_arch;
121 const char EXP_CHARS[] = "eE";
123 /* Chars that mean this number is a floating point constant. */
124 /* As in 0f12.456 */
125 /* or 0d1.2345e12 */
126 const char FLT_CHARS[] = "rRsSfFdDxXpP";
128 #define C(a,b) ENCODE_RELAX(a,b)
130 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
131 #define GET_WHAT(x) ((x>>4))
133 /* These are the three types of relaxable instrction. */
134 #define COND_JUMP 1
135 #define COND_JUMP_DELAY 2
136 #define UNCOND_JUMP 3
137 #define END 4
139 #define UNDEF_DISP 0
140 #define COND8 1
141 #define COND12 2
142 #define COND32 3
143 #define UNCOND12 1
144 #define UNCOND32 2
145 #define UNDEF_WORD_DISP 4
147 #define UNCOND12 1
148 #define UNCOND32 2
150 /* Branch displacements are from the address of the branch plus
151 four, thus all minimum and maximum values have 4 added to them. */
152 #define COND8_F 258
153 #define COND8_M -252
154 #define COND8_LENGTH 2
156 /* There is one extra instruction before the branch, so we must add
157 two more bytes to account for it. */
158 #define COND12_F 4100
159 #define COND12_M -4090
160 #define COND12_LENGTH 6
162 #define COND12_DELAY_LENGTH 4
164 /* ??? The minimum and maximum values are wrong, but this does not matter
165 since this relocation type is not supported yet. */
166 #define COND32_F (1<<30)
167 #define COND32_M -(1<<30)
168 #define COND32_LENGTH 14
170 #define UNCOND12_F 4098
171 #define UNCOND12_M -4092
172 #define UNCOND12_LENGTH 2
174 /* ??? The minimum and maximum values are wrong, but this does not matter
175 since this relocation type is not supported yet. */
176 #define UNCOND32_F (1<<30)
177 #define UNCOND32_M -(1<<30)
178 #define UNCOND32_LENGTH 14
180 #define EMPTY { 0, 0, 0, 0 }
182 const relax_typeS md_relax_table[C (END, 0)] = {
183 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
184 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
186 EMPTY,
187 /* C (COND_JUMP, COND8) */
188 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
189 /* C (COND_JUMP, COND12) */
190 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
191 /* C (COND_JUMP, COND32) */
192 { COND32_F, COND32_M, COND32_LENGTH, 0, },
193 EMPTY, EMPTY, EMPTY, EMPTY,
194 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
196 EMPTY,
197 /* C (COND_JUMP_DELAY, COND8) */
198 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
199 /* C (COND_JUMP_DELAY, COND12) */
200 { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
201 /* C (COND_JUMP_DELAY, COND32) */
202 { COND32_F, COND32_M, COND32_LENGTH, 0, },
203 EMPTY, EMPTY, EMPTY, EMPTY,
204 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
206 EMPTY,
207 /* C (UNCOND_JUMP, UNCOND12) */
208 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
209 /* C (UNCOND_JUMP, UNCOND32) */
210 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
211 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
212 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
215 #undef EMPTY
217 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
220 #ifdef OBJ_ELF
221 /* Parse @got, etc. and return the desired relocation.
222 If we have additional arithmetic expression, then we fill in new_exp_p. */
223 static bfd_reloc_code_real_type
224 sh_elf_suffix (str_p, exp_p, new_exp_p)
225 char **str_p;
226 expressionS *exp_p, *new_exp_p;
228 struct map_bfd {
229 char *string;
230 int length;
231 bfd_reloc_code_real_type reloc;
234 char ident[20];
235 char *str = *str_p;
236 char *str2;
237 int ch;
238 int len;
239 struct map_bfd *ptr;
241 #define MAP(str,reloc) { str, sizeof (str)-1, reloc }
243 static struct map_bfd mapping[] = {
244 MAP ("got", BFD_RELOC_32_GOT_PCREL),
245 MAP ("plt", BFD_RELOC_32_PLT_PCREL),
246 MAP ("gotoff", BFD_RELOC_32_GOTOFF),
247 { (char *)0, 0, BFD_RELOC_UNUSED }
250 if (*str++ != '@')
251 return BFD_RELOC_UNUSED;
253 for (ch = *str, str2 = ident;
254 (str2 < ident + sizeof (ident) - 1
255 && (isalnum (ch) || ch == '@'));
256 ch = *++str)
258 *str2++ = (islower (ch)) ? ch : tolower (ch);
261 *str2 = '\0';
262 len = str2 - ident;
264 ch = ident[0];
265 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
266 if (ch == ptr->string[0]
267 && len == ptr->length
268 && memcmp (ident, ptr->string, ptr->length) == 0)
270 /* Now check for identifier@suffix+constant */
271 if (*str == '-' || *str == '+')
273 char *orig_line = input_line_pointer;
275 input_line_pointer = str;
276 expression (new_exp_p);
277 if (new_exp_p->X_op == O_constant)
279 exp_p->X_add_number += new_exp_p->X_add_number;
280 str = input_line_pointer;
282 if (new_exp_p->X_op == O_subtract)
283 str = input_line_pointer;
285 if (&input_line_pointer != str_p)
286 input_line_pointer = orig_line;
289 *str_p = str;
290 return ptr->reloc;
293 return BFD_RELOC_UNUSED;
296 /* The regular cons() function, that reads constants, doesn't support
297 suffixes such as @GOT, @GOTOFF and @PLT, that generate
298 machine-specific relocation types. So we must define it here. */
299 /* Clobbers input_line_pointer, checks end-of-line. */
300 static void
301 sh_elf_cons (nbytes)
302 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
304 expressionS exp, new_exp;
305 bfd_reloc_code_real_type reloc;
306 const char *name;
308 if (is_it_end_of_statement ())
310 demand_empty_rest_of_line ();
311 return;
316 expression (&exp);
317 new_exp.X_op = O_absent;
318 new_exp.X_add_symbol = new_exp.X_op_symbol = NULL;
319 /* If the _GLOBAL_OFFSET_TABLE_ symbol hasn't been found yet,
320 use the name of the symbol to tell whether it's the
321 _GLOBAL_OFFSET_TABLE_. If it has, comparing the symbols is
322 sufficient. */
323 if (! GOT_symbol && exp.X_add_symbol)
324 name = S_GET_NAME (exp.X_add_symbol);
325 else
326 name = NULL;
327 /* Check whether this expression involves the
328 _GLOBAL_OFFSET_TABLE_ symbol, by itself or added to a
329 difference of two other symbols. */
330 if (((GOT_symbol && GOT_symbol == exp.X_add_symbol)
331 || (! GOT_symbol && name
332 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0))
333 && (exp.X_op == O_symbol
334 || (exp.X_op == O_add
335 && ((symbol_get_value_expression (exp.X_op_symbol)->X_op)
336 == O_subtract))))
338 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput,
339 BFD_RELOC_32);
340 int size = bfd_get_reloc_size (reloc_howto);
342 if (GOT_symbol == NULL)
343 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
345 if (size > nbytes)
346 as_bad (_("%s relocations do not fit in %d bytes\n"),
347 reloc_howto->name, nbytes);
348 else
350 register char *p = frag_more ((int) nbytes);
351 int offset = nbytes - size;
353 fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
354 size, &exp, 0, TC_RELOC_GLOBAL_OFFSET_TABLE);
357 /* Check if this symbol involves one of the magic suffixes, such
358 as @GOT, @GOTOFF or @PLT, and determine which relocation type
359 to use. */
360 else if ((exp.X_op == O_symbol || (exp.X_op == O_add && exp.X_op_symbol))
361 && *input_line_pointer == '@'
362 && ((reloc = sh_elf_suffix (&input_line_pointer, &exp, &new_exp))
363 != BFD_RELOC_UNUSED))
365 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput,
366 reloc);
367 int size = bfd_get_reloc_size (reloc_howto);
369 /* Force a GOT to be generated. */
370 if (GOT_symbol == NULL)
371 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
373 if (size > nbytes)
374 as_bad (_("%s relocations do not fit in %d bytes\n"),
375 reloc_howto->name, nbytes);
376 else
378 register char *p = frag_more ((int) nbytes);
379 int offset = nbytes - size;
381 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size,
382 &exp, 0, reloc);
383 if (new_exp.X_op != O_absent)
384 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size,
385 &new_exp, 0, BFD_RELOC_32);
388 else
389 emit_expr (&exp, (unsigned int) nbytes);
391 while (*input_line_pointer++ == ',');
393 input_line_pointer--; /* Put terminator back into stream. */
394 if (*input_line_pointer == '#' || *input_line_pointer == '!')
396 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
398 else
399 demand_empty_rest_of_line ();
401 #endif /* OBJ_ELF */
404 /* This function is called once, at assembler startup time. This should
405 set up all the tables, etc that the MD part of the assembler needs. */
407 void
408 md_begin ()
410 sh_opcode_info *opcode;
411 char *prev_name = "";
412 int target_arch;
414 #ifdef TE_PE
415 /* The WinCE OS only supports little endian executables. */
416 target_big_endian = 0;
417 #else
418 if (! shl)
419 target_big_endian = 1;
420 #endif
422 target_arch = arch_sh1_up & ~(sh_dsp ? arch_sh3e_up : arch_sh_dsp_up);
423 valid_arch = target_arch;
425 opcode_hash_control = hash_new ();
427 /* Insert unique names into hash table. */
428 for (opcode = sh_table; opcode->name; opcode++)
430 if (strcmp (prev_name, opcode->name))
432 if (! (opcode->arch & target_arch))
433 continue;
434 prev_name = opcode->name;
435 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
437 else
439 /* Make all the opcodes with the same name point to the same
440 string. */
441 opcode->name = prev_name;
446 static int reg_m;
447 static int reg_n;
448 static int reg_x, reg_y;
449 static int reg_efg;
450 static int reg_b;
452 typedef struct
454 sh_arg_type type;
455 int reg;
456 expressionS immediate;
458 sh_operand_info;
460 #define IDENT_CHAR(c) (isalnum (c) || (c) == '_')
462 /* Try to parse a reg name. Return the number of chars consumed. */
464 static int
465 parse_reg (src, mode, reg)
466 char *src;
467 int *mode;
468 int *reg;
470 char l0 = tolower (src[0]);
471 char l1 = l0 ? tolower (src[1]) : 0;
473 /* We use ! IDENT_CHAR for the next character after the register name, to
474 make sure that we won't accidentally recognize a symbol name such as
475 'sram' or sr_ram as being a reference to the register 'sr'. */
477 if (l0 == 'r')
479 if (l1 == '1')
481 if (src[2] >= '0' && src[2] <= '5'
482 && ! IDENT_CHAR ((unsigned char) src[3]))
484 *mode = A_REG_N;
485 *reg = 10 + src[2] - '0';
486 return 3;
489 if (l1 >= '0' && l1 <= '9'
490 && ! IDENT_CHAR ((unsigned char) src[2]))
492 *mode = A_REG_N;
493 *reg = (l1 - '0');
494 return 2;
496 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
497 && ! IDENT_CHAR ((unsigned char) src[7]))
499 *mode = A_REG_B;
500 *reg = (l1 - '0');
501 return 7;
504 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
506 *mode = A_RE;
507 return 2;
509 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
511 *mode = A_RS;
512 return 2;
516 if (l0 == 'a')
518 if (l1 == '0')
520 if (! IDENT_CHAR ((unsigned char) src[2]))
522 *mode = DSP_REG_N;
523 *reg = A_A0_NUM;
524 return 2;
526 if (tolower (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
528 *mode = DSP_REG_N;
529 *reg = A_A0G_NUM;
530 return 3;
533 if (l1 == '1')
535 if (! IDENT_CHAR ((unsigned char) src[2]))
537 *mode = DSP_REG_N;
538 *reg = A_A1_NUM;
539 return 2;
541 if (tolower (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
543 *mode = DSP_REG_N;
544 *reg = A_A1G_NUM;
545 return 3;
549 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
550 && ! IDENT_CHAR ((unsigned char) src[3]))
552 *mode = A_REG_N;
553 *reg = 4 + (l1 - '0');
554 return 3;
556 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
557 && ! IDENT_CHAR ((unsigned char) src[3]))
559 *mode = A_REG_N;
560 *reg = 6 + (l1 - '0');
561 return 3;
563 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
564 && ! IDENT_CHAR ((unsigned char) src[3]))
566 int n = l1 - '0';
568 *mode = A_REG_N;
569 *reg = n | ((~n & 2) << 1);
570 return 3;
574 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[3]))
576 if (l1 == 's')
578 *mode = A_REG_N;
579 *reg = 8;
580 return 2;
582 if (l1 == 'x')
584 *mode = A_REG_N;
585 *reg = 8;
586 return 2;
588 if (l1 == 'y')
590 *mode = A_REG_N;
591 *reg = 9;
592 return 2;
596 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
597 && ! IDENT_CHAR ((unsigned char) src[2]))
599 *mode = DSP_REG_N;
600 *reg = A_X0_NUM + l1 - '0';
601 return 2;
604 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
605 && ! IDENT_CHAR ((unsigned char) src[2]))
607 *mode = DSP_REG_N;
608 *reg = A_Y0_NUM + l1 - '0';
609 return 2;
612 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
613 && ! IDENT_CHAR ((unsigned char) src[2]))
615 *mode = DSP_REG_N;
616 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
617 return 2;
620 if (l0 == 's'
621 && l1 == 's'
622 && tolower (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
624 *mode = A_SSR;
625 return 3;
628 if (l0 == 's' && l1 == 'p' && tolower (src[2]) == 'c'
629 && ! IDENT_CHAR ((unsigned char) src[3]))
631 *mode = A_SPC;
632 return 3;
635 if (l0 == 's' && l1 == 'g' && tolower (src[2]) == 'r'
636 && ! IDENT_CHAR ((unsigned char) src[3]))
638 *mode = A_SGR;
639 return 3;
642 if (l0 == 'd' && l1 == 's' && tolower (src[2]) == 'r'
643 && ! IDENT_CHAR ((unsigned char) src[3]))
645 *mode = A_DSR;
646 return 3;
649 if (l0 == 'd' && l1 == 'b' && tolower (src[2]) == 'r'
650 && ! IDENT_CHAR ((unsigned char) src[3]))
652 *mode = A_DBR;
653 return 3;
656 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
658 *mode = A_SR;
659 return 2;
662 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
664 *mode = A_REG_N;
665 *reg = 15;
666 return 2;
669 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
671 *mode = A_PR;
672 return 2;
674 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
676 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
677 and use an uninitialized immediate. */
678 *mode = A_PC;
679 return 2;
681 if (l0 == 'g' && l1 == 'b' && tolower (src[2]) == 'r'
682 && ! IDENT_CHAR ((unsigned char) src[3]))
684 *mode = A_GBR;
685 return 3;
687 if (l0 == 'v' && l1 == 'b' && tolower (src[2]) == 'r'
688 && ! IDENT_CHAR ((unsigned char) src[3]))
690 *mode = A_VBR;
691 return 3;
694 if (l0 == 'm' && l1 == 'a' && tolower (src[2]) == 'c'
695 && ! IDENT_CHAR ((unsigned char) src[4]))
697 if (src[3] == 'l')
699 *mode = A_MACL;
700 return 4;
702 if (src[3] == 'h')
704 *mode = A_MACH;
705 return 4;
708 if (l0 == 'm' && l1 == 'o' && tolower (src[2]) == 'd'
709 && ! IDENT_CHAR ((unsigned char) src[4]))
711 *mode = A_MOD;
712 return 3;
714 if (l0 == 'f' && l1 == 'r')
716 if (src[2] == '1')
718 if (src[3] >= '0' && src[3] <= '5'
719 && ! IDENT_CHAR ((unsigned char) src[4]))
721 *mode = F_REG_N;
722 *reg = 10 + src[3] - '0';
723 return 4;
726 if (src[2] >= '0' && src[2] <= '9'
727 && ! IDENT_CHAR ((unsigned char) src[3]))
729 *mode = F_REG_N;
730 *reg = (src[2] - '0');
731 return 3;
734 if (l0 == 'd' && l1 == 'r')
736 if (src[2] == '1')
738 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
739 && ! IDENT_CHAR ((unsigned char) src[4]))
741 *mode = D_REG_N;
742 *reg = 10 + src[3] - '0';
743 return 4;
746 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
747 && ! IDENT_CHAR ((unsigned char) src[3]))
749 *mode = D_REG_N;
750 *reg = (src[2] - '0');
751 return 3;
754 if (l0 == 'x' && l1 == 'd')
756 if (src[2] == '1')
758 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
759 && ! IDENT_CHAR ((unsigned char) src[4]))
761 *mode = X_REG_N;
762 *reg = 11 + src[3] - '0';
763 return 4;
766 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
767 && ! IDENT_CHAR ((unsigned char) src[3]))
769 *mode = X_REG_N;
770 *reg = (src[2] - '0') + 1;
771 return 3;
774 if (l0 == 'f' && l1 == 'v')
776 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
778 *mode = V_REG_N;
779 *reg = 12;
780 return 4;
782 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
783 && ! IDENT_CHAR ((unsigned char) src[3]))
785 *mode = V_REG_N;
786 *reg = (src[2] - '0');
787 return 3;
790 if (l0 == 'f' && l1 == 'p' && tolower (src[2]) == 'u'
791 && tolower (src[3]) == 'l'
792 && ! IDENT_CHAR ((unsigned char) src[4]))
794 *mode = FPUL_N;
795 return 4;
798 if (l0 == 'f' && l1 == 'p' && tolower (src[2]) == 's'
799 && tolower (src[3]) == 'c'
800 && tolower (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
802 *mode = FPSCR_N;
803 return 5;
806 if (l0 == 'x' && l1 == 'm' && tolower (src[2]) == 't'
807 && tolower (src[3]) == 'r'
808 && tolower (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
810 *mode = XMTRX_M4;
811 return 5;
814 return 0;
817 static symbolS *
818 dot ()
820 const char *fake;
822 /* JF: '.' is pseudo symbol with value of current location
823 in current segment. */
824 fake = FAKE_LABEL_NAME;
825 return symbol_new (fake,
826 now_seg,
827 (valueT) frag_now_fix (),
828 frag_now);
831 static char *
832 parse_exp (s, op)
833 char *s;
834 sh_operand_info *op;
836 char *save;
837 char *new;
839 save = input_line_pointer;
840 input_line_pointer = s;
841 expression (&op->immediate);
842 if (op->immediate.X_op == O_absent)
843 as_bad (_("missing operand"));
844 new = input_line_pointer;
845 input_line_pointer = save;
846 return new;
849 /* The many forms of operand:
851 Rn Register direct
852 @Rn Register indirect
853 @Rn+ Autoincrement
854 @-Rn Autodecrement
855 @(disp:4,Rn)
856 @(disp:8,GBR)
857 @(disp:8,PC)
859 @(R0,Rn)
860 @(R0,GBR)
862 disp:8
863 disp:12
864 #imm8
865 pr, gbr, vbr, macl, mach
868 static char *
869 parse_at (src, op)
870 char *src;
871 sh_operand_info *op;
873 int len;
874 int mode;
875 src++;
876 if (src[0] == '-')
878 /* Must be predecrement. */
879 src++;
881 len = parse_reg (src, &mode, &(op->reg));
882 if (mode != A_REG_N)
883 as_bad (_("illegal register after @-"));
885 op->type = A_DEC_N;
886 src += len;
888 else if (src[0] == '(')
890 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
891 @(r0, rn) */
892 src++;
893 len = parse_reg (src, &mode, &(op->reg));
894 if (len && mode == A_REG_N)
896 src += len;
897 if (op->reg != 0)
899 as_bad (_("must be @(r0,...)"));
901 if (src[0] == ',')
902 src++;
903 /* Now can be rn or gbr */
904 len = parse_reg (src, &mode, &(op->reg));
905 if (mode == A_GBR)
907 op->type = A_R0_GBR;
909 else if (mode == A_REG_N)
911 op->type = A_IND_R0_REG_N;
913 else
915 as_bad (_("syntax error in @(r0,...)"));
918 else
920 /* Must be an @(disp,.. thing) */
921 src = parse_exp (src, op);
922 if (src[0] == ',')
923 src++;
924 /* Now can be rn, gbr or pc */
925 len = parse_reg (src, &mode, &op->reg);
926 if (len)
928 if (mode == A_REG_N)
930 op->type = A_DISP_REG_N;
932 else if (mode == A_GBR)
934 op->type = A_DISP_GBR;
936 else if (mode == A_PC)
938 /* Turn a plain @(4,pc) into @(.+4,pc). */
939 if (op->immediate.X_op == O_constant)
941 op->immediate.X_add_symbol = dot();
942 op->immediate.X_op = O_symbol;
944 op->type = A_DISP_PC;
946 else
948 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
951 else
953 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
956 src += len;
957 if (src[0] != ')')
958 as_bad (_("expecting )"));
959 else
960 src++;
962 else
964 src += parse_reg (src, &mode, &(op->reg));
965 if (mode != A_REG_N)
967 as_bad (_("illegal register after @"));
969 if (src[0] == '+')
971 src++;
972 if ((src[0] == 'r' && src[1] == '8')
973 || (src[0] == 'i' && (src[1] == 'x' || src[1] == 's')))
975 src += 2;
976 op->type = A_PMOD_N;
978 if ((src[0] == 'r' && src[1] == '9')
979 || (src[0] == 'i' && src[1] == 'y'))
981 src += 2;
982 op->type = A_PMODY_N;
984 else
985 op->type = A_INC_N;
987 else
989 op->type = A_IND_N;
992 return src;
995 static void
996 get_operand (ptr, op)
997 char **ptr;
998 sh_operand_info *op;
1000 char *src = *ptr;
1001 int mode = -1;
1002 unsigned int len;
1004 if (src[0] == '#')
1006 src++;
1007 *ptr = parse_exp (src, op);
1008 op->type = A_IMM;
1009 return;
1012 else if (src[0] == '@')
1014 *ptr = parse_at (src, op);
1015 return;
1017 len = parse_reg (src, &mode, &(op->reg));
1018 if (len)
1020 *ptr = src + len;
1021 op->type = mode;
1022 return;
1024 else
1026 /* Not a reg, the only thing left is a displacement. */
1027 *ptr = parse_exp (src, op);
1028 op->type = A_DISP_PC;
1029 return;
1033 static char *
1034 get_operands (info, args, operand)
1035 sh_opcode_info *info;
1036 char *args;
1037 sh_operand_info *operand;
1039 char *ptr = args;
1040 if (info->arg[0])
1042 /* The pre-processor will eliminate whitespace in front of '@'
1043 after the first argument; we may be called multiple times
1044 from assemble_ppi, so don't insist on finding whitespace here. */
1045 if (*ptr == ' ')
1046 ptr++;
1048 get_operand (&ptr, operand + 0);
1049 if (info->arg[1])
1051 if (*ptr == ',')
1053 ptr++;
1055 get_operand (&ptr, operand + 1);
1056 /* ??? Hack: psha/pshl have a varying operand number depending on
1057 the type of the first operand. We handle this by having the
1058 three-operand version first and reducing the number of operands
1059 parsed to two if we see that the first operand is an immediate.
1060 This works because no insn with three operands has an immediate
1061 as first operand. */
1062 if (info->arg[2] && operand[0].type != A_IMM)
1064 if (*ptr == ',')
1066 ptr++;
1068 get_operand (&ptr, operand + 2);
1070 else
1072 operand[2].type = 0;
1075 else
1077 operand[1].type = 0;
1078 operand[2].type = 0;
1081 else
1083 operand[0].type = 0;
1084 operand[1].type = 0;
1085 operand[2].type = 0;
1087 return ptr;
1090 /* Passed a pointer to a list of opcodes which use different
1091 addressing modes, return the opcode which matches the opcodes
1092 provided. */
1094 static sh_opcode_info *
1095 get_specific (opcode, operands)
1096 sh_opcode_info *opcode;
1097 sh_operand_info *operands;
1099 sh_opcode_info *this_try = opcode;
1100 char *name = opcode->name;
1101 int n = 0;
1103 while (opcode->name)
1105 this_try = opcode++;
1106 if (this_try->name != name)
1108 /* We've looked so far down the table that we've run out of
1109 opcodes with the same name. */
1110 return 0;
1113 /* Look at both operands needed by the opcodes and provided by
1114 the user - since an arg test will often fail on the same arg
1115 again and again, we'll try and test the last failing arg the
1116 first on each opcode try. */
1117 for (n = 0; this_try->arg[n]; n++)
1119 sh_operand_info *user = operands + n;
1120 sh_arg_type arg = this_try->arg[n];
1122 switch (arg)
1124 case A_IMM:
1125 case A_BDISP12:
1126 case A_BDISP8:
1127 case A_DISP_GBR:
1128 case A_DISP_PC:
1129 case A_MACH:
1130 case A_PR:
1131 case A_MACL:
1132 if (user->type != arg)
1133 goto fail;
1134 break;
1135 case A_R0:
1136 /* opcode needs r0 */
1137 if (user->type != A_REG_N || user->reg != 0)
1138 goto fail;
1139 break;
1140 case A_R0_GBR:
1141 if (user->type != A_R0_GBR || user->reg != 0)
1142 goto fail;
1143 break;
1144 case F_FR0:
1145 if (user->type != F_REG_N || user->reg != 0)
1146 goto fail;
1147 break;
1149 case A_REG_N:
1150 case A_INC_N:
1151 case A_DEC_N:
1152 case A_IND_N:
1153 case A_IND_R0_REG_N:
1154 case A_DISP_REG_N:
1155 case F_REG_N:
1156 case D_REG_N:
1157 case X_REG_N:
1158 case V_REG_N:
1159 case FPUL_N:
1160 case FPSCR_N:
1161 case A_PMOD_N:
1162 case A_PMODY_N:
1163 case DSP_REG_N:
1164 /* Opcode needs rn */
1165 if (user->type != arg)
1166 goto fail;
1167 reg_n = user->reg;
1168 break;
1169 case DX_REG_N:
1170 if (user->type != D_REG_N && user->type != X_REG_N)
1171 goto fail;
1172 reg_n = user->reg;
1173 break;
1174 case A_GBR:
1175 case A_SR:
1176 case A_VBR:
1177 case A_DSR:
1178 case A_MOD:
1179 case A_RE:
1180 case A_RS:
1181 case A_SSR:
1182 case A_SPC:
1183 case A_SGR:
1184 case A_DBR:
1185 if (user->type != arg)
1186 goto fail;
1187 break;
1189 case A_REG_B:
1190 if (user->type != arg)
1191 goto fail;
1192 reg_b = user->reg;
1193 break;
1195 case A_REG_M:
1196 case A_INC_M:
1197 case A_DEC_M:
1198 case A_IND_M:
1199 case A_IND_R0_REG_M:
1200 case A_DISP_REG_M:
1201 case DSP_REG_M:
1202 /* Opcode needs rn */
1203 if (user->type != arg - A_REG_M + A_REG_N)
1204 goto fail;
1205 reg_m = user->reg;
1206 break;
1208 case DSP_REG_X:
1209 if (user->type != DSP_REG_N)
1210 goto fail;
1211 switch (user->reg)
1213 case A_X0_NUM:
1214 reg_x = 0;
1215 break;
1216 case A_X1_NUM:
1217 reg_x = 1;
1218 break;
1219 case A_A0_NUM:
1220 reg_x = 2;
1221 break;
1222 case A_A1_NUM:
1223 reg_x = 3;
1224 break;
1225 default:
1226 goto fail;
1228 break;
1230 case DSP_REG_Y:
1231 if (user->type != DSP_REG_N)
1232 goto fail;
1233 switch (user->reg)
1235 case A_Y0_NUM:
1236 reg_y = 0;
1237 break;
1238 case A_Y1_NUM:
1239 reg_y = 1;
1240 break;
1241 case A_M0_NUM:
1242 reg_y = 2;
1243 break;
1244 case A_M1_NUM:
1245 reg_y = 3;
1246 break;
1247 default:
1248 goto fail;
1250 break;
1252 case DSP_REG_E:
1253 if (user->type != DSP_REG_N)
1254 goto fail;
1255 switch (user->reg)
1257 case A_X0_NUM:
1258 reg_efg = 0 << 10;
1259 break;
1260 case A_X1_NUM:
1261 reg_efg = 1 << 10;
1262 break;
1263 case A_Y0_NUM:
1264 reg_efg = 2 << 10;
1265 break;
1266 case A_A1_NUM:
1267 reg_efg = 3 << 10;
1268 break;
1269 default:
1270 goto fail;
1272 break;
1274 case DSP_REG_F:
1275 if (user->type != DSP_REG_N)
1276 goto fail;
1277 switch (user->reg)
1279 case A_Y0_NUM:
1280 reg_efg |= 0 << 8;
1281 break;
1282 case A_Y1_NUM:
1283 reg_efg |= 1 << 8;
1284 break;
1285 case A_X0_NUM:
1286 reg_efg |= 2 << 8;
1287 break;
1288 case A_A1_NUM:
1289 reg_efg |= 3 << 8;
1290 break;
1291 default:
1292 goto fail;
1294 break;
1296 case DSP_REG_G:
1297 if (user->type != DSP_REG_N)
1298 goto fail;
1299 switch (user->reg)
1301 case A_M0_NUM:
1302 reg_efg |= 0 << 2;
1303 break;
1304 case A_M1_NUM:
1305 reg_efg |= 1 << 2;
1306 break;
1307 case A_A0_NUM:
1308 reg_efg |= 2 << 2;
1309 break;
1310 case A_A1_NUM:
1311 reg_efg |= 3 << 2;
1312 break;
1313 default:
1314 goto fail;
1316 break;
1318 case A_A0:
1319 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1320 goto fail;
1321 break;
1322 case A_X0:
1323 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1324 goto fail;
1325 break;
1326 case A_X1:
1327 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1328 goto fail;
1329 break;
1330 case A_Y0:
1331 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1332 goto fail;
1333 break;
1334 case A_Y1:
1335 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1336 goto fail;
1337 break;
1339 case F_REG_M:
1340 case D_REG_M:
1341 case X_REG_M:
1342 case V_REG_M:
1343 case FPUL_M:
1344 case FPSCR_M:
1345 /* Opcode needs rn */
1346 if (user->type != arg - F_REG_M + F_REG_N)
1347 goto fail;
1348 reg_m = user->reg;
1349 break;
1350 case DX_REG_M:
1351 if (user->type != D_REG_N && user->type != X_REG_N)
1352 goto fail;
1353 reg_m = user->reg;
1354 break;
1355 case XMTRX_M4:
1356 if (user->type != XMTRX_M4)
1357 goto fail;
1358 reg_m = 4;
1359 break;
1361 default:
1362 printf (_("unhandled %d\n"), arg);
1363 goto fail;
1366 if ( !(valid_arch & this_try->arch))
1367 goto fail;
1368 valid_arch &= this_try->arch;
1369 return this_try;
1370 fail:
1374 return 0;
1378 check (operand, low, high)
1379 expressionS *operand;
1380 int low;
1381 int high;
1383 if (operand->X_op != O_constant
1384 || operand->X_add_number < low
1385 || operand->X_add_number > high)
1387 as_bad (_("operand must be absolute in range %d..%d"), low, high);
1389 return operand->X_add_number;
1392 static void
1393 insert (where, how, pcrel, op)
1394 char *where;
1395 int how;
1396 int pcrel;
1397 sh_operand_info *op;
1399 fix_new_exp (frag_now,
1400 where - frag_now->fr_literal,
1402 &op->immediate,
1403 pcrel,
1404 how);
1407 static void
1408 build_relax (opcode, op)
1409 sh_opcode_info *opcode;
1410 sh_operand_info *op;
1412 int high_byte = target_big_endian ? 0 : 1;
1413 char *p;
1415 if (opcode->arg[0] == A_BDISP8)
1417 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
1418 p = frag_var (rs_machine_dependent,
1419 md_relax_table[C (what, COND32)].rlx_length,
1420 md_relax_table[C (what, COND8)].rlx_length,
1421 C (what, 0),
1422 op->immediate.X_add_symbol,
1423 op->immediate.X_add_number,
1425 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
1427 else if (opcode->arg[0] == A_BDISP12)
1429 p = frag_var (rs_machine_dependent,
1430 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
1431 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
1432 C (UNCOND_JUMP, 0),
1433 op->immediate.X_add_symbol,
1434 op->immediate.X_add_number,
1436 p[high_byte] = (opcode->nibbles[0] << 4);
1441 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
1443 static char *
1444 insert_loop_bounds (output, operand)
1445 char *output;
1446 sh_operand_info *operand;
1448 char *name;
1449 symbolS *end_sym;
1451 /* Since the low byte of the opcode will be overwritten by the reloc, we
1452 can just stash the high byte into both bytes and ignore endianness. */
1453 output[0] = 0x8c;
1454 output[1] = 0x8c;
1455 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1456 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1458 if (sh_relax)
1460 static int count = 0;
1462 /* If the last loop insn is a two-byte-insn, it is in danger of being
1463 swapped with the insn after it. To prevent this, create a new
1464 symbol - complete with SH_LABEL reloc - after the last loop insn.
1465 If the last loop insn is four bytes long, the symbol will be
1466 right in the middle, but four byte insns are not swapped anyways. */
1467 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
1468 Hence a 9 digit number should be enough to count all REPEATs. */
1469 name = alloca (11);
1470 sprintf (name, "_R%x", count++ & 0x3fffffff);
1471 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
1472 /* Make this a local symbol. */
1473 #ifdef OBJ_COFF
1474 SF_SET_LOCAL (end_sym);
1475 #endif /* OBJ_COFF */
1476 symbol_table_insert (end_sym);
1477 end_sym->sy_value = operand[1].immediate;
1478 end_sym->sy_value.X_add_number += 2;
1479 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
1482 output = frag_more (2);
1483 output[0] = 0x8e;
1484 output[1] = 0x8e;
1485 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1486 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1488 return frag_more (2);
1491 /* Now we know what sort of opcodes it is, let's build the bytes. */
1493 static unsigned int
1494 build_Mytes (opcode, operand)
1495 sh_opcode_info *opcode;
1496 sh_operand_info *operand;
1499 int index;
1500 char nbuf[4];
1501 char *output = frag_more (2);
1502 unsigned int size = 2;
1503 int low_byte = target_big_endian ? 1 : 0;
1504 nbuf[0] = 0;
1505 nbuf[1] = 0;
1506 nbuf[2] = 0;
1507 nbuf[3] = 0;
1509 for (index = 0; index < 4; index++)
1511 sh_nibble_type i = opcode->nibbles[index];
1512 if (i < 16)
1514 nbuf[index] = i;
1516 else
1518 switch (i)
1520 case REG_N:
1521 nbuf[index] = reg_n;
1522 break;
1523 case REG_M:
1524 nbuf[index] = reg_m;
1525 break;
1526 case SDT_REG_N:
1527 if (reg_n < 2 || reg_n > 5)
1528 as_bad (_("Invalid register: 'r%d'"), reg_n);
1529 nbuf[index] = (reg_n & 3) | 4;
1530 break;
1531 case REG_NM:
1532 nbuf[index] = reg_n | (reg_m >> 2);
1533 break;
1534 case REG_B:
1535 nbuf[index] = reg_b | 0x08;
1536 break;
1537 case IMM0_4BY4:
1538 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
1539 break;
1540 case IMM0_4BY2:
1541 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
1542 break;
1543 case IMM0_4:
1544 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
1545 break;
1546 case IMM1_4BY4:
1547 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
1548 break;
1549 case IMM1_4BY2:
1550 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
1551 break;
1552 case IMM1_4:
1553 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
1554 break;
1555 case IMM0_8BY4:
1556 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
1557 break;
1558 case IMM0_8BY2:
1559 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
1560 break;
1561 case IMM0_8:
1562 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
1563 break;
1564 case IMM1_8BY4:
1565 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
1566 break;
1567 case IMM1_8BY2:
1568 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
1569 break;
1570 case IMM1_8:
1571 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
1572 break;
1573 case PCRELIMM_8BY4:
1574 insert (output, BFD_RELOC_SH_PCRELIMM8BY4, 1, operand);
1575 break;
1576 case PCRELIMM_8BY2:
1577 insert (output, BFD_RELOC_SH_PCRELIMM8BY2, 1, operand);
1578 break;
1579 case REPEAT:
1580 output = insert_loop_bounds (output, operand);
1581 nbuf[index] = opcode->nibbles[3];
1582 operand += 2;
1583 break;
1584 default:
1585 printf (_("failed for %d\n"), i);
1589 if (!target_big_endian)
1591 output[1] = (nbuf[0] << 4) | (nbuf[1]);
1592 output[0] = (nbuf[2] << 4) | (nbuf[3]);
1594 else
1596 output[0] = (nbuf[0] << 4) | (nbuf[1]);
1597 output[1] = (nbuf[2] << 4) | (nbuf[3]);
1599 return size;
1602 /* Find an opcode at the start of *STR_P in the hash table, and set
1603 *STR_P to the first character after the last one read. */
1605 static sh_opcode_info *
1606 find_cooked_opcode (str_p)
1607 char **str_p;
1609 char *str = *str_p;
1610 unsigned char *op_start;
1611 unsigned char *op_end;
1612 char name[20];
1613 int nlen = 0;
1615 /* Drop leading whitespace. */
1616 while (*str == ' ')
1617 str++;
1619 /* Find the op code end.
1620 The pre-processor will eliminate whitespace in front of
1621 any '@' after the first argument; we may be called from
1622 assemble_ppi, so the opcode might be terminated by an '@'. */
1623 for (op_start = op_end = (unsigned char *) (str);
1624 *op_end
1625 && nlen < 20
1626 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
1627 op_end++)
1629 unsigned char c = op_start[nlen];
1631 /* The machine independent code will convert CMP/EQ into cmp/EQ
1632 because it thinks the '/' is the end of the symbol. Moreover,
1633 all but the first sub-insn is a parallel processing insn won't
1634 be capitailzed. Instead of hacking up the machine independent
1635 code, we just deal with it here. */
1636 c = isupper (c) ? tolower (c) : c;
1637 name[nlen] = c;
1638 nlen++;
1641 name[nlen] = 0;
1642 *str_p = op_end;
1644 if (nlen == 0)
1645 as_bad (_("can't find opcode "));
1647 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
1650 /* Assemble a parallel processing insn. */
1651 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
1653 static unsigned int
1654 assemble_ppi (op_end, opcode)
1655 char *op_end;
1656 sh_opcode_info *opcode;
1658 int movx = 0;
1659 int movy = 0;
1660 int cond = 0;
1661 int field_b = 0;
1662 char *output;
1663 int move_code;
1664 unsigned int size;
1666 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
1667 Make sure we encode a defined insn pattern. */
1668 reg_x = 0;
1669 reg_y = 0;
1671 for (;;)
1673 sh_operand_info operand[3];
1675 if (opcode->arg[0] != A_END)
1676 op_end = get_operands (opcode, op_end, operand);
1677 opcode = get_specific (opcode, operand);
1678 if (opcode == 0)
1680 /* Couldn't find an opcode which matched the operands. */
1681 char *where = frag_more (2);
1682 size = 2;
1684 where[0] = 0x0;
1685 where[1] = 0x0;
1686 as_bad (_("invalid operands for opcode"));
1687 return size;
1690 if (opcode->nibbles[0] != PPI)
1691 as_bad (_("insn can't be combined with parallel processing insn"));
1693 switch (opcode->nibbles[1])
1696 case NOPX:
1697 if (movx)
1698 as_bad (_("multiple movx specifications"));
1699 movx = DDT_BASE;
1700 break;
1701 case NOPY:
1702 if (movy)
1703 as_bad (_("multiple movy specifications"));
1704 movy = DDT_BASE;
1705 break;
1707 case MOVX:
1708 if (movx)
1709 as_bad (_("multiple movx specifications"));
1710 if (reg_n < 4 || reg_n > 5)
1711 as_bad (_("invalid movx address register"));
1712 if (opcode->nibbles[2] & 8)
1714 if (reg_m == A_A1_NUM)
1715 movx = 1 << 7;
1716 else if (reg_m != A_A0_NUM)
1717 as_bad (_("invalid movx dsp register"));
1719 else
1721 if (reg_x > 1)
1722 as_bad (_("invalid movx dsp register"));
1723 movx = reg_x << 7;
1725 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
1726 break;
1728 case MOVY:
1729 if (movy)
1730 as_bad (_("multiple movy specifications"));
1731 if (opcode->nibbles[2] & 8)
1733 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
1734 so add 8 more. */
1735 movy = 8;
1736 if (reg_m == A_A1_NUM)
1737 movy += 1 << 6;
1738 else if (reg_m != A_A0_NUM)
1739 as_bad (_("invalid movy dsp register"));
1741 else
1743 if (reg_y > 1)
1744 as_bad (_("invalid movy dsp register"));
1745 movy = reg_y << 6;
1747 if (reg_n < 6 || reg_n > 7)
1748 as_bad (_("invalid movy address register"));
1749 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
1750 break;
1752 case PSH:
1753 if (operand[0].immediate.X_op != O_constant)
1754 as_bad (_("dsp immediate shift value not constant"));
1755 field_b = ((opcode->nibbles[2] << 12)
1756 | (operand[0].immediate.X_add_number & 127) << 4
1757 | reg_n);
1758 break;
1759 case PPI3:
1760 if (field_b)
1761 as_bad (_("multiple parallel processing specifications"));
1762 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
1763 + (reg_x << 6) + (reg_y << 4) + reg_n);
1764 break;
1765 case PDC:
1766 if (cond)
1767 as_bad (_("multiple condition specifications"));
1768 cond = opcode->nibbles[2] << 8;
1769 if (*op_end)
1770 goto skip_cond_check;
1771 break;
1772 case PPIC:
1773 if (field_b)
1774 as_bad (_("multiple parallel processing specifications"));
1775 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
1776 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
1777 cond = 0;
1778 break;
1779 case PMUL:
1780 if (field_b)
1782 if ((field_b & 0xef00) != 0xa100)
1783 as_bad (_("insn cannot be combined with pmuls"));
1784 field_b -= 0x8100;
1785 switch (field_b & 0xf)
1787 case A_X0_NUM:
1788 field_b += 0 - A_X0_NUM;
1789 break;
1790 case A_Y0_NUM:
1791 field_b += 1 - A_Y0_NUM;
1792 break;
1793 case A_A0_NUM:
1794 field_b += 2 - A_A0_NUM;
1795 break;
1796 case A_A1_NUM:
1797 field_b += 3 - A_A1_NUM;
1798 break;
1799 default:
1800 as_bad (_("bad padd / psub pmuls output operand"));
1803 field_b += 0x4000 + reg_efg;
1804 break;
1805 default:
1806 abort ();
1808 if (cond)
1810 as_bad (_("condition not followed by conditionalizable insn"));
1811 cond = 0;
1813 if (! *op_end)
1814 break;
1815 skip_cond_check:
1816 opcode = find_cooked_opcode (&op_end);
1817 if (opcode == NULL)
1819 (as_bad
1820 (_("unrecognized characters at end of parallel processing insn")));
1821 break;
1825 move_code = movx | movy;
1826 if (field_b)
1828 /* Parallel processing insn. */
1829 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
1831 output = frag_more (4);
1832 size = 4;
1833 if (! target_big_endian)
1835 output[3] = ppi_code >> 8;
1836 output[2] = ppi_code;
1838 else
1840 output[2] = ppi_code >> 8;
1841 output[3] = ppi_code;
1843 move_code |= 0xf800;
1845 else
1847 /* Just a double data transfer. */
1848 output = frag_more (2);
1849 size = 2;
1851 if (! target_big_endian)
1853 output[1] = move_code >> 8;
1854 output[0] = move_code;
1856 else
1858 output[0] = move_code >> 8;
1859 output[1] = move_code;
1861 return size;
1864 /* This is the guts of the machine-dependent assembler. STR points to a
1865 machine dependent instruction. This function is supposed to emit
1866 the frags/bytes it assembles to. */
1868 void
1869 md_assemble (str)
1870 char *str;
1872 unsigned char *op_end;
1873 sh_operand_info operand[3];
1874 sh_opcode_info *opcode;
1875 unsigned int size = 0;
1877 opcode = find_cooked_opcode (&str);
1878 op_end = str;
1880 if (opcode == NULL)
1882 as_bad (_("unknown opcode"));
1883 return;
1886 if (sh_relax
1887 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
1889 /* Output a CODE reloc to tell the linker that the following
1890 bytes are instructions, not data. */
1891 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1892 BFD_RELOC_SH_CODE);
1893 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
1896 if (opcode->nibbles[0] == PPI)
1898 size = assemble_ppi (op_end, opcode);
1900 else
1902 if (opcode->arg[0] == A_BDISP12
1903 || opcode->arg[0] == A_BDISP8)
1905 parse_exp (op_end + 1, &operand[0]);
1906 build_relax (opcode, &operand[0]);
1908 else
1910 if (opcode->arg[0] == A_END)
1912 /* Ignore trailing whitespace. If there is any, it has already
1913 been compressed to a single space. */
1914 if (*op_end == ' ')
1915 op_end++;
1917 else
1919 op_end = get_operands (opcode, op_end, operand);
1921 opcode = get_specific (opcode, operand);
1923 if (opcode == 0)
1925 /* Couldn't find an opcode which matched the operands. */
1926 char *where = frag_more (2);
1927 size = 2;
1929 where[0] = 0x0;
1930 where[1] = 0x0;
1931 as_bad (_("invalid operands for opcode"));
1933 else
1935 if (*op_end)
1936 as_bad (_("excess operands: '%s'"), op_end);
1938 size = build_Mytes (opcode, operand);
1943 dwarf2_emit_insn (size);
1946 /* This routine is called each time a label definition is seen. It
1947 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
1949 void
1950 sh_frob_label ()
1952 static fragS *last_label_frag;
1953 static int last_label_offset;
1955 if (sh_relax
1956 && seg_info (now_seg)->tc_segment_info_data.in_code)
1958 int offset;
1960 offset = frag_now_fix ();
1961 if (frag_now != last_label_frag
1962 || offset != last_label_offset)
1964 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
1965 last_label_frag = frag_now;
1966 last_label_offset = offset;
1971 /* This routine is called when the assembler is about to output some
1972 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
1974 void
1975 sh_flush_pending_output ()
1977 if (sh_relax
1978 && seg_info (now_seg)->tc_segment_info_data.in_code)
1980 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1981 BFD_RELOC_SH_DATA);
1982 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
1986 symbolS *
1987 md_undefined_symbol (name)
1988 char *name;
1990 #ifdef OBJ_ELF
1991 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE. Otherwise we
1992 have no need to default values of symbols. */
1993 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
1995 if (!GOT_symbol)
1997 if (symbol_find (name))
1998 as_bad ("GOT already in the symbol table");
2000 GOT_symbol = symbol_new (name, undefined_section,
2001 (valueT)0, & zero_address_frag);
2004 return GOT_symbol;
2006 #endif /* OBJ_ELF */
2008 return 0;
2011 #ifdef OBJ_COFF
2012 #ifndef BFD_ASSEMBLER
2014 void
2015 tc_crawl_symbol_chain (headers)
2016 object_headers *headers;
2018 printf (_("call to tc_crawl_symbol_chain \n"));
2021 void
2022 tc_headers_hook (headers)
2023 object_headers *headers;
2025 printf (_("call to tc_headers_hook \n"));
2028 #endif
2029 #endif
2031 /* Various routines to kill one day. */
2032 /* Equal to MAX_PRECISION in atof-ieee.c. */
2033 #define MAX_LITTLENUMS 6
2035 /* Turn a string in input_line_pointer into a floating point constant
2036 of type TYPE, and store the appropriate bytes in *LITP. The number
2037 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2038 returned, or NULL on OK. */
2040 char *
2041 md_atof (type, litP, sizeP)
2042 int type;
2043 char *litP;
2044 int *sizeP;
2046 int prec;
2047 LITTLENUM_TYPE words[4];
2048 char *t;
2049 int i;
2051 switch (type)
2053 case 'f':
2054 prec = 2;
2055 break;
2057 case 'd':
2058 prec = 4;
2059 break;
2061 default:
2062 *sizeP = 0;
2063 return _("bad call to md_atof");
2066 t = atof_ieee (input_line_pointer, type, words);
2067 if (t)
2068 input_line_pointer = t;
2070 *sizeP = prec * 2;
2072 if (! target_big_endian)
2074 for (i = prec - 1; i >= 0; i--)
2076 md_number_to_chars (litP, (valueT) words[i], 2);
2077 litP += 2;
2080 else
2082 for (i = 0; i < prec; i++)
2084 md_number_to_chars (litP, (valueT) words[i], 2);
2085 litP += 2;
2089 return NULL;
2092 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
2093 call instruction. It refers to a label of the instruction which
2094 loads the register which the call uses. We use it to generate a
2095 special reloc for the linker. */
2097 static void
2098 s_uses (ignore)
2099 int ignore ATTRIBUTE_UNUSED;
2101 expressionS ex;
2103 if (! sh_relax)
2104 as_warn (_(".uses pseudo-op seen when not relaxing"));
2106 expression (&ex);
2108 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2110 as_bad (_("bad .uses format"));
2111 ignore_rest_of_line ();
2112 return;
2115 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
2117 demand_empty_rest_of_line ();
2120 CONST char *md_shortopts = "";
2121 struct option md_longopts[] =
2123 #define OPTION_RELAX (OPTION_MD_BASE)
2124 #define OPTION_LITTLE (OPTION_MD_BASE + 1)
2125 #define OPTION_SMALL (OPTION_LITTLE + 1)
2126 #define OPTION_DSP (OPTION_SMALL + 1)
2128 {"relax", no_argument, NULL, OPTION_RELAX},
2129 {"little", no_argument, NULL, OPTION_LITTLE},
2130 {"small", no_argument, NULL, OPTION_SMALL},
2131 {"dsp", no_argument, NULL, OPTION_DSP},
2132 {NULL, no_argument, NULL, 0}
2134 size_t md_longopts_size = sizeof (md_longopts);
2137 md_parse_option (c, arg)
2138 int c;
2139 char *arg ATTRIBUTE_UNUSED;
2141 switch (c)
2143 case OPTION_RELAX:
2144 sh_relax = 1;
2145 break;
2147 case OPTION_LITTLE:
2148 shl = 1;
2149 target_big_endian = 0;
2150 break;
2152 case OPTION_SMALL:
2153 sh_small = 1;
2154 break;
2156 case OPTION_DSP:
2157 sh_dsp = 1;
2158 break;
2160 default:
2161 return 0;
2164 return 1;
2167 void
2168 md_show_usage (stream)
2169 FILE *stream;
2171 fprintf (stream, _("\
2172 SH options:\n\
2173 -little generate little endian code\n\
2174 -relax alter jump instructions for long displacements\n\
2175 -small align sections to 4 byte boundaries, not 16\n\
2176 -dsp enable sh-dsp insns, and disable sh3e / sh4 insns.\n"));
2179 void
2180 tc_Nout_fix_to_chars ()
2182 printf (_("call to tc_Nout_fix_to_chars \n"));
2183 abort ();
2186 /* This struct is used to pass arguments to sh_count_relocs through
2187 bfd_map_over_sections. */
2189 struct sh_count_relocs
2191 /* Symbol we are looking for. */
2192 symbolS *sym;
2193 /* Count of relocs found. */
2194 int count;
2197 /* Count the number of fixups in a section which refer to a particular
2198 symbol. When using BFD_ASSEMBLER, this is called via
2199 bfd_map_over_sections. */
2201 static void
2202 sh_count_relocs (abfd, sec, data)
2203 bfd *abfd ATTRIBUTE_UNUSED;
2204 segT sec;
2205 PTR data;
2207 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
2208 segment_info_type *seginfo;
2209 symbolS *sym;
2210 fixS *fix;
2212 seginfo = seg_info (sec);
2213 if (seginfo == NULL)
2214 return;
2216 sym = info->sym;
2217 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2219 if (fix->fx_addsy == sym)
2221 ++info->count;
2222 fix->fx_tcbit = 1;
2227 /* Handle the count relocs for a particular section. When using
2228 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
2230 static void
2231 sh_frob_section (abfd, sec, ignore)
2232 bfd *abfd ATTRIBUTE_UNUSED;
2233 segT sec;
2234 PTR ignore ATTRIBUTE_UNUSED;
2236 segment_info_type *seginfo;
2237 fixS *fix;
2239 seginfo = seg_info (sec);
2240 if (seginfo == NULL)
2241 return;
2243 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2245 symbolS *sym;
2246 bfd_vma val;
2247 fixS *fscan;
2248 struct sh_count_relocs info;
2250 if (fix->fx_r_type != BFD_RELOC_SH_USES)
2251 continue;
2253 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2254 symbol in the same section. */
2255 sym = fix->fx_addsy;
2256 if (sym == NULL
2257 || fix->fx_subsy != NULL
2258 || fix->fx_addnumber != 0
2259 || S_GET_SEGMENT (sym) != sec
2260 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2261 || S_GET_STORAGE_CLASS (sym) == C_EXT
2262 #endif
2263 || S_IS_EXTERNAL (sym))
2265 as_warn_where (fix->fx_file, fix->fx_line,
2266 _(".uses does not refer to a local symbol in the same section"));
2267 continue;
2270 /* Look through the fixups again, this time looking for one
2271 at the same location as sym. */
2272 val = S_GET_VALUE (sym);
2273 for (fscan = seginfo->fix_root;
2274 fscan != NULL;
2275 fscan = fscan->fx_next)
2276 if (val == fscan->fx_frag->fr_address + fscan->fx_where
2277 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
2278 && fscan->fx_r_type != BFD_RELOC_SH_CODE
2279 && fscan->fx_r_type != BFD_RELOC_SH_DATA
2280 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
2281 break;
2282 if (fscan == NULL)
2284 as_warn_where (fix->fx_file, fix->fx_line,
2285 _("can't find fixup pointed to by .uses"));
2286 continue;
2289 if (fscan->fx_tcbit)
2291 /* We've already done this one. */
2292 continue;
2295 /* The variable fscan should also be a fixup to a local symbol
2296 in the same section. */
2297 sym = fscan->fx_addsy;
2298 if (sym == NULL
2299 || fscan->fx_subsy != NULL
2300 || fscan->fx_addnumber != 0
2301 || S_GET_SEGMENT (sym) != sec
2302 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2303 || S_GET_STORAGE_CLASS (sym) == C_EXT
2304 #endif
2305 || S_IS_EXTERNAL (sym))
2307 as_warn_where (fix->fx_file, fix->fx_line,
2308 _(".uses target does not refer to a local symbol in the same section"));
2309 continue;
2312 /* Now we look through all the fixups of all the sections,
2313 counting the number of times we find a reference to sym. */
2314 info.sym = sym;
2315 info.count = 0;
2316 #ifdef BFD_ASSEMBLER
2317 bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
2318 #else
2320 int iscan;
2322 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
2323 sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
2325 #endif
2327 if (info.count < 1)
2328 abort ();
2330 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
2331 We have already adjusted the value of sym to include the
2332 fragment address, so we undo that adjustment here. */
2333 subseg_change (sec, 0);
2334 fix_new (symbol_get_frag (sym),
2335 S_GET_VALUE (sym) - symbol_get_frag (sym)->fr_address,
2336 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
2340 /* This function is called after the symbol table has been completed,
2341 but before the relocs or section contents have been written out.
2342 If we have seen any .uses pseudo-ops, they point to an instruction
2343 which loads a register with the address of a function. We look
2344 through the fixups to find where the function address is being
2345 loaded from. We then generate a COUNT reloc giving the number of
2346 times that function address is referred to. The linker uses this
2347 information when doing relaxing, to decide when it can eliminate
2348 the stored function address entirely. */
2350 void
2351 sh_frob_file ()
2353 if (! sh_relax)
2354 return;
2356 #ifdef BFD_ASSEMBLER
2357 bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
2358 #else
2360 int iseg;
2362 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
2363 sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
2365 #endif
2368 /* Called after relaxing. Set the correct sizes of the fragments, and
2369 create relocs so that md_apply_fix will fill in the correct values. */
2371 void
2372 md_convert_frag (headers, seg, fragP)
2373 #ifdef BFD_ASSEMBLER
2374 bfd *headers ATTRIBUTE_UNUSED;
2375 #else
2376 object_headers *headers;
2377 #endif
2378 segT seg;
2379 fragS *fragP;
2381 int donerelax = 0;
2383 switch (fragP->fr_subtype)
2385 case C (COND_JUMP, COND8):
2386 case C (COND_JUMP_DELAY, COND8):
2387 subseg_change (seg, 0);
2388 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2389 1, BFD_RELOC_SH_PCDISP8BY2);
2390 fragP->fr_fix += 2;
2391 fragP->fr_var = 0;
2392 break;
2394 case C (UNCOND_JUMP, UNCOND12):
2395 subseg_change (seg, 0);
2396 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2397 1, BFD_RELOC_SH_PCDISP12BY2);
2398 fragP->fr_fix += 2;
2399 fragP->fr_var = 0;
2400 break;
2402 case C (UNCOND_JUMP, UNCOND32):
2403 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
2404 if (fragP->fr_symbol == NULL)
2405 as_bad_where (fragP->fr_file, fragP->fr_line,
2406 _("displacement overflows 12-bit field"));
2407 else if (S_IS_DEFINED (fragP->fr_symbol))
2408 as_bad_where (fragP->fr_file, fragP->fr_line,
2409 _("displacement to defined symbol %s overflows 12-bit field"),
2410 S_GET_NAME (fragP->fr_symbol));
2411 else
2412 as_bad_where (fragP->fr_file, fragP->fr_line,
2413 _("displacement to undefined symbol %s overflows 12-bit field"),
2414 S_GET_NAME (fragP->fr_symbol));
2415 /* Stabilize this frag, so we don't trip an assert. */
2416 fragP->fr_fix += fragP->fr_var;
2417 fragP->fr_var = 0;
2418 break;
2420 case C (COND_JUMP, COND12):
2421 case C (COND_JUMP_DELAY, COND12):
2422 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
2423 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
2424 was due to gas incorrectly relaxing an out-of-range conditional
2425 branch with delay slot. It turned:
2426 bf.s L6 (slot mov.l r12,@(44,r0))
2427 into:
2429 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
2430 30: 00 09 nop
2431 32: 10 cb mov.l r12,@(44,r0)
2432 Therefore, branches with delay slots have to be handled
2433 differently from ones without delay slots. */
2435 unsigned char *buffer =
2436 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
2437 int highbyte = target_big_endian ? 0 : 1;
2438 int lowbyte = target_big_endian ? 1 : 0;
2439 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
2441 /* Toggle the true/false bit of the bcond. */
2442 buffer[highbyte] ^= 0x2;
2444 /* If this is a dalayed branch, we may not put the the bra in the
2445 slot. So we change it to a non-delayed branch, like that:
2446 b! cond slot_label; bra disp; slot_label: slot_insn
2447 ??? We should try if swapping the conditional branch and
2448 its delay-slot insn already makes the branch reach. */
2450 /* Build a relocation to six / four bytes farther on. */
2451 subseg_change (seg, 0);
2452 fix_new (fragP, fragP->fr_fix, 2,
2453 #ifdef BFD_ASSEMBLER
2454 section_symbol (seg),
2455 #else
2456 seg_info (seg)->dot,
2457 #endif
2458 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
2459 1, BFD_RELOC_SH_PCDISP8BY2);
2461 /* Set up a jump instruction. */
2462 buffer[highbyte + 2] = 0xa0;
2463 buffer[lowbyte + 2] = 0;
2464 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
2465 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
2467 if (delay)
2469 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
2470 fragP->fr_fix += 4;
2472 else
2474 /* Fill in a NOP instruction. */
2475 buffer[highbyte + 4] = 0x0;
2476 buffer[lowbyte + 4] = 0x9;
2478 fragP->fr_fix += 6;
2480 fragP->fr_var = 0;
2481 donerelax = 1;
2483 break;
2485 case C (COND_JUMP, COND32):
2486 case C (COND_JUMP_DELAY, COND32):
2487 case C (COND_JUMP, UNDEF_WORD_DISP):
2488 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
2489 if (fragP->fr_symbol == NULL)
2490 as_bad_where (fragP->fr_file, fragP->fr_line,
2491 _("displacement overflows 8-bit field"));
2492 else if (S_IS_DEFINED (fragP->fr_symbol))
2493 as_bad_where (fragP->fr_file, fragP->fr_line,
2494 _("displacement to defined symbol %s overflows 8-bit field"),
2495 S_GET_NAME (fragP->fr_symbol));
2496 else
2497 as_bad_where (fragP->fr_file, fragP->fr_line,
2498 _("displacement to undefined symbol %s overflows 8-bit field "),
2499 S_GET_NAME (fragP->fr_symbol));
2500 /* Stabilize this frag, so we don't trip an assert. */
2501 fragP->fr_fix += fragP->fr_var;
2502 fragP->fr_var = 0;
2503 break;
2505 default:
2506 abort ();
2509 if (donerelax && !sh_relax)
2510 as_warn_where (fragP->fr_file, fragP->fr_line,
2511 _("overflow in branch to %s; converted into longer instruction sequence"),
2512 (fragP->fr_symbol != NULL
2513 ? S_GET_NAME (fragP->fr_symbol)
2514 : ""));
2517 valueT
2518 md_section_align (seg, size)
2519 segT seg ATTRIBUTE_UNUSED;
2520 valueT size;
2522 #ifdef BFD_ASSEMBLER
2523 #ifdef OBJ_ELF
2524 return size;
2525 #else /* ! OBJ_ELF */
2526 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
2527 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
2528 #endif /* ! OBJ_ELF */
2529 #else /* ! BFD_ASSEMBLER */
2530 return ((size + (1 << section_alignment[(int) seg]) - 1)
2531 & (-1 << section_alignment[(int) seg]));
2532 #endif /* ! BFD_ASSEMBLER */
2535 /* This static variable is set by s_uacons to tell sh_cons_align that
2536 the expession does not need to be aligned. */
2538 static int sh_no_align_cons = 0;
2540 /* This handles the unaligned space allocation pseudo-ops, such as
2541 .uaword. .uaword is just like .word, but the value does not need
2542 to be aligned. */
2544 static void
2545 s_uacons (bytes)
2546 int bytes;
2548 /* Tell sh_cons_align not to align this value. */
2549 sh_no_align_cons = 1;
2550 cons (bytes);
2553 /* If a .word, et. al., pseud-op is seen, warn if the value is not
2554 aligned correctly. Note that this can cause warnings to be issued
2555 when assembling initialized structured which were declared with the
2556 packed attribute. FIXME: Perhaps we should require an option to
2557 enable this warning? */
2559 void
2560 sh_cons_align (nbytes)
2561 int nbytes;
2563 int nalign;
2564 char *p;
2566 if (sh_no_align_cons)
2568 /* This is an unaligned pseudo-op. */
2569 sh_no_align_cons = 0;
2570 return;
2573 nalign = 0;
2574 while ((nbytes & 1) == 0)
2576 ++nalign;
2577 nbytes >>= 1;
2580 if (nalign == 0)
2581 return;
2583 if (now_seg == absolute_section)
2585 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
2586 as_warn (_("misaligned data"));
2587 return;
2590 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
2591 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
2593 record_alignment (now_seg, nalign);
2596 /* When relaxing, we need to output a reloc for any .align directive
2597 that requests alignment to a four byte boundary or larger. This is
2598 also where we check for misaligned data. */
2600 void
2601 sh_handle_align (frag)
2602 fragS *frag;
2604 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
2606 if (frag->fr_type == rs_align_code)
2608 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
2609 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
2611 char *p = frag->fr_literal + frag->fr_fix;
2613 if (bytes & 1)
2615 *p++ = 0;
2616 bytes--;
2617 frag->fr_fix += 1;
2620 if (target_big_endian)
2622 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
2623 frag->fr_var = sizeof big_nop_pattern;
2625 else
2627 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
2628 frag->fr_var = sizeof little_nop_pattern;
2631 else if (frag->fr_type == rs_align_test)
2633 if (bytes != 0)
2634 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
2637 if (sh_relax
2638 && (frag->fr_type == rs_align
2639 || frag->fr_type == rs_align_code)
2640 && frag->fr_address + frag->fr_fix > 0
2641 && frag->fr_offset > 1
2642 && now_seg != bss_section)
2643 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
2644 BFD_RELOC_SH_ALIGN);
2647 /* This macro decides whether a particular reloc is an entry in a
2648 switch table. It is used when relaxing, because the linker needs
2649 to know about all such entries so that it can adjust them if
2650 necessary. */
2652 #ifdef BFD_ASSEMBLER
2653 #define SWITCH_TABLE_CONS(fix) (0)
2654 #else
2655 #define SWITCH_TABLE_CONS(fix) \
2656 ((fix)->fx_r_type == 0 \
2657 && ((fix)->fx_size == 2 \
2658 || (fix)->fx_size == 1 \
2659 || (fix)->fx_size == 4))
2660 #endif
2662 #define SWITCH_TABLE(fix) \
2663 ((fix)->fx_addsy != NULL \
2664 && (fix)->fx_subsy != NULL \
2665 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
2666 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
2667 && ((fix)->fx_r_type == BFD_RELOC_32 \
2668 || (fix)->fx_r_type == BFD_RELOC_16 \
2669 || (fix)->fx_r_type == BFD_RELOC_8 \
2670 || SWITCH_TABLE_CONS (fix)))
2672 /* See whether we need to force a relocation into the output file.
2673 This is used to force out switch and PC relative relocations when
2674 relaxing. */
2677 sh_force_relocation (fix)
2678 fixS *fix;
2681 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2682 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2683 || fix->fx_r_type == BFD_RELOC_SH_LOOP_START
2684 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END)
2685 return 1;
2687 if (! sh_relax)
2688 return 0;
2690 return (fix->fx_pcrel
2691 || SWITCH_TABLE (fix)
2692 || fix->fx_r_type == BFD_RELOC_SH_COUNT
2693 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
2694 || fix->fx_r_type == BFD_RELOC_SH_CODE
2695 || fix->fx_r_type == BFD_RELOC_SH_DATA
2696 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
2699 #ifdef OBJ_ELF
2700 boolean
2701 sh_fix_adjustable (fixP)
2702 fixS *fixP;
2705 if (fixP->fx_addsy == NULL)
2706 return 1;
2708 if (fixP->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
2709 || fixP->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
2710 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
2711 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
2712 || fixP->fx_r_type == BFD_RELOC_8_PCREL
2713 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH16
2714 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH32)
2715 return 1;
2717 if (! TC_RELOC_RTSYM_LOC_FIXUP (fixP)
2718 || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
2719 || fixP->fx_r_type == BFD_RELOC_RVA)
2720 return 0;
2722 /* We need the symbol name for the VTABLE entries */
2723 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2724 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2725 return 0;
2727 return 1;
2730 void
2731 sh_elf_final_processing ()
2733 int val;
2735 /* Set file-specific flags to indicate if this code needs
2736 a processor with the sh-dsp / sh3e ISA to execute. */
2737 if (valid_arch & arch_sh1)
2738 val = EF_SH1;
2739 else if (valid_arch & arch_sh2)
2740 val = EF_SH2;
2741 else if (valid_arch & arch_sh_dsp)
2742 val = EF_SH_DSP;
2743 else if (valid_arch & arch_sh3)
2744 val = EF_SH3;
2745 else if (valid_arch & arch_sh3_dsp)
2746 val = EF_SH_DSP;
2747 else if (valid_arch & arch_sh3e)
2748 val = EF_SH3E;
2749 else if (valid_arch & arch_sh4)
2750 val = EF_SH4;
2751 else
2752 abort ();
2754 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
2755 elf_elfheader (stdoutput)->e_flags |= val;
2757 #endif
2759 /* Apply a fixup to the object file. */
2761 #ifdef BFD_ASSEMBLER
2763 md_apply_fix (fixP, valp)
2764 fixS *fixP;
2765 valueT *valp;
2766 #else
2767 void
2768 md_apply_fix (fixP, val)
2769 fixS *fixP;
2770 long val;
2771 #endif
2773 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2774 int lowbyte = target_big_endian ? 1 : 0;
2775 int highbyte = target_big_endian ? 0 : 1;
2776 #ifdef BFD_ASSEMBLER
2777 long val = *valp;
2778 #endif
2779 long max, min;
2780 int shift;
2782 #ifdef BFD_ASSEMBLER
2783 /* A difference between two symbols, the second of which is in the
2784 current section, is transformed in a PC-relative relocation to
2785 the other symbol. We have to adjust the relocation type here. */
2786 if (fixP->fx_pcrel)
2788 switch (fixP->fx_r_type)
2790 default:
2791 break;
2793 case BFD_RELOC_32:
2794 fixP->fx_r_type = BFD_RELOC_32_PCREL;
2795 break;
2797 /* Currently, we only support 32-bit PCREL relocations.
2798 We'd need a new reloc type to handle 16_PCREL, and
2799 8_PCREL is already taken for R_SH_SWITCH8, which
2800 apparently does something completely different than what
2801 we need. FIXME. */
2802 case BFD_RELOC_16:
2803 bfd_set_error (bfd_error_bad_value);
2804 return false;
2806 case BFD_RELOC_8:
2807 bfd_set_error (bfd_error_bad_value);
2808 return false;
2812 /* The function adjust_reloc_syms won't convert a reloc against a weak
2813 symbol into a reloc against a section, but bfd_install_relocation
2814 will screw up if the symbol is defined, so we have to adjust val here
2815 to avoid the screw up later.
2817 For ordinary relocs, this does not happen for ELF, since for ELF,
2818 bfd_install_relocation uses the "special function" field of the
2819 howto, and does not execute the code that needs to be undone, as long
2820 as the special function does not return bfd_reloc_continue.
2821 It can happen for GOT- and PLT-type relocs the way they are
2822 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
2823 doesn't matter here since those relocs don't use VAL; see below. */
2824 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2825 && fixP->fx_addsy != NULL
2826 && S_IS_WEAK (fixP->fx_addsy))
2827 val -= S_GET_VALUE (fixP->fx_addsy);
2828 #endif
2830 #ifndef BFD_ASSEMBLER
2831 if (fixP->fx_r_type == 0)
2833 if (fixP->fx_size == 2)
2834 fixP->fx_r_type = BFD_RELOC_16;
2835 else if (fixP->fx_size == 4)
2836 fixP->fx_r_type = BFD_RELOC_32;
2837 else if (fixP->fx_size == 1)
2838 fixP->fx_r_type = BFD_RELOC_8;
2839 else
2840 abort ();
2842 #endif
2844 max = min = 0;
2845 shift = 0;
2846 switch (fixP->fx_r_type)
2848 case BFD_RELOC_SH_IMM4:
2849 max = 0xf;
2850 *buf = (*buf & 0xf0) | (val & 0xf);
2851 break;
2853 case BFD_RELOC_SH_IMM4BY2:
2854 max = 0xf;
2855 shift = 1;
2856 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
2857 break;
2859 case BFD_RELOC_SH_IMM4BY4:
2860 max = 0xf;
2861 shift = 2;
2862 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
2863 break;
2865 case BFD_RELOC_SH_IMM8BY2:
2866 max = 0xff;
2867 shift = 1;
2868 *buf = val >> 1;
2869 break;
2871 case BFD_RELOC_SH_IMM8BY4:
2872 max = 0xff;
2873 shift = 2;
2874 *buf = val >> 2;
2875 break;
2877 case BFD_RELOC_8:
2878 case BFD_RELOC_SH_IMM8:
2879 /* Sometimes the 8 bit value is sign extended (e.g., add) and
2880 sometimes it is not (e.g., and). We permit any 8 bit value.
2881 Note that adding further restrictions may invalidate
2882 reasonable looking assembly code, such as ``and -0x1,r0''. */
2883 max = 0xff;
2884 min = -0xff;
2885 *buf++ = val;
2886 break;
2888 case BFD_RELOC_SH_PCRELIMM8BY4:
2889 /* The lower two bits of the PC are cleared before the
2890 displacement is added in. We can assume that the destination
2891 is on a 4 byte bounday. If this instruction is also on a 4
2892 byte boundary, then we want
2893 (target - here) / 4
2894 and target - here is a multiple of 4.
2895 Otherwise, we are on a 2 byte boundary, and we want
2896 (target - (here - 2)) / 4
2897 and target - here is not a multiple of 4. Computing
2898 (target - (here - 2)) / 4 == (target - here + 2) / 4
2899 works for both cases, since in the first case the addition of
2900 2 will be removed by the division. target - here is in the
2901 variable val. */
2902 val = (val + 2) / 4;
2903 if (val & ~0xff)
2904 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2905 buf[lowbyte] = val;
2906 break;
2908 case BFD_RELOC_SH_PCRELIMM8BY2:
2909 val /= 2;
2910 if (val & ~0xff)
2911 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2912 buf[lowbyte] = val;
2913 break;
2915 case BFD_RELOC_SH_PCDISP8BY2:
2916 val /= 2;
2917 if (val < -0x80 || val > 0x7f)
2918 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2919 buf[lowbyte] = val;
2920 break;
2922 case BFD_RELOC_SH_PCDISP12BY2:
2923 val /= 2;
2924 if (val < -0x800 || val > 0x7ff)
2925 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2926 buf[lowbyte] = val & 0xff;
2927 buf[highbyte] |= (val >> 8) & 0xf;
2928 break;
2930 case BFD_RELOC_32:
2931 case BFD_RELOC_32_PCREL:
2932 md_number_to_chars (buf, val, 4);
2933 break;
2935 case BFD_RELOC_16:
2936 md_number_to_chars (buf, val, 2);
2937 break;
2939 case BFD_RELOC_SH_USES:
2940 /* Pass the value into sh_coff_reloc_mangle. */
2941 fixP->fx_addnumber = val;
2942 break;
2944 case BFD_RELOC_SH_COUNT:
2945 case BFD_RELOC_SH_ALIGN:
2946 case BFD_RELOC_SH_CODE:
2947 case BFD_RELOC_SH_DATA:
2948 case BFD_RELOC_SH_LABEL:
2949 /* Nothing to do here. */
2950 break;
2952 case BFD_RELOC_SH_LOOP_START:
2953 case BFD_RELOC_SH_LOOP_END:
2955 case BFD_RELOC_VTABLE_INHERIT:
2956 case BFD_RELOC_VTABLE_ENTRY:
2957 fixP->fx_done = 0;
2958 #ifdef BFD_ASSEMBLER
2959 return 0;
2960 #else
2961 return;
2962 #endif
2964 #ifdef OBJ_ELF
2965 case BFD_RELOC_32_PLT_PCREL:
2966 /* Make the jump instruction point to the address of the operand. At
2967 runtime we merely add the offset to the actual PLT entry. */
2968 *valp = 0xfffffffc;
2969 break;
2971 case BFD_RELOC_SH_GOTPC:
2972 /* This is tough to explain. We end up with this one if we have
2973 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
2974 The goal here is to obtain the absolute address of the GOT,
2975 and it is strongly preferable from a performance point of
2976 view to avoid using a runtime relocation for this. There are
2977 cases where you have something like:
2979 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
2981 and here no correction would be required. Internally in the
2982 assembler we treat operands of this form as not being pcrel
2983 since the '.' is explicitly mentioned, and I wonder whether
2984 it would simplify matters to do it this way. Who knows. In
2985 earlier versions of the PIC patches, the pcrel_adjust field
2986 was used to store the correction, but since the expression is
2987 not pcrel, I felt it would be confusing to do it this way. */
2988 *valp -= 1;
2989 md_number_to_chars (buf, val, 4);
2990 break;
2992 case BFD_RELOC_32_GOT_PCREL:
2993 *valp = 0; /* Fully resolved at runtime. No addend. */
2994 md_number_to_chars (buf, 0, 4);
2995 break;
2997 case BFD_RELOC_32_GOTOFF:
2998 break;
2999 #endif
3001 default:
3002 abort ();
3005 if (shift != 0)
3007 if ((val & ((1 << shift) - 1)) != 0)
3008 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
3009 if (val >= 0)
3010 val >>= shift;
3011 else
3012 val = ((val >> shift)
3013 | ((long) -1 & ~ ((long) -1 >> shift)));
3015 if (max != 0 && (val < min || val > max))
3016 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
3018 #ifdef BFD_ASSEMBLER
3019 return 0;
3020 #endif
3023 /* Called just before address relaxation. Return the length
3024 by which a fragment must grow to reach it's destination. */
3027 md_estimate_size_before_relax (fragP, segment_type)
3028 register fragS *fragP;
3029 register segT segment_type;
3031 switch (fragP->fr_subtype)
3033 case C (UNCOND_JUMP, UNDEF_DISP):
3034 /* Used to be a branch to somewhere which was unknown. */
3035 if (!fragP->fr_symbol)
3037 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3038 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
3040 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3042 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3043 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
3045 else
3047 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
3048 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
3049 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
3051 break;
3053 default:
3054 abort ();
3055 case C (COND_JUMP, UNDEF_DISP):
3056 case C (COND_JUMP_DELAY, UNDEF_DISP):
3057 /* Used to be a branch to somewhere which was unknown. */
3058 if (fragP->fr_symbol
3059 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3061 int what = GET_WHAT (fragP->fr_subtype);
3062 /* Got a symbol and it's defined in this segment, become byte
3063 sized - maybe it will fix up. */
3064 fragP->fr_subtype = C (what, COND8);
3065 fragP->fr_var = md_relax_table[C (what, COND8)].rlx_length;
3067 else if (fragP->fr_symbol)
3069 int what = GET_WHAT (fragP->fr_subtype);
3070 /* Its got a segment, but its not ours, so it will always be long. */
3071 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
3072 fragP->fr_var = md_relax_table[C (what, COND32)].rlx_length;
3073 return md_relax_table[C (what, COND32)].rlx_length;
3075 else
3077 int what = GET_WHAT (fragP->fr_subtype);
3078 /* We know the abs value. */
3079 fragP->fr_subtype = C (what, COND8);
3080 fragP->fr_var = md_relax_table[C (what, COND8)].rlx_length;
3083 break;
3085 return fragP->fr_var;
3088 /* Put number into target byte order. */
3090 void
3091 md_number_to_chars (ptr, use, nbytes)
3092 char *ptr;
3093 valueT use;
3094 int nbytes;
3096 if (! target_big_endian)
3097 number_to_chars_littleendian (ptr, use, nbytes);
3098 else
3099 number_to_chars_bigendian (ptr, use, nbytes);
3102 long
3103 md_pcrel_from (fixP)
3104 fixS *fixP;
3106 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
3109 #ifdef OBJ_COFF
3112 tc_coff_sizemachdep (frag)
3113 fragS *frag;
3115 return md_relax_table[frag->fr_subtype].rlx_length;
3118 #endif /* OBJ_COFF */
3120 #ifndef BFD_ASSEMBLER
3121 #ifdef OBJ_COFF
3123 /* Map BFD relocs to SH COFF relocs. */
3125 struct reloc_map
3127 bfd_reloc_code_real_type bfd_reloc;
3128 int sh_reloc;
3131 static const struct reloc_map coff_reloc_map[] =
3133 { BFD_RELOC_32, R_SH_IMM32 },
3134 { BFD_RELOC_16, R_SH_IMM16 },
3135 { BFD_RELOC_8, R_SH_IMM8 },
3136 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
3137 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
3138 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
3139 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
3140 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
3141 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
3142 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
3143 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
3144 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
3145 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
3146 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
3147 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
3148 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
3149 { BFD_RELOC_SH_USES, R_SH_USES },
3150 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
3151 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
3152 { BFD_RELOC_SH_CODE, R_SH_CODE },
3153 { BFD_RELOC_SH_DATA, R_SH_DATA },
3154 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
3155 { BFD_RELOC_UNUSED, 0 }
3158 /* Adjust a reloc for the SH. This is similar to the generic code,
3159 but does some minor tweaking. */
3161 void
3162 sh_coff_reloc_mangle (seg, fix, intr, paddr)
3163 segment_info_type *seg;
3164 fixS *fix;
3165 struct internal_reloc *intr;
3166 unsigned int paddr;
3168 symbolS *symbol_ptr = fix->fx_addsy;
3169 symbolS *dot;
3171 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
3173 if (! SWITCH_TABLE (fix))
3175 const struct reloc_map *rm;
3177 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
3178 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
3179 break;
3180 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
3181 as_bad_where (fix->fx_file, fix->fx_line,
3182 _("Can not represent %s relocation in this object file format"),
3183 bfd_get_reloc_code_name (fix->fx_r_type));
3184 intr->r_type = rm->sh_reloc;
3185 intr->r_offset = 0;
3187 else
3189 know (sh_relax);
3191 if (fix->fx_r_type == BFD_RELOC_16)
3192 intr->r_type = R_SH_SWITCH16;
3193 else if (fix->fx_r_type == BFD_RELOC_8)
3194 intr->r_type = R_SH_SWITCH8;
3195 else if (fix->fx_r_type == BFD_RELOC_32)
3196 intr->r_type = R_SH_SWITCH32;
3197 else
3198 abort ();
3200 /* For a switch reloc, we set r_offset to the difference between
3201 the reloc address and the subtrahend. When the linker is
3202 doing relaxing, it can use the determine the starting and
3203 ending points of the switch difference expression. */
3204 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
3207 /* PC relative relocs are always against the current section. */
3208 if (symbol_ptr == NULL)
3210 switch (fix->fx_r_type)
3212 case BFD_RELOC_SH_PCRELIMM8BY2:
3213 case BFD_RELOC_SH_PCRELIMM8BY4:
3214 case BFD_RELOC_SH_PCDISP8BY2:
3215 case BFD_RELOC_SH_PCDISP12BY2:
3216 case BFD_RELOC_SH_USES:
3217 symbol_ptr = seg->dot;
3218 break;
3219 default:
3220 break;
3224 if (fix->fx_r_type == BFD_RELOC_SH_USES)
3226 /* We can't store the offset in the object file, since this
3227 reloc does not take up any space, so we store it in r_offset.
3228 The fx_addnumber field was set in md_apply_fix. */
3229 intr->r_offset = fix->fx_addnumber;
3231 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
3233 /* We can't store the count in the object file, since this reloc
3234 does not take up any space, so we store it in r_offset. The
3235 fx_offset field was set when the fixup was created in
3236 sh_coff_frob_file. */
3237 intr->r_offset = fix->fx_offset;
3238 /* This reloc is always absolute. */
3239 symbol_ptr = NULL;
3241 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
3243 /* Store the alignment in the r_offset field. */
3244 intr->r_offset = fix->fx_offset;
3245 /* This reloc is always absolute. */
3246 symbol_ptr = NULL;
3248 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
3249 || fix->fx_r_type == BFD_RELOC_SH_DATA
3250 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
3252 /* These relocs are always absolute. */
3253 symbol_ptr = NULL;
3256 /* Turn the segment of the symbol into an offset. */
3257 if (symbol_ptr != NULL)
3259 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
3260 if (dot != NULL)
3261 intr->r_symndx = dot->sy_number;
3262 else
3263 intr->r_symndx = symbol_ptr->sy_number;
3265 else
3266 intr->r_symndx = -1;
3269 #endif /* OBJ_COFF */
3270 #endif /* ! BFD_ASSEMBLER */
3272 #ifdef BFD_ASSEMBLER
3274 /* Create a reloc. */
3276 arelent *
3277 tc_gen_reloc (section, fixp)
3278 asection *section ATTRIBUTE_UNUSED;
3279 fixS *fixp;
3281 arelent *rel;
3282 bfd_reloc_code_real_type r_type;
3284 rel = (arelent *) xmalloc (sizeof (arelent));
3285 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3286 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3287 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3289 r_type = fixp->fx_r_type;
3291 if (SWITCH_TABLE (fixp))
3293 rel->addend = rel->address - S_GET_VALUE (fixp->fx_subsy);
3294 if (r_type == BFD_RELOC_16)
3295 r_type = BFD_RELOC_SH_SWITCH16;
3296 else if (r_type == BFD_RELOC_8)
3297 r_type = BFD_RELOC_8_PCREL;
3298 else if (r_type == BFD_RELOC_32)
3299 r_type = BFD_RELOC_SH_SWITCH32;
3300 else
3301 abort ();
3303 else if (r_type == BFD_RELOC_SH_USES)
3304 rel->addend = fixp->fx_addnumber;
3305 else if (r_type == BFD_RELOC_SH_COUNT)
3306 rel->addend = fixp->fx_offset;
3307 else if (r_type == BFD_RELOC_SH_ALIGN)
3308 rel->addend = fixp->fx_offset;
3309 else if (r_type == BFD_RELOC_VTABLE_INHERIT
3310 || r_type == BFD_RELOC_VTABLE_ENTRY)
3311 rel->addend = fixp->fx_offset;
3312 else if (r_type == BFD_RELOC_SH_LOOP_START
3313 || r_type == BFD_RELOC_SH_LOOP_END)
3314 rel->addend = fixp->fx_offset;
3315 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
3317 rel->addend = 0;
3318 rel->address = rel->addend = fixp->fx_offset;
3320 else if (fixp->fx_pcrel)
3321 rel->addend = fixp->fx_addnumber;
3322 else if (r_type == BFD_RELOC_32 || r_type == BFD_RELOC_32_GOTOFF)
3323 rel->addend = fixp->fx_addnumber;
3324 else
3325 rel->addend = 0;
3327 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
3328 if (rel->howto == NULL)
3330 as_bad_where (fixp->fx_file, fixp->fx_line,
3331 _("Cannot represent relocation type %s"),
3332 bfd_get_reloc_code_name (r_type));
3333 /* Set howto to a garbage value so that we can keep going. */
3334 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3335 assert (rel->howto != NULL);
3338 return rel;
3341 #endif /* BFD_ASSEMBLER */