OSDN Git Service

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