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