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