* config/tc-sh.c (md_assemble): Call as_bad when there are excess
[external/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, 1999 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 /*
22    Written By Steve Chamberlain
23    sac@cygnus.com
24  */
25
26 #include <stdio.h>
27 #include "as.h"
28 #include "bfd.h"
29 #include "subsegs.h"
30 #define DEFINE_TABLE
31 #include "opcodes/sh-opc.h"
32 #include <ctype.h>
33 const char comment_chars[] = "!";
34 const char line_separator_chars[] = ";";
35 const char line_comment_chars[] = "!#";
36
37 static void s_uses PARAMS ((int));
38
39 static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
40 static void sh_frob_section PARAMS ((bfd *, segT, PTR));
41
42 /* This table describes all the machine specific pseudo-ops the assembler
43    has to support.  The fields are:
44    pseudo-op name without dot
45    function to call to execute this pseudo-op
46    Integer arg to pass to the function
47  */
48
49 void cons ();
50 void s_align_bytes ();
51 static void s_uacons PARAMS ((int));
52
53 int shl = 0;
54
55 static void
56 little (ignore)
57      int ignore;
58 {
59   shl = 1;
60   target_big_endian = 0;
61 }
62
63 const pseudo_typeS md_pseudo_table[] =
64 {
65   {"int", cons, 4},
66   {"word", cons, 2},
67   {"form", listing_psize, 0},
68   {"little", little, 0},
69   {"heading", listing_title, 0},
70   {"import", s_ignore, 0},
71   {"page", listing_eject, 0},
72   {"program", s_ignore, 0},
73   {"uses", s_uses, 0},
74   {"uaword", s_uacons, 2},
75   {"ualong", s_uacons, 4},
76   {0, 0, 0}
77 };
78
79 /*int md_reloc_size; */
80
81 int sh_relax;           /* set if -relax seen */
82
83 /* Whether -small was seen.  */
84
85 int sh_small;
86
87 const char EXP_CHARS[] = "eE";
88
89 /* Chars that mean this number is a floating point constant */
90 /* As in 0f12.456 */
91 /* or    0d1.2345e12 */
92 const char FLT_CHARS[] = "rRsSfFdDxXpP";
93
94 #define C(a,b) ENCODE_RELAX(a,b)
95
96 #define JREG 14                 /* Register used as a temp when relaxing */
97 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
98 #define GET_WHAT(x) ((x>>4))
99
100 /* These are the three types of relaxable instrction */
101 #define COND_JUMP 1
102 #define COND_JUMP_DELAY 2
103 #define UNCOND_JUMP  3
104 #define END 4
105
106 #define UNDEF_DISP 0
107 #define COND8  1
108 #define COND12 2
109 #define COND32 3
110 #define UNCOND12 1
111 #define UNCOND32 2
112 #define UNDEF_WORD_DISP 4
113
114 #define UNCOND12 1
115 #define UNCOND32 2
116
117 /* Branch displacements are from the address of the branch plus
118    four, thus all minimum and maximum values have 4 added to them.  */
119 #define COND8_F 258
120 #define COND8_M -252
121 #define COND8_LENGTH 2
122
123 /* There is one extra instruction before the branch, so we must add
124    two more bytes to account for it.  */
125 #define COND12_F 4100
126 #define COND12_M -4090
127 #define COND12_LENGTH 6
128
129 #define COND12_DELAY_LENGTH 4
130
131 /* ??? The minimum and maximum values are wrong, but this does not matter
132    since this relocation type is not supported yet.  */
133 #define COND32_F (1<<30)
134 #define COND32_M -(1<<30)
135 #define COND32_LENGTH 14
136
137 #define UNCOND12_F 4098
138 #define UNCOND12_M -4092
139 #define UNCOND12_LENGTH 2
140
141 /* ??? The minimum and maximum values are wrong, but this does not matter
142    since this relocation type is not supported yet.  */
143 #define UNCOND32_F (1<<30)
144 #define UNCOND32_M -(1<<30)
145 #define UNCOND32_LENGTH 14
146
147 const relax_typeS md_relax_table[C (END, 0)] = {
148   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
149   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
150
151   { 0 },
152   /* C (COND_JUMP, COND8) */
153   { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
154   /* C (COND_JUMP, COND12) */
155   { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
156   /* C (COND_JUMP, COND32) */
157   { COND32_F, COND32_M, COND32_LENGTH, 0, },
158   { 0 }, { 0 }, { 0 }, { 0 },
159   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
160
161   { 0 },
162   /* C (COND_JUMP_DELAY, COND8) */
163   { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
164   /* C (COND_JUMP_DELAY, COND12) */
165   { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
166   /* C (COND_JUMP_DELAY, COND32) */
167   { COND32_F, COND32_M, COND32_LENGTH, 0, },
168   { 0 }, { 0 }, { 0 }, { 0 },
169   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
170
171   { 0 },
172   /* C (UNCOND_JUMP, UNCOND12) */
173   { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
174   /* C (UNCOND_JUMP, UNCOND32) */
175   { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
176   { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
177   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
178 };
179
180 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics */
181
182 /*
183    This function is called once, at assembler startup time.  This should
184    set up all the tables, etc that the MD part of the assembler needs
185  */
186
187 void
188 md_begin ()
189 {
190   sh_opcode_info *opcode;
191   char *prev_name = "";
192
193   if (! shl)
194     target_big_endian = 1;
195
196   opcode_hash_control = hash_new ();
197
198   /* Insert unique names into hash table */
199   for (opcode = sh_table; opcode->name; opcode++)
200     {
201       if (strcmp (prev_name, opcode->name))
202         {
203           prev_name = opcode->name;
204           hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
205         }
206       else
207         {
208           /* Make all the opcodes with the same name point to the same
209              string */
210           opcode->name = prev_name;
211         }
212     }
213 }
214
215 static int reg_m;
216 static int reg_n;
217 static int reg_b;
218
219 static expressionS immediate;   /* absolute expression */
220
221 typedef struct
222   {
223     sh_arg_type type;
224     int reg;
225   }
226
227 sh_operand_info;
228
229 /* try and parse a reg name, returns number of chars consumed */
230 static int
231 parse_reg (src, mode, reg)
232      char *src;
233      int *mode;
234      int *reg;
235 {
236   /* We use !isalnum for the next character after the register name, to
237      make sure that we won't accidentally recognize a symbol name such as
238      'sram' as being a reference to the register 'sr'.  */
239
240   if (src[0] == 'r')
241     {
242       if (src[1] >= '0' && src[1] <= '7' && strncmp(&src[2], "_bank", 5) == 0
243           && ! isalnum ((unsigned char) src[7]))
244         {
245           *mode = A_REG_B;
246           *reg  = (src[1] - '0');
247           return 7;
248         }
249     }
250
251   if (src[0] == 'r')
252     {
253       if (src[1] == '1')
254         {
255           if (src[2] >= '0' && src[2] <= '5'
256               && ! isalnum ((unsigned char) src[3]))
257             {
258               *mode = A_REG_N;
259               *reg = 10 + src[2] - '0';
260               return 3;
261             }
262         }
263       if (src[1] >= '0' && src[1] <= '9'
264           && ! isalnum ((unsigned char) src[2]))
265         {
266           *mode = A_REG_N;
267           *reg = (src[1] - '0');
268           return 2;
269         }
270     }
271
272   if (src[0] == 's'
273       && src[1] == 's'
274       && src[2] == 'r' && ! isalnum ((unsigned char) src[3]))
275     {
276       *mode = A_SSR;
277       return 3;
278     }
279
280   if (src[0] == 's' && src[1] == 'p' && src[2] == 'c'
281       && ! isalnum ((unsigned char) src[3]))
282     {
283       *mode = A_SPC;
284       return 3;
285     }
286
287   if (src[0] == 's' && src[1] == 'g' && src[2] == 'r'
288       && ! isalnum ((unsigned char) src[3]))
289     {
290       *mode = A_SGR;
291       return 3;
292     }
293
294   if (src[0] == 'd' && src[1] == 'b' && src[2] == 'r'
295       && ! isalnum ((unsigned char) src[3]))
296     {
297       *mode = A_DBR;
298       return 3;
299     }
300
301   if (src[0] == 's' && src[1] == 'r' && ! isalnum ((unsigned char) src[2]))
302     {
303       *mode = A_SR;
304       return 2;
305     }
306
307   if (src[0] == 's' && src[1] == 'p' && ! isalnum ((unsigned char) src[2]))
308     {
309       *mode = A_REG_N;
310       *reg = 15;
311       return 2;
312     }
313
314   if (src[0] == 'p' && src[1] == 'r' && ! isalnum ((unsigned char) src[2]))
315     {
316       *mode = A_PR;
317       return 2;
318     }
319   if (src[0] == 'p' && src[1] == 'c' && ! isalnum ((unsigned char) src[2]))
320     {
321       *mode = A_DISP_PC;
322       return 2;
323     }
324   if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r'
325       && ! isalnum ((unsigned char) src[3]))
326     {
327       *mode = A_GBR;
328       return 3;
329     }
330   if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r'
331       && ! isalnum ((unsigned char) src[3]))
332     {
333       *mode = A_VBR;
334       return 3;
335     }
336
337   if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c'
338       && ! isalnum ((unsigned char) src[4]))
339     {
340       if (src[3] == 'l')
341         {
342           *mode = A_MACL;
343           return 4;
344         }
345       if (src[3] == 'h')
346         {
347           *mode = A_MACH;
348           return 4;
349         }
350     }
351   if (src[0] == 'f' && src[1] == 'r')
352     {
353       if (src[2] == '1')
354         {
355           if (src[3] >= '0' && src[3] <= '5'
356               && ! isalnum ((unsigned char) src[4]))
357             {
358               *mode = F_REG_N;
359               *reg = 10 + src[3] - '0';
360               return 4;
361             }
362         }
363       if (src[2] >= '0' && src[2] <= '9'
364           && ! isalnum ((unsigned char) src[3]))
365         {
366           *mode = F_REG_N;
367           *reg = (src[2] - '0');
368           return 3;
369         }
370     }
371   if (src[0] == 'd' && src[1] == 'r')
372     {
373       if (src[2] == '1')
374         {
375           if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
376               && ! isalnum ((unsigned char) src[4]))
377             {
378               *mode = D_REG_N;
379               *reg = 10 + src[3] - '0';
380               return 4;
381             }
382         }
383       if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
384           && ! isalnum ((unsigned char) src[3]))
385         {
386           *mode = D_REG_N;
387           *reg = (src[2] - '0');
388           return 3;
389         }
390     }
391   if (src[0] == 'x' && src[1] == 'd')
392     {
393       if (src[2] == '1')
394         {
395           if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
396               && ! isalnum ((unsigned char) src[4]))
397             {
398               *mode = X_REG_N;
399               *reg = 11 + src[3] - '0';
400               return 4;
401             }
402         }
403       if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
404           && ! isalnum ((unsigned char) src[3]))
405         {
406           *mode = X_REG_N;
407           *reg = (src[2] - '0') + 1;
408           return 3;
409         }
410     }
411   if (src[0] == 'f' && src[1] == 'v')
412     {
413       if (src[2] == '1'&& src[3] == '2' && ! isalnum ((unsigned char) src[4]))
414         {
415           *mode = V_REG_N;
416           *reg = 12;
417           return 4;
418         }
419       if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
420           && ! isalnum ((unsigned char) src[3]))
421         {
422           *mode = V_REG_N;
423           *reg = (src[2] - '0');
424           return 3;
425         }
426     }
427   if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l'
428       && ! isalnum ((unsigned char) src[4]))
429     {
430       *mode = FPUL_N;
431       return 4;
432     }
433
434   if (src[0] == 'f' && src[1] == 'p' && src[2] == 's' && src[3] == 'c'
435       && src[4] == 'r' && ! isalnum ((unsigned char) src[5]))
436     {
437       *mode = FPSCR_N;
438       return 5;
439     }
440
441   if (src[0] == 'x' && src[1] == 'm' && src[2] == 't' && src[3] == 'r'
442       && src[4] == 'x' && ! isalnum ((unsigned char) src[5]))
443     {
444       *mode = XMTRX_M4;
445       return 5;
446     }
447
448   return 0;
449 }
450
451 static symbolS *dot()
452 {
453   const char *fake;
454
455   /* JF: '.' is pseudo symbol with value of current location
456      in current segment.  */
457   fake = FAKE_LABEL_NAME;
458   return  symbol_new (fake,
459                       now_seg,
460                       (valueT) frag_now_fix (),
461                       frag_now);
462
463 }
464
465
466 static
467 char *
468 parse_exp (s)
469      char *s;
470 {
471   char *save;
472   char *new;
473
474   save = input_line_pointer;
475   input_line_pointer = s;
476   expression (&immediate);
477   if (immediate.X_op == O_absent)
478     as_bad (_("missing operand"));
479   new = input_line_pointer;
480   input_line_pointer = save;
481   return new;
482 }
483
484
485 /* The many forms of operand:
486
487    Rn                   Register direct
488    @Rn                  Register indirect
489    @Rn+                 Autoincrement
490    @-Rn                 Autodecrement
491    @(disp:4,Rn)
492    @(disp:8,GBR)
493    @(disp:8,PC)
494
495    @(R0,Rn)
496    @(R0,GBR)
497
498    disp:8
499    disp:12
500    #imm8
501    pr, gbr, vbr, macl, mach
502
503  */
504
505 static
506 char *
507 parse_at (src, op)
508      char *src;
509      sh_operand_info *op;
510 {
511   int len;
512   int mode;
513   src++;
514   if (src[0] == '-')
515     {
516       /* Must be predecrement */
517       src++;
518
519       len = parse_reg (src, &mode, &(op->reg));
520       if (mode != A_REG_N)
521         as_bad (_("illegal register after @-"));
522
523       op->type = A_DEC_N;
524       src += len;
525     }
526   else if (src[0] == '(')
527     {
528       /* Could be @(disp, rn), @(disp, gbr), @(disp, pc),  @(r0, gbr) or
529          @(r0, rn) */
530       src++;
531       len = parse_reg (src, &mode, &(op->reg));
532       if (len && mode == A_REG_N)
533         {
534           src += len;
535           if (op->reg != 0)
536             {
537               as_bad (_("must be @(r0,...)"));
538             }
539           if (src[0] == ',')
540             src++;
541           /* Now can be rn or gbr */
542           len = parse_reg (src, &mode, &(op->reg));
543           if (mode == A_GBR)
544             {
545               op->type = A_R0_GBR;
546             }
547           else if (mode == A_REG_N)
548             {
549               op->type = A_IND_R0_REG_N;
550             }
551           else
552             {
553               as_bad (_("syntax error in @(r0,...)"));
554             }
555         }
556       else
557         {
558           /* Must be an @(disp,.. thing) */
559           src = parse_exp (src);
560           if (src[0] == ',')
561             src++;
562           /* Now can be rn, gbr or pc */
563           len = parse_reg (src, &mode, &op->reg);
564           if (len)
565             {
566               if (mode == A_REG_N)
567                 {
568                   op->type = A_DISP_REG_N;
569                 }
570               else if (mode == A_GBR)
571                 {
572                   op->type = A_DISP_GBR;
573                 }
574               else if (mode == A_DISP_PC)
575                 {
576                   /* Turn a plain @(4,pc) into @(.+4,pc) */
577                   if (immediate.X_op == O_constant) { 
578                     immediate.X_add_symbol = dot();
579                     immediate.X_op = O_symbol;
580                   }
581                   op->type = A_DISP_PC;
582                 }
583               else
584                 {
585                   as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
586                 }
587             }
588           else
589             {
590               as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
591             }
592         }
593       src += len;
594       if (src[0] != ')')
595         as_bad (_("expecting )"));
596       else
597         src++;
598     }
599   else
600     {
601       src += parse_reg (src, &mode, &(op->reg));
602       if (mode != A_REG_N)
603         {
604           as_bad (_("illegal register after @"));
605         }
606       if (src[0] == '+')
607         {
608           op->type = A_INC_N;
609           src++;
610         }
611       else
612         {
613           op->type = A_IND_N;
614         }
615     }
616   return src;
617 }
618
619 static void
620 get_operand (ptr, op)
621      char **ptr;
622      sh_operand_info *op;
623 {
624   char *src = *ptr;
625   int mode = -1;
626   unsigned int len;
627
628   if (src[0] == '#')
629     {
630       src++;
631       *ptr = parse_exp (src);
632       op->type = A_IMM;
633       return;
634     }
635
636   else if (src[0] == '@')
637     {
638       *ptr = parse_at (src, op);
639       return;
640     }
641   len = parse_reg (src, &mode, &(op->reg));
642   if (len)
643     {
644       *ptr = src + len;
645       op->type = mode;
646       return;
647     }
648   else
649     {
650       /* Not a reg, the only thing left is a displacement */
651       *ptr = parse_exp (src);
652       op->type = A_DISP_PC;
653       return;
654     }
655 }
656
657 static
658 char *
659 get_operands (info, args, operand)
660      sh_opcode_info *info;
661      char *args;
662      sh_operand_info *operand;
663
664 {
665   char *ptr = args;
666   if (info->arg[0])
667     {
668       ptr++;
669
670       get_operand (&ptr, operand + 0);
671       if (info->arg[1])
672         {
673           if (*ptr == ',')
674             {
675               ptr++;
676             }
677           get_operand (&ptr, operand + 1);
678           if (info->arg[2])
679             {
680               if (*ptr == ',')
681                 {
682                   ptr++;
683                 }
684               get_operand (&ptr, operand + 2);
685             }
686           else
687             {
688               operand[2].type = 0;
689             }
690         }
691       else
692         {
693           operand[1].type = 0;
694           operand[2].type = 0;
695         }
696     }
697   else
698     {
699       operand[0].type = 0;
700       operand[1].type = 0;
701       operand[2].type = 0;
702     }
703   return ptr;
704 }
705
706 /* Passed a pointer to a list of opcodes which use different
707    addressing modes, return the opcode which matches the opcodes
708    provided
709  */
710
711 static
712 sh_opcode_info *
713 get_specific (opcode, operands)
714      sh_opcode_info *opcode;
715      sh_operand_info *operands;
716 {
717   sh_opcode_info *this_try = opcode;
718   char *name = opcode->name;
719   int n = 0;
720   while (opcode->name)
721     {
722       this_try = opcode++;
723       if (this_try->name != name)
724         {
725           /* We've looked so far down the table that we've run out of
726              opcodes with the same name */
727           return 0;
728         }
729       /* look at both operands needed by the opcodes and provided by
730          the user - since an arg test will often fail on the same arg
731          again and again, we'll try and test the last failing arg the
732          first on each opcode try */
733
734       for (n = 0; this_try->arg[n]; n++)
735         {
736           sh_operand_info *user = operands + n;
737           sh_arg_type arg = this_try->arg[n];
738           switch (arg)
739             {
740             case A_IMM:
741             case A_BDISP12:
742             case A_BDISP8:
743             case A_DISP_GBR:
744             case A_DISP_PC:
745             case A_MACH:
746             case A_PR:
747             case A_MACL:
748               if (user->type != arg)
749                 goto fail;
750               break;
751             case A_R0:
752               /* opcode needs r0 */
753               if (user->type != A_REG_N || user->reg != 0)
754                 goto fail;
755               break;
756             case A_R0_GBR:
757               if (user->type != A_R0_GBR || user->reg != 0)
758                 goto fail;
759               break;
760             case F_FR0:
761               if (user->type != F_REG_N || user->reg != 0)
762                 goto fail;
763               break;
764
765             case A_REG_N:
766             case A_INC_N:
767             case A_DEC_N:
768             case A_IND_N:
769             case A_IND_R0_REG_N:
770             case A_DISP_REG_N:
771             case F_REG_N:
772             case D_REG_N:
773             case X_REG_N:
774             case V_REG_N:
775             case FPUL_N:
776             case FPSCR_N:
777               /* Opcode needs rn */
778               if (user->type != arg)
779                 goto fail;
780               reg_n = user->reg;
781               break;
782             case FD_REG_N:
783               if (user->type != F_REG_N && user->type != D_REG_N)
784                 goto fail;
785               reg_n = user->reg;
786               break;
787             case DX_REG_N:
788               if (user->type != D_REG_N && user->type != X_REG_N)
789                 goto fail;
790               reg_n = user->reg;
791               break;
792             case A_GBR:
793             case A_SR:
794             case A_VBR:
795             case A_SSR:
796             case A_SPC:
797             case A_SGR:
798             case A_DBR:
799               if (user->type != arg)
800                 goto fail;
801               break;
802
803             case A_REG_B:
804               if (user->type != arg)
805                 goto fail;
806               reg_b = user->reg;
807               break;
808
809             case A_REG_M:
810             case A_INC_M:
811             case A_DEC_M:
812             case A_IND_M:
813             case A_IND_R0_REG_M:
814             case A_DISP_REG_M:
815               /* Opcode needs rn */
816               if (user->type != arg - A_REG_M + A_REG_N)
817                 goto fail;
818               reg_m = user->reg;
819               break;
820
821             case F_REG_M:
822             case D_REG_M:
823             case X_REG_M:
824             case V_REG_M:
825             case FPUL_M:
826             case FPSCR_M:
827               /* Opcode needs rn */
828               if (user->type != arg - F_REG_M + F_REG_N)
829                 goto fail;
830               reg_m = user->reg;
831               break;
832             case DX_REG_M:
833               if (user->type != D_REG_N && user->type != X_REG_N)
834                 goto fail;
835               reg_m = user->reg;
836               break;
837             case XMTRX_M4:
838               if (user->type != XMTRX_M4)
839                 goto fail;
840               reg_m = 4;
841               break;
842         
843             default:
844               printf (_("unhandled %d\n"), arg);
845               goto fail;
846             }
847         }
848       return this_try;
849     fail:;
850     }
851
852   return 0;
853 }
854
855 int
856 check (operand, low, high)
857      expressionS *operand;
858      int low;
859      int high;
860 {
861   if (operand->X_op != O_constant
862       || operand->X_add_number < low
863       || operand->X_add_number > high)
864     {
865       as_bad (_("operand must be absolute in range %d..%d"), low, high);
866     }
867   return operand->X_add_number;
868 }
869
870
871 static void
872 insert (where, how, pcrel)
873      char *where;
874      int how;
875      int pcrel;
876 {
877   fix_new_exp (frag_now,
878                where - frag_now->fr_literal,
879                2,
880                &immediate,
881                pcrel,
882                how);
883 }
884
885 static void
886 build_relax (opcode)
887      sh_opcode_info *opcode;
888 {
889   int high_byte = target_big_endian ? 0 : 1;
890   char *p;
891
892   if (opcode->arg[0] == A_BDISP8)
893     {
894       int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
895       p = frag_var (rs_machine_dependent,
896                     md_relax_table[C (what, COND32)].rlx_length,
897                     md_relax_table[C (what, COND8)].rlx_length,
898                     C (what, 0),
899                     immediate.X_add_symbol,
900                     immediate.X_add_number,
901                     0);
902       p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
903     }
904   else if (opcode->arg[0] == A_BDISP12)
905     {
906       p = frag_var (rs_machine_dependent,
907                     md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
908                     md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
909                     C (UNCOND_JUMP, 0),
910                     immediate.X_add_symbol,
911                     immediate.X_add_number,
912                     0);
913       p[high_byte] = (opcode->nibbles[0] << 4);
914     }
915
916 }
917
918 /* Now we know what sort of opcodes it is, lets build the bytes -
919  */
920 static void
921 build_Mytes (opcode, operand)
922      sh_opcode_info *opcode;
923      sh_operand_info *operand;
924
925 {
926   int index;
927   char nbuf[4];
928   char *output = frag_more (2);
929   int low_byte = target_big_endian ? 1 : 0;
930   nbuf[0] = 0;
931   nbuf[1] = 0;
932   nbuf[2] = 0;
933   nbuf[3] = 0;
934
935   for (index = 0; index < 4; index++)
936     {
937       sh_nibble_type i = opcode->nibbles[index];
938       if (i < 16)
939         {
940           nbuf[index] = i;
941         }
942       else
943         {
944           switch (i)
945             {
946             case REG_N:
947               nbuf[index] = reg_n;
948               break;
949             case REG_M:
950               nbuf[index] = reg_m;
951               break;
952             case REG_NM:
953               nbuf[index] = reg_n | (reg_m >> 2);
954               break;
955             case REG_B:
956               nbuf[index] = reg_b | 0x08;
957               break;
958             case DISP_4:
959               insert (output + low_byte, BFD_RELOC_SH_IMM4, 0);
960               break;
961             case IMM_4BY4:
962               insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0);
963               break;
964             case IMM_4BY2:
965               insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0);
966               break;
967             case IMM_4:
968               insert (output + low_byte, BFD_RELOC_SH_IMM4, 0);
969               break;
970             case IMM_8BY4:
971               insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0);
972               break;
973             case IMM_8BY2:
974               insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0);
975               break;
976             case IMM_8:
977               insert (output + low_byte, BFD_RELOC_SH_IMM8, 0);
978               break;
979             case PCRELIMM_8BY4:
980               insert (output, BFD_RELOC_SH_PCRELIMM8BY4, 1);
981               break;
982             case PCRELIMM_8BY2:
983               insert (output, BFD_RELOC_SH_PCRELIMM8BY2, 1);
984               break;
985             default:
986               printf (_("failed for %d\n"), i);
987             }
988         }
989     }
990   if (! target_big_endian) {
991     output[1] = (nbuf[0] << 4) | (nbuf[1]);
992     output[0] = (nbuf[2] << 4) | (nbuf[3]);
993   }
994   else {
995     output[0] = (nbuf[0] << 4) | (nbuf[1]);
996     output[1] = (nbuf[2] << 4) | (nbuf[3]);
997   }
998 }
999
1000 /* This is the guts of the machine-dependent assembler.  STR points to a
1001    machine dependent instruction.  This function is supposed to emit
1002    the frags/bytes it assembles to.
1003  */
1004
1005 void
1006 md_assemble (str)
1007      char *str;
1008 {
1009   unsigned char *op_start;
1010   unsigned char *op_end;
1011   sh_operand_info operand[3];
1012   sh_opcode_info *opcode;
1013   char name[20];
1014   int nlen = 0;
1015   /* Drop leading whitespace */
1016   while (*str == ' ')
1017     str++;
1018
1019   /* find the op code end */
1020   for (op_start = op_end = (unsigned char *) (str);
1021        *op_end
1022        && nlen < 20
1023        && !is_end_of_line[*op_end] && *op_end != ' ';
1024        op_end++)
1025     {
1026       unsigned char c = op_start[nlen];
1027
1028       /* The machine independent code will convert CMP/EQ into cmp/EQ
1029          because it thinks the '/' is the end of the symbol.  Instead of
1030          hacking up the machine independent code, we just deal with it
1031          here.  */
1032       c = isupper (c) ? tolower (c) : c;
1033       name[nlen] = c;
1034       nlen++;
1035     }
1036   name[nlen] = 0;
1037
1038   if (nlen == 0)
1039     {
1040       as_bad (_("can't find opcode "));
1041     }
1042
1043   opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
1044
1045   if (opcode == NULL)
1046     {
1047       as_bad (_("unknown opcode"));
1048       return;
1049     }
1050
1051   if (sh_relax
1052       && ! seg_info (now_seg)->tc_segment_info_data.in_code)
1053     {
1054       /* Output a CODE reloc to tell the linker that the following
1055          bytes are instructions, not data.  */
1056       fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1057                BFD_RELOC_SH_CODE);
1058       seg_info (now_seg)->tc_segment_info_data.in_code = 1;
1059     }
1060
1061   if (opcode->arg[0] == A_BDISP12
1062       || opcode->arg[0] == A_BDISP8)
1063     {
1064       parse_exp (op_end + 1);
1065       build_relax (opcode);
1066     }
1067   else
1068     {
1069       if (opcode->arg[0] == A_END)
1070         {
1071           /* Ignore trailing whitespace.  If there is any, it has already
1072              been compressed to a single space.  */
1073           if (*op_end == ' ')
1074             op_end++;
1075         }
1076       else
1077         {
1078           op_end = get_operands (opcode, op_end, operand);
1079         }
1080       opcode = get_specific (opcode, operand);
1081
1082       if (opcode == 0)
1083         {
1084           /* Couldn't find an opcode which matched the operands */
1085           char *where = frag_more (2);
1086
1087           where[0] = 0x0;
1088           where[1] = 0x0;
1089           as_bad (_("invalid operands for opcode"));
1090           return;
1091         }
1092
1093       if (*op_end)
1094         as_bad (_("excess operands: '%s'"), op_end);
1095
1096       build_Mytes (opcode, operand);
1097     }
1098
1099 }
1100
1101 /* This routine is called each time a label definition is seen.  It
1102    emits a BFD_RELOC_SH_LABEL reloc if necessary.  */
1103
1104 void
1105 sh_frob_label ()
1106 {
1107   static fragS *last_label_frag;
1108   static int last_label_offset;
1109
1110   if (sh_relax
1111       && seg_info (now_seg)->tc_segment_info_data.in_code)
1112     {
1113       int offset;
1114
1115       offset = frag_now_fix ();
1116       if (frag_now != last_label_frag
1117           || offset != last_label_offset)
1118         {       
1119           fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
1120           last_label_frag = frag_now;
1121           last_label_offset = offset;
1122         }
1123     }
1124 }
1125
1126 /* This routine is called when the assembler is about to output some
1127    data.  It emits a BFD_RELOC_SH_DATA reloc if necessary.  */
1128
1129 void
1130 sh_flush_pending_output ()
1131 {
1132   if (sh_relax
1133       && seg_info (now_seg)->tc_segment_info_data.in_code)
1134     {
1135       fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1136                BFD_RELOC_SH_DATA);
1137       seg_info (now_seg)->tc_segment_info_data.in_code = 0;
1138     }
1139 }
1140
1141 symbolS *
1142 DEFUN (md_undefined_symbol, (name),
1143        char *name)
1144 {
1145   return 0;
1146 }
1147
1148 #ifdef OBJ_COFF
1149
1150 void
1151 DEFUN (tc_crawl_symbol_chain, (headers),
1152        object_headers * headers)
1153 {
1154   printf (_("call to tc_crawl_symbol_chain \n"));
1155 }
1156
1157 void
1158 DEFUN (tc_headers_hook, (headers),
1159        object_headers * headers)
1160 {
1161   printf (_("call to tc_headers_hook \n"));
1162 }
1163
1164 #endif
1165
1166 /* Various routines to kill one day */
1167 /* Equal to MAX_PRECISION in atof-ieee.c */
1168 #define MAX_LITTLENUMS 6
1169
1170 /* Turn a string in input_line_pointer into a floating point constant of type
1171    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1172    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
1173  */
1174 char *
1175 md_atof (type, litP, sizeP)
1176      int type;
1177      char *litP;
1178      int *sizeP;
1179 {
1180   int prec;
1181   LITTLENUM_TYPE words[4];
1182   char *t;
1183   int i;
1184
1185   switch (type)
1186     {
1187     case 'f':
1188       prec = 2;
1189       break;
1190
1191     case 'd':
1192       prec = 4;
1193       break;
1194
1195     default:
1196       *sizeP = 0;
1197       return _("bad call to md_atof");
1198     }
1199
1200   t = atof_ieee (input_line_pointer, type, words);
1201   if (t)
1202     input_line_pointer = t;
1203
1204   *sizeP = prec * 2;
1205
1206   if (! target_big_endian)
1207     {
1208       for (i = prec - 1; i >= 0; i--)
1209         {
1210           md_number_to_chars (litP, (valueT) words[i], 2);
1211           litP += 2;
1212         }
1213     }
1214   else
1215     {
1216       for (i = 0; i < prec; i++)
1217         {
1218           md_number_to_chars (litP, (valueT) words[i], 2);
1219           litP += 2;
1220         }
1221     }
1222      
1223   return NULL;
1224 }
1225
1226 /* Handle the .uses pseudo-op.  This pseudo-op is used just before a
1227    call instruction.  It refers to a label of the instruction which
1228    loads the register which the call uses.  We use it to generate a
1229    special reloc for the linker.  */
1230
1231 static void
1232 s_uses (ignore)
1233      int ignore;
1234 {
1235   expressionS ex;
1236
1237   if (! sh_relax)
1238     as_warn (_(".uses pseudo-op seen when not relaxing"));
1239
1240   expression (&ex);
1241
1242   if (ex.X_op != O_symbol || ex.X_add_number != 0)
1243     {
1244       as_bad (_("bad .uses format"));
1245       ignore_rest_of_line ();
1246       return;
1247     }
1248
1249   fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
1250
1251   demand_empty_rest_of_line ();
1252 }
1253 \f
1254 CONST char *md_shortopts = "";
1255 struct option md_longopts[] = {
1256
1257 #define OPTION_RELAX  (OPTION_MD_BASE)
1258 #define OPTION_LITTLE (OPTION_MD_BASE + 1)
1259 #define OPTION_SMALL (OPTION_LITTLE + 1)
1260
1261   {"relax", no_argument, NULL, OPTION_RELAX},
1262   {"little", no_argument, NULL, OPTION_LITTLE},
1263   {"small", no_argument, NULL, OPTION_SMALL},
1264   {NULL, no_argument, NULL, 0}
1265 };
1266 size_t md_longopts_size = sizeof(md_longopts);
1267
1268 int
1269 md_parse_option (c, arg)
1270      int c;
1271      char *arg;
1272 {
1273   switch (c)
1274     {
1275     case OPTION_RELAX:
1276       sh_relax = 1;
1277       break;
1278
1279     case OPTION_LITTLE:
1280       shl = 1;
1281       target_big_endian = 0;
1282       break;
1283
1284     case OPTION_SMALL:
1285       sh_small = 1;
1286       break;
1287
1288     default:
1289       return 0;
1290     }
1291
1292   return 1;
1293 }
1294
1295 void
1296 md_show_usage (stream)
1297      FILE *stream;
1298 {
1299   fprintf(stream, _("\
1300 SH options:\n\
1301 -little                 generate little endian code\n\
1302 -relax                  alter jump instructions for long displacements\n\
1303 -small                  align sections to 4 byte boundaries, not 16\n"));
1304 }
1305 \f
1306 void
1307 tc_Nout_fix_to_chars ()
1308 {
1309   printf (_("call to tc_Nout_fix_to_chars \n"));
1310   abort ();
1311 }
1312
1313 /* This struct is used to pass arguments to sh_count_relocs through
1314    bfd_map_over_sections.  */
1315
1316 struct sh_count_relocs
1317 {
1318   /* Symbol we are looking for.  */
1319   symbolS *sym;
1320   /* Count of relocs found.  */
1321   int count;
1322 };
1323
1324 /* Count the number of fixups in a section which refer to a particular
1325    symbol.  When using BFD_ASSEMBLER, this is called via
1326    bfd_map_over_sections.  */
1327
1328 /*ARGSUSED*/
1329 static void
1330 sh_count_relocs (abfd, sec, data)
1331      bfd *abfd;
1332      segT sec;
1333      PTR data;
1334 {
1335   struct sh_count_relocs *info = (struct sh_count_relocs *) data;
1336   segment_info_type *seginfo;
1337   symbolS *sym;
1338   fixS *fix;
1339
1340   seginfo = seg_info (sec);
1341   if (seginfo == NULL)
1342     return;
1343
1344   sym = info->sym;
1345   for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
1346     {
1347       if (fix->fx_addsy == sym)
1348         {
1349           ++info->count;
1350           fix->fx_tcbit = 1;
1351         }
1352     }
1353 }
1354
1355 /* Handle the count relocs for a particular section.  When using
1356    BFD_ASSEMBLER, this is called via bfd_map_over_sections.  */
1357
1358 /*ARGSUSED*/
1359 static void
1360 sh_frob_section (abfd, sec, ignore)
1361      bfd *abfd;
1362      segT sec;
1363      PTR ignore;
1364 {
1365   segment_info_type *seginfo;
1366   fixS *fix;
1367
1368   seginfo = seg_info (sec);
1369   if (seginfo == NULL)
1370     return;
1371
1372   for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
1373     {
1374       symbolS *sym;
1375       bfd_vma val;
1376       fixS *fscan;
1377       struct sh_count_relocs info;
1378
1379       if (fix->fx_r_type != BFD_RELOC_SH_USES)
1380         continue;
1381
1382       /* The BFD_RELOC_SH_USES reloc should refer to a defined local
1383          symbol in the same section.  */
1384       sym = fix->fx_addsy;
1385       if (sym == NULL
1386           || fix->fx_subsy != NULL
1387           || fix->fx_addnumber != 0
1388           || S_GET_SEGMENT (sym) != sec
1389 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
1390           || S_GET_STORAGE_CLASS (sym) == C_EXT
1391 #endif
1392           || S_IS_EXTERNAL (sym))
1393         {
1394           as_warn_where (fix->fx_file, fix->fx_line,
1395                          _(".uses does not refer to a local symbol in the same section"));
1396           continue;
1397         }
1398
1399       /* Look through the fixups again, this time looking for one
1400          at the same location as sym.  */
1401       val = S_GET_VALUE (sym);
1402       for (fscan = seginfo->fix_root;
1403            fscan != NULL;
1404            fscan = fscan->fx_next)
1405         if (val == fscan->fx_frag->fr_address + fscan->fx_where
1406             && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
1407             && fscan->fx_r_type != BFD_RELOC_SH_CODE
1408             && fscan->fx_r_type != BFD_RELOC_SH_DATA
1409             && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
1410           break;
1411       if (fscan == NULL)
1412         {
1413           as_warn_where (fix->fx_file, fix->fx_line,
1414                          _("can't find fixup pointed to by .uses"));
1415           continue;
1416         }
1417
1418       if (fscan->fx_tcbit)
1419         {
1420           /* We've already done this one.  */
1421           continue;
1422         }
1423
1424       /* fscan should also be a fixup to a local symbol in the same
1425          section.  */
1426       sym = fscan->fx_addsy;
1427       if (sym == NULL
1428           || fscan->fx_subsy != NULL
1429           || fscan->fx_addnumber != 0
1430           || S_GET_SEGMENT (sym) != sec
1431 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
1432           || S_GET_STORAGE_CLASS (sym) == C_EXT
1433 #endif
1434           || S_IS_EXTERNAL (sym))
1435         {
1436           as_warn_where (fix->fx_file, fix->fx_line,
1437                          _(".uses target does not refer to a local symbol in the same section"));
1438           continue;
1439         }
1440
1441       /* Now we look through all the fixups of all the sections,
1442          counting the number of times we find a reference to sym.  */
1443       info.sym = sym;
1444       info.count = 0;
1445 #ifdef BFD_ASSEMBLER
1446       bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
1447 #else
1448       {
1449         int iscan;
1450
1451         for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
1452           sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
1453       }
1454 #endif
1455
1456       if (info.count < 1)
1457         abort ();
1458
1459       /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
1460          We have already adjusted the value of sym to include the
1461          fragment address, so we undo that adjustment here.  */
1462       subseg_change (sec, 0);
1463       fix_new (symbol_get_frag (sym),
1464                S_GET_VALUE (sym) - symbol_get_frag (sym)->fr_address,
1465                4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
1466     }
1467 }
1468
1469 /* This function is called after the symbol table has been completed,
1470    but before the relocs or section contents have been written out.
1471    If we have seen any .uses pseudo-ops, they point to an instruction
1472    which loads a register with the address of a function.  We look
1473    through the fixups to find where the function address is being
1474    loaded from.  We then generate a COUNT reloc giving the number of
1475    times that function address is referred to.  The linker uses this
1476    information when doing relaxing, to decide when it can eliminate
1477    the stored function address entirely.  */
1478
1479 void
1480 sh_frob_file ()
1481 {
1482   if (! sh_relax)
1483     return;
1484
1485 #ifdef BFD_ASSEMBLER
1486   bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
1487 #else
1488   {
1489     int iseg;
1490
1491     for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
1492       sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
1493   }
1494 #endif
1495 }
1496
1497 /* Called after relaxing.  Set the correct sizes of the fragments, and
1498    create relocs so that md_apply_fix will fill in the correct values.  */
1499
1500 void
1501 md_convert_frag (headers, seg, fragP)
1502 #ifdef BFD_ASSEMBLER
1503      bfd *headers;
1504 #else
1505      object_headers *headers;
1506 #endif
1507      segT seg;
1508      fragS *fragP;
1509 {
1510   int donerelax = 0;
1511
1512   switch (fragP->fr_subtype)
1513     {
1514     case C (COND_JUMP, COND8):
1515     case C (COND_JUMP_DELAY, COND8):
1516       subseg_change (seg, 0);
1517       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1518                1, BFD_RELOC_SH_PCDISP8BY2);
1519       fragP->fr_fix += 2;
1520       fragP->fr_var = 0;
1521       break;
1522
1523     case C (UNCOND_JUMP, UNCOND12):
1524       subseg_change (seg, 0);
1525       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1526                1, BFD_RELOC_SH_PCDISP12BY2);
1527       fragP->fr_fix += 2;
1528       fragP->fr_var = 0;
1529       break;
1530
1531     case C (UNCOND_JUMP, UNCOND32):
1532     case C (UNCOND_JUMP, UNDEF_WORD_DISP):
1533       if (fragP->fr_symbol == NULL)
1534         as_bad (_("at 0x%lx, displacement overflows 12-bit field"),
1535                 (unsigned long) fragP->fr_address);
1536       else if (S_IS_DEFINED (fragP->fr_symbol))
1537         as_bad (_("at 0x%lx, displacement to defined symbol %s overflows 12-bit field"),
1538                 (unsigned long) fragP->fr_address,              
1539                 S_GET_NAME (fragP->fr_symbol));
1540       else
1541         as_bad (_("at 0x%lx, displacement to undefined symbol %s overflows 12-bit field"),
1542                 (unsigned long) fragP->fr_address,              
1543                 S_GET_NAME (fragP->fr_symbol));
1544
1545 #if 0                           /* This code works, but generates poor code and the compiler
1546                                    should never produce a sequence that requires it to be used.  */
1547
1548       /* A jump wont fit in 12 bits, make code which looks like
1549          bra foo
1550          mov.w @(0, PC), r14
1551          .long disp
1552          foo: bra @r14
1553          */
1554       int t = buffer[0] & 0x10;
1555
1556       buffer[highbyte] = 0xa0;  /* branch over move and disp */
1557       buffer[lowbyte] = 3;
1558       buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
1559       buffer[lowbyte+2] = 0x00;
1560
1561       buffer[highbyte+4] = 0;   /* space for 32 bit jump disp */
1562       buffer[lowbyte+4] = 0;
1563       buffer[highbyte+6] = 0;
1564       buffer[lowbyte+6] = 0;
1565
1566       buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
1567       buffer[lowbyte+8] = t ? 0xb : 0x2b;
1568
1569       buffer[highbyte+10] = 0x20; /* build nop */
1570       buffer[lowbyte+10] = 0x0b;
1571
1572       /* Make reloc for the long disp */
1573       fix_new (fragP,
1574                fragP->fr_fix + 4,
1575                4,
1576                fragP->fr_symbol,
1577                fragP->fr_offset,
1578                0,
1579                BFD_RELOC_32);
1580       fragP->fr_fix += UNCOND32_LENGTH;
1581       fragP->fr_var = 0;
1582       donerelax = 1;
1583 #endif
1584
1585       break;
1586
1587     case C (COND_JUMP, COND12):
1588     case C (COND_JUMP_DELAY, COND12):
1589       /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1590       /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
1591          was due to gas incorrectly relaxing an out-of-range conditional
1592          branch with delay slot.  It turned:
1593                      bf.s    L6              (slot mov.l   r12,@(44,r0))
1594          into:
1595    
1596 2c:  8f 01 a0 8b     bf.s    32 <_main+32>   (slot bra       L6)
1597 30:  00 09           nop
1598 32:  10 cb           mov.l   r12,@(44,r0)
1599          Therefore, branches with delay slots have to be handled
1600          differently from ones without delay slots.  */
1601       {
1602         unsigned char *buffer =
1603           (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1604         int highbyte = target_big_endian ? 0 : 1;
1605         int lowbyte = target_big_endian ? 1 : 0;
1606         int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
1607
1608         /* Toggle the true/false bit of the bcond.  */
1609         buffer[highbyte] ^= 0x2;
1610
1611         /* If this is a dalayed branch, we may not put the the bra in the
1612            slot.  So we change it to a non-delayed branch, like that:
1613            b! cond slot_label; bra disp; slot_label: slot_insn
1614            ??? We should try if swapping the conditional branch and
1615            its delay-slot insn already makes the branch reach.  */
1616
1617         /* Build a relocation to six / four bytes farther on.  */
1618         subseg_change (seg, 0);
1619         fix_new (fragP, fragP->fr_fix, 2,
1620 #ifdef BFD_ASSEMBLER
1621                  section_symbol (seg),
1622 #else
1623                  seg_info (seg)->dot,
1624 #endif
1625                  fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
1626                  1, BFD_RELOC_SH_PCDISP8BY2);
1627
1628         /* Set up a jump instruction.  */
1629         buffer[highbyte + 2] = 0xa0;
1630         buffer[lowbyte + 2] = 0;
1631         fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
1632                  fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
1633
1634         if (delay)
1635           {
1636             buffer[highbyte] &= ~0x4; /* Removes delay slot from branch.  */
1637             fragP->fr_fix += 4;
1638           }
1639         else
1640           {
1641             /* Fill in a NOP instruction.  */
1642             buffer[highbyte + 4] = 0x0;
1643             buffer[lowbyte + 4] = 0x9;
1644
1645             fragP->fr_fix += 6;
1646           }
1647         fragP->fr_var = 0;
1648         donerelax = 1;
1649       }
1650       break;
1651
1652     case C (COND_JUMP, COND32):
1653     case C (COND_JUMP_DELAY, COND32):
1654     case C (COND_JUMP, UNDEF_WORD_DISP):
1655     case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
1656       if (fragP->fr_symbol == NULL)
1657         as_bad (_("at 0x%lx, displacement overflows 8-bit field"), 
1658                 (unsigned long) fragP->fr_address);
1659       else if (S_IS_DEFINED (fragP->fr_symbol))
1660         as_bad (_("at 0x%lx, displacement to defined symbol %s overflows 8-bit field "),
1661                 (unsigned long) fragP->fr_address,              
1662                 S_GET_NAME (fragP->fr_symbol));
1663       else
1664         as_bad (_("at 0x%lx, displacement to undefined symbol %s overflows 8-bit field "),
1665                 (unsigned long) fragP->fr_address,              
1666                 S_GET_NAME (fragP->fr_symbol));
1667
1668 #if 0                           /* This code works, but generates poor code, and the compiler
1669                                    should never produce a sequence that requires it to be used.  */
1670
1671       /* A bcond won't fit and it won't go into a 12 bit
1672          displacement either, the code sequence looks like:
1673          b!cond foop
1674          mov.w @(n, PC), r14
1675          jmp  @r14
1676          nop
1677          .long where
1678          foop:
1679          */
1680
1681       buffer[0] ^= 0x2;         /* Toggle T/F bit */
1682 #define JREG 14
1683       buffer[1] = 5;            /* branch over mov, jump, nop and ptr */
1684       buffer[2] = 0xd0 | JREG;  /* Build mov insn */
1685       buffer[3] = 0x2;
1686       buffer[4] = 0x40 | JREG;  /* Build jmp @JREG */
1687       buffer[5] = 0x0b;
1688       buffer[6] = 0x20;         /* build nop */
1689       buffer[7] = 0x0b;
1690       buffer[8] = 0;            /* space for 32 bit jump disp */
1691       buffer[9] = 0;
1692       buffer[10] = 0;
1693       buffer[11] = 0;
1694       buffer[12] = 0;
1695       buffer[13] = 0;
1696       /* Make reloc for the long disp */
1697       fix_new (fragP,
1698                fragP->fr_fix + 8,
1699                4,
1700                fragP->fr_symbol,
1701                fragP->fr_offset,
1702                0,
1703                BFD_RELOC_32);
1704       fragP->fr_fix += COND32_LENGTH;
1705       fragP->fr_var = 0;
1706       donerelax = 1;
1707 #endif
1708
1709       break;
1710
1711     default:
1712       abort ();
1713     }
1714
1715   if (donerelax && !sh_relax)
1716     as_warn_where (fragP->fr_file, fragP->fr_line,
1717                    _("overflow in branch to %s; converted into longer instruction sequence"),
1718                    (fragP->fr_symbol != NULL
1719                     ? S_GET_NAME (fragP->fr_symbol)
1720                     : ""));
1721 }
1722
1723 valueT
1724 DEFUN (md_section_align, (seg, size),
1725        segT seg AND
1726        valueT size)
1727 {
1728 #ifdef BFD_ASSEMBLER
1729 #ifdef OBJ_ELF
1730   return size;
1731 #else /* ! OBJ_ELF */
1732   return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
1733           & (-1 << bfd_get_section_alignment (stdoutput, seg)));
1734 #endif /* ! OBJ_ELF */
1735 #else /* ! BFD_ASSEMBLER */
1736   return ((size + (1 << section_alignment[(int) seg]) - 1)
1737           & (-1 << section_alignment[(int) seg]));
1738 #endif /* ! BFD_ASSEMBLER */
1739 }
1740
1741 /* This static variable is set by s_uacons to tell sh_cons_align that
1742    the expession does not need to be aligned.  */
1743
1744 static int sh_no_align_cons = 0;
1745
1746 /* This handles the unaligned space allocation pseudo-ops, such as
1747    .uaword.  .uaword is just like .word, but the value does not need
1748    to be aligned.  */
1749
1750 static void
1751 s_uacons (bytes)
1752      int bytes;
1753 {
1754   /* Tell sh_cons_align not to align this value.  */
1755   sh_no_align_cons = 1;
1756   cons (bytes);
1757 }
1758
1759 /* If a .word, et. al., pseud-op is seen, warn if the value is not
1760    aligned correctly.  Note that this can cause warnings to be issued
1761    when assembling initialized structured which were declared with the
1762    packed attribute.  FIXME: Perhaps we should require an option to
1763    enable this warning?  */
1764
1765 void
1766 sh_cons_align (nbytes)
1767      int nbytes;
1768 {
1769   int nalign;
1770   char *p;
1771
1772   if (sh_no_align_cons)
1773     {
1774       /* This is an unaligned pseudo-op.  */
1775       sh_no_align_cons = 0;
1776       return;
1777     }
1778
1779   nalign = 0;
1780   while ((nbytes & 1) == 0)
1781     {
1782       ++nalign;
1783       nbytes >>= 1;
1784     }
1785
1786   if (nalign == 0)
1787     return;
1788
1789   if (now_seg == absolute_section)
1790     {
1791       if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
1792         as_warn (_("misaligned data"));
1793       return;
1794     }
1795
1796   p = frag_var (rs_align_code, 1, 1, (relax_substateT) 0,
1797                 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
1798
1799   record_alignment (now_seg, nalign);
1800 }
1801
1802 /* When relaxing, we need to output a reloc for any .align directive
1803    that requests alignment to a four byte boundary or larger.  This is
1804    also where we check for misaligned data.  */
1805
1806 void
1807 sh_handle_align (frag)
1808      fragS *frag;
1809 {
1810   if (sh_relax
1811       && frag->fr_type == rs_align
1812       && frag->fr_address + frag->fr_fix > 0
1813       && frag->fr_offset > 1
1814       && now_seg != bss_section)
1815     fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
1816              BFD_RELOC_SH_ALIGN);
1817
1818   if (frag->fr_type == rs_align_code
1819       && frag->fr_next->fr_address - frag->fr_address - frag->fr_fix != 0)
1820     as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
1821 }
1822
1823 /* This macro decides whether a particular reloc is an entry in a
1824    switch table.  It is used when relaxing, because the linker needs
1825    to know about all such entries so that it can adjust them if
1826    necessary.  */
1827
1828 #ifdef BFD_ASSEMBLER
1829 #define SWITCH_TABLE_CONS(fix) (0)
1830 #else
1831 #define SWITCH_TABLE_CONS(fix)                          \
1832   ((fix)->fx_r_type == 0                                \
1833    && ((fix)->fx_size == 2                              \
1834        || (fix)->fx_size == 1                           \
1835        || (fix)->fx_size == 4))
1836 #endif
1837
1838 #define SWITCH_TABLE(fix)                               \
1839   ((fix)->fx_addsy != NULL                              \
1840    && (fix)->fx_subsy != NULL                           \
1841    && S_GET_SEGMENT ((fix)->fx_addsy) == text_section   \
1842    && S_GET_SEGMENT ((fix)->fx_subsy) == text_section   \
1843    && ((fix)->fx_r_type == BFD_RELOC_32                 \
1844        || (fix)->fx_r_type == BFD_RELOC_16              \
1845        || (fix)->fx_r_type == BFD_RELOC_8               \
1846        || SWITCH_TABLE_CONS (fix)))
1847
1848 /* See whether we need to force a relocation into the output file.
1849    This is used to force out switch and PC relative relocations when
1850    relaxing.  */
1851
1852 int
1853 sh_force_relocation (fix)
1854      fixS *fix;
1855 {
1856
1857   if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1858       || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1859     return 1;
1860
1861   if (! sh_relax)
1862     return 0;
1863
1864   return (fix->fx_pcrel
1865           || SWITCH_TABLE (fix)
1866           || fix->fx_r_type == BFD_RELOC_SH_COUNT
1867           || fix->fx_r_type == BFD_RELOC_SH_ALIGN
1868           || fix->fx_r_type == BFD_RELOC_SH_CODE
1869           || fix->fx_r_type == BFD_RELOC_SH_DATA
1870           || fix->fx_r_type == BFD_RELOC_SH_LABEL);
1871 }
1872
1873 #ifdef OBJ_ELF
1874 boolean
1875 sh_fix_adjustable (fixP)
1876    fixS *fixP;
1877 {
1878
1879   if (fixP->fx_addsy == NULL)
1880     return 1;
1881   
1882   /* We need the symbol name for the VTABLE entries */
1883   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1884       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1885     return 0;
1886
1887   return 1;
1888 }
1889 #endif
1890
1891 /* Apply a fixup to the object file.  */
1892
1893 #ifdef BFD_ASSEMBLER
1894 int
1895 md_apply_fix (fixP, valp)
1896      fixS *fixP;
1897      valueT *valp;
1898 #else
1899 void
1900 md_apply_fix (fixP, val)
1901      fixS *fixP;
1902      long val;
1903 #endif
1904 {
1905   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1906   int lowbyte = target_big_endian ? 1 : 0;
1907   int highbyte = target_big_endian ? 0 : 1;
1908 #ifdef BFD_ASSEMBLER
1909   long val = *valp;
1910 #endif
1911   long max, min;
1912   int shift;
1913
1914 #ifdef BFD_ASSEMBLER
1915   /* adjust_reloc_syms won't convert a reloc against a weak symbol
1916      into a reloc against a section, but bfd_install_relocation will
1917      screw up if the symbol is defined, so we have to adjust val here
1918      to avoid the screw up later.  */
1919   if (fixP->fx_addsy != NULL
1920       && S_IS_WEAK (fixP->fx_addsy))
1921     val -= S_GET_VALUE  (fixP->fx_addsy);
1922 #endif
1923
1924 #ifndef BFD_ASSEMBLER
1925   if (fixP->fx_r_type == 0)
1926     {
1927       if (fixP->fx_size == 2)
1928         fixP->fx_r_type = BFD_RELOC_16;
1929       else if (fixP->fx_size == 4)
1930         fixP->fx_r_type = BFD_RELOC_32;
1931       else if (fixP->fx_size == 1)
1932         fixP->fx_r_type = BFD_RELOC_8;
1933       else
1934         abort ();
1935     }
1936 #endif
1937
1938   max = min = 0;
1939   shift = 0;
1940   switch (fixP->fx_r_type)
1941     {
1942     case BFD_RELOC_SH_IMM4:
1943       max = 0xf;
1944       *buf = (*buf & 0xf0) | (val & 0xf);
1945       break;
1946
1947     case BFD_RELOC_SH_IMM4BY2:
1948       max = 0xf;
1949       shift = 1;
1950       *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1951       break;
1952
1953     case BFD_RELOC_SH_IMM4BY4:
1954       max = 0xf;
1955       shift = 2;
1956       *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1957       break;
1958
1959     case BFD_RELOC_SH_IMM8BY2:
1960       max = 0xff;
1961       shift = 1;
1962       *buf = val >> 1;
1963       break;
1964
1965     case BFD_RELOC_SH_IMM8BY4:
1966       max = 0xff;
1967       shift = 2;
1968       *buf = val >> 2;
1969       break;
1970
1971     case BFD_RELOC_8:
1972     case BFD_RELOC_SH_IMM8:
1973       /* Sometimes the 8 bit value is sign extended (e.g., add) and
1974          sometimes it is not (e.g., and).  We permit any 8 bit value.
1975          Note that adding further restrictions may invalidate
1976          reasonable looking assembly code, such as ``and -0x1,r0''.  */
1977       max = 0xff;
1978       min = - 0xff;
1979       *buf++ = val;
1980       break;
1981
1982     case BFD_RELOC_SH_PCRELIMM8BY4:
1983       /* The lower two bits of the PC are cleared before the
1984          displacement is added in.  We can assume that the destination
1985          is on a 4 byte bounday.  If this instruction is also on a 4
1986          byte boundary, then we want
1987            (target - here) / 4
1988          and target - here is a multiple of 4.
1989          Otherwise, we are on a 2 byte boundary, and we want
1990            (target - (here - 2)) / 4
1991          and target - here is not a multiple of 4.  Computing
1992            (target - (here - 2)) / 4 == (target - here + 2) / 4
1993          works for both cases, since in the first case the addition of
1994          2 will be removed by the division.  target - here is in the
1995          variable val.  */
1996       val = (val + 2) / 4;
1997       if (val & ~0xff)
1998         as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
1999       buf[lowbyte] = val;
2000       break;
2001
2002     case BFD_RELOC_SH_PCRELIMM8BY2:
2003       val /= 2;
2004       if (val & ~0xff)
2005         as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2006       buf[lowbyte] = val;
2007       break;
2008
2009     case BFD_RELOC_SH_PCDISP8BY2:
2010       val /= 2;
2011       if (val < -0x80 || val > 0x7f)
2012         as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2013       buf[lowbyte] = val;
2014       break;
2015
2016     case BFD_RELOC_SH_PCDISP12BY2:
2017       val /= 2;
2018       if (val < -0x800 || val >= 0x7ff)
2019         as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2020       buf[lowbyte] = val & 0xff;
2021       buf[highbyte] |= (val >> 8) & 0xf;
2022       break;
2023
2024     case BFD_RELOC_32:
2025       if (! target_big_endian) 
2026         {
2027           *buf++ = val >> 0;
2028           *buf++ = val >> 8;
2029           *buf++ = val >> 16;
2030           *buf++ = val >> 24;
2031         }
2032       else 
2033         {
2034           *buf++ = val >> 24;
2035           *buf++ = val >> 16;
2036           *buf++ = val >> 8;
2037           *buf++ = val >> 0;
2038         }
2039       break;
2040
2041     case BFD_RELOC_16:
2042       if (! target_big_endian)
2043         {
2044           *buf++ = val >> 0;
2045           *buf++ = val >> 8;
2046         } 
2047       else 
2048         {
2049           *buf++ = val >> 8;
2050           *buf++ = val >> 0;
2051         }
2052       break;
2053
2054     case BFD_RELOC_SH_USES:
2055       /* Pass the value into sh_coff_reloc_mangle.  */
2056       fixP->fx_addnumber = val;
2057       break;
2058
2059     case BFD_RELOC_SH_COUNT:
2060     case BFD_RELOC_SH_ALIGN:
2061     case BFD_RELOC_SH_CODE:
2062     case BFD_RELOC_SH_DATA:
2063     case BFD_RELOC_SH_LABEL:
2064       /* Nothing to do here.  */
2065       break;
2066
2067     case BFD_RELOC_VTABLE_INHERIT:
2068     case BFD_RELOC_VTABLE_ENTRY:
2069       fixP->fx_done = 0;
2070 #ifdef BFD_ASSEMBLER
2071       return 0;
2072 #else
2073       return;
2074 #endif
2075
2076     default:
2077       abort ();
2078     }
2079
2080   if (shift != 0)
2081     {
2082       if ((val & ((1 << shift) - 1)) != 0)
2083         as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
2084       if (val >= 0)
2085         val >>= shift;
2086       else
2087         val = ((val >> shift)
2088                | ((long) -1 & ~ ((long) -1 >> shift)));
2089     }
2090   if (max != 0 && (val < min || val > max))
2091     as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
2092
2093 #ifdef BFD_ASSEMBLER
2094   return 0;
2095 #endif
2096 }
2097
2098 /* Called just before address relaxation.  Return the length
2099    by which a fragment must grow to reach it's destination.  */
2100
2101 int
2102 md_estimate_size_before_relax (fragP, segment_type)
2103      register fragS *fragP;
2104      register segT segment_type;
2105 {
2106   switch (fragP->fr_subtype)
2107     {
2108     case C (UNCOND_JUMP, UNDEF_DISP):
2109       /* used to be a branch to somewhere which was unknown */
2110       if (!fragP->fr_symbol)
2111         {
2112           fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
2113           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
2114         }
2115       else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2116         {
2117           fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
2118           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
2119         }
2120       else
2121         {
2122           fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
2123           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
2124           return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
2125         }
2126       break;
2127
2128     default:
2129       abort ();
2130     case C (COND_JUMP, UNDEF_DISP):
2131     case C (COND_JUMP_DELAY, UNDEF_DISP):
2132       /* used to be a branch to somewhere which was unknown */
2133       if (fragP->fr_symbol
2134           && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2135         {
2136           int what = GET_WHAT (fragP->fr_subtype);
2137           /* Got a symbol and it's defined in this segment, become byte
2138              sized - maybe it will fix up */
2139           fragP->fr_subtype = C (what, COND8);
2140           fragP->fr_var = md_relax_table[C (what, COND8)].rlx_length;
2141         }
2142       else if (fragP->fr_symbol)
2143         {
2144           int what = GET_WHAT (fragP->fr_subtype);
2145           /* Its got a segment, but its not ours, so it will always be long */
2146           fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
2147           fragP->fr_var = md_relax_table[C (what, COND32)].rlx_length;
2148           return md_relax_table[C (what, COND32)].rlx_length;
2149         }
2150       else
2151         {
2152           int what = GET_WHAT (fragP->fr_subtype);
2153           /* We know the abs value */
2154           fragP->fr_subtype = C (what, COND8);
2155           fragP->fr_var = md_relax_table[C (what, COND8)].rlx_length;
2156         }
2157
2158       break;
2159     }
2160   return fragP->fr_var;
2161 }
2162
2163 /* Put number into target byte order */
2164
2165 void
2166 md_number_to_chars (ptr, use, nbytes)
2167      char *ptr;
2168      valueT use;
2169      int nbytes;
2170 {
2171   if (! target_big_endian)
2172     number_to_chars_littleendian (ptr, use, nbytes);
2173   else
2174     number_to_chars_bigendian (ptr, use, nbytes);
2175 }
2176
2177 long
2178 md_pcrel_from (fixP)
2179      fixS *fixP;
2180 {
2181   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
2182 }
2183
2184 #ifdef OBJ_COFF
2185
2186 int
2187 tc_coff_sizemachdep (frag)
2188      fragS *frag;
2189 {
2190   return md_relax_table[frag->fr_subtype].rlx_length;
2191 }
2192
2193 #endif /* OBJ_COFF */
2194
2195 /* When we align the .text section, insert the correct NOP pattern.  */
2196
2197 int
2198 sh_do_align (n, fill, len, max)
2199      int n;
2200      const char *fill;
2201      int len;
2202      int max;
2203 {
2204   if (fill == NULL
2205       && subseg_text_p (now_seg)
2206       && n > 1)
2207     {
2208       static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
2209       static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
2210
2211       /* First align to a 2 byte boundary, in case there is an odd
2212          .byte.  */
2213       frag_align (1, 0, 0);
2214       if (target_big_endian)
2215         frag_align_pattern (n, big_nop_pattern, sizeof big_nop_pattern, max);
2216       else
2217         frag_align_pattern (n, little_nop_pattern, sizeof little_nop_pattern,
2218                             max);
2219       return 1;
2220     }
2221
2222   return 0;
2223 }
2224
2225 #ifndef BFD_ASSEMBLER
2226 #ifdef OBJ_COFF
2227
2228 /* Map BFD relocs to SH COFF relocs.  */
2229
2230 struct reloc_map
2231 {
2232   bfd_reloc_code_real_type bfd_reloc;
2233   int sh_reloc;
2234 };
2235
2236 static const struct reloc_map coff_reloc_map[] =
2237 {
2238   { BFD_RELOC_32, R_SH_IMM32 },
2239   { BFD_RELOC_16, R_SH_IMM16 },
2240   { BFD_RELOC_8, R_SH_IMM8 },
2241   { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
2242   { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
2243   { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
2244   { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
2245   { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
2246   { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
2247   { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
2248   { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
2249   { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
2250   { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
2251   { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
2252   { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
2253   { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
2254   { BFD_RELOC_SH_USES, R_SH_USES },
2255   { BFD_RELOC_SH_COUNT, R_SH_COUNT },
2256   { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
2257   { BFD_RELOC_SH_CODE, R_SH_CODE },
2258   { BFD_RELOC_SH_DATA, R_SH_DATA },
2259   { BFD_RELOC_SH_LABEL, R_SH_LABEL },
2260   { BFD_RELOC_UNUSED, 0 }
2261 };
2262
2263 /* Adjust a reloc for the SH.  This is similar to the generic code,
2264    but does some minor tweaking.  */
2265
2266 void
2267 sh_coff_reloc_mangle (seg, fix, intr, paddr)
2268      segment_info_type *seg;
2269      fixS *fix;
2270      struct internal_reloc *intr;
2271      unsigned int paddr;
2272 {
2273   symbolS *symbol_ptr = fix->fx_addsy;
2274   symbolS *dot;
2275
2276   intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
2277
2278   if (! SWITCH_TABLE (fix))
2279     {
2280       const struct reloc_map *rm;
2281
2282       for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
2283         if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
2284           break;
2285       if (rm->bfd_reloc == BFD_RELOC_UNUSED)
2286         as_bad_where (fix->fx_file, fix->fx_line,
2287                       _("Can not represent %s relocation in this object file format"),
2288                       bfd_get_reloc_code_name (fix->fx_r_type));
2289       intr->r_type = rm->sh_reloc;
2290       intr->r_offset = 0;
2291     }
2292   else
2293     {
2294       know (sh_relax);
2295
2296       if (fix->fx_r_type == BFD_RELOC_16)
2297         intr->r_type = R_SH_SWITCH16;
2298       else if (fix->fx_r_type == BFD_RELOC_8)
2299         intr->r_type = R_SH_SWITCH8;
2300       else if (fix->fx_r_type == BFD_RELOC_32)
2301         intr->r_type = R_SH_SWITCH32;
2302       else
2303         abort ();
2304
2305       /* For a switch reloc, we set r_offset to the difference between
2306          the reloc address and the subtrahend.  When the linker is
2307          doing relaxing, it can use the determine the starting and
2308          ending points of the switch difference expression.  */
2309       intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
2310     }
2311
2312   /* PC relative relocs are always against the current section.  */
2313   if (symbol_ptr == NULL)
2314     {
2315       switch (fix->fx_r_type)
2316         {
2317         case BFD_RELOC_SH_PCRELIMM8BY2:
2318         case BFD_RELOC_SH_PCRELIMM8BY4:
2319         case BFD_RELOC_SH_PCDISP8BY2:
2320         case BFD_RELOC_SH_PCDISP12BY2:
2321         case BFD_RELOC_SH_USES:
2322           symbol_ptr = seg->dot;
2323           break;
2324         default:
2325           break;
2326         }
2327     }
2328
2329   if (fix->fx_r_type == BFD_RELOC_SH_USES)
2330     {
2331       /* We can't store the offset in the object file, since this
2332          reloc does not take up any space, so we store it in r_offset.
2333          The fx_addnumber field was set in md_apply_fix.  */
2334       intr->r_offset = fix->fx_addnumber;
2335     }
2336   else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
2337     {
2338       /* We can't store the count in the object file, since this reloc
2339          does not take up any space, so we store it in r_offset.  The
2340          fx_offset field was set when the fixup was created in
2341          sh_coff_frob_file.  */
2342       intr->r_offset = fix->fx_offset;
2343       /* This reloc is always absolute.  */
2344       symbol_ptr = NULL;
2345     }
2346   else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
2347     {
2348       /* Store the alignment in the r_offset field.  */
2349       intr->r_offset = fix->fx_offset;
2350       /* This reloc is always absolute.  */
2351       symbol_ptr = NULL;
2352     }
2353   else if (fix->fx_r_type == BFD_RELOC_SH_CODE
2354            || fix->fx_r_type == BFD_RELOC_SH_DATA
2355            || fix->fx_r_type == BFD_RELOC_SH_LABEL)
2356     {
2357       /* These relocs are always absolute.  */
2358       symbol_ptr = NULL;
2359     }
2360
2361   /* Turn the segment of the symbol into an offset.  */
2362   if (symbol_ptr != NULL)
2363     {
2364       dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
2365       if (dot != NULL)
2366         intr->r_symndx = dot->sy_number;
2367       else
2368         intr->r_symndx = symbol_ptr->sy_number;
2369     }
2370   else
2371     intr->r_symndx = -1;
2372 }
2373
2374 #endif /* OBJ_COFF */
2375 #endif /* ! BFD_ASSEMBLER */
2376
2377 #ifdef BFD_ASSEMBLER
2378
2379 /* Create a reloc.  */
2380
2381 arelent *
2382 tc_gen_reloc (section, fixp)
2383      asection *section;
2384      fixS *fixp;
2385 {
2386   arelent *rel;
2387   bfd_reloc_code_real_type r_type;
2388
2389   rel = (arelent *) xmalloc (sizeof (arelent));
2390   rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2391   *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2392   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2393
2394   r_type = fixp->fx_r_type;
2395
2396   if (SWITCH_TABLE (fixp))
2397     {
2398       rel->addend = rel->address - S_GET_VALUE (fixp->fx_subsy);
2399       if (r_type == BFD_RELOC_16)
2400         r_type = BFD_RELOC_SH_SWITCH16;
2401       else if (r_type == BFD_RELOC_8)
2402         r_type = BFD_RELOC_8_PCREL;
2403       else if (r_type == BFD_RELOC_32)
2404         r_type = BFD_RELOC_SH_SWITCH32;
2405       else
2406         abort ();
2407     }
2408   else if (r_type == BFD_RELOC_SH_USES)
2409     rel->addend = fixp->fx_addnumber;
2410   else if (r_type == BFD_RELOC_SH_COUNT)
2411     rel->addend = fixp->fx_offset;
2412   else if (r_type == BFD_RELOC_SH_ALIGN)
2413     rel->addend = fixp->fx_offset;
2414   else if (r_type == BFD_RELOC_VTABLE_INHERIT
2415            || r_type == BFD_RELOC_VTABLE_ENTRY)
2416     rel->addend = fixp->fx_offset;
2417   else if (fixp->fx_pcrel)
2418     rel->addend = fixp->fx_addnumber;
2419   else
2420     rel->addend = 0;
2421
2422   rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
2423   if (rel->howto == NULL)
2424     {
2425       as_bad_where (fixp->fx_file, fixp->fx_line,
2426                     _("Cannot represent relocation type %s"),
2427                     bfd_get_reloc_code_name (r_type));
2428       /* Set howto to a garbage value so that we can keep going.  */
2429       rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2430       assert (rel->howto != NULL);
2431     }
2432
2433   return rel;
2434 }
2435
2436 #endif /* BFD_ASSEMBLER */