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