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