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