For SH port, make relocation overflow an error instead of a warning.
[external/binutils.git] / gas / config / tc-sh.c
1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
2
3    Copyright (C) 1993, 1994, 1995 Free Software Foundation.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 /*
23    Written By Steve Chamberlain
24    sac@cygnus.com
25  */
26
27 #include <stdio.h>
28 #include "as.h"
29 #include "bfd.h"
30 #include "subsegs.h"
31 #define DEFINE_TABLE
32 #include "opcodes/sh-opc.h"
33 #include <ctype.h>
34 const char comment_chars[] = "!";
35 const char line_separator_chars[] = ";";
36 const char line_comment_chars[] = "!#";
37
38 static void s_uses PARAMS ((int));
39
40 /* This table describes all the machine specific pseudo-ops the assembler
41    has to support.  The fields are:
42    pseudo-op name without dot
43    function to call to execute this pseudo-op
44    Integer arg to pass to the function
45  */
46
47 void cons ();
48 void s_align_bytes ();
49
50 int shl = 0;
51
52 static void
53 little (ignore)
54      int ignore;
55 {
56   shl = 1;
57   target_big_endian = 0;
58 }
59
60 const pseudo_typeS md_pseudo_table[] =
61 {
62   {"int", cons, 4},
63   {"word", cons, 2},
64   {"form", listing_psize, 0},
65   {"little", little, 0},
66   {"heading", listing_title, 0},
67   {"import", s_ignore, 0},
68   {"page", listing_eject, 0},
69   {"program", s_ignore, 0},
70   {"uses", s_uses, 0},
71   {0, 0, 0}
72 };
73
74 /*int md_reloc_size; */
75
76 int sh_relax;           /* set if -relax seen */
77
78 const char EXP_CHARS[] = "eE";
79
80 /* Chars that mean this number is a floating point constant */
81 /* As in 0f12.456 */
82 /* or    0d1.2345e12 */
83 const char FLT_CHARS[] = "rRsSfFdDxXpP";
84
85 #define C(a,b) ENCODE_RELAX(a,b)
86
87 #define JREG 14                 /* Register used as a temp when relaxing */
88 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
89 #define GET_WHAT(x) ((x>>4))
90
91 /* These are the two types of relaxable instrction */
92 #define COND_JUMP 1
93 #define UNCOND_JUMP  2
94
95 #define UNDEF_DISP 0
96 #define COND8  1
97 #define COND12 2
98 #define COND32 3
99 #define UNCOND12 1
100 #define UNCOND32 2
101 #define UNDEF_WORD_DISP 4
102 #define END 5
103
104 #define UNCOND12 1
105 #define UNCOND32 2
106
107 /* Branch displacements are from the address of the branch plus
108    four, thus all minimum and maximum values have 4 added to them.  */
109 #define COND8_F 258
110 #define COND8_M -252
111 #define COND8_LENGTH 2
112
113 /* There is one extra instruction before the branch, so we must add
114    two more bytes to account for it.  */
115 #define COND12_F 4100
116 #define COND12_M -4090
117 #define COND12_LENGTH 6
118
119 /* ??? The minimum and maximum values are wrong, but this does not matter
120    since this relocation type is not supported yet.  */
121 #define COND32_F (1<<30)
122 #define COND32_M -(1<<30)
123 #define COND32_LENGTH 14
124
125 #define UNCOND12_F 4098
126 #define UNCOND12_M -4092
127 #define UNCOND12_LENGTH 2
128
129 /* ??? The minimum and maximum values are wrong, but this does not matter
130    since this relocation type is not supported yet.  */
131 #define UNCOND32_F (1<<30)
132 #define UNCOND32_M -(1<<30)
133 #define UNCOND32_LENGTH 14
134
135 const relax_typeS md_relax_table[C (END, 0)] = {
136   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
137   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
138
139   { 0 },
140   /* C (COND_JUMP, COND8) */
141   { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
142   /* C (COND_JUMP, COND12) */
143   { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
144   /* C (COND_JUMP, COND32) */
145   { COND32_F, COND32_M, COND32_LENGTH, 0, },
146   { 0 }, { 0 }, { 0 }, { 0 },
147   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
148
149   { 0 },
150   /* C (UNCOND_JUMP, UNCOND12) */
151   { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
152   /* C (UNCOND_JUMP, UNCOND32) */
153   { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
154   { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
155   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
156 };
157
158 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics */
159
160 /*
161    This function is called once, at assembler startup time.  This should
162    set up all the tables, etc that the MD part of the assembler needs
163  */
164
165 void
166 md_begin ()
167 {
168   sh_opcode_info *opcode;
169   char *prev_name = "";
170
171   if (! shl)
172     target_big_endian = 1;
173
174   opcode_hash_control = hash_new ();
175
176   /* Insert unique names into hash table */
177   for (opcode = sh_table; opcode->name; opcode++)
178     {
179       if (strcmp (prev_name, opcode->name))
180         {
181           prev_name = opcode->name;
182           hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
183         }
184       else
185         {
186           /* Make all the opcodes with the same name point to the same
187              string */
188           opcode->name = prev_name;
189         }
190     }
191 }
192
193 static int reg_m;
194 static int reg_n;
195 static int reg_b;
196
197 static expressionS immediate;   /* absolute expression */
198
199 typedef struct
200   {
201     sh_arg_type type;
202     int reg;
203   }
204
205 sh_operand_info;
206
207 /* try and parse a reg name, returns number of chars consumed */
208 static int
209 parse_reg (src, mode, reg)
210      char *src;
211      int *mode;
212      int *reg;
213 {
214   if (src[0] == 'r') 
215     {
216       if (src[1] >= '0' && src[1] <= '7' && strncmp(&src[2], "_bank", 5) == 0)
217         {
218           *mode = A_REG_B;
219           *reg  = (src[1] - '0');
220           return 7;
221         }
222     }
223
224   if (src[0] == 'r')
225     {
226       if (src[1] == '1')
227         {
228           if (src[2] >= '0' && src[2] <= '5')
229             {
230               *mode = A_REG_N;
231               *reg = 10 + src[2] - '0';
232               return 3;
233             }
234         }
235       if (src[1] >= '0' && src[1] <= '9')
236         {
237           *mode = A_REG_N;
238           *reg = (src[1] - '0');
239           return 2;
240         }
241     }
242
243   if (src[0] == 's' && src[1] == 's' && src[2] == 'r')
244     {
245       *mode = A_SSR;
246       return 3;
247     }
248
249   if (src[0] == 's' && src[1] == 'p' && src[2] == 'c')
250     {
251       *mode = A_SPC;
252       return 3;
253     }
254
255   if (src[0] == 's' && src[1] == 'r')
256     {
257       *mode = A_SR;
258       return 2;
259     }
260
261   if (src[0] == 's' && src[1] == 'p')
262     {
263       *mode = A_REG_N;
264       *reg = 15;
265       return 2;
266     }
267
268   if (src[0] == 'p' && src[1] == 'r')
269     {
270       *mode = A_PR;
271       return 2;
272     }
273   if (src[0] == 'p' && src[1] == 'c')
274     {
275       *mode = A_DISP_PC;
276       return 2;
277     }
278   if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r')
279     {
280       *mode = A_GBR;
281       return 3;
282     }
283   if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r')
284     {
285       *mode = A_VBR;
286       return 3;
287     }
288
289   if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c')
290     {
291       if (src[3] == 'l')
292         {
293           *mode = A_MACL;
294           return 4;
295         }
296       if (src[3] == 'h')
297         {
298           *mode = A_MACH;
299           return 4;
300         }
301     }
302 /* start-sanitize-sh3e */
303   if (src[0] == 'f' && src[1] == 'r')
304     {
305       if (src[2] == '1')
306         {
307           if (src[3] >= '0' && src[3] <= '5')
308             {
309               *mode = F_REG_N;
310               *reg = 10 + src[3] - '0';
311               return 4;
312             }
313         }
314       if (src[2] >= '0' && src[2] <= '9')
315         {
316           *mode = F_REG_N;
317           *reg = (src[2] - '0');
318           return 3;
319         }
320     }
321   if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l')
322     {
323       *mode = FPUL_N;
324       return 4;
325     }
326
327   if (src[0] == 'f' && src[1] == 'p'
328       && src[2] == 's' && src[3] == 'c' && src[4] == 'r')
329     {
330       *mode = FPSCR_N;
331       return 5;
332     }
333 /* end-sanitize-sh3e */
334
335   return 0;
336 }
337
338 static symbolS *dot()
339 {
340   const char *fake;
341
342   /* JF: '.' is pseudo symbol with value of current location
343      in current segment.  */
344   fake = FAKE_LABEL_NAME;
345   return  symbol_new (fake,
346                       now_seg,
347                       (valueT) frag_now_fix (),
348                       frag_now);
349
350 }
351
352
353 static
354 char *
355 parse_exp (s)
356      char *s;
357 {
358   char *save;
359   char *new;
360
361   save = input_line_pointer;
362   input_line_pointer = s;
363   expression (&immediate);
364   if (immediate.X_op == O_absent)
365     as_bad ("missing operand");
366   new = input_line_pointer;
367   input_line_pointer = save;
368   return new;
369 }
370
371
372 /* The many forms of operand:
373
374    Rn                   Register direct
375    @Rn                  Register indirect
376    @Rn+                 Autoincrement
377    @-Rn                 Autodecrement
378    @(disp:4,Rn)
379    @(disp:8,GBR)
380    @(disp:8,PC)
381
382    @(R0,Rn)
383    @(R0,GBR)
384
385    disp:8
386    disp:12
387    #imm8
388    pr, gbr, vbr, macl, mach
389
390  */
391
392 static
393 char *
394 parse_at (src, op)
395      char *src;
396      sh_operand_info *op;
397 {
398   int len;
399   int mode;
400   src++;
401   if (src[0] == '-')
402     {
403       /* Must be predecrement */
404       src++;
405
406       len = parse_reg (src, &mode, &(op->reg));
407       if (mode != A_REG_N)
408         as_bad ("illegal register after @-");
409
410       op->type = A_DEC_N;
411       src += len;
412     }
413   else if (src[0] == '(')
414     {
415       /* Could be @(disp, rn), @(disp, gbr), @(disp, pc),  @(r0, gbr) or
416          @(r0, rn) */
417       src++;
418       len = parse_reg (src, &mode, &(op->reg));
419       if (len && mode == A_REG_N)
420         {
421           src += len;
422           if (op->reg != 0)
423             {
424               as_bad ("must be @(r0,...)");
425             }
426           if (src[0] == ',')
427             src++;
428           /* Now can be rn or gbr */
429           len = parse_reg (src, &mode, &(op->reg));
430           if (mode == A_GBR)
431             {
432               op->type = A_R0_GBR;
433             }
434           else if (mode == A_REG_N)
435             {
436               op->type = A_IND_R0_REG_N;
437             }
438           else
439             {
440               as_bad ("syntax error in @(r0,...)");
441             }
442         }
443       else
444         {
445           /* Must be an @(disp,.. thing) */
446           src = parse_exp (src);
447           if (src[0] == ',')
448             src++;
449           /* Now can be rn, gbr or pc */
450           len = parse_reg (src, &mode, &op->reg);
451           if (len)
452             {
453               if (mode == A_REG_N)
454                 {
455                   op->type = A_DISP_REG_N;
456                 }
457               else if (mode == A_GBR)
458                 {
459                   op->type = A_DISP_GBR;
460                 }
461               else if (mode == A_DISP_PC)
462                 {
463                   /* Turn a plain @(4,pc) into @(.+4,pc) */
464                   if (immediate.X_op == O_constant) { 
465                     immediate.X_add_symbol = dot();
466                     immediate.X_op = O_symbol;
467                   }
468                   op->type = A_DISP_PC;
469                 }
470               else
471                 {
472                   as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
473                 }
474             }
475           else
476             {
477               as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
478             }
479         }
480       src += len;
481       if (src[0] != ')')
482         as_bad ("expecting )");
483       else
484         src++;
485     }
486   else
487     {
488       src += parse_reg (src, &mode, &(op->reg));
489       if (mode != A_REG_N)
490         {
491           as_bad ("illegal register after @");
492         }
493       if (src[0] == '+')
494         {
495           op->type = A_INC_N;
496           src++;
497         }
498       else
499         {
500           op->type = A_IND_N;
501         }
502     }
503   return src;
504 }
505
506 static void
507 get_operand (ptr, op)
508      char **ptr;
509      sh_operand_info *op;
510 {
511   char *src = *ptr;
512   int mode = -1;
513   unsigned int len;
514
515   if (src[0] == '#')
516     {
517       src++;
518       *ptr = parse_exp (src);
519       op->type = A_IMM;
520       return;
521     }
522
523   else if (src[0] == '@')
524     {
525       *ptr = parse_at (src, op);
526       return;
527     }
528   len = parse_reg (src, &mode, &(op->reg));
529   if (len)
530     {
531       *ptr = src + len;
532       op->type = mode;
533       return;
534     }
535   else
536     {
537       /* Not a reg, the only thing left is a displacement */
538       *ptr = parse_exp (src);
539       op->type = A_DISP_PC;
540       return;
541     }
542 }
543
544 static
545 char *
546 get_operands (info, args, operand)
547      sh_opcode_info *info;
548      char *args;
549      sh_operand_info *operand;
550
551 {
552   char *ptr = args;
553   if (info->arg[0])
554     {
555       ptr++;
556
557       get_operand (&ptr, operand + 0);
558       if (info->arg[1])
559         {
560           if (*ptr == ',')
561             {
562               ptr++;
563             }
564           get_operand (&ptr, operand + 1);
565 /* start-sanitize-sh3e */
566           if (info->arg[2])
567             {
568               if (*ptr == ',')
569                 {
570                   ptr++;
571                 }
572               get_operand (&ptr, operand + 2);
573             }
574           else
575             {
576               operand[2].type = 0;
577             }
578 /* end-sanitize-sh3e */
579         }
580       else
581         {
582           operand[1].type = 0;
583 /* start-sanitize-sh3e */
584           operand[2].type = 0;
585 /* end-sanitize-sh3e */
586         }
587     }
588   else
589     {
590       operand[0].type = 0;
591       operand[1].type = 0;
592 /* start-sanitize-sh3e */
593       operand[2].type = 0;
594 /* end-sanitize-sh3e */
595     }
596   return ptr;
597 }
598
599 /* Passed a pointer to a list of opcodes which use different
600    addressing modes, return the opcode which matches the opcodes
601    provided
602  */
603
604 static
605 sh_opcode_info *
606 get_specific (opcode, operands)
607      sh_opcode_info *opcode;
608      sh_operand_info *operands;
609 {
610   sh_opcode_info *this_try = opcode;
611   char *name = opcode->name;
612   int n = 0;
613   while (opcode->name)
614     {
615       this_try = opcode++;
616       if (this_try->name != name)
617         {
618           /* We've looked so far down the table that we've run out of
619              opcodes with the same name */
620           return 0;
621         }
622       /* look at both operands needed by the opcodes and provided by
623          the user - since an arg test will often fail on the same arg
624          again and again, we'll try and test the last failing arg the
625          first on each opcode try */
626
627       for (n = 0; this_try->arg[n]; n++)
628         {
629           sh_operand_info *user = operands + n;
630           sh_arg_type arg = this_try->arg[n];
631           switch (arg)
632             {
633             case A_IMM:
634             case A_BDISP12:
635             case A_BDISP8:
636             case A_DISP_GBR:
637             case A_DISP_PC:
638             case A_MACH:
639             case A_PR:
640             case A_MACL:
641               if (user->type != arg)
642                 goto fail;
643               break;
644             case A_R0:
645               /* opcode needs r0 */
646               if (user->type != A_REG_N || user->reg != 0)
647                 goto fail;
648               break;
649             case A_R0_GBR:
650               if (user->type != A_R0_GBR || user->reg != 0)
651                 goto fail;
652               break;
653 /* start-sanitize-sh3e */
654             case F_FR0:
655               if (user->type != F_REG_N || user->reg != 0)
656                 goto fail;
657               break;
658 /* end-sanitize-sh3e */
659
660             case A_REG_N:
661             case A_INC_N:
662             case A_DEC_N:
663             case A_IND_N:
664             case A_IND_R0_REG_N:
665             case A_DISP_REG_N:
666 /* start-sanitize-sh3e */
667             case F_REG_N:
668             case FPUL_N:
669             case FPSCR_N:
670 /* end-sanitize-sh3e */
671               /* Opcode needs rn */
672               if (user->type != arg)
673                 goto fail;
674               reg_n = user->reg;
675               break;
676             case A_GBR:
677             case A_SR:
678             case A_VBR:
679             case A_SSR:
680             case A_SPC:
681               if (user->type != arg)
682                 goto fail;
683               break;
684
685             case A_REG_B:
686               if (user->type != arg)
687                 goto fail;
688               reg_b = user->reg;
689               break;
690
691             case A_REG_M:
692             case A_INC_M:
693             case A_DEC_M:
694             case A_IND_M:
695             case A_IND_R0_REG_M:
696             case A_DISP_REG_M:
697               /* Opcode needs rn */
698               if (user->type != arg - A_REG_M + A_REG_N)
699                 goto fail;
700               reg_m = user->reg;
701               break;
702
703 /* start-sanitize-sh3e */
704             case F_REG_M:
705             case FPUL_M:
706             case FPSCR_M:
707               /* Opcode needs rn */
708               if (user->type != arg - F_REG_M + F_REG_N)
709                 goto fail;
710               reg_m = user->reg;
711               break;
712 /* end-sanitize-sh3e */
713         
714             default:
715               printf ("unhandled %d\n", arg);
716               goto fail;
717             }
718         }
719       return this_try;
720     fail:;
721     }
722
723   return 0;
724 }
725
726 int
727 check (operand, low, high)
728      expressionS *operand;
729      int low;
730      int high;
731 {
732   if (operand->X_op != O_constant
733       || operand->X_add_number < low
734       || operand->X_add_number > high)
735     {
736       as_bad ("operand must be absolute in range %d..%d", low, high);
737     }
738   return operand->X_add_number;
739 }
740
741
742 static void
743 insert (where, how, pcrel)
744      char *where;
745      int how;
746      int pcrel;
747 {
748   fix_new_exp (frag_now,
749                where - frag_now->fr_literal,
750                2,
751                &immediate,
752                pcrel,
753                how);
754 }
755
756 static void
757 build_relax (opcode)
758      sh_opcode_info *opcode;
759 {
760   int high_byte = target_big_endian ? 0 : 1;
761   char *p;
762
763   if (opcode->arg[0] == A_BDISP8)
764     {
765       p = frag_var (rs_machine_dependent,
766                     md_relax_table[C (COND_JUMP, COND32)].rlx_length,
767                     md_relax_table[C (COND_JUMP, COND8)].rlx_length,
768                     C (COND_JUMP, 0),
769                     immediate.X_add_symbol,
770                     immediate.X_add_number,
771                     0);
772       p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
773     }
774   else if (opcode->arg[0] == A_BDISP12)
775     {
776       p = frag_var (rs_machine_dependent,
777                     md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
778                     md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
779                     C (UNCOND_JUMP, 0),
780                     immediate.X_add_symbol,
781                     immediate.X_add_number,
782                     0);
783       p[high_byte] = (opcode->nibbles[0] << 4);
784     }
785
786 }
787
788 /* Now we know what sort of opcodes it is, lets build the bytes -
789  */
790 static void
791 build_Mytes (opcode, operand)
792      sh_opcode_info *opcode;
793      sh_operand_info *operand;
794
795 {
796   int index;
797   char nbuf[4];
798   char *output = frag_more (2);
799   int low_byte = target_big_endian ? 1 : 0;
800   nbuf[0] = 0;
801   nbuf[1] = 0;
802   nbuf[2] = 0;
803   nbuf[3] = 0;
804
805   for (index = 0; index < 4; index++)
806     {
807       sh_nibble_type i = opcode->nibbles[index];
808       if (i < 16)
809         {
810           nbuf[index] = i;
811         }
812       else
813         {
814           switch (i)
815             {
816             case REG_N:
817               nbuf[index] = reg_n;
818               break;
819             case REG_M:
820               nbuf[index] = reg_m;
821               break;
822             case REG_B:
823               nbuf[index] = reg_b | 0x08;
824               break;
825             case DISP_4:
826               insert (output + low_byte, R_SH_IMM4, 0);
827               break;
828             case IMM_4BY4:
829               insert (output + low_byte, R_SH_IMM4BY4, 0);
830               break;
831             case IMM_4BY2:
832               insert (output + low_byte, R_SH_IMM4BY2, 0);
833               break;
834             case IMM_4:
835               insert (output + low_byte, R_SH_IMM4, 0);
836               break;
837             case IMM_8BY4:
838               insert (output + low_byte, R_SH_IMM8BY4, 0);
839               break;
840             case IMM_8BY2:
841               insert (output + low_byte, R_SH_IMM8BY2, 0);
842               break;
843             case IMM_8:
844               insert (output + low_byte, R_SH_IMM8, 0);
845               break;
846             case PCRELIMM_8BY4:
847               insert (output, R_SH_PCRELIMM8BY4, 1);
848               break;
849             case PCRELIMM_8BY2:
850               insert (output, R_SH_PCRELIMM8BY2, 1);
851               break;
852             default:
853               printf ("failed for %d\n", i);
854             }
855         }
856     }
857   if (! target_big_endian) {
858     output[1] = (nbuf[0] << 4) | (nbuf[1]);
859     output[0] = (nbuf[2] << 4) | (nbuf[3]);
860   }
861   else {
862     output[0] = (nbuf[0] << 4) | (nbuf[1]);
863     output[1] = (nbuf[2] << 4) | (nbuf[3]);
864   }
865 }
866
867 /* This is the guts of the machine-dependent assembler.  STR points to a
868    machine dependent instruction.  This function is supposed to emit
869    the frags/bytes it assembles to.
870  */
871
872 void
873 md_assemble (str)
874      char *str;
875 {
876   unsigned char *op_start;
877   unsigned char *op_end;
878   sh_operand_info operand[3];
879   sh_opcode_info *opcode;
880   char name[20];
881   int nlen = 0;
882   /* Drop leading whitespace */
883   while (*str == ' ')
884     str++;
885
886   /* find the op code end */
887   for (op_start = op_end = (unsigned char *) (str);
888        *op_end
889        && nlen < 20
890        && !is_end_of_line[*op_end] && *op_end != ' ';
891        op_end++)
892     {
893       name[nlen] = op_start[nlen];
894       nlen++;
895     }
896   name[nlen] = 0;
897
898   if (nlen == 0)
899     {
900       as_bad ("can't find opcode ");
901     }
902
903   opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
904
905   if (opcode == NULL)
906     {
907       as_bad ("unknown opcode");
908       return;
909     }
910
911   if (opcode->arg[0] == A_BDISP12
912       || opcode->arg[0] == A_BDISP8)
913     {
914       parse_exp (op_end + 1);
915       build_relax (opcode);
916     }
917   else
918     {
919       if (opcode->arg[0] != A_END)
920         {
921           get_operands (opcode, op_end, operand);
922         }
923       opcode = get_specific (opcode, operand);
924
925       if (opcode == 0)
926         {
927           /* Couldn't find an opcode which matched the operands */
928           char *where = frag_more (2);
929
930           where[0] = 0x0;
931           where[1] = 0x0;
932           as_bad ("invalid operands for opcode");
933           return;
934         }
935
936       build_Mytes (opcode, operand);
937     }
938
939 }
940
941 void
942 DEFUN (tc_crawl_symbol_chain, (headers),
943        object_headers * headers)
944 {
945   printf ("call to tc_crawl_symbol_chain \n");
946 }
947
948 symbolS *
949 DEFUN (md_undefined_symbol, (name),
950        char *name)
951 {
952   return 0;
953 }
954
955 void
956 DEFUN (tc_headers_hook, (headers),
957        object_headers * headers)
958 {
959   printf ("call to tc_headers_hook \n");
960 }
961
962 /* Various routines to kill one day */
963 /* Equal to MAX_PRECISION in atof-ieee.c */
964 #define MAX_LITTLENUMS 6
965
966 /* Turn a string in input_line_pointer into a floating point constant of type
967    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
968    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
969  */
970 char *
971 md_atof (type, litP, sizeP)
972      int type;
973      char *litP;
974      int *sizeP;
975 {
976   int prec;
977   LITTLENUM_TYPE words[4];
978   char *t;
979   int i;
980
981   switch (type)
982     {
983     case 'f':
984       prec = 2;
985       break;
986
987     case 'd':
988       prec = 4;
989       break;
990
991     default:
992       *sizeP = 0;
993       return "bad call to md_atof";
994     }
995
996   t = atof_ieee (input_line_pointer, type, words);
997   if (t)
998     input_line_pointer = t;
999
1000   *sizeP = prec * 2;
1001
1002   if (! target_big_endian)
1003     {
1004       for (i = prec - 1; i >= 0; i--)
1005         {
1006           md_number_to_chars (litP, (valueT) words[i], 2);
1007           litP += 2;
1008         }
1009     }
1010   else
1011     {
1012       for (i = 0; i < prec; i++)
1013         {
1014           md_number_to_chars (litP, (valueT) words[i], 2);
1015           litP += 2;
1016         }
1017     }
1018      
1019   return NULL;
1020 }
1021
1022 /* Handle the .uses pseudo-op.  This pseudo-op is used just before a
1023    call instruction.  It refers to a label of the instruction which
1024    loads the register which the call uses.  We use it to generate a
1025    special reloc for the linker.  */
1026
1027 static void
1028 s_uses (ignore)
1029      int ignore;
1030 {
1031   expressionS ex;
1032
1033   if (! sh_relax)
1034     as_warn (".uses pseudo-op seen when not relaxing");
1035
1036   expression (&ex);
1037
1038   if (ex.X_op != O_symbol || ex.X_add_number != 0)
1039     {
1040       as_bad ("bad .uses format");
1041       ignore_rest_of_line ();
1042       return;
1043     }
1044
1045   fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, R_SH_USES);
1046
1047   demand_empty_rest_of_line ();
1048 }
1049 \f
1050 CONST char *md_shortopts = "";
1051 struct option md_longopts[] = {
1052
1053 #define OPTION_RELAX  (OPTION_MD_BASE)
1054 #define OPTION_LITTLE (OPTION_MD_BASE+1)
1055
1056   {"relax", no_argument, NULL, OPTION_RELAX},
1057   {"little", no_argument, NULL, OPTION_LITTLE},
1058   {NULL, no_argument, NULL, 0}
1059 };
1060 size_t md_longopts_size = sizeof(md_longopts);
1061
1062 int
1063 md_parse_option (c, arg)
1064      int c;
1065      char *arg;
1066 {
1067   switch (c)
1068     {
1069     case OPTION_RELAX:
1070       sh_relax = 1;
1071       break;
1072     case OPTION_LITTLE:
1073       shl = 1;
1074       target_big_endian = 0;
1075       break;
1076
1077     default:
1078       return 0;
1079     }
1080
1081   return 1;
1082 }
1083
1084 void
1085 md_show_usage (stream)
1086      FILE *stream;
1087 {
1088   fprintf(stream, "\
1089 SH options:\n\
1090 -little                 generate little endian code\n\
1091 -relax                  alter jump instructions for long displacements\n");
1092 }
1093 \f
1094 int md_short_jump_size;
1095
1096 void
1097 tc_Nout_fix_to_chars ()
1098 {
1099   printf ("call to tc_Nout_fix_to_chars \n");
1100   abort ();
1101 }
1102
1103 void
1104 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1105      char *ptr;
1106      addressT from_Nddr;
1107      addressT to_Nddr;
1108      fragS *frag;
1109      symbolS *to_symbol;
1110 {
1111   as_fatal ("failed sanity check.");
1112 }
1113
1114 void
1115 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1116      char *ptr;
1117      addressT from_Nddr, to_Nddr;
1118      fragS *frag;
1119      symbolS *to_symbol;
1120 {
1121   as_fatal ("failed sanity check.");
1122 }
1123
1124 /* This is function is called after the symbol table has been
1125    completed, but before md_convert_frag has been called.  If we have
1126    seen any .uses pseudo-ops, they point to an instruction which loads
1127    a register with the address of a function.  We look through the
1128    fixups to find where the function address is being loaded from.  We
1129    then generate a COUNT reloc giving the number of times that
1130    function address is referred to.  The linker uses this information
1131    when doing relaxing, to decide when it can eliminate the stored
1132    function address entirely.  */
1133
1134 void
1135 sh_coff_frob_file ()
1136 {
1137   int iseg;
1138
1139   if (! sh_relax)
1140     return;
1141
1142   for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
1143     {
1144       fixS *fix;
1145
1146       for (fix = segment_info[iseg].fix_root; fix != NULL; fix = fix->fx_next)
1147         {
1148           symbolS *sym;
1149           bfd_vma val;
1150           bfd_vma paddr;
1151           fixS *fscan;
1152           int iscan;
1153           int count;
1154
1155           if (fix->fx_r_type != R_SH_USES)
1156             continue;
1157
1158           /* The R_SH_USES reloc should refer to a defined local
1159              symbol in the same section.  */
1160           sym = fix->fx_addsy;
1161           if (sym == NULL
1162               || fix->fx_subsy != NULL
1163               || fix->fx_addnumber != 0
1164               || S_GET_SEGMENT (sym) != iseg
1165               || S_GET_STORAGE_CLASS (sym) == C_EXT)
1166             {
1167               as_warn_where (fix->fx_file, fix->fx_line,
1168                              ".uses does not refer to a local symbol in the same section");
1169               continue;
1170             }
1171
1172           /* Look through the fixups again, this time looking for one
1173              at the same location as sym.  */
1174           val = S_GET_VALUE (sym);
1175           paddr = segment_info[iseg].scnhdr.s_paddr;
1176           for (fscan = segment_info[iseg].fix_root;
1177                fscan != NULL;
1178                fscan = fscan->fx_next)
1179             if (val == paddr + fscan->fx_frag->fr_address + fscan->fx_where)
1180               break;
1181           if (fscan == NULL)
1182             {
1183               as_warn_where (fix->fx_file, fix->fx_line,
1184                              "can't find fixup pointed to by .uses");
1185               continue;
1186             }
1187
1188           if (fscan->fx_tcbit)
1189             {
1190               /* We've already done this one.  */
1191               continue;
1192             }
1193
1194           /* fscan should also be a fixup to a local symbol in the same
1195              section.  */
1196           sym = fscan->fx_addsy;
1197           if (sym == NULL
1198               || fscan->fx_subsy != NULL
1199               || fscan->fx_addnumber != 0
1200               || S_GET_SEGMENT (sym) != iseg
1201               || S_GET_STORAGE_CLASS (sym) == C_EXT)
1202             {
1203               as_warn_where (fix->fx_file, fix->fx_line,
1204                              ".uses target does not refer to a local symbol in the same section");
1205               continue;
1206             }
1207
1208           /* Now we look through all the fixups of all the sections,
1209              counting the number of times we find a reference to sym.  */
1210           count = 0;
1211           for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
1212             {
1213               paddr = segment_info[iscan].scnhdr.s_paddr;
1214               for (fscan = segment_info[iscan].fix_root;
1215                    fscan != NULL;
1216                    fscan = fscan->fx_next)
1217                 {
1218                   if (fscan->fx_addsy == sym)
1219                     {
1220                       ++count;
1221                       fscan->fx_tcbit = 1;
1222                     }
1223                 }
1224             }
1225
1226           if (count < 1)
1227             abort ();
1228
1229           /* Generate a R_SH_COUNT fixup at the location of sym.  We
1230              have already adjusted the value of sym to include the
1231              fragment address, so we undo that adjustment here.  */
1232           subseg_change (iseg, 0);
1233           fix_new (sym->sy_frag, S_GET_VALUE (sym) - sym->sy_frag->fr_address,
1234                    4, &abs_symbol, count, 0, R_SH_COUNT);
1235         }
1236     }
1237 }
1238
1239 /* Called after relaxing.  Set the correct sizes of the fragments, and
1240    create relocs so that md_apply_fix will fill in the correct values.  */
1241
1242 void
1243 md_convert_frag (headers, seg, fragP)
1244      object_headers *headers;
1245      segT seg;
1246      fragS *fragP;
1247 {
1248   int donerelax = 0;
1249
1250   switch (fragP->fr_subtype)
1251     {
1252     case C (COND_JUMP, COND8):
1253       subseg_change (seg, 0);
1254       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1255                1, R_SH_PCDISP8BY2);
1256       fragP->fr_fix += 2;
1257       fragP->fr_var = 0;
1258       break;
1259
1260     case C (UNCOND_JUMP, UNCOND12):
1261       subseg_change (seg, 0);
1262       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1263                1, R_SH_PCDISP);
1264       fragP->fr_fix += 2;
1265       fragP->fr_var = 0;
1266       break;
1267
1268     case C (UNCOND_JUMP, UNCOND32):
1269     case C (UNCOND_JUMP, UNDEF_WORD_DISP):
1270       if (fragP->fr_symbol == NULL)
1271         as_bad ("at 0x%lx, displacement overflows 12-bit field",
1272                 (unsigned long) fragP->fr_address);
1273       else
1274         as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 12-bit field",
1275                 (unsigned long) fragP->fr_address,              
1276                 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1277                 S_GET_NAME (fragP->fr_symbol));
1278
1279 #if 0                           /* This code works, but generates poor code and the compiler
1280                                    should never produce a sequence that requires it to be used.  */
1281
1282       /* A jump wont fit in 12 bits, make code which looks like
1283          bra foo
1284          mov.w @(0, PC), r14
1285          .long disp
1286          foo: bra @r14
1287          */
1288       int t = buffer[0] & 0x10;
1289
1290       buffer[highbyte] = 0xa0;  /* branch over move and disp */
1291       buffer[lowbyte] = 3;
1292       buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
1293       buffer[lowbyte+2] = 0x00;
1294
1295       buffer[highbyte+4] = 0;   /* space for 32 bit jump disp */
1296       buffer[lowbyte+4] = 0;
1297       buffer[highbyte+6] = 0;
1298       buffer[lowbyte+6] = 0;
1299
1300       buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
1301       buffer[lowbyte+8] = t ? 0xb : 0x2b;
1302
1303       buffer[highbyte+10] = 0x20; /* build nop */
1304       buffer[lowbyte+10] = 0x0b;
1305
1306       /* Make reloc for the long disp */
1307       fix_new (fragP,
1308                fragP->fr_fix + 4,
1309                4,
1310                fragP->fr_symbol,
1311                fragP->fr_offset,
1312                0,
1313                R_SH_IMM32);
1314       fragP->fr_fix += UNCOND32_LENGTH;
1315       fragP->fr_var = 0;
1316       donerelax = 1;
1317 #endif
1318
1319       break;
1320
1321     case C (COND_JUMP, COND12):
1322       /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1323       {
1324         unsigned char *buffer =
1325           (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1326         int highbyte = target_big_endian ? 0 : 1;
1327         int lowbyte = target_big_endian ? 1 : 0;
1328
1329         /* Toggle the true/false bit of the bcond.  */
1330         buffer[highbyte] ^= 0x2;
1331
1332         /* Build a relocation to six bytes farther on.  */
1333         subseg_change (seg, 0);
1334         fix_new (fragP, fragP->fr_fix, 2,
1335                  segment_info[seg].dot,
1336                  fragP->fr_address + fragP->fr_fix + 6,
1337                  1, R_SH_PCDISP8BY2);
1338
1339         /* Set up a jump instruction.  */
1340         buffer[highbyte + 2] = 0xa0;
1341         buffer[lowbyte + 2] = 0;
1342         fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
1343                  fragP->fr_offset, 1, R_SH_PCDISP);
1344
1345         /* Fill in a NOP instruction.  */
1346         buffer[highbyte + 4] = 0x0;
1347         buffer[lowbyte + 4] = 0x9;
1348
1349         fragP->fr_fix += 6;
1350         fragP->fr_var = 0;
1351         donerelax = 1;
1352       }
1353       break;
1354
1355     case C (COND_JUMP, COND32):
1356     case C (COND_JUMP, UNDEF_WORD_DISP):
1357       if (fragP->fr_symbol == NULL)
1358         as_bad ("at %0xlx, displacement overflows 8-bit field", 
1359                 (unsigned long) fragP->fr_address);
1360       else  
1361         as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 8-bit field ",
1362                 (unsigned long) fragP->fr_address,              
1363                 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1364                 S_GET_NAME (fragP->fr_symbol));
1365
1366 #if 0                           /* This code works, but generates poor code, and the compiler
1367                                    should never produce a sequence that requires it to be used.  */
1368
1369       /* A bcond won't fit and it won't go into a 12 bit
1370          displacement either, the code sequence looks like:
1371          b!cond foop
1372          mov.w @(n, PC), r14
1373          jmp  @r14
1374          nop
1375          .long where
1376          foop:
1377          */
1378
1379       buffer[0] ^= 0x2;         /* Toggle T/F bit */
1380 #define JREG 14
1381       buffer[1] = 5;            /* branch over mov, jump, nop and ptr */
1382       buffer[2] = 0xd0 | JREG;  /* Build mov insn */
1383       buffer[3] = 0x2;
1384       buffer[4] = 0x40 | JREG;  /* Build jmp @JREG */
1385       buffer[5] = 0x0b;
1386       buffer[6] = 0x20;         /* build nop */
1387       buffer[7] = 0x0b;
1388       buffer[8] = 0;            /* space for 32 bit jump disp */
1389       buffer[9] = 0;
1390       buffer[10] = 0;
1391       buffer[11] = 0;
1392       buffer[12] = 0;
1393       buffer[13] = 0;
1394       /* Make reloc for the long disp */
1395       fix_new (fragP,
1396                fragP->fr_fix + 8,
1397                4,
1398                fragP->fr_symbol,
1399                fragP->fr_offset,
1400                0,
1401                R_SH_IMM32);
1402       fragP->fr_fix += COND32_LENGTH;
1403       fragP->fr_var = 0;
1404       donerelax = 1;
1405 #endif
1406
1407       break;
1408
1409     default:
1410       abort ();
1411     }
1412
1413   if (donerelax && !sh_relax)
1414     as_warn ("Offset doesn't fit at 0x%lx, trying to get to %s+0x%lx",
1415              (unsigned long) fragP->fr_address,
1416              fragP->fr_symbol ? S_GET_NAME(fragP->fr_symbol): "",
1417              (unsigned long) fragP->fr_offset);
1418 }
1419
1420 valueT
1421 DEFUN (md_section_align, (seg, size),
1422        segT seg AND
1423        valueT size)
1424 {
1425   return ((size + (1 << section_alignment[(int) seg]) - 1)
1426           & (-1 << section_alignment[(int) seg]));
1427
1428 }
1429
1430 /* When relaxing, we need to output a reloc for any .align directive
1431    that requests alignment to a four byte boundary or larger.  */
1432
1433 void
1434 sh_handle_align (frag)
1435      fragS *frag;
1436 {
1437   if (sh_relax
1438       && frag->fr_type == rs_align
1439       && frag->fr_address + frag->fr_fix > 0
1440       && frag->fr_offset > 1)
1441     fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
1442              R_SH_ALIGN);
1443 }
1444
1445 /* This macro decides whether a particular reloc is an entry in a
1446    switch table.  It is used when relaxing, because the linker needs
1447    to know about all such entries so that it can adjust them if
1448    necessary.  */
1449
1450 #define SWITCH_TABLE(fix)                               \
1451   ((fix)->fx_addsy != NULL                              \
1452    && (fix)->fx_subsy != NULL                           \
1453    && S_GET_SEGMENT ((fix)->fx_addsy) == text_section   \
1454    && S_GET_SEGMENT ((fix)->fx_subsy) == text_section   \
1455    && ((fix)->fx_r_type == R_SH_IMM32                   \
1456        || (fix)->fx_r_type == R_SH_IMM16                \
1457        || ((fix)->fx_r_type == 0                        \
1458            && ((fix)->fx_size == 2                      \
1459                || (fix)->fx_size == 4))))
1460
1461 /* See whether we need to force a relocation into the output file.
1462    This is used to force out switch and PC relative relocations when
1463    relaxing.  */
1464
1465 int
1466 sh_force_relocation (fix)
1467      fixS *fix;
1468 {
1469   if (! sh_relax)
1470     return 0;
1471
1472   return (fix->fx_pcrel
1473           || SWITCH_TABLE (fix)
1474           || fix->fx_r_type == R_SH_COUNT
1475           || fix->fx_r_type == R_SH_ALIGN);
1476 }
1477
1478 /* Apply a fixup to the object file.  */
1479
1480 void
1481 md_apply_fix (fixP, val)
1482      fixS *fixP;
1483      long val;
1484 {
1485   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1486   int lowbyte = target_big_endian ? 1 : 0;
1487   int highbyte = target_big_endian ? 0 : 1;
1488
1489   if (fixP->fx_r_type == 0)
1490     {
1491       if (fixP->fx_size == 2)
1492         fixP->fx_r_type = R_SH_IMM16;
1493       else if (fixP->fx_size == 4)
1494         fixP->fx_r_type = R_SH_IMM32;
1495       else if (fixP->fx_size == 1)
1496         fixP->fx_r_type = R_SH_IMM8;
1497       else
1498         abort ();
1499     }
1500
1501   switch (fixP->fx_r_type)
1502     {
1503     case R_SH_IMM4:
1504       *buf = (*buf & 0xf0) | (val & 0xf);
1505       break;
1506
1507     case R_SH_IMM4BY2:
1508       *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1509       break;
1510
1511     case R_SH_IMM4BY4:
1512       *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1513       break;
1514
1515     case R_SH_IMM8BY2:
1516       *buf = val >> 1;
1517       break;
1518
1519     case R_SH_IMM8BY4:
1520       *buf = val >> 2;
1521       break;
1522
1523     case R_SH_IMM8:
1524       *buf++ = val;
1525       break;
1526
1527     case R_SH_PCRELIMM8BY4:
1528       /* The lower two bits of the PC are cleared before the
1529          displacement is added in.  We can assume that the destination
1530          is on a 4 byte bounday.  If this instruction is also on a 4
1531          byte boundary, then we want
1532            (target - here) / 4
1533          and target - here is a multiple of 4.
1534          Otherwise, we are on a 2 byte boundary, and we want
1535            (target - (here - 2)) / 4
1536          and target - here is not a multiple of 4.  Computing
1537            (target - (here - 2)) / 4 == (target - here + 2) / 4
1538          works for both cases, since in the first case the addition of
1539          2 will be removed by the division.  target - here is in the
1540          variable val.  */
1541       val = (val + 2) / 4;
1542       if (val & ~0xff)
1543         as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1544       buf[lowbyte] = val;
1545       break;
1546
1547     case R_SH_PCRELIMM8BY2:
1548       val /= 2;
1549       if (val & ~0xff)
1550         as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1551       buf[lowbyte] = val;
1552       break;
1553
1554     case R_SH_PCDISP8BY2:
1555       val /= 2;
1556       if (val < -0x80 || val > 0x7f)
1557         as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1558       buf[lowbyte] = val;
1559       break;
1560
1561     case R_SH_PCDISP:
1562       val /= 2;
1563       if (val < -0x800 || val >= 0x7ff)
1564         as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1565       buf[lowbyte] = val & 0xff;
1566       buf[highbyte] |= (val >> 8) & 0xf;
1567       break;
1568
1569     case R_SH_IMM32:
1570       if (! target_big_endian) 
1571         {
1572           *buf++ = val >> 0;
1573           *buf++ = val >> 8;
1574           *buf++ = val >> 16;
1575           *buf++ = val >> 24;
1576         }
1577       else 
1578         {
1579           *buf++ = val >> 24;
1580           *buf++ = val >> 16;
1581           *buf++ = val >> 8;
1582           *buf++ = val >> 0;
1583         }
1584       break;
1585
1586     case R_SH_IMM16:
1587       if (! target_big_endian)
1588         {
1589           *buf++ = val >> 0;
1590           *buf++ = val >> 8;
1591         } 
1592       else 
1593         {
1594           *buf++ = val >> 8;
1595           *buf++ = val >> 0;
1596         }
1597       break;
1598
1599     case R_SH_USES:
1600       /* Pass the value into sh_coff_reloc_mangle.  */
1601       fixP->fx_addnumber = val;
1602       break;
1603
1604     case R_SH_COUNT:
1605     case R_SH_ALIGN:
1606       /* Nothing to do here.  */
1607       break;
1608
1609     default:
1610       abort ();
1611     }
1612 }
1613
1614 int md_long_jump_size;
1615
1616 /* Called just before address relaxation.  Return the length
1617    by which a fragment must grow to reach it's destination.  */
1618
1619 int
1620 md_estimate_size_before_relax (fragP, segment_type)
1621      register fragS *fragP;
1622      register segT segment_type;
1623 {
1624   switch (fragP->fr_subtype)
1625     {
1626     case C (UNCOND_JUMP, UNDEF_DISP):
1627       /* used to be a branch to somewhere which was unknown */
1628       if (!fragP->fr_symbol)
1629         {
1630           fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1631           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1632         }
1633       else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1634         {
1635           fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1636           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1637         }
1638       else
1639         {
1640           fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
1641           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1642           return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1643         }
1644       break;
1645
1646     default:
1647       abort ();
1648     case C (COND_JUMP, UNDEF_DISP):
1649       /* used to be a branch to somewhere which was unknown */
1650       if (fragP->fr_symbol
1651           && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1652         {
1653           /* Got a symbol and it's defined in this segment, become byte
1654              sized - maybe it will fix up */
1655           fragP->fr_subtype = C (COND_JUMP, COND8);
1656           fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1657         }
1658       else if (fragP->fr_symbol)
1659         {
1660           /* Its got a segment, but its not ours, so it will always be long */
1661           fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
1662           fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1663           return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1664         }
1665       else
1666         {
1667           /* We know the abs value */
1668           fragP->fr_subtype = C (COND_JUMP, COND8);
1669           fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1670         }
1671
1672       break;
1673     }
1674   return fragP->fr_var;
1675 }
1676
1677 /* Put number into target byte order */
1678
1679 void
1680 md_number_to_chars (ptr, use, nbytes)
1681      char *ptr;
1682      valueT use;
1683      int nbytes;
1684 {
1685   if (! target_big_endian)
1686     number_to_chars_littleendian (ptr, use, nbytes);
1687   else
1688     number_to_chars_bigendian (ptr, use, nbytes);
1689 }
1690
1691 long
1692 md_pcrel_from (fixP)
1693      fixS *fixP;
1694 {
1695   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
1696 }
1697
1698 int
1699 tc_coff_sizemachdep (frag)
1700      fragS *frag;
1701 {
1702   return md_relax_table[frag->fr_subtype].rlx_length;
1703 }
1704
1705 #ifdef OBJ_COFF
1706
1707 /* Adjust a reloc for the SH.  This is similar to the generic code,
1708    but does some minor tweaking.  */
1709
1710 void
1711 sh_coff_reloc_mangle (seg, fix, intr, paddr)
1712      segment_info_type *seg;
1713      fixS *fix;
1714      struct internal_reloc *intr;
1715      unsigned int paddr;
1716 {
1717   symbolS *symbol_ptr = fix->fx_addsy;
1718   symbolS *dot;
1719
1720   intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
1721
1722   if (! SWITCH_TABLE (fix))
1723     {
1724       intr->r_type = fix->fx_r_type;
1725       intr->r_offset = 0;
1726     }
1727   else
1728     {
1729       know (sh_relax);
1730
1731       if (fix->fx_r_type == R_SH_IMM16)
1732         intr->r_type = R_SH_SWITCH16;
1733       else if (fix->fx_r_type == R_SH_IMM32)
1734         intr->r_type = R_SH_SWITCH32;
1735       else
1736         abort ();
1737
1738       /* For a switch reloc, we set r_offset to the difference between
1739          the reloc address and the subtrahend.  When the linker is
1740          doing relaxing, it can use the determine the starting and
1741          ending points of the switch difference expression.  */
1742       intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
1743     }
1744
1745   /* PC relative relocs are always against the current section.  */
1746   if (symbol_ptr == NULL)
1747     {
1748       switch (fix->fx_r_type)
1749         {
1750         case R_SH_PCRELIMM8BY2:
1751         case R_SH_PCRELIMM8BY4:
1752         case R_SH_PCDISP8BY2:
1753         case R_SH_PCDISP:
1754         case R_SH_USES:
1755           symbol_ptr = seg->dot;
1756           break;
1757         default:
1758           break;
1759         }
1760     }
1761
1762   if (fix->fx_r_type == R_SH_USES)
1763     {
1764       /* We can't store the offset in the object file, since this
1765          reloc does not take up any space, so we store it in r_offset.
1766          The fx_addnumber field was set in md_apply_fix.  */
1767       intr->r_offset = fix->fx_addnumber;
1768     }
1769   else if (fix->fx_r_type == R_SH_COUNT)
1770     {
1771       /* We can't store the count in the object file, since this reloc
1772          does not take up any space, so we store it in r_offset.  The
1773          fx_offset field was set when the fixup was created in
1774          sh_coff_frob_file.  */
1775       intr->r_offset = fix->fx_offset;
1776       /* This reloc is always absolute.  */
1777       symbol_ptr = NULL;
1778     }
1779   else if (fix->fx_r_type == R_SH_ALIGN)
1780     {
1781       /* Store the alignment in the r_offset field.  */
1782       intr->r_offset = fix->fx_offset;
1783       /* This reloc is always absolute.  */
1784       symbol_ptr = NULL;
1785     }
1786
1787   /* Turn the segment of the symbol into an offset.  */
1788   if (symbol_ptr != NULL)
1789     {
1790       dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1791       if (dot != NULL)
1792         intr->r_symndx = dot->sy_number;
1793       else
1794         intr->r_symndx = symbol_ptr->sy_number;
1795     }
1796   else
1797     intr->r_symndx = -1;
1798 }
1799
1800 #endif