1 /* tc-sh64.c -- Assemble code for the SuperH SH SHcompact and SHmedia.
2    Copyright 2000, 2001, 2002, 2003 Free Software Foundation.
3 
4    This file is part of GAS, the GNU Assembler.
5 
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10 
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20 
21 /* This file defines SHmedia ISA-specific functions and includes tc-sh.c.
22    The SHcompact ISA is in all useful aspects the "old" sh4 as implemented
23    in tc-sh.c.  Not making this file part of tc-sh.c makes it easier to
24    keep a leaner sh[1-4]-only implementation.  */
25 
26 #define HAVE_SH64
27 
28 #include <stdio.h>
29 #include "as.h"
30 #include "safe-ctype.h"
31 #include "opcodes/sh64-opc.h"
32 
33 #ifndef OBJ_ELF
34 #error This file assumes object output is in the ELF format
35 #endif
36 
37 /* Suffix used when we make "datalabel" symbol copies.  It must not
38    collide with anything that can normally appear in a symbol, "faked
39    symbol" or local symbol.  */
40 #define DATALABEL_SUFFIX " DL"
41 
42 /* See shmedia_md_apply_fix3 and shmedia_md_pcrel_from_section for usage.  */
43 #define SHMEDIA_MD_PCREL_FROM_FIX(FIXP) \
44  ((FIXP)->fx_size + (FIXP)->fx_where + (FIXP)->fx_frag->fr_address - 4)
45 
46 /* We use this internally to see which one is PT and which is a PTA/PTB
47    that should be error-checked.  We give it a better name here (but not
48    one that looks official).  Adding it to reloc.c would make it look too
49    much of a real reloc; it is just used temporarily as a fixup-type.  */
50 #define SHMEDIA_BFD_RELOC_PT BFD_RELOC_12_PCREL
51 
52 typedef struct
53  {
54    shmedia_arg_type type;
55 
56    /* These could go into a union, but that would uglify the code.  */
57    int reg;
58    expressionS immediate;
59 
60    /* If IMMEDIATE was a shift-expression, like "(S >> N) & 65535", where
61       N = 0, 16, 32, 48, used to extract a certain 16-bit-field to make up
62       a MOVI or SHORI relocation for a symbol, then we put the
63       corresponding reloc-type here and modify the "immediate" expression
64       to S.  Otherwise, this is just BFD_RELOC_NONE.  */
65    bfd_reloc_code_real_type reloctype;
66  } shmedia_operand_info;
67 
68 /* Frag containing last base instruction.  This is put in the TC field in
69    a frag, so we can emit fixups for fr_opcode without needing to make
70    sure that the opcode is in the same frag as any variant operand.  */
71 fragS *sh64_last_insn_frag = NULL;
72 
73 typedef struct
74  {
75    shmedia_operand_info operands[3];
76    unsigned long ops_val;
77  } shmedia_operands_info;
78 
79 enum sh64_abi_values
80  { sh64_abi_unspecified, sh64_abi_32, sh64_abi_64 };
81 
82 /* What ISA are we assembling code for?  */
83 enum sh64_isa_values sh64_isa_mode = sh64_isa_unspecified;
84 
85 /* What ABI was specified, if any (implicitly or explicitly)?  */
86 static enum sh64_abi_values sh64_abi = sh64_abi_unspecified;
87 
88 /* A note that says if we're in a sequence of insns without label
89    settings, segment or ISA mode changes or emitted data.  */
90 static bfd_boolean seen_insn = FALSE;
91 
92 /* This is set to TRUE in shmedia_md_end, so that we don't emit any
93    .cranges entries when the assembler calls output functions while
94    grinding along after all input is seen.  */
95 static bfd_boolean sh64_end_of_assembly = FALSE;
96 
97 /* Controlled by the option -no-mix, this invalidates mixing SHcompact and
98    SHmedia code in the same section, and also invalidates mixing data and
99    SHmedia code in the same section.  No .cranges will therefore be
100    emitted, unless -shcompact-const-crange is specified and there is a
101    constant pool in SHcompact code.  */
102 static bfd_boolean sh64_mix = TRUE;
103 
104 static bfd_boolean sh64_shcompact_const_crange = FALSE;
105 
106 /* Controlled by the option -no-expand, this says whether or not we expand
107    MOVI and PT/PTA/PTB.  When we do not expand these insns to fit an
108    operand, we will emit errors for operands out of range and generate the
109    basic instruction and reloc for an external symbol.  */
110 static bfd_boolean sh64_expand = TRUE;
111 
112 /* Controlled by the option -expand-pt32, this says whether we expand
113    PT/PTA/PTB of an external symbol to (only) 32 or (the full) 64 bits
114    when -abi=64 is in effect.  */
115 static bfd_boolean sh64_pt32 = FALSE;
116 
117 /* When emitting a .cranges descriptor, we want to avoid getting recursive
118    calls through emit_expr.  */
119 static bfd_boolean emitting_crange = FALSE;
120 
121 /* SHmedia mnemonics.  */
122 static struct hash_control *shmedia_opcode_hash_control = NULL;
123 
124 static const unsigned char shmedia_big_nop_pattern[4] =
125  {
126    (SHMEDIA_NOP_OPC >> 24) & 255, (SHMEDIA_NOP_OPC >> 16) & 255,
127    (SHMEDIA_NOP_OPC >> 8) & 255, SHMEDIA_NOP_OPC & 255
128  };
129 
130 static const unsigned char shmedia_little_nop_pattern[4] =
131  {
132    SHMEDIA_NOP_OPC & 255, (SHMEDIA_NOP_OPC >> 8) & 255,
133    (SHMEDIA_NOP_OPC >> 16) & 255, (SHMEDIA_NOP_OPC >> 24) & 255
134  };
135 
136 static void shmedia_md_begin (void);
137 static int shmedia_parse_reg (char *, int *, int *, shmedia_arg_type);
138 static void shmedia_md_assemble (char *);
139 static void shmedia_md_apply_fix3 (fixS *, valueT *);
140 static int shmedia_md_estimate_size_before_relax (fragS *, segT);
141 static int shmedia_init_reloc (arelent *, fixS *);
142 static char *shmedia_get_operands (shmedia_opcode_info *, char *,
143 				   shmedia_operands_info *);
144 static void s_sh64_mode (int);
145 static void s_sh64_abi (int);
146 static void shmedia_md_convert_frag (bfd *, segT, fragS *, bfd_boolean);
147 static void shmedia_check_limits  (offsetT *, bfd_reloc_code_real_type,
148 				   fixS *);
149 static void sh64_set_contents_type (enum sh64_elf_cr_type);
150 static void shmedia_get_operand (char **, shmedia_operand_info *,
151 				 shmedia_arg_type);
152 static unsigned long shmedia_immediate_op (char *, shmedia_operand_info *,
153 					   int, bfd_reloc_code_real_type);
154 static char *shmedia_parse_exp (char *, shmedia_operand_info *);
155 static void shmedia_frob_file_before_adjust (void);
156 static void sh64_emit_crange (symbolS *, symbolS *, enum sh64_elf_cr_type);
157 static void sh64_flush_last_crange (bfd *, asection *, void *);
158 static void sh64_flag_output (void);
159 static void sh64_update_contents_mark (bfd_boolean);
160 static void sh64_vtable_entry (int);
161 static void sh64_vtable_inherit (int);
162 static char *strip_datalabels (void);
163 static int shmedia_build_Mytes (shmedia_opcode_info *,
164 				shmedia_operands_info *);
165 static shmedia_opcode_info *shmedia_find_cooked_opcode (char **);
166 static unsigned long shmedia_mask_number (unsigned long,
167 					  bfd_reloc_code_real_type);
168 
169 #include "tc-sh.c"
170 
171 void
172 shmedia_md_end (void)
173 {
174   symbolS *symp;
175 
176   /* First, update the last range to include whatever data was last
177      emitted.  */
178   sh64_update_contents_mark (TRUE);
179 
180   /* Make sure frags generated after this point are not marked with the
181      wrong ISA; make them easily spottable.  We still want to distinguish
182      it from sh64_isa_unspecified when we compile for SHcompact or
183      SHmedia.  */
184   if (sh64_isa_mode != sh64_isa_unspecified)
185     sh64_isa_mode = sh64_isa_sh5_guard;
186 
187   sh64_end_of_assembly = TRUE;
188 
189   bfd_map_over_sections (stdoutput, sh64_flush_last_crange, NULL);
190 
191   /* Iterate over segments and emit the last .cranges descriptor.  */
192   for (symp = symbol_rootP; symp != NULL; symp = symp->sy_next)
193     {
194       symbolS *mainsym = *symbol_get_tc (symp);
195 
196       /* Is this a datalabel symbol; does it have a pointer to the main
197 	 symbol?  */
198       if (mainsym != NULL)
199 	{
200 	  /* If the datalabel symbol is undefined, check if the main
201 	     symbol has changed in that respect.  */
202 	  if (S_GET_SEGMENT (symp) == undefined_section)
203 	    {
204 	      segT symseg;
205 
206 	      symseg = S_GET_SEGMENT (mainsym);
207 
208 	      /* If the symbol is now defined to something that is not
209 		 global and without STO_SH5_ISA32, we just equate the
210 		 datalabel symbol to the main symbol, and the lack of
211 		 STO_SH5_ISA32 will handle the datalabelness.  */
212 	      if (symseg != undefined_section)
213 		{
214 		  if (S_GET_OTHER (mainsym) != STO_SH5_ISA32)
215 		    {
216 		      symp->sy_value.X_op = O_symbol;
217 		      symp->sy_value.X_add_symbol = mainsym;
218 		      symp->sy_value.X_op_symbol = NULL;
219 		      symp->sy_value.X_add_number = 0;
220 		      S_SET_SEGMENT (symp, S_GET_SEGMENT (mainsym));
221 		      symbol_set_frag (symp, &zero_address_frag);
222 		      copy_symbol_attributes (symp, mainsym);
223 		    }
224 		  else
225 		    {
226 		      /* An undefined symbol has since we saw it at
227 			 "datalabel", been defined to a BranchTarget
228 			 symbol.  What we need to do here is very similar
229 			 to when we find the "datalabel" for a defined
230 			 symbol.  FIXME: Break out to common function.  */
231 		      symbol_set_value_expression (symp,
232 						   symbol_get_value_expression
233 						   (mainsym));
234 		      S_SET_SEGMENT (symp, symseg);
235 		      symbol_set_frag (symp, symbol_get_frag (mainsym));
236 		      copy_symbol_attributes (symp, mainsym);
237 
238 		      /* Unset the BranchTarget mark that can be set at
239 			 attribute-copying.  */
240 		      S_SET_OTHER (symp,
241 				   S_GET_OTHER (symp) & ~STO_SH5_ISA32);
242 
243 		      /* The GLOBAL and WEAK attributes are not copied
244 			 over by copy_symbol_attributes.  Do it here.  */
245 		      if (S_IS_WEAK (mainsym))
246 			S_SET_WEAK (symp);
247 		      else if (S_IS_EXTERNAL (mainsym))
248 			S_SET_EXTERNAL (symp);
249 		    }
250 		}
251 	      else
252 		{
253 		  /* A symbol that was defined at the time we saw
254 		     "datalabel" can since have been attributed with being
255 		     weak or global.  */
256 		  if (S_IS_WEAK (mainsym))
257 		    S_SET_WEAK (symp);
258 		  else if (S_IS_EXTERNAL (mainsym))
259 		    S_SET_EXTERNAL (symp);
260 		}
261 	    }
262 	}
263     }
264 
265   for (symp = symbol_rootP; symp != NULL; symp = symp->sy_next)
266     if (S_GET_OTHER (symp) & STO_SH5_ISA32)
267       symp->sy_value.X_add_number++;
268 }
269 
270 /* When resolving symbols, the main assembler has done us a misfavour.  It
271    has removed the equation to the main symbol for a datalabel reference
272    that should be equal to the main symbol, e.g. when it's a global or
273    weak symbol and is a non-BranchTarget symbol anyway.  We change that
274    back, so that relocs are against the main symbol, not the local "section
275    + offset" value.  */
276 
277 static void
278 shmedia_frob_file_before_adjust (void)
279 {
280   symbolS *symp;
281   for (symp = symbol_rootP; symp != NULL; symp = symp->sy_next)
282     {
283       symbolS *mainsym = *symbol_get_tc (symp);
284 
285       if (mainsym != NULL
286 	  && S_GET_OTHER (mainsym) != STO_SH5_ISA32
287 	  && (S_IS_EXTERN (mainsym) || S_IS_WEAK (mainsym)))
288 	{
289 	  symp->sy_value.X_op = O_symbol;
290 	  symp->sy_value.X_add_symbol = mainsym;
291 	  symp->sy_value.X_op_symbol = NULL;
292 	  symp->sy_value.X_add_number = 0;
293 
294 	  /* For the "equation trick" to work, we have to set the section
295 	     to undefined.  */
296 	  S_SET_SEGMENT (symp, undefined_section);
297 	  symbol_set_frag (symp, &zero_address_frag);
298 	  copy_symbol_attributes (symp, mainsym);
299 
300 	  /* Don't forget to remove the STO_SH5_ISA32 attribute after
301 	     copying the other attributes.  */
302 	  S_SET_OTHER (symp, S_GET_OTHER (symp) & ~STO_SH5_ISA32);
303 	}
304     }
305 }
306 
307 /* We need to mark the current location after the alignment.  This is
308    copied code the caller, do_align.  We mark the frag location before and
309    after as we need and arrange to skip the same code in do_align.
310 
311    An alternative to code duplication is to call the do_align recursively,
312    arranging to fall through into do_align if we're already here.  That
313    would require do_align as an incoming function parameter, since it's
314    static in read.c.  That solution was discarded a too kludgy.  */
315 
316 void
317 sh64_do_align (int n, const char *fill, int len, int max)
318 {
319   /* Update region, or put a data region in front.  */
320   sh64_update_contents_mark (TRUE);
321 
322   /* Only make a frag if we HAVE to...  */
323   if (n != 0 && !need_pass_2)
324     {
325       if (fill == NULL)
326 	{
327 	  if (subseg_text_p (now_seg))
328 	    frag_align_code (n, max);
329 	  else
330 	    frag_align (n, 0, max);
331 	}
332       else if (len <= 1)
333 	frag_align (n, *fill, max);
334       else
335 	frag_align_pattern (n, fill, len, max);
336     }
337 
338   /* Update mark for current region with current type.  */
339   sh64_update_contents_mark (FALSE);
340 }
341 
342 /* The MAX_MEM_FOR_RS_ALIGN_CODE worker.  We have to find out the ISA of
343    the current segment at this position.  We can't look just at
344    sh64_isa_shmedia, and we can't look at frag_now.  This is brittle:
345    callers are currently frag_align_code from subsegs_finish in write.c
346    (end of assembly) and frag_align_code from do_align in read.c (during
347    assembly).  */
348 
349 int
350 sh64_max_mem_for_rs_align_code (void)
351 {
352   segment_info_type *seginfo;
353   fragS *mode_start_frag;
354   seginfo = seg_info (now_seg);
355 
356   /* We don't use the contents type we find at the tc_segment_info_data,
357      since that does not give us absolute information about the ISA; the
358      contents type can presumably be CRT_DATA and we'd be none the wiser.
359      Instead we use the information stored at the frag of the symbol at
360      the start of this range.  If any information is missing or NULL,
361      assume SHcompact.  */
362   return
363     /* If the current ISA mode is SHmedia, that's the mode that we're
364        going to assign to the new frag, so request enough memory for
365        it, even if we switch modes afterwards, otherwise we may
366        allocate too little memory and end up overflowing our buffer.  */
367     (sh64_isa_mode == sh64_isa_shmedia
368      || (sh64_isa_mode != sh64_isa_unspecified
369 	 && seginfo != NULL
370 	 && seginfo->tc_segment_info_data.mode_start_symbol != NULL
371 	 && ((mode_start_frag
372 	      = (symbol_get_frag
373 		 (seginfo->tc_segment_info_data.mode_start_symbol)))
374 	     != NULL)
375 	 && mode_start_frag->tc_frag_data.isa == sh64_isa_shmedia))
376     ? (3 + 4) : (2 + 1);
377 }
378 
379 /* Put in SHmedia NOP:s if the alignment was created when in SHmedia mode.  */
380 
381 void
382 sh64_handle_align (fragS * frag)
383 {
384   int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
385   char * p  = frag->fr_literal + frag->fr_fix;
386 
387   if (frag->tc_frag_data.isa == sh64_isa_shmedia
388       && frag->fr_type == rs_align_code)
389     {
390       while (bytes & 3)
391 	{
392 	  *p++ = 0;
393 	  bytes--;
394 	  frag->fr_fix += 1;
395 	}
396 
397       if (target_big_endian)
398 	{
399 	  memcpy (p, shmedia_big_nop_pattern,
400 		  sizeof shmedia_big_nop_pattern);
401 	  frag->fr_var = sizeof shmedia_big_nop_pattern;
402 	}
403       else
404 	{
405 	  memcpy (p, shmedia_little_nop_pattern,
406 		  sizeof shmedia_little_nop_pattern);
407 	  frag->fr_var = sizeof shmedia_little_nop_pattern;
408 	}
409     }
410   else
411     /* Punt to SHcompact function.  */
412     sh_handle_align (frag);
413 }
414 
415 /* Set SEC_SH64_ISA32 for SHmedia sections.  */
416 
417 void
418 shmedia_frob_section_type (asection *sec)
419 {
420   segment_info_type *seginfo;
421   seginfo = seg_info (sec);
422 
423   /* This and elf32-sh64.c:sh64_elf_fake_sections are the only places
424      where we use anything else than ELF header flags to communicate the
425      section as containing SHmedia or other contents.  BFD SEC_* section
426      flags are running out and should not be overloaded with
427      target-specific semantics.  This target is ELF only (semantics not
428      defined for other formats), so we use the target-specific pointer
429      field of the ELF section data.  */
430   if (seginfo && sh64_abi == sh64_abi_32)
431     {
432       struct sh64_section_data *sec_elf_data;
433       flagword sec_type = 0;
434 
435       if (seginfo->tc_segment_info_data.emitted_ranges != 0)
436 	sec_type = SHF_SH5_ISA32_MIXED;
437       else if (seginfo->tc_segment_info_data.contents_type == CRT_SH5_ISA32)
438 	sec_type = SHF_SH5_ISA32;
439 
440       sec_elf_data = sh64_elf_section_data (sec)->sh64_info;
441       if (sec_elf_data == NULL)
442 	{
443 	  sec_elf_data = xcalloc (1, sizeof (*sec_elf_data));
444 	  sh64_elf_section_data (sec)->sh64_info = sec_elf_data;
445 	}
446 
447       sec_elf_data->contents_flags = sec_type;
448     }
449 }
450 
451 /* This function is called by write_object_file right before the symbol
452    table is written.  We subtract 1 from all symbols marked STO_SH5_ISA32,
453    as their values are temporarily incremented in shmedia_md_end, before
454    symbols values are used by relocs and fixups.
455 
456    To increment all symbols and then decrement here is admittedly a
457    hackish solution.  The alternative is to add infrastructure and hooks
458    to symbol evaluation that evaluates symbols differently internally to
459    the value output into the object file, but at the moment that just
460    seems too much for little benefit.  */
461 
462 void
463 sh64_adjust_symtab (void)
464 {
465   symbolS *symp;
466 
467   for (symp = symbol_rootP; symp; symp = symbol_next (symp))
468     {
469       symbolS *main_symbol = *symbol_get_tc (symp);
470 
471       if (main_symbol)
472 	{
473 	  char *sym_name = (char *) S_GET_NAME (symp);
474 
475 	  /* All datalabels not used in relocs should be gone by now.
476 
477 	     We change those remaining to have the name of the main
478 	     symbol, and we set the ELF type of the symbol of the reloc to
479 	     STT_DATALABEL.  */
480 	  sym_name[strlen (sym_name) - strlen (DATALABEL_SUFFIX)] = 0;
481 	  elf_symbol (symbol_get_bfdsym (symp))->internal_elf_sym.st_info
482 	    = STT_DATALABEL;
483 
484 	  /* Also set this symbol to "undefined", so we'll have only one
485 	     definition.  */
486 	  S_SET_SEGMENT (symp, undefined_section);
487 	}
488       else if (S_GET_OTHER (symp) & STO_SH5_ISA32)
489 	{
490 	  /* It's important to change the BFD symbol value, since it is now
491 	     set to the GAS symbolS value.  */
492 	  symp->bsym->value--;
493 
494 	  /* Note that we do *not* adjust symp->sy_value.X_add_number.  If
495 	     you do this, the test case in sh/sh64/immexpr2.s will fail.
496 	     This is because *after* symbols have been output but before
497 	     relocs are output, fixups are inspected one more time, and
498 	     some leftover expressions are resolved.  To resolve to the
499 	     same values, those expressions must have the same GAS symbol
500 	     values before as after symbols have been output.  We could
501 	     "symp->sy_value.X_add_number++" on the STO_SH5_ISA32 symbols
502 	     through tc_frob_file after symbols have been output, but that
503 	     would be too gross.  */
504 	}
505     }
506 }
507 
508 /* Fill-in an allocated arelent.  */
509 
510 static int
511 shmedia_init_reloc (arelent *rel, fixS *fixP)
512 {
513   /* Adjust parts of *relp according to *fixp, and tell that it has been
514      done, so default initializations will not happen.   */
515   switch (fixP->fx_r_type)
516     {
517     case BFD_RELOC_64:
518     case BFD_RELOC_64_PCREL:
519     case BFD_RELOC_SH_IMM_LOW16:
520     case BFD_RELOC_SH_IMM_MEDLOW16:
521     case BFD_RELOC_SH_IMM_MEDHI16:
522     case BFD_RELOC_SH_IMM_HI16:
523     case BFD_RELOC_SH_IMM_LOW16_PCREL:
524     case BFD_RELOC_SH_IMM_MEDLOW16_PCREL:
525     case BFD_RELOC_SH_IMM_MEDHI16_PCREL:
526     case BFD_RELOC_SH_IMM_HI16_PCREL:
527     case BFD_RELOC_SH_IMMU5:
528     case BFD_RELOC_SH_IMMU6:
529     case BFD_RELOC_SH_IMMS6:
530     case BFD_RELOC_SH_IMMS10:
531     case BFD_RELOC_SH_IMMS10BY2:
532     case BFD_RELOC_SH_IMMS10BY4:
533     case BFD_RELOC_SH_IMMS10BY8:
534     case BFD_RELOC_SH_IMMS16:
535     case BFD_RELOC_SH_IMMU16:
536     case BFD_RELOC_SH_PT_16:
537     case BFD_RELOC_SH_GOT_LOW16:
538     case BFD_RELOC_SH_GOT_MEDLOW16:
539     case BFD_RELOC_SH_GOT_MEDHI16:
540     case BFD_RELOC_SH_GOT_HI16:
541     case BFD_RELOC_SH_GOT10BY4:
542     case BFD_RELOC_SH_GOT10BY8:
543     case BFD_RELOC_SH_GOTPLT_LOW16:
544     case BFD_RELOC_SH_GOTPLT_MEDLOW16:
545     case BFD_RELOC_SH_GOTPLT_MEDHI16:
546     case BFD_RELOC_SH_GOTPLT_HI16:
547     case BFD_RELOC_SH_GOTPLT10BY4:
548     case BFD_RELOC_SH_GOTPLT10BY8:
549     case BFD_RELOC_SH_GOTOFF_LOW16:
550     case BFD_RELOC_SH_GOTOFF_MEDLOW16:
551     case BFD_RELOC_SH_GOTOFF_MEDHI16:
552     case BFD_RELOC_SH_GOTOFF_HI16:
553     case BFD_RELOC_SH_GOTPC_LOW16:
554     case BFD_RELOC_SH_GOTPC_MEDLOW16:
555     case BFD_RELOC_SH_GOTPC_MEDHI16:
556     case BFD_RELOC_SH_GOTPC_HI16:
557     case BFD_RELOC_SH_PLT_LOW16:
558     case BFD_RELOC_SH_PLT_MEDLOW16:
559     case BFD_RELOC_SH_PLT_MEDHI16:
560     case BFD_RELOC_SH_PLT_HI16:
561       rel->addend = fixP->fx_addnumber + fixP->fx_offset;
562       return 1;
563 
564     case BFD_RELOC_SH_IMMS6BY32:
565       /* This must be resolved in assembly; we do not support it as a
566 	 reloc in an object file.  */
567       as_bad_where (fixP->fx_file, fixP->fx_line,
568 		    _("This operand must be constant at assembly time"));
569       break;
570 
571       /* There are valid cases where we get here for other than SHmedia
572 	 relocs, so don't make a BAD_CASE out of this.  */
573     default:
574       ;
575     }
576 
577   return 0;
578 }
579 
580 /* Hook called from md_apply_fix3 in tc-sh.c.  */
581 
582 static void
583 shmedia_md_apply_fix3 (fixS *fixP, valueT *valp)
584 {
585   offsetT val = *valp;
586   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
587   unsigned long insn
588     = target_big_endian ? bfd_getb32 (buf) : bfd_getl32 (buf);
589   bfd_reloc_code_real_type orig_fx_r_type = fixP->fx_r_type;
590 
591   /* Change a 64-bit pc-relative reloc into the correct type, just like
592      tc-sh.c:md_apply_fix.  */
593   if (fixP->fx_pcrel)
594     {
595       switch (orig_fx_r_type)
596 	{
597 	case BFD_RELOC_64:
598 	case BFD_RELOC_SH_IMM_LOW16:
599 	case BFD_RELOC_SH_IMM_MEDLOW16:
600 	case BFD_RELOC_SH_IMM_MEDHI16:
601 	case BFD_RELOC_SH_IMM_HI16:
602 	  /* Because write.c calls MD_PCREL_FROM_SECTION twice, we need to
603 	     undo one of the adjustments, if the relocation is not
604 	     actually for a symbol within the same segment (which we
605 	     cannot check, because we're not called from md_apply_fix3, so
606 	     we have to keep the reloc).  FIXME: This is a bug in
607 	     write.c:fixup_segment affecting most targets that change
608 	     ordinary relocs to pcrel relocs in md_apply_fix.  */
609 	  fixP->fx_offset
610 	    = *valp + SHMEDIA_MD_PCREL_FROM_FIX (fixP);
611 	  break;
612 
613 	case BFD_RELOC_SH_PLT_LOW16:
614 	case BFD_RELOC_SH_PLT_MEDLOW16:
615 	case BFD_RELOC_SH_PLT_MEDHI16:
616 	case BFD_RELOC_SH_PLT_HI16:
617 	case BFD_RELOC_SH_GOTPC_LOW16:
618 	case BFD_RELOC_SH_GOTPC_MEDLOW16:
619 	case BFD_RELOC_SH_GOTPC_MEDHI16:
620 	case BFD_RELOC_SH_GOTPC_HI16:
621 	  *valp = 0;
622 	  return;
623 
624 	default:
625 	  ;
626 	}
627 
628       /* We might need to change some relocs into the corresponding
629 	 PC-relative one.  */
630       switch (orig_fx_r_type)
631 	{
632 	case BFD_RELOC_64:
633 	  fixP->fx_r_type = BFD_RELOC_64_PCREL;
634 	  break;
635 
636 	case BFD_RELOC_SH_IMM_LOW16:
637 	  fixP->fx_r_type = BFD_RELOC_SH_IMM_LOW16_PCREL;
638 	  break;
639 
640 	case BFD_RELOC_SH_IMM_MEDLOW16:
641 	  fixP->fx_r_type = BFD_RELOC_SH_IMM_MEDLOW16_PCREL;
642 	  break;
643 
644 	case BFD_RELOC_SH_IMM_MEDHI16:
645 	  fixP->fx_r_type = BFD_RELOC_SH_IMM_MEDHI16_PCREL;
646 	  break;
647 
648 	case BFD_RELOC_SH_IMM_HI16:
649 	  fixP->fx_r_type = BFD_RELOC_SH_IMM_HI16_PCREL;
650 	  break;
651 
652 	case SHMEDIA_BFD_RELOC_PT:
653 	  /* This is how we see a difference between PT and PTA when not
654 	     expanding (in which case we handle it in
655 	     shmedia_md_convert_frag).  Note that we don't see a
656 	     difference after the reloc is emitted.  */
657 	  fixP->fx_r_type = BFD_RELOC_SH_PT_16;
658 	  break;
659 
660 	case BFD_RELOC_SH_PT_16:
661 	  /* This tells us there was a PTA or PTB insn explicitly
662 	     expressed as such (not as PT).  We "or" in a 1 into the
663 	     lowest bit in the (unused) destination field to tell the
664 	     linker that it should check the right ISA type of the
665 	     destination and not just change a PTA to PTB (if necessary).  */
666 	  md_number_to_chars (buf, insn | (1 << 10), 4);
667 	  break;
668 
669 	case BFD_RELOC_64_PCREL:
670 	case BFD_RELOC_SH_IMM_LOW16_PCREL:
671 	case BFD_RELOC_SH_IMM_MEDLOW16_PCREL:
672 	case BFD_RELOC_SH_IMM_MEDHI16_PCREL:
673 	case BFD_RELOC_SH_IMM_HI16_PCREL:
674 	  /* Already handled.  */
675 	  break;
676 
677 	default:
678 	  /* Everything else that changes into a pc-relative relocation is
679 	     an error.  */
680 	  as_bad_where (fixP->fx_file, fixP->fx_line,
681 			_("Invalid operand expression"));
682 	  break;
683 	}
684 
685       return;
686     }
687 
688   /* If an expression looked like it was PC-relative, but was completely
689      resolvable, we end up here with the result only in *VALP, and no
690      relocation will be emitted.  */
691   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
692     {
693       /* Emit error for an out-of-range value.  */
694       shmedia_check_limits (valp, fixP->fx_r_type, fixP);
695 
696       switch (fixP->fx_r_type)
697 	{
698 	case BFD_RELOC_SH_IMM_LOW16:
699 	  md_number_to_chars (buf, insn | ((val & 65535) << 10), 4);
700 	  break;
701 
702 	case BFD_RELOC_SH_IMM_MEDLOW16:
703 	  md_number_to_chars (buf,
704 			      insn
705 			      | ((valueT) (val & ((valueT) 65535 << 16))
706 				 >> (16 - 10)), 4);
707 	  break;
708 
709 	case BFD_RELOC_SH_IMM_MEDHI16:
710 	  md_number_to_chars (buf,
711 			      insn
712 			      | ((valueT) (val & ((valueT) 65535 << 32))
713 				 >> (32 - 10)), 4);
714 	  break;
715 
716 	case BFD_RELOC_SH_IMM_HI16:
717 	  md_number_to_chars (buf,
718 			      insn
719 			      | ((valueT) (val & ((valueT) 65535 << 48))
720 				 >> (48 - 10)), 4);
721 	  break;
722 
723 	case BFD_RELOC_SH_IMMS16:
724 	case BFD_RELOC_SH_IMMU16:
725 	  md_number_to_chars (buf, insn | ((val & 65535) << 10), 4);
726 	  break;
727 
728 	case BFD_RELOC_SH_IMMS10:
729 	  md_number_to_chars (buf, insn | ((val & 0x3ff) << 10), 4);
730 	  break;
731 
732 	case BFD_RELOC_SH_IMMS10BY2:
733 	  md_number_to_chars (buf,
734 			      insn | ((val & (0x3ff << 1)) << (10 - 1)), 4);
735 	  break;
736 
737 	case BFD_RELOC_SH_IMMS10BY4:
738 	  md_number_to_chars (buf,
739 			      insn | ((val & (0x3ff << 2)) << (10 - 2)), 4);
740 	  break;
741 
742 	case BFD_RELOC_SH_SHMEDIA_CODE:
743 	  /* We just ignore and remove this one for the moment.  FIXME:
744 	     Use it when implementing relaxing.  */
745 	  break;
746 
747 	case BFD_RELOC_64:
748 	  md_number_to_chars (buf, val, 8);
749 	  break;
750 
751 	case SHMEDIA_BFD_RELOC_PT:
752 	  /* Change a PT to PTB if the operand turned out to be SHcompact.
753 	     The basic opcode specified with PT is equivalent to PTA.  */
754 	  if ((val & 1) == 0)
755 	    insn |= SHMEDIA_PTB_BIT;
756 	  /* Fall through.  */
757 
758 	case BFD_RELOC_SH_PT_16:
759 	  if (! sh64_expand || sh_relax)
760 	    {
761 	      /* Check if the operand of a PTA or PTB was for the "wrong"
762 		 ISA.  A PT had an incoming fixup of SHMEDIA_BFD_RELOC_PT,
763 		 which we have changed to the right type above.  */
764 	      if (orig_fx_r_type != SHMEDIA_BFD_RELOC_PT)
765 		{
766 		  if ((insn & SHMEDIA_PTB_BIT) != 0 && (val & 1) != 0)
767 		    as_bad_where (fixP->fx_file, fixP->fx_line,
768 				  _("PTB operand is a SHmedia symbol"));
769 		  else if ((insn & SHMEDIA_PTB_BIT) == 0 && (val & 1) == 0)
770 		    as_bad_where (fixP->fx_file, fixP->fx_line,
771 				  _("PTA operand is a SHcompact symbol"));
772 		}
773 
774 	      md_number_to_chars (buf,
775 				  insn | ((val & (0xffff << 2))
776 					  << (10 - 2)),
777 				  4);
778 	      break;
779 	    }
780 	  /* Fall through.  */
781 
782 	default:
783 	  /* This isn't a BAD_CASE, because presumably we can get here
784 	     from unexpected operands.  Since we don't handle them, make
785 	     them syntax errors.  */
786 	  as_bad_where (fixP->fx_file, fixP->fx_line,
787 			_("invalid expression in operand"));
788 	}
789       fixP->fx_done = 1;
790     }
791 }
792 
793 /* Hook called from md_convert_frag in tc-sh.c.  */
794 
795 static void
796 shmedia_md_convert_frag (bfd *output_bfd ATTRIBUTE_UNUSED,
797 			 segT seg ATTRIBUTE_UNUSED, fragS *fragP,
798 			 bfd_boolean final)
799 {
800   /* Pointer to first byte in variable-sized part of the frag.	*/
801   char *var_partp;
802 
803   /* Pointer to first opcode byte in frag.  */
804   char *opcodep;
805 
806   /* Pointer to frag of opcode.  */
807   fragS *opc_fragP = fragP->tc_frag_data.opc_frag;
808 
809   /* Size in bytes of variable-sized part of frag.  */
810   int var_part_size = 0;
811 
812   /* This is part of *fragP.  It contains all information about addresses
813      and offsets to varying parts.  */
814   symbolS *symbolP = fragP->fr_symbol;
815 
816   bfd_boolean reloc_needed
817     = (! final
818        || sh_relax
819        || symbolP == NULL
820        || ! S_IS_DEFINED (symbolP)
821        || S_IS_EXTERN (symbolP)
822        || S_IS_WEAK (symbolP)
823        || (S_GET_SEGMENT (fragP->fr_symbol) != absolute_section
824 	   && S_GET_SEGMENT (fragP->fr_symbol) != seg));
825 
826   bfd_reloc_code_real_type reloctype = BFD_RELOC_NONE;
827 
828   unsigned long var_part_offset;
829 
830   /* Where, in file space, does addr point?  */
831   bfd_vma target_address;
832   bfd_vma opcode_address;
833 
834   /* What was the insn?  */
835   unsigned long insn;
836   know (fragP->fr_type == rs_machine_dependent);
837 
838   var_part_offset = fragP->fr_fix;
839   var_partp = fragP->fr_literal + var_part_offset;
840   opcodep = fragP->fr_opcode;
841 
842   insn = target_big_endian ? bfd_getb32 (opcodep) : bfd_getl32 (opcodep);
843 
844   target_address
845     = ((symbolP && final && ! sh_relax ? S_GET_VALUE (symbolP) : 0)
846        + fragP->fr_offset);
847 
848   /* The opcode that would be extended is the last four "fixed" bytes.  */
849   opcode_address = fragP->fr_address + fragP->fr_fix - 4;
850 
851   switch (fragP->fr_subtype)
852     {
853     case C (SH64PCREL16PT_64, SH64PCREL16):
854     case C (SH64PCREL16PT_32, SH64PCREL16):
855       /* We can get a PT to a relaxed SHcompact address if it is in the
856 	 same section; a mixed-ISA section.  Change the opcode to PTB if
857 	 so.  */
858       if ((target_address & 1) == 0)
859 	insn |= SHMEDIA_PTB_BIT;
860       /* Fall through.  */
861 
862     case C (SH64PCREL16_32, SH64PCREL16):
863     case C (SH64PCREL16_64, SH64PCREL16):
864       /* Check that a PTA or PTB points to the right type of target.  We
865 	 can get here for a SHcompact target if we are in a mixed-ISA
866 	 section.  */
867       if (((target_address & 1) == 0) && ((insn & SHMEDIA_PTB_BIT) == 0))
868 	as_bad_where (fragP->fr_file, fragP->fr_line,
869 		      _("PTA operand is a SHcompact symbol"));
870       if (((target_address & 1) != 0) && ((insn & SHMEDIA_PTB_BIT) != 0))
871 	as_bad_where (fragP->fr_file, fragP->fr_line,
872 		      _("PTB operand is a SHmedia symbol"));
873 
874       /* When relaxing, we do not output the address in the insn, but
875 	 instead a 1 into the low bit.  This matches what the linker
876 	 expects to find for a BFD_RELOC_SH_PT_16 reloc, when it checks
877 	 correctness for PTA/PTB insn; used when the target address is
878 	 unknown (which is not the case here).  */
879       md_number_to_chars (opcodep,
880 			  insn
881 			  | (((sh_relax
882 			       ? 1 : ((target_address - opcode_address) / 4))
883 			      & ((1 << 16) - 1)) << 10),
884 			  4);
885 
886       /* Note that we do not emit info that this was originally a PT since
887 	 we have resolved to which one of PTA or PTB it will be.  */
888       if (sh_relax)
889 	fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
890 		 fragP->fr_symbol, fragP->fr_offset, 1, BFD_RELOC_SH_PT_16);
891       var_part_size = 0;
892       break;
893 
894     case C (SH64PCREL16_32, SH64PCRELPLT):
895     case C (SH64PCREL16PT_32, SH64PCRELPLT):
896       reloctype = BFD_RELOC_32_PLT_PCREL;
897       reloc_needed = 1;
898       /* Fall through */
899 
900     case C (SH64PCREL16_32, SH64PCREL32):
901     case C (SH64PCREL16_64, SH64PCREL32):
902     case C (SH64PCREL16PT_32, SH64PCREL32):
903     case C (SH64PCREL16PT_64, SH64PCREL32):
904       /* In the fixed bit, put in a MOVI.  */
905       md_number_to_chars (opcodep,
906 			  SHMEDIA_MOVI_OPC
907 			  | (SHMEDIA_TEMP_REG << 4)
908 			  | ((((reloc_needed
909 				? 0 : (target_address - (opcode_address + 8))
910 				) >> 16) & 65535) << 10),
911 			  4);
912 
913       /* Fill in a SHORI for the low part.  */
914       md_number_to_chars (var_partp,
915 			  SHMEDIA_SHORI_OPC
916 			  | (SHMEDIA_TEMP_REG << 4)
917 			  | (((reloc_needed
918 			       ? 0 : (target_address - (opcode_address + 8)))
919 			      & 65535) << 10),
920 			  4);
921 
922       /* End with a "PTREL R25,TRd".  */
923       md_number_to_chars (var_partp + 4,
924 			  SHMEDIA_PTREL_OPC | (insn & SHMEDIA_LIKELY_BIT)
925 			  | (SHMEDIA_TEMP_REG << 10)
926 			  | (insn & (7 << 4)),
927 			  4);
928 
929       /* We need relocs only if the target symbol was undefined or if
930 	 we're relaxing.  */
931       if (reloc_needed)
932 	{
933 	  fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
934 		   fragP->fr_symbol, fragP->fr_offset - 8, 1,
935 		   reloctype == BFD_RELOC_32_PLT_PCREL
936 		   ? BFD_RELOC_SH_PLT_MEDLOW16
937 		   : BFD_RELOC_SH_IMM_MEDLOW16_PCREL);
938 	  fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
939 		   fragP->fr_offset - 4, 1,
940 		   reloctype == BFD_RELOC_32_PLT_PCREL
941 		   ? BFD_RELOC_SH_PLT_LOW16
942 		   : BFD_RELOC_SH_IMM_LOW16_PCREL);
943 	}
944 
945       var_part_size = 8;
946       break;
947 
948     case C (SH64PCREL16_64, SH64PCREL48):
949     case C (SH64PCREL16PT_64, SH64PCREL48):
950       /* In the fixed bit, put in a MOVI.  */
951       md_number_to_chars (opcodep,
952 			  SHMEDIA_MOVI_OPC
953 			  | (SHMEDIA_TEMP_REG << 4)
954 			  | ((((reloc_needed
955 				? 0 : (target_address - (opcode_address + 12))
956 				) >> 32) & 65535) << 10),
957 			  4);
958 
959       /* The first SHORI, for the medium part.  */
960       md_number_to_chars (var_partp,
961 			  SHMEDIA_SHORI_OPC
962 			  | (SHMEDIA_TEMP_REG << 4)
963 			  | ((((reloc_needed
964 				? 0 : (target_address - (opcode_address + 12))
965 				) >> 16) & 65535) << 10),
966 			  4);
967 
968       /* Fill in a SHORI for the low part.  */
969       md_number_to_chars (var_partp + 4,
970 			  SHMEDIA_SHORI_OPC
971 			  | (SHMEDIA_TEMP_REG << 4)
972 			  | (((reloc_needed
973 			       ? 0 : (target_address - (opcode_address + 12)))
974 			      & 65535) << 10),
975 			  4);
976 
977       /* End with a "PTREL R25,TRd".  */
978       md_number_to_chars (var_partp + 8,
979 			  SHMEDIA_PTREL_OPC | (insn & SHMEDIA_LIKELY_BIT)
980 			  | (SHMEDIA_TEMP_REG << 10)
981 			  | (insn & (7 << 4)),
982 			  4);
983 
984       /* We need relocs only if the target symbol was undefined or if
985 	 we're relaxing.  */
986       if (reloc_needed)
987 	{
988 	  fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
989 		   fragP->fr_symbol, fragP->fr_offset - 12, 1,
990 		   reloctype == BFD_RELOC_32_PLT_PCREL
991 		   ? BFD_RELOC_SH_PLT_MEDHI16
992 		   : BFD_RELOC_SH_IMM_MEDHI16_PCREL);
993 	  fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
994 		   fragP->fr_offset - 8, 1,
995 		   reloctype == BFD_RELOC_32_PLT_PCREL
996 		   ? BFD_RELOC_SH_PLT_MEDLOW16
997 		   : BFD_RELOC_SH_IMM_MEDLOW16_PCREL);
998 	  fix_new (fragP, var_partp - fragP->fr_literal + 4, 4, fragP->fr_symbol,
999 		   fragP->fr_offset - 4, 1,
1000 		   reloctype == BFD_RELOC_32_PLT_PCREL
1001 		   ? BFD_RELOC_SH_PLT_LOW16
1002 		   : BFD_RELOC_SH_IMM_LOW16_PCREL);
1003 	}
1004 
1005       var_part_size = 12;
1006       break;
1007 
1008     case C (SH64PCREL16_64, SH64PCRELPLT):
1009     case C (SH64PCREL16PT_64, SH64PCRELPLT):
1010       reloctype = BFD_RELOC_32_PLT_PCREL;
1011       reloc_needed = 1;
1012       /* Fall through */
1013 
1014     case C (SH64PCREL16_64, SH64PCREL64):
1015     case C (SH64PCREL16PT_64, SH64PCREL64):
1016       /* In the fixed bit, put in a MOVI.  */
1017       md_number_to_chars (opcodep,
1018 			  SHMEDIA_MOVI_OPC
1019 			  | (SHMEDIA_TEMP_REG << 4)
1020 			  | ((((reloc_needed
1021 				? 0 : (target_address - (opcode_address + 16))
1022 				) >> 48) & 65535) << 10),
1023 			  4);
1024 
1025       /* The first SHORI, for the medium-high part.  */
1026       md_number_to_chars (var_partp,
1027 			  SHMEDIA_SHORI_OPC
1028 			  | (SHMEDIA_TEMP_REG << 4)
1029 			  | ((((reloc_needed
1030 				? 0 : (target_address - (opcode_address + 16))
1031 				) >> 32) & 65535) << 10),
1032 			  4);
1033 
1034       /* A SHORI, for the medium-low part.  */
1035       md_number_to_chars (var_partp + 4,
1036 			  SHMEDIA_SHORI_OPC
1037 			  | (SHMEDIA_TEMP_REG << 4)
1038 			  | ((((reloc_needed
1039 				? 0 : (target_address - (opcode_address + 16))
1040 				) >> 16) & 65535) << 10),
1041 			  4);
1042 
1043       /* Fill in a SHORI for the low part.  */
1044       md_number_to_chars (var_partp + 8,
1045 			  SHMEDIA_SHORI_OPC
1046 			  | (SHMEDIA_TEMP_REG << 4)
1047 			  | (((reloc_needed
1048 			       ? 0 : (target_address - (opcode_address + 16)))
1049 			      & 65535) << 10),
1050 			  4);
1051 
1052       /* End with a "PTREL R25,TRd".  */
1053       md_number_to_chars (var_partp + 12,
1054 			  SHMEDIA_PTREL_OPC | (insn & SHMEDIA_LIKELY_BIT)
1055 			  | (SHMEDIA_TEMP_REG << 10)
1056 			  | (insn & (7 << 4)),
1057 			  4);
1058 
1059       /* We need relocs only if the target symbol was undefined or if
1060 	 we're relaxing.  */
1061       if (reloc_needed)
1062 	{
1063 	  fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1064 		   fragP->fr_symbol, fragP->fr_offset - 16, 1,
1065 		   reloctype == BFD_RELOC_32_PLT_PCREL
1066 		   ? BFD_RELOC_SH_PLT_HI16
1067 		   : BFD_RELOC_SH_IMM_HI16_PCREL);
1068 	  fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
1069 		   fragP->fr_offset - 12, 1,
1070 		   reloctype == BFD_RELOC_32_PLT_PCREL
1071 		   ? BFD_RELOC_SH_PLT_MEDHI16
1072 		   : BFD_RELOC_SH_IMM_MEDHI16_PCREL);
1073 	  fix_new (fragP, var_partp - fragP->fr_literal + 4, 4, fragP->fr_symbol,
1074 		   fragP->fr_offset - 8, 1,
1075 		   reloctype == BFD_RELOC_32_PLT_PCREL
1076 		   ? BFD_RELOC_SH_PLT_MEDLOW16
1077 		   : BFD_RELOC_SH_IMM_MEDLOW16_PCREL);
1078 	  fix_new (fragP, var_partp - fragP->fr_literal + 8, 4, fragP->fr_symbol,
1079 		   fragP->fr_offset - 4, 1,
1080 		   reloctype == BFD_RELOC_32_PLT_PCREL
1081 		   ? BFD_RELOC_SH_PLT_LOW16
1082 		   : BFD_RELOC_SH_IMM_LOW16_PCREL);
1083 	}
1084 
1085       var_part_size = 16;
1086       break;
1087 
1088     case C (MOVI_IMM_64, MOVI_GOTOFF):
1089       reloctype = BFD_RELOC_32_GOTOFF;
1090       reloc_needed = 1;
1091       /* Fall through.  */
1092 
1093     case C (MOVI_IMM_64, UNDEF_MOVI):
1094     case C (MOVI_IMM_64, MOVI_64):
1095       {
1096 	/* We only get here for undefined symbols, so we can simplify
1097 	   handling compared to those above; we have 0 in the parts that
1098 	   will be filled with the symbol parts.  */
1099 
1100 	int reg = (insn >> 4) & 0x3f;
1101 
1102 	/* In the fixed bit, put in a MOVI.  */
1103 	md_number_to_chars (opcodep, SHMEDIA_MOVI_OPC | (reg << 4), 4);
1104 	fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1105 		 fragP->fr_symbol, fragP->fr_offset, 0,
1106 		 reloctype == BFD_RELOC_NONE
1107 		 ? BFD_RELOC_SH_IMM_HI16
1108 		 : reloctype == BFD_RELOC_32_GOTOFF
1109 		 ? BFD_RELOC_SH_GOTOFF_HI16
1110 		 : (abort (), BFD_RELOC_SH_IMM_HI16));
1111 
1112 	/* The first SHORI, for the medium-high part.  */
1113 	md_number_to_chars (var_partp, SHMEDIA_SHORI_OPC | (reg << 4), 4);
1114 	fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
1115 		 fragP->fr_offset, 0,
1116 		 reloctype == BFD_RELOC_NONE
1117 		 ? BFD_RELOC_SH_IMM_MEDHI16
1118 		 : reloctype == BFD_RELOC_32_GOTOFF
1119 		 ? BFD_RELOC_SH_GOTOFF_MEDHI16
1120 		 : (abort (), BFD_RELOC_SH_IMM_MEDHI16));
1121 
1122 	/* A SHORI, for the medium-low part.  */
1123 	md_number_to_chars (var_partp + 4,
1124 			    SHMEDIA_SHORI_OPC | (reg << 4), 4);
1125 	fix_new (fragP, var_partp - fragP->fr_literal + 4, 4, fragP->fr_symbol,
1126 		 fragP->fr_offset, 0,
1127 		 reloctype == BFD_RELOC_NONE
1128 		 ? BFD_RELOC_SH_IMM_MEDLOW16
1129 		 : reloctype == BFD_RELOC_32_GOTOFF
1130 		 ? BFD_RELOC_SH_GOTOFF_MEDLOW16
1131 		 : (abort (), BFD_RELOC_SH_IMM_MEDLOW16));
1132 
1133 	/* Fill in a SHORI for the low part.  */
1134 	md_number_to_chars (var_partp + 8,
1135 			    SHMEDIA_SHORI_OPC | (reg << 4), 4);
1136 	fix_new (fragP, var_partp - fragP->fr_literal + 8, 4, fragP->fr_symbol,
1137 		 fragP->fr_offset, 0,
1138 		 reloctype == BFD_RELOC_NONE
1139 		 ? BFD_RELOC_SH_IMM_LOW16
1140 		 : reloctype == BFD_RELOC_32_GOTOFF
1141 		 ? BFD_RELOC_SH_GOTOFF_LOW16
1142 		 : (abort (), BFD_RELOC_SH_IMM_LOW16));
1143 
1144 	var_part_size = 12;
1145 	break;
1146       }
1147 
1148     case C (MOVI_IMM_32, MOVI_GOTOFF):
1149       reloctype = BFD_RELOC_32_GOTOFF;
1150       reloc_needed = 1;
1151       /* Fall through.  */
1152 
1153     case C (MOVI_IMM_32, UNDEF_MOVI):
1154     case C (MOVI_IMM_32, MOVI_32):
1155       {
1156 	/* Note that we only get here for undefined symbols.  */
1157 
1158 	int reg = (insn >> 4) & 0x3f;
1159 
1160 	/* A MOVI, for the high part.  */
1161 	md_number_to_chars (opcodep, SHMEDIA_MOVI_OPC | (reg << 4), 4);
1162 	fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1163 		 fragP->fr_symbol, fragP->fr_offset, 0,
1164 		 reloctype == BFD_RELOC_NONE
1165 		 ? BFD_RELOC_SH_IMM_MEDLOW16
1166 		 : reloctype == BFD_RELOC_32_GOTOFF
1167 		 ? BFD_RELOC_SH_GOTOFF_MEDLOW16
1168 		 : reloctype == BFD_RELOC_SH_GOTPC
1169 		 ? BFD_RELOC_SH_GOTPC_MEDLOW16
1170 		 : reloctype == BFD_RELOC_32_PLT_PCREL
1171 		 ? BFD_RELOC_SH_PLT_MEDLOW16
1172 		 : (abort (), BFD_RELOC_SH_IMM_MEDLOW16));
1173 
1174 	/* Fill in a SHORI for the low part.  */
1175 	md_number_to_chars (var_partp,
1176 			    SHMEDIA_SHORI_OPC | (reg << 4), 4);
1177 	fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
1178 		 fragP->fr_offset, 0,
1179 		 reloctype == BFD_RELOC_NONE
1180 		 ? BFD_RELOC_SH_IMM_LOW16
1181 		 : reloctype == BFD_RELOC_32_GOTOFF
1182 		 ? BFD_RELOC_SH_GOTOFF_LOW16
1183 		 : reloctype == BFD_RELOC_SH_GOTPC
1184 		 ? BFD_RELOC_SH_GOTPC_LOW16
1185 		 : reloctype == BFD_RELOC_32_PLT_PCREL
1186 		 ? BFD_RELOC_SH_PLT_LOW16
1187 		 : (abort (), BFD_RELOC_SH_IMM_LOW16));
1188 
1189 	var_part_size = 4;
1190 	break;
1191       }
1192 
1193     case C (MOVI_IMM_32_PCREL, MOVI_16):
1194     case C (MOVI_IMM_64_PCREL, MOVI_16):
1195       md_number_to_chars (opcodep,
1196 			  insn
1197 			  | (((reloc_needed
1198 			       ? 0 : (target_address - opcode_address))
1199 			      & 65535) << 10),
1200 			  4);
1201       if (reloc_needed)
1202 	fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1203 		 fragP->fr_symbol, fragP->fr_offset, 1,
1204 		 BFD_RELOC_SH_IMM_LOW16_PCREL);
1205       var_part_size = 0;
1206       break;
1207 
1208     case C (MOVI_IMM_32, MOVI_16):
1209     case C (MOVI_IMM_64, MOVI_16):
1210       md_number_to_chars (opcodep,
1211 			  insn
1212 			  | (((reloc_needed ? 0 : target_address)
1213 			      & 65535) << 10),
1214 			  4);
1215       if (reloc_needed)
1216 	abort ();
1217       var_part_size = 0;
1218       break;
1219 
1220     case C (MOVI_IMM_32_PCREL, MOVI_PLT):
1221       reloctype = BFD_RELOC_32_PLT_PCREL;
1222       goto movi_imm_32_pcrel_reloc_needed;
1223 
1224     case C (MOVI_IMM_32_PCREL, MOVI_GOTPC):
1225       reloctype = BFD_RELOC_SH_GOTPC;
1226       /* Fall through.  */
1227 
1228     movi_imm_32_pcrel_reloc_needed:
1229       reloc_needed = 1;
1230       /* Fall through.  */
1231 
1232     case C (MOVI_IMM_32_PCREL, MOVI_32):
1233     case C (MOVI_IMM_64_PCREL, MOVI_32):
1234       {
1235 	int reg = (insn >> 4) & 0x3f;
1236 
1237 	md_number_to_chars (opcodep,
1238 			    insn
1239 			    | (((((reloc_needed
1240 				   ? 0 : (target_address - opcode_address)))
1241 				>> 16) & 65535) << 10), 4);
1242 
1243 	/* A SHORI, for the low part.  */
1244 	md_number_to_chars (var_partp,
1245 			    SHMEDIA_SHORI_OPC
1246 			    | (reg << 4)
1247 			    | (((reloc_needed
1248 				 ? 0 : (target_address - opcode_address))
1249 				& 65535) << 10), 4);
1250 	if (reloc_needed)
1251 	  {
1252 	    fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1253 		     fragP->fr_symbol, fragP->fr_offset, 1,
1254 		     reloctype == BFD_RELOC_NONE
1255 		     ? BFD_RELOC_SH_IMM_MEDLOW16_PCREL
1256 		     : reloctype == BFD_RELOC_SH_GOTPC
1257 		     ? BFD_RELOC_SH_GOTPC_MEDLOW16
1258 		     : reloctype == BFD_RELOC_32_PLT_PCREL
1259 		     ? BFD_RELOC_SH_PLT_MEDLOW16
1260 		     : (abort (), BFD_RELOC_SH_IMM_MEDLOW16_PCREL));
1261 	    fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
1262 		     fragP->fr_offset + 4, 1,
1263 		     reloctype == BFD_RELOC_NONE
1264 		     ? BFD_RELOC_SH_IMM_LOW16_PCREL
1265 		     : reloctype == BFD_RELOC_SH_GOTPC
1266 		     ? BFD_RELOC_SH_GOTPC_LOW16
1267 		     : reloctype == BFD_RELOC_32_PLT_PCREL
1268 		     ? BFD_RELOC_SH_PLT_LOW16
1269 		     : (abort (), BFD_RELOC_SH_IMM_LOW16_PCREL));
1270 	  }
1271 	var_part_size = 4;
1272       }
1273       break;
1274 
1275     case C (MOVI_IMM_32_PCREL, MOVI_48):
1276     case C (MOVI_IMM_64_PCREL, MOVI_48):
1277       {
1278 	int reg = (insn >> 4) & 0x3f;
1279 
1280 	md_number_to_chars (opcodep,
1281 			    insn
1282 			    | (((((reloc_needed
1283 				   ? 0 : (target_address - opcode_address)))
1284 				>> 32) & 65535) << 10), 4);
1285 
1286 	/* A SHORI, for the medium part.  */
1287 	md_number_to_chars (var_partp,
1288 			    SHMEDIA_SHORI_OPC
1289 			    | (reg << 4)
1290 			    | ((((reloc_needed
1291 				  ? 0 : (target_address - opcode_address))
1292 				 >> 16) & 65535) << 10), 4);
1293 
1294 	/* A SHORI, for the low part.  */
1295 	md_number_to_chars (var_partp + 4,
1296 			    SHMEDIA_SHORI_OPC
1297 			    | (reg << 4)
1298 			    | (((reloc_needed
1299 				 ? 0 : (target_address - opcode_address))
1300 				& 65535) << 10), 4);
1301 	if (reloc_needed)
1302 	  {
1303 	    fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1304 		     fragP->fr_symbol, fragP->fr_offset, 1,
1305 		     BFD_RELOC_SH_IMM_MEDHI16_PCREL);
1306 	    fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
1307 		     fragP->fr_offset + 4, 1, BFD_RELOC_SH_IMM_MEDLOW16_PCREL);
1308 	    fix_new (fragP, var_partp - fragP->fr_literal + 4, 4, fragP->fr_symbol,
1309 		     fragP->fr_offset + 8, 1, BFD_RELOC_SH_IMM_LOW16_PCREL);
1310 	  }
1311 	var_part_size = 8;
1312       }
1313       break;
1314 
1315     case C (MOVI_IMM_64_PCREL, MOVI_PLT):
1316       reloctype = BFD_RELOC_32_PLT_PCREL;
1317       goto movi_imm_64_pcrel_reloc_needed;
1318 
1319     case C (MOVI_IMM_64_PCREL, MOVI_GOTPC):
1320       reloctype = BFD_RELOC_SH_GOTPC;
1321       /* Fall through.  */
1322 
1323     movi_imm_64_pcrel_reloc_needed:
1324       reloc_needed = 1;
1325       /* Fall through.  */
1326 
1327     case C (MOVI_IMM_32_PCREL, MOVI_64):
1328     case C (MOVI_IMM_64_PCREL, MOVI_64):
1329       {
1330 	int reg = (insn >> 4) & 0x3f;
1331 
1332 	md_number_to_chars (opcodep,
1333 			    insn
1334 			    | (((((reloc_needed
1335 				   ? 0 : (target_address - opcode_address)))
1336 				>> 48) & 65535) << 10), 4);
1337 
1338 	/* A SHORI, for the medium-high part.  */
1339 	md_number_to_chars (var_partp,
1340 			    SHMEDIA_SHORI_OPC
1341 			    | (reg << 4)
1342 			    | ((((reloc_needed
1343 				  ? 0 : (target_address - opcode_address))
1344 				 >> 32) & 65535) << 10), 4);
1345 
1346 	/* A SHORI, for the medium-low part.  */
1347 	md_number_to_chars (var_partp + 4,
1348 			    SHMEDIA_SHORI_OPC
1349 			    | (reg << 4)
1350 			    | ((((reloc_needed
1351 				  ? 0 : (target_address - opcode_address))
1352 				 >> 16) & 65535) << 10), 4);
1353 
1354 	/* A SHORI, for the low part.  */
1355 	md_number_to_chars (var_partp + 8,
1356 			    SHMEDIA_SHORI_OPC
1357 			    | (reg << 4)
1358 			    | (((reloc_needed
1359 				 ? 0 : (target_address - opcode_address))
1360 				& 65535) << 10), 4);
1361 	if (reloc_needed)
1362 	  {
1363 	    fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1364 		     fragP->fr_symbol, fragP->fr_offset, 1,
1365 		     reloctype == BFD_RELOC_NONE
1366 		     ? BFD_RELOC_SH_IMM_HI16_PCREL
1367 		     : reloctype == BFD_RELOC_SH_GOTPC
1368 		     ? BFD_RELOC_SH_GOTPC_HI16
1369 		     : reloctype == BFD_RELOC_32_PLT_PCREL
1370 		     ? BFD_RELOC_SH_PLT_HI16
1371 		     : (abort (), BFD_RELOC_SH_IMM_HI16_PCREL));
1372 	    fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
1373 		     fragP->fr_offset + 4, 1,
1374 		     reloctype == BFD_RELOC_NONE
1375 		     ? BFD_RELOC_SH_IMM_MEDHI16_PCREL
1376 		     : reloctype == BFD_RELOC_SH_GOTPC
1377 		     ? BFD_RELOC_SH_GOTPC_MEDHI16
1378 		     : reloctype == BFD_RELOC_32_PLT_PCREL
1379 		     ? BFD_RELOC_SH_PLT_MEDHI16
1380 		     : (abort (), BFD_RELOC_SH_IMM_MEDHI16_PCREL));
1381 	    fix_new (fragP, var_partp - fragP->fr_literal + 4, 4,
1382 		     fragP->fr_symbol,
1383 		     fragP->fr_offset + 8, 1,
1384 		     reloctype == BFD_RELOC_NONE
1385 		     ? BFD_RELOC_SH_IMM_MEDLOW16_PCREL
1386 		     : reloctype == BFD_RELOC_SH_GOTPC
1387 		     ? BFD_RELOC_SH_GOTPC_MEDLOW16
1388 		     : reloctype == BFD_RELOC_32_PLT_PCREL
1389 		     ? BFD_RELOC_SH_PLT_MEDLOW16
1390 		     : (abort (), BFD_RELOC_SH_IMM_MEDLOW16_PCREL));
1391 	    fix_new (fragP, var_partp - fragP->fr_literal + 8, 4,
1392 		     fragP->fr_symbol,
1393 		     fragP->fr_offset + 12, 1,
1394 		     reloctype == BFD_RELOC_NONE
1395 		     ? BFD_RELOC_SH_IMM_LOW16_PCREL
1396 		     : reloctype == BFD_RELOC_SH_GOTPC
1397 		     ? BFD_RELOC_SH_GOTPC_LOW16
1398 		     : reloctype == BFD_RELOC_32_PLT_PCREL
1399 		     ? BFD_RELOC_SH_PLT_LOW16
1400 		     : (abort (), BFD_RELOC_SH_IMM_LOW16_PCREL));
1401 	  }
1402 	var_part_size = 12;
1403       }
1404       break;
1405 
1406     default:
1407       BAD_CASE (fragP->fr_subtype);
1408     }
1409 
1410   fragP->fr_fix += var_part_size;
1411   fragP->fr_var = 0;
1412 }
1413 
1414 /* Mask NUMBER (originating from a signed number) corresponding to the HOW
1415    reloc.  */
1416 
1417 static unsigned long
1418 shmedia_mask_number (unsigned long number, bfd_reloc_code_real_type how)
1419 {
1420   switch (how)
1421     {
1422     case BFD_RELOC_SH_IMMU5:
1423       number &= (1 << 5) - 1;
1424       break;
1425 
1426     case BFD_RELOC_SH_IMMS6:
1427     case BFD_RELOC_SH_IMMU6:
1428       number &= (1 << 6) - 1;
1429       break;
1430 
1431     case BFD_RELOC_SH_IMMS6BY32:
1432       number = (number & ((1 << (6 + 5)) - 1)) >> 5;
1433       break;
1434 
1435     case BFD_RELOC_SH_IMMS10:
1436       number &= (1 << 10) - 1;
1437       break;
1438 
1439     case BFD_RELOC_SH_IMMS10BY2:
1440       number = (number & ((1 << (10 + 1)) - 1)) >> 1;
1441       break;
1442 
1443     case BFD_RELOC_SH_IMMS10BY4:
1444       number = (number & ((1 << (10 + 2)) - 1)) >> 2;
1445       break;
1446 
1447     case BFD_RELOC_SH_IMMS10BY8:
1448       number = (number & ((1 << (10 + 3)) - 1)) >> 3;
1449       break;
1450 
1451     case BFD_RELOC_SH_IMMS16:
1452     case BFD_RELOC_SH_IMMU16:
1453       number &= (1 << 16) - 1;
1454       break;
1455 
1456     default:
1457       BAD_CASE (how);
1458     }
1459 
1460   return number;
1461 }
1462 
1463 /* Emit errors for values out-of-range, using as_bad_where if FRAGP is
1464    non-NULL, as_bad otherwise.  */
1465 
1466 static void
1467 shmedia_check_limits (offsetT *valp, bfd_reloc_code_real_type reloc,
1468 		      fixS *fixp)
1469 {
1470   offsetT val = *valp;
1471 
1472   char *msg = NULL;
1473 
1474   switch (reloc)
1475     {
1476     case BFD_RELOC_SH_IMMU5:
1477       if (val < 0 || val > (1 << 5) - 1)
1478 	msg = _("invalid operand, not a 5-bit unsigned value: %d");
1479       break;
1480 
1481     case BFD_RELOC_SH_IMMS6:
1482       if (val < -(1 << 5) || val > (1 << 5) - 1)
1483 	msg = _("invalid operand, not a 6-bit signed value: %d");
1484       break;
1485 
1486     case BFD_RELOC_SH_IMMU6:
1487       if (val < 0 || val > (1 << 6) - 1)
1488 	msg = _("invalid operand, not a 6-bit unsigned value: %d");
1489       break;
1490 
1491     case BFD_RELOC_SH_IMMS6BY32:
1492       if (val < -(1 << 10) || val > (1 << 10) - 1)
1493 	msg = _("invalid operand, not a 11-bit signed value: %d");
1494       else if (val & 31)
1495 	msg = _("invalid operand, not a multiple of 32: %d");
1496       break;
1497 
1498     case BFD_RELOC_SH_IMMS10:
1499       if (val < -(1 << 9) || val > (1 << 9) - 1)
1500 	msg = _("invalid operand, not a 10-bit signed value: %d");
1501       break;
1502 
1503     case BFD_RELOC_SH_IMMS10BY2:
1504       if (val < -(1 << 10) || val > (1 << 10) - 1)
1505 	msg = _("invalid operand, not a 11-bit signed value: %d");
1506       else if (val & 1)
1507 	msg = _("invalid operand, not an even value: %d");
1508       break;
1509 
1510     case BFD_RELOC_SH_IMMS10BY4:
1511       if (val < -(1 << 11) || val > (1 << 11) - 1)
1512 	msg = _("invalid operand, not a 12-bit signed value: %d");
1513       else if (val & 3)
1514 	msg = _("invalid operand, not a multiple of 4: %d");
1515       break;
1516 
1517     case BFD_RELOC_SH_IMMS10BY8:
1518       if (val < -(1 << 12) || val > (1 << 12) - 1)
1519 	msg = _("invalid operand, not a 13-bit signed value: %d");
1520       else if (val & 7)
1521 	msg = _("invalid operand, not a multiple of 8: %d");
1522       break;
1523 
1524     case BFD_RELOC_SH_IMMS16:
1525       if (val < -(1 << 15) || val > (1 << 15) - 1)
1526 	msg = _("invalid operand, not a 16-bit signed value: %d");
1527       break;
1528 
1529     case BFD_RELOC_SH_IMMU16:
1530       if (val < 0 || val > (1 << 16) - 1)
1531 	msg = _("invalid operand, not an 16-bit unsigned value: %d");
1532       break;
1533 
1534     case BFD_RELOC_SH_PT_16:
1535     case SHMEDIA_BFD_RELOC_PT:
1536       if (val < -(1 << 15) * 4 || val > ((1 << 15) - 1) * 4 + 1)
1537 	msg = _("operand out of range for PT, PTA and PTB");
1538       else if ((val % 4) != 0 && ((val - 1) % 4) != 0)
1539 	msg = _("operand not a multiple of 4 for PT, PTA or PTB: %d");
1540       break;
1541 
1542       /* These have no limits; they take a 16-bit slice of a 32- or 64-bit
1543 	 number.  */
1544     case BFD_RELOC_SH_IMM_HI16:
1545     case BFD_RELOC_SH_IMM_MEDHI16:
1546     case BFD_RELOC_SH_IMM_MEDLOW16:
1547     case BFD_RELOC_SH_IMM_LOW16:
1548     case BFD_RELOC_SH_IMM_HI16_PCREL:
1549     case BFD_RELOC_SH_IMM_MEDHI16_PCREL:
1550     case BFD_RELOC_SH_IMM_MEDLOW16_PCREL:
1551     case BFD_RELOC_SH_IMM_LOW16_PCREL:
1552 
1553     case BFD_RELOC_SH_SHMEDIA_CODE:
1554       break;
1555 
1556       /* This one has limits out of our reach.  */
1557     case BFD_RELOC_64:
1558       break;
1559 
1560     default:
1561       BAD_CASE (reloc);
1562     }
1563 
1564   if (msg)
1565     {
1566       if (fixp)
1567 	as_bad_where (fixp->fx_file, fixp->fx_line, msg, val);
1568       else
1569 	as_bad (msg, val);
1570     }
1571 }
1572 
1573 /* Handle an immediate operand by checking limits and noting it for later
1574    evaluation if not computable yet, and return a bitfield suitable to
1575    "or" into the opcode (non-zero if the value was a constant number).  */
1576 
1577 static unsigned long
1578 shmedia_immediate_op (char *where, shmedia_operand_info *op, int pcrel,
1579 		      bfd_reloc_code_real_type how)
1580 {
1581   unsigned long retval = 0;
1582 
1583   /* If this is not an absolute number, make it a fixup.  A constant in
1584      place of a pc-relative operand also needs a fixup.  */
1585   if (op->immediate.X_op != O_constant || pcrel)
1586     fix_new_exp (frag_now,
1587 		 where - frag_now->fr_literal,
1588 		 4,
1589 		 &op->immediate,
1590 		 pcrel,
1591 		 how);
1592   else
1593     {
1594       /* Check that the number is within limits as represented by the
1595 	 reloc, and return the number.  */
1596       shmedia_check_limits (&op->immediate.X_add_number, how, NULL);
1597 
1598       retval
1599 	= shmedia_mask_number ((unsigned long) op->immediate.X_add_number,
1600 			       how);
1601     }
1602 
1603   return retval << 10;
1604 }
1605 
1606 /* Try and parse a register name case-insensitively, return the number of
1607    chars consumed.  */
1608 
1609 static int
1610 shmedia_parse_reg (char *src, int *mode, int *reg, shmedia_arg_type argtype)
1611 {
1612   int l0 = TOLOWER (src[0]);
1613   int l1 = l0 ? TOLOWER (src[1]) : 0;
1614 
1615   if (l0 == 'r')
1616     {
1617       if (src[1] >= '1' && src[1] <= '5')
1618 	{
1619 	  if (src[2] >= '0' && src[2] <= '9'
1620 	      && ! IDENT_CHAR ((unsigned char) src[3]))
1621 	    {
1622 	      *mode = A_GREG_M;
1623 	      *reg = 10 * (src[1] - '0') + src[2] - '0';
1624 	      return 3;
1625 	    }
1626 	}
1627 
1628       if (src[1] == '6')
1629 	{
1630 	  if (src[2] >= '0' && src[2] <= '3'
1631 	      && ! IDENT_CHAR ((unsigned char) src[3]))
1632 	    {
1633 	      *mode = A_GREG_M;
1634 	      *reg = 60 + src[2] - '0';
1635 	      return 3;
1636 	    }
1637 	}
1638 
1639       if (src[1] >= '0' && src[1] <= '9'
1640 	  && ! IDENT_CHAR ((unsigned char) src[2]))
1641 	{
1642 	  *mode = A_GREG_M;
1643 	  *reg = (src[1] - '0');
1644 	  return 2;
1645 	}
1646     }
1647 
1648   if (l0 == 't' && l1 == 'r')
1649     {
1650       if (src[2] >= '0' && src[2] <= '7'
1651 	  && ! IDENT_CHAR ((unsigned char) src[3]))
1652 	{
1653 	  *mode = A_TREG_B;
1654 	  *reg = (src[2] - '0');
1655 	  return 3;
1656 	}
1657     }
1658 
1659   if (l0 == 'f' && l1 == 'r')
1660     {
1661       if (src[2] >= '1' && src[2] <= '5')
1662 	{
1663 	  if (src[3] >= '0' && src[3] <= '9'
1664 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1665 	    {
1666 	      *mode = A_FREG_G;
1667 	      *reg = 10 * (src[2] - '0') + src[3] - '0';
1668 	      return 4;
1669 	    }
1670 	}
1671       if (src[2] == '6')
1672 	{
1673 	  if (src[3] >= '0' && src[3] <= '3'
1674 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1675 	    {
1676 	      *mode = A_FREG_G;
1677 	      *reg = 60 + src[3] - '0';
1678 	      return 4;
1679 	    }
1680 	}
1681       if (src[2] >= '0' && src[2] <= '9'
1682 	  && ! IDENT_CHAR ((unsigned char) src[3]))
1683 	{
1684 	  *mode = A_FREG_G;
1685 	  *reg = (src[2] - '0');
1686 	  return 3;
1687 	}
1688     }
1689 
1690   if (l0 == 'f' && l1 == 'v')
1691     {
1692       if (src[2] >= '1' && src[2] <= '5')
1693 	{
1694 	  if (src[3] >= '0' && src[3] <= '9'
1695 	      && ((10 * (src[2] - '0') + src[3] - '0') % 4) == 0
1696 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1697 	    {
1698 	      *mode = A_FVREG_G;
1699 	      *reg = 10 * (src[2] - '0') + src[3] - '0';
1700 	      return 4;
1701 	    }
1702 	}
1703       if (src[2] == '6')
1704 	{
1705 	  if (src[3] == '0'
1706 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1707 	    {
1708 	      *mode = A_FVREG_G;
1709 	      *reg = 60 + src[3] - '0';
1710 	      return 4;
1711 	    }
1712 	}
1713       if (src[2] >= '0' && src[2] <= '9'
1714 	  && ((src[2] - '0') % 4) == 0
1715 	  && ! IDENT_CHAR ((unsigned char) src[3]))
1716 	{
1717 	  *mode = A_FVREG_G;
1718 	  *reg = (src[2] - '0');
1719 	  return 3;
1720 	}
1721     }
1722 
1723   if (l0 == 'd' && l1 == 'r')
1724     {
1725       if (src[2] >= '1' && src[2] <= '5')
1726 	{
1727 	  if (src[3] >= '0' && src[3] <= '9'
1728 	      && ((src[3] - '0') % 2) == 0
1729 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1730 	    {
1731 	      *mode = A_DREG_G;
1732 	      *reg = 10 * (src[2] - '0') + src[3] - '0';
1733 	      return 4;
1734 	    }
1735 	}
1736 
1737       if (src[2] == '6')
1738 	{
1739 	  if ((src[3] == '0' || src[3] == '2')
1740 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1741 	    {
1742 	      *mode = A_DREG_G;
1743 	      *reg = 60 + src[3] - '0';
1744 	      return 4;
1745 	    }
1746 	}
1747 
1748       if (src[2] >= '0' && src[2] <= '9'
1749 	  && ((src[2] - '0') % 2) == 0
1750 	  && ! IDENT_CHAR ((unsigned char) src[3]))
1751 	{
1752 	  *mode = A_DREG_G;
1753 	  *reg = (src[2] - '0');
1754 	  return 3;
1755 	}
1756     }
1757 
1758   if (l0 == 'f' && l1 == 'p')
1759     {
1760       if (src[2] >= '1' && src[2] <= '5')
1761 	{
1762 	  if (src[3] >= '0' && src[3] <= '9'
1763 	      && ((src[3] - '0') % 2) == 0
1764 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1765 	    {
1766 	      *mode = A_FPREG_G;
1767 	      *reg = 10 * (src[2] - '0') + src[3] - '0';
1768 	      return 4;
1769 	    }
1770 	}
1771 
1772       if (src[2] == '6')
1773 	{
1774 	  if ((src[3] == '0' || src[3] == '2')
1775 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1776 	    {
1777 	      *mode = A_FPREG_G;
1778 	      *reg = 60 + src[3] - '0';
1779 	      return 4;
1780 	    }
1781 	}
1782 
1783       if (src[2] >= '0' && src[2] <= '9'
1784 	  && ((src[2] - '0') % 2) == 0
1785 	  && ! IDENT_CHAR ((unsigned char) src[3]))
1786 	{
1787 	  *mode = A_FPREG_G;
1788 	  *reg = (src[2] - '0');
1789 	  return 3;
1790 	}
1791     }
1792 
1793   if (l0 == 'm' && strncasecmp (src, "mtrx", 4) == 0)
1794     {
1795       if (src[4] == '0' && ! IDENT_CHAR ((unsigned char) src[5]))
1796 	{
1797 	  *mode = A_FMREG_G;
1798 	  *reg = 0;
1799 	  return 5;
1800 	}
1801 
1802       if (src[4] == '1' && src[5] == '6'
1803 	  && ! IDENT_CHAR ((unsigned char) src[6]))
1804 	{
1805 	  *mode = A_FMREG_G;
1806 	  *reg = 16;
1807 	  return 6;
1808 	}
1809 
1810       if (src[4] == '3' && src[5] == '2'
1811 	  && ! IDENT_CHAR ((unsigned char) src[6]))
1812 	{
1813 	  *mode = A_FMREG_G;
1814 	  *reg = 32;
1815 	  return 6;
1816 	}
1817 
1818       if (src[4] == '4' && src[5] == '8'
1819 	  && ! IDENT_CHAR ((unsigned char) src[6]))
1820 	{
1821 	  *mode = A_FMREG_G;
1822 	  *reg = 48;
1823 	  return 6;
1824 	}
1825     }
1826 
1827   if (l0 == 'c' && l1 == 'r')
1828     {
1829       if (src[2] >= '1' && src[2] <= '5')
1830 	{
1831 	  if (src[3] >= '0' && src[3] <= '9'
1832 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1833 	    {
1834 	      *mode = A_CREG_K;
1835 	      *reg = 10 * (src[2] - '0') + src[3] - '0';
1836 	      return 4;
1837 	    }
1838 	}
1839       if (src[2] == '6')
1840 	{
1841 	  if (src[3] >= '0' && src[3] <= '3'
1842 	      && ! IDENT_CHAR ((unsigned char) src[4]))
1843 	    {
1844 	      *mode = A_CREG_K;
1845 	      *reg = 60 + src[3] - '0';
1846 	      return 4;
1847 	    }
1848 	}
1849       if (src[2] >= '0' && src[2] <= '9'
1850 	  && ! IDENT_CHAR ((unsigned char) src[3]))
1851 	{
1852 	  *mode = A_CREG_K;
1853 	  *reg = (src[2] - '0');
1854 	  return 3;
1855 	}
1856     }
1857 
1858   /* We either have an error, a symbol or a control register by predefined
1859      name.  To keep things simple but still fast for normal cases, we do
1860      linear search in the (not to big) table of predefined control
1861      registers.  We only do this when we *expect* a control register.
1862      Those instructions should be rare enough that linear searching is ok.
1863      Or just read them into a hash-table in shmedia_md_begin.  Since they
1864      cannot be specified in the same place of symbol operands, don't add
1865      them there to the *main* symbol table as being in "reg_section".  */
1866   if (argtype == A_CREG_J || argtype == A_CREG_K)
1867     {
1868       const shmedia_creg_info *cregp;
1869       int len = 0;
1870 
1871       for (cregp = shmedia_creg_table; cregp->name != NULL; cregp++)
1872 	{
1873 	  len = strlen (cregp->name);
1874 	  if (strncasecmp (cregp->name, src, len) == 0
1875 	      && ! IDENT_CHAR (src[len]))
1876 	    break;
1877 	}
1878 
1879       if (cregp->name != NULL)
1880 	{
1881 	  *mode = A_CREG_K;
1882 	  *reg = cregp->cregno;
1883 	  return len;
1884 	}
1885     }
1886 
1887   return 0;
1888 }
1889 
1890 /* Called from md_estimate_size_before_relax in tc-sh.c  */
1891 
1892 static int
1893 shmedia_md_estimate_size_before_relax (fragS *fragP,
1894 				       segT segment_type ATTRIBUTE_UNUSED)
1895 {
1896   int old_fr_fix;
1897   expressionS *exp;
1898 
1899   /* For ELF, we can't relax externally visible symbols; see tc-i386.c.  */
1900   bfd_boolean sym_relaxable
1901     = (fragP->fr_symbol
1902        && S_GET_SEGMENT (fragP->fr_symbol) == segment_type
1903        && ! S_IS_EXTERNAL (fragP->fr_symbol)
1904        && ! S_IS_WEAK (fragP->fr_symbol));
1905 
1906   old_fr_fix = fragP->fr_fix;
1907 
1908   switch (fragP->fr_subtype)
1909     {
1910     case C (SH64PCREL16_32, UNDEF_SH64PCREL):
1911     case C (SH64PCREL16PT_32, UNDEF_SH64PCREL):
1912       /* Used to be to somewhere which was unknown.  */
1913       if (sym_relaxable)
1914 	{
1915 	  int what = GET_WHAT (fragP->fr_subtype);
1916 
1917 	  /* In this segment, so head for shortest.  */
1918 	  fragP->fr_subtype = C (what, SH64PCREL16);
1919 	}
1920       else
1921 	{
1922 	  int what = GET_WHAT (fragP->fr_subtype);
1923 	  /* We know the abs value, but we don't know where we will be
1924 	     linked, so we must make it the longest.  Presumably we could
1925 	     switch to a non-pcrel representation, but having absolute
1926 	     values in PT operands should be rare enough not to be worth
1927 	     adding that code.  */
1928 	  fragP->fr_subtype = C (what, SH64PCREL32);
1929 	}
1930       fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
1931       break;
1932 
1933     case C (SH64PCREL16_64, UNDEF_SH64PCREL):
1934     case C (SH64PCREL16PT_64, UNDEF_SH64PCREL):
1935       /* Used to be to somewhere which was unknown.  */
1936       if (sym_relaxable)
1937 	{
1938 	  int what = GET_WHAT (fragP->fr_subtype);
1939 
1940 	  /* In this segment, so head for shortest.  */
1941 	  fragP->fr_subtype = C (what, SH64PCREL16);
1942 	}
1943       else
1944 	{
1945 	  int what = GET_WHAT (fragP->fr_subtype);
1946 	  /* We know the abs value, but we don't know where we will be
1947 	     linked, so we must make it the longest.  Presumably we could
1948 	     switch to a non-pcrel representation, but having absolute
1949 	     values in PT operands should be rare enough not to be worth
1950 	     adding that code.  */
1951 	  fragP->fr_subtype = C (what, SH64PCREL64);
1952 	}
1953       fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
1954       break;
1955 
1956     case C (MOVI_IMM_64, UNDEF_MOVI):
1957     case C (MOVI_IMM_32, UNDEF_MOVI):
1958       exp = NULL;
1959 
1960       /* Look inside the "symbol".  If we find a PC-relative expression,
1961 	 change this to a PC-relative, relaxable expression.  */
1962       if (fragP->fr_symbol != NULL
1963 	  && (exp = symbol_get_value_expression (fragP->fr_symbol)) != NULL
1964 	  && exp->X_op == O_subtract
1965 	  && exp->X_op_symbol != NULL
1966 	  && S_GET_SEGMENT (exp->X_op_symbol) == segment_type)
1967 	{
1968 	  int what = GET_WHAT (fragP->fr_subtype);
1969 	  int what_high = what == MOVI_IMM_32 ? MOVI_32 : MOVI_64;
1970 	  expressionS *opexp
1971 	    = symbol_get_value_expression (exp->X_op_symbol);
1972 	  expressionS *addexp
1973 	    = symbol_get_value_expression (exp->X_add_symbol);
1974 
1975 	  /* Change the MOVI expression to the "X" in "X - Y" and subtract
1976 	     Y:s offset to this location from X.  Note that we can only
1977 	     allow an Y which is offset from this frag.  */
1978 	  if (opexp != NULL
1979 	      && addexp != NULL
1980 	      && opexp->X_op == O_constant
1981 	      && fragP == symbol_get_frag (exp->X_op_symbol))
1982 	    {
1983 	      /* At this point, before relaxing, the add-number of opexp
1984 		 is the offset from the fr_fix part.  */
1985 	      fragP->fr_offset
1986 		= (exp->X_add_number
1987 		   - (opexp->X_add_number - (fragP->fr_fix - 4)));
1988 	      fragP->fr_symbol = exp->X_add_symbol;
1989 
1990 	      what = what == MOVI_IMM_32
1991 		? MOVI_IMM_32_PCREL : MOVI_IMM_64_PCREL;
1992 
1993 	      /* Check the "X" symbol to estimate the size of this
1994 		 PC-relative expression.  */
1995 	      if (S_GET_SEGMENT (exp->X_add_symbol) == segment_type
1996 		  && ! S_IS_EXTERNAL (exp->X_add_symbol)
1997 		  && ! S_IS_WEAK (exp->X_add_symbol))
1998 		fragP->fr_subtype = C (what, MOVI_16);
1999 	      else
2000 		fragP->fr_subtype = C (what, what_high);
2001 
2002 	      /* This is now a PC-relative expression, fit to be relaxed.  */
2003 	    }
2004 	  else
2005 	    fragP->fr_subtype = C (what, what_high);
2006 	}
2007       else if (fragP->fr_symbol == NULL
2008 	       || (S_GET_SEGMENT (fragP->fr_symbol) == absolute_section
2009 		   && exp->X_op == O_constant))
2010 	{
2011 	  unsigned long insn
2012 	    = (target_big_endian
2013 	       ? bfd_getb32 (fragP->fr_opcode)
2014 	       : bfd_getl32 (fragP->fr_opcode));
2015 	  offsetT one = (offsetT) 1;
2016 	  offsetT value = fragP->fr_offset
2017 	    + (fragP->fr_symbol == NULL ? 0 : S_GET_VALUE (fragP->fr_symbol));
2018 
2019 	  if (value >= ((offsetT) -1 << 15) && value < ((offsetT) 1 << 15))
2020 	    {
2021 	      /* Fits in 16-bit signed number.  */
2022 	      int what = GET_WHAT (fragP->fr_subtype);
2023 	      fragP->fr_subtype = C (what, MOVI_16);
2024 
2025 	      /* Just "or" in the value.  */
2026 	      md_number_to_chars (fragP->fr_opcode,
2027 				  insn | ((value & ((1 << 16) - 1)) << 10),
2028 				  4);
2029 	    }
2030 	  else if (value >= -(one << 31)
2031 		   && (value < (one << 31)
2032 		       || (sh64_abi == sh64_abi_32 && value < (one << 32))))
2033 	    {
2034 	      /* The value fits in a 32-bit signed number.  */
2035 	      int reg = (insn >> 4) & 0x3f;
2036 
2037 	      /* Just "or" in the high bits of the value, making the first
2038 		 MOVI.  */
2039 	      md_number_to_chars (fragP->fr_opcode,
2040 				  insn
2041 				  | (((value >> 16) & ((1 << 16) - 1)) << 10),
2042 				  4);
2043 
2044 	      /* Add a SHORI with the low bits.  Note that this insn lives
2045 		 in the variable fragment part.  */
2046 	      md_number_to_chars (fragP->fr_literal + old_fr_fix,
2047 				  SHMEDIA_SHORI_OPC
2048 				  | (reg << 4)
2049 				  | ((value & ((1 << 16) - 1)) << 10),
2050 				  4);
2051 
2052 	      /* We took a piece of the variable part.  */
2053 	      fragP->fr_fix += 4;
2054 	    }
2055 	  else if (GET_WHAT (fragP->fr_subtype) == MOVI_IMM_32)
2056 	    {
2057 	      /* Value out of range.  */
2058 	      as_bad_where (fragP->fr_file, fragP->fr_line,
2059 			    _("MOVI operand is not a 32-bit signed value: 0x%8x%08x"),
2060 			    ((unsigned int) (value >> 32)
2061 			     & (unsigned int) 0xffffffff),
2062 			    (unsigned int) value & (unsigned int) 0xffffffff);
2063 
2064 	      /* Must advance size, or we will get internal inconsistency
2065 		 and fall into an assert.  */
2066 	      fragP->fr_fix += 4;
2067 	    }
2068 	  /* Now we know we are allowed to expand to 48- and 64-bit values.  */
2069 	  else if (value >= -(one << 47) && value < (one << 47))
2070 	    {
2071 	      /* The value fits in a 48-bit signed number.  */
2072 	      int reg = (insn >> 4) & 0x3f;
2073 
2074 	      /* Just "or" in the high bits of the value, making the first
2075 		 MOVI.  */
2076 	      md_number_to_chars (fragP->fr_opcode,
2077 				  insn
2078 				  | (((value >> 32) & ((1 << 16) - 1)) << 10),
2079 				  4);
2080 
2081 	      /* Add a SHORI with the middle bits.  Note that this insn lives
2082 		 in the variable fragment part.  */
2083 	      md_number_to_chars (fragP->fr_literal + old_fr_fix,
2084 				  SHMEDIA_SHORI_OPC
2085 				  | (reg << 4)
2086 				  | (((value >> 16) & ((1 << 16) - 1)) << 10),
2087 				  4);
2088 
2089 	      /* Add a SHORI with the low bits.  */
2090 	      md_number_to_chars (fragP->fr_literal + old_fr_fix + 4,
2091 				  SHMEDIA_SHORI_OPC
2092 				  | (reg << 4)
2093 				  | ((value & ((1 << 16) - 1)) << 10),
2094 				  4);
2095 
2096 	      /* We took a piece of the variable part.  */
2097 	      fragP->fr_fix += 8;
2098 	    }
2099 	  else
2100 	    {
2101 	      /* A 64-bit number.  */
2102 	      int reg = (insn >> 4) & 0x3f;
2103 
2104 	      /* Just "or" in the high bits of the value, making the first
2105 		 MOVI.  */
2106 	      md_number_to_chars (fragP->fr_opcode,
2107 				  insn
2108 				  | (((value >> 48) & ((1 << 16) - 1)) << 10),
2109 				  4);
2110 
2111 	      /* Add a SHORI with the midhigh bits.  Note that this insn lives
2112 		 in the variable fragment part.  */
2113 	      md_number_to_chars (fragP->fr_literal + old_fr_fix,
2114 				  SHMEDIA_SHORI_OPC
2115 				  | (reg << 4)
2116 				  | (((value >> 32) & ((1 << 16) - 1)) << 10),
2117 				  4);
2118 
2119 	      /* Add a SHORI with the midlow bits.  */
2120 	      md_number_to_chars (fragP->fr_literal + old_fr_fix + 4,
2121 				  SHMEDIA_SHORI_OPC
2122 				  | (reg << 4)
2123 				  | (((value >> 16) & ((1 << 16) - 1)) << 10),
2124 				  4);
2125 
2126 	      /* Add a SHORI with the low bits.  */
2127 	      md_number_to_chars (fragP->fr_literal + old_fr_fix + 8,
2128 				  SHMEDIA_SHORI_OPC
2129 				  | (reg << 4)
2130 				  | ((value & ((1 << 16) - 1)) << 10), 4);
2131 	      /* We took all of the variable part.  */
2132 	      fragP->fr_fix += 12;
2133 	    }
2134 
2135 	  /* MOVI expansions that get here have not been converted to
2136 	     PC-relative frags, but instead expanded by
2137 	     md_number_to_chars or by calling shmedia_md_convert_frag
2138 	     with final == FALSE.  We must not have them around as
2139 	     frags anymore; symbols would be prematurely evaluated
2140 	     when relaxing.  We will not need to have md_convert_frag
2141 	     called again with them; any further handling is through
2142 	     the already emitted fixups.  */
2143 	  frag_wane (fragP);
2144 	  break;
2145 	}
2146       fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
2147       break;
2148 
2149       /* For relaxation states that remain unchanged, report the
2150          estimated length.  */
2151     case C (SH64PCREL16_32, SH64PCREL16):
2152     case C (SH64PCREL16PT_32, SH64PCREL16):
2153     case C (SH64PCREL16_32, SH64PCREL32):
2154     case C (SH64PCREL16PT_32, SH64PCREL32):
2155     case C (SH64PCREL16_32, SH64PCRELPLT):
2156     case C (SH64PCREL16PT_32, SH64PCRELPLT):
2157     case C (SH64PCREL16_64, SH64PCREL16):
2158     case C (SH64PCREL16PT_64, SH64PCREL16):
2159     case C (SH64PCREL16_64, SH64PCREL32):
2160     case C (SH64PCREL16PT_64, SH64PCREL32):
2161     case C (SH64PCREL16_64, SH64PCREL48):
2162     case C (SH64PCREL16PT_64, SH64PCREL48):
2163     case C (SH64PCREL16_64, SH64PCREL64):
2164     case C (SH64PCREL16PT_64, SH64PCREL64):
2165     case C (SH64PCREL16_64, SH64PCRELPLT):
2166     case C (SH64PCREL16PT_64, SH64PCRELPLT):
2167     case C (MOVI_IMM_32, MOVI_16):
2168     case C (MOVI_IMM_32, MOVI_32):
2169     case C (MOVI_IMM_32, MOVI_GOTOFF):
2170     case C (MOVI_IMM_32_PCREL, MOVI_16):
2171     case C (MOVI_IMM_32_PCREL, MOVI_32):
2172     case C (MOVI_IMM_32_PCREL, MOVI_PLT):
2173     case C (MOVI_IMM_32_PCREL, MOVI_GOTPC):
2174     case C (MOVI_IMM_64, MOVI_16):
2175     case C (MOVI_IMM_64, MOVI_32):
2176     case C (MOVI_IMM_64, MOVI_48):
2177     case C (MOVI_IMM_64, MOVI_64):
2178     case C (MOVI_IMM_64, MOVI_GOTOFF):
2179     case C (MOVI_IMM_64_PCREL, MOVI_16):
2180     case C (MOVI_IMM_64_PCREL, MOVI_32):
2181     case C (MOVI_IMM_64_PCREL, MOVI_48):
2182     case C (MOVI_IMM_64_PCREL, MOVI_64):
2183     case C (MOVI_IMM_64_PCREL, MOVI_PLT):
2184     case C (MOVI_IMM_64_PCREL, MOVI_GOTPC):
2185       fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
2186       break;
2187 
2188     default:
2189       abort ();
2190     }
2191 
2192   return fragP->fr_var + (fragP->fr_fix - old_fr_fix);
2193 }
2194 
2195 /* Parse an expression, SH64-style.  Copied from tc-sh.c, but with
2196    datatypes adjusted.  */
2197 
2198 static char *
2199 shmedia_parse_exp (char *s, shmedia_operand_info *op)
2200 {
2201   char *save;
2202   char *new;
2203 
2204   save = input_line_pointer;
2205   input_line_pointer = s;
2206   expression (&op->immediate);
2207   if (op->immediate.X_op == O_absent)
2208     as_bad (_("missing operand"));
2209   new = input_line_pointer;
2210   input_line_pointer = save;
2211   return new;
2212 }
2213 
2214 /* Parse an operand.  Store pointer to next character in *PTR.  */
2215 
2216 static void
2217 shmedia_get_operand (char **ptr, shmedia_operand_info *op,
2218 		     shmedia_arg_type argtype)
2219 {
2220   char *src = *ptr;
2221   int mode = -1;
2222   unsigned int len;
2223 
2224   len = shmedia_parse_reg (src, &mode, &(op->reg), argtype);
2225   if (len)
2226     {
2227       *ptr = src + len;
2228       op->type = mode;
2229     }
2230   else
2231     {
2232       /* Not a reg, so it must be a displacement.  */
2233       *ptr = shmedia_parse_exp (src, op);
2234       op->type = A_IMMM;
2235 
2236       /* This is just an initialization; shmedia_get_operands will change
2237 	 as needed.  */
2238       op->reloctype = BFD_RELOC_NONE;
2239     }
2240 }
2241 
2242 /* Parse the operands for this insn; return NULL if invalid, else return
2243    how much text was consumed.  */
2244 
2245 static char *
2246 shmedia_get_operands (shmedia_opcode_info *info, char *args,
2247 		      shmedia_operands_info *operands)
2248 {
2249   char *ptr = args;
2250   int i;
2251 
2252   if (*ptr == ' ')
2253     ptr++;
2254 
2255   for (i = 0; info->arg[i] != 0; i++)
2256     {
2257       memset (operands->operands + i, 0, sizeof (operands->operands[0]));
2258 
2259       /* No operand to get for these fields.  */
2260       if (info->arg[i] == A_REUSE_PREV)
2261 	continue;
2262 
2263       shmedia_get_operand (&ptr, &operands->operands[i], info->arg[i]);
2264 
2265       /* Check operands type match.  */
2266       switch (info->arg[i])
2267 	{
2268 	case A_GREG_M:
2269 	case A_GREG_N:
2270 	case A_GREG_D:
2271 	  if (operands->operands[i].type != A_GREG_M)
2272 	    return NULL;
2273 	  break;
2274 
2275 	case A_FREG_G:
2276 	case A_FREG_H:
2277 	case A_FREG_F:
2278 	  if (operands->operands[i].type != A_FREG_G)
2279 	    return NULL;
2280 	  break;
2281 
2282 	case A_FVREG_G:
2283 	case A_FVREG_H:
2284 	case A_FVREG_F:
2285 	  if (operands->operands[i].type != A_FVREG_G)
2286 	    return NULL;
2287 	  break;
2288 
2289 	case A_FMREG_G:
2290 	case A_FMREG_H:
2291 	case A_FMREG_F:
2292 	  if (operands->operands[i].type != A_FMREG_G)
2293 	    return NULL;
2294 	  break;
2295 
2296 	case A_FPREG_G:
2297 	case A_FPREG_H:
2298 	case A_FPREG_F:
2299 	  if (operands->operands[i].type != A_FPREG_G)
2300 	    return NULL;
2301 	  break;
2302 
2303 	case A_DREG_G:
2304 	case A_DREG_H:
2305 	case A_DREG_F:
2306 	  if (operands->operands[i].type != A_DREG_G)
2307 	    return NULL;
2308 	  break;
2309 
2310 	case A_TREG_A:
2311 	case A_TREG_B:
2312 	  if (operands->operands[i].type != A_TREG_B)
2313 	    return NULL;
2314 	  break;
2315 
2316 	case A_CREG_J:
2317 	case A_CREG_K:
2318 	  if (operands->operands[i].type != A_CREG_K)
2319 	    return NULL;
2320 	  break;
2321 
2322 	case A_IMMS16:
2323 	case A_IMMU16:
2324 	  /* Check for an expression that looks like S & 65535 or
2325 	     (S >> N) & 65535, where N = 0, 16, 32, 48.
2326 
2327 	     Get the S and put at operands->operands[i].immediate, and
2328 	     adjust operands->operands[i].reloctype.  */
2329 	  {
2330 	    expressionS *imm_expr = &operands->operands[i].immediate;
2331 	    expressionS *right_expr;
2332 
2333 	    if (operands->operands[i].type == A_IMMM
2334 		&& imm_expr->X_op == O_bit_and
2335 		&& imm_expr->X_op_symbol != NULL
2336 		&& ((right_expr
2337 		     = symbol_get_value_expression (imm_expr->X_op_symbol))
2338 		    ->X_op == O_constant)
2339 		&& right_expr->X_add_number == 0xffff)
2340 	      {
2341 		symbolS *inner = imm_expr->X_add_symbol;
2342 		bfd_reloc_code_real_type reloctype = BFD_RELOC_SH_IMM_LOW16;
2343 		expressionS *inner_expr
2344 		  = symbol_get_value_expression (inner);
2345 
2346 		if (inner_expr->X_op == O_right_shift)
2347 		  {
2348 		    expressionS *inner_right;
2349 
2350 		    if (inner_expr->X_op_symbol != NULL
2351 		      && ((inner_right
2352 			   = symbol_get_value_expression (inner_expr
2353 							  ->X_op_symbol))
2354 			  ->X_op == O_constant))
2355 		      {
2356 			offsetT addnum
2357 			  = inner_right->X_add_number;
2358 
2359 			if (addnum == 0 || addnum == 16 || addnum == 32
2360 			    || addnum == 48)
2361 			  {
2362 			    reloctype
2363 			      = (addnum == 0
2364 				 ? BFD_RELOC_SH_IMM_LOW16
2365 				 : (addnum == 16
2366 				    ? BFD_RELOC_SH_IMM_MEDLOW16
2367 				    : (addnum == 32
2368 				       ? BFD_RELOC_SH_IMM_MEDHI16
2369 				       : BFD_RELOC_SH_IMM_HI16)));
2370 
2371 			    inner = inner_expr->X_add_symbol;
2372 			    inner_expr = symbol_get_value_expression (inner);
2373 			  }
2374 		      }
2375 		  }
2376 
2377 		/* I'm not sure I understand the logic, but evidently the
2378 		   inner expression of a lone symbol is O_constant, with
2379 		   the actual symbol in expr_section.  For a constant, the
2380 		   section would be absolute_section.  For sym+offset,
2381 		   it's O_symbol as always.  See expr.c:make_expr_symbol,
2382 		   first statements.  */
2383 
2384 		if (inner_expr->X_op == O_constant
2385 		    && S_GET_SEGMENT (inner) != absolute_section)
2386 		  {
2387 		    operands->operands[i].immediate.X_op = O_symbol;
2388 		    operands->operands[i].immediate.X_add_symbol = inner;
2389 		    operands->operands[i].immediate.X_add_number = 0;
2390 		  }
2391 		else
2392 		  operands->operands[i].immediate
2393 		    = *symbol_get_value_expression (inner);
2394 
2395 		operands->operands[i].reloctype = reloctype;
2396 	      }
2397 	  }
2398 	  /* Fall through.  */
2399 	case A_IMMS6:
2400 	case A_IMMS6BY32:
2401 	case A_IMMS10:
2402 	case A_IMMS10BY1:
2403 	case A_IMMS10BY2:
2404 	case A_IMMS10BY4:
2405 	case A_IMMS10BY8:
2406 	case A_PCIMMS16BY4:
2407 	case A_PCIMMS16BY4_PT:
2408 	case A_IMMU5:
2409 	case A_IMMU6:
2410 	  if (operands->operands[i].type != A_IMMM)
2411 	    return NULL;
2412 
2413 	  if (sh_check_fixup (&operands->operands[i].immediate,
2414 			      &operands->operands[i].reloctype))
2415 	    {
2416 	      as_bad (_("invalid PIC reference"));
2417 	      return NULL;
2418 	    }
2419 
2420 	  break;
2421 
2422 	default:
2423 	  BAD_CASE (info->arg[i]);
2424 	}
2425 
2426       if (*ptr == ',' && info->arg[i + 1])
2427 	ptr++;
2428     }
2429   return ptr;
2430 }
2431 
2432 
2433 /* Find an opcode at the start of *STR_P in the hash table, and set
2434    *STR_P to the first character after the last one read.  */
2435 
2436 static shmedia_opcode_info *
2437 shmedia_find_cooked_opcode (char **str_p)
2438 {
2439   char *str = *str_p;
2440   char *op_start;
2441   char *op_end;
2442   char name[20];
2443   unsigned int nlen = 0;
2444 
2445   /* Drop leading whitespace.  */
2446   while (*str == ' ')
2447     str++;
2448 
2449   /* Find the op code end.  */
2450   for (op_start = op_end = str;
2451        *op_end
2452        && nlen < sizeof (name) - 1
2453        && ! is_end_of_line[(unsigned char) *op_end]
2454        && ! ISSPACE ((unsigned char) *op_end);
2455        op_end++)
2456     {
2457       unsigned char c = op_start[nlen];
2458 
2459       /* The machine independent code will convert CMP/EQ into cmp/EQ
2460 	 because it thinks the '/' is the end of the symbol.  Moreover,
2461 	 all but the first sub-insn is a parallel processing insn won't
2462 	 be capitalized.  Instead of hacking up the machine independent
2463 	 code, we just deal with it here.  */
2464       c = TOLOWER (c);
2465       name[nlen] = c;
2466       nlen++;
2467     }
2468 
2469   name[nlen] = 0;
2470   *str_p = op_end;
2471 
2472   if (nlen == 0)
2473     as_bad (_("can't find opcode"));
2474 
2475   return
2476     (shmedia_opcode_info *) hash_find (shmedia_opcode_hash_control, name);
2477 }
2478 
2479 /* Build up an instruction, including allocating the frag.  */
2480 
2481 static int
2482 shmedia_build_Mytes (shmedia_opcode_info *opcode,
2483 		     shmedia_operands_info *operands)
2484 {
2485   unsigned long insn = opcode->opcode_base;
2486   int i, j;
2487   char *insn_loc = frag_more (4);
2488 
2489   /* The parameter to dwarf2_emit_insn is actually the offset to the start
2490      of the insn from the fix piece of instruction that was emitted.
2491      Since we want .debug_line addresses to record (address | 1) for
2492      SHmedia insns, we get the wanted effect by taking one off the size,
2493      knowing it's a multiple of 4.  We count from the first fix piece of
2494      the insn.  There must be no frags changes (frag_more or frag_var)
2495      calls in-between the frag_more call we account for, and this
2496      dwarf2_emit_insn call.  */
2497   dwarf2_emit_insn (3);
2498 
2499   /* This is stored into any frag_var operand.  */
2500   sh64_last_insn_frag = frag_now;
2501 
2502   /* Loop over opcode info, emit an instruction.  */
2503   for (i = 0, j = 0; opcode->arg[i]; i++)
2504     {
2505       shmedia_arg_type argtype = opcode->arg[i];
2506       shmedia_operand_info *opjp = &operands->operands[j];
2507       switch (argtype)
2508 	{
2509 	case A_TREG_A:
2510 	case A_TREG_B:
2511 	case A_GREG_M:
2512 	case A_GREG_N:
2513 	case A_GREG_D:
2514 	case A_FREG_G:
2515 	case A_FREG_H:
2516 	case A_FREG_F:
2517 	case A_FVREG_G:
2518 	case A_FVREG_H:
2519 	case A_FVREG_F:
2520 	case A_FMREG_G:
2521 	case A_FMREG_H:
2522 	case A_FMREG_F:
2523 	case A_FPREG_G:
2524 	case A_FPREG_H:
2525 	case A_FPREG_F:
2526 	case A_DREG_G:
2527 	case A_DREG_H:
2528 	case A_DREG_F:
2529 	case A_CREG_J:
2530 	case A_CREG_K:
2531 	  /* Six-bit register fields.  They just get filled with the
2532 	     parsed register number.  */
2533 	  insn |= (opjp->reg << opcode->nibbles[i]);
2534 	  j++;
2535 	  break;
2536 
2537 	case A_REUSE_PREV:
2538 	  /* Copy the register for the previous operand to this position.  */
2539 	  insn |= (operands->operands[j - 1].reg << opcode->nibbles[i]);
2540 	  j++;
2541 	  break;
2542 
2543 	case A_IMMS6:
2544 	  insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2545 					BFD_RELOC_SH_IMMS6);
2546 	  j++;
2547 	  break;
2548 
2549 	case A_IMMS6BY32:
2550 	  insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2551 					BFD_RELOC_SH_IMMS6BY32);
2552 	  j++;
2553 	  break;
2554 
2555 	case A_IMMS10BY1:
2556 	case A_IMMS10:
2557 	  insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2558 					BFD_RELOC_SH_IMMS10);
2559 	  j++;
2560 	  break;
2561 
2562 	case A_IMMS10BY2:
2563 	  insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2564 					BFD_RELOC_SH_IMMS10BY2);
2565 	  j++;
2566 	  break;
2567 
2568 	case A_IMMS10BY4:
2569 	  if (opjp->reloctype == BFD_RELOC_NONE)
2570 	    insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2571 					  BFD_RELOC_SH_IMMS10BY4);
2572 	  else if (opjp->reloctype == BFD_RELOC_SH_GOTPLT32)
2573 	    insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2574 					  BFD_RELOC_SH_GOTPLT10BY4);
2575 	  else if (opjp->reloctype == BFD_RELOC_32_GOT_PCREL)
2576 	    insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2577 					  BFD_RELOC_SH_GOT10BY4);
2578 	  else
2579 	    as_bad (_("invalid PIC reference"));
2580 	  j++;
2581 	  break;
2582 
2583 	case A_IMMS10BY8:
2584 	  if (opjp->reloctype == BFD_RELOC_NONE)
2585 	    insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2586 					  BFD_RELOC_SH_IMMS10BY8);
2587 	  else if (opjp->reloctype == BFD_RELOC_SH_GOTPLT32)
2588 	    insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2589 					  BFD_RELOC_SH_GOTPLT10BY8);
2590 	  else if (opjp->reloctype == BFD_RELOC_32_GOT_PCREL)
2591 	    insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2592 					  BFD_RELOC_SH_GOT10BY8);
2593 	  else
2594 	    as_bad (_("invalid PIC reference"));
2595 	  j++;
2596 	  break;
2597 
2598 	case A_IMMS16:
2599 	  /* Sneak a peek if this is the MOVI insn.  If so, check if we
2600 	     should expand it.  */
2601 	  if (opjp->reloctype == BFD_RELOC_32_GOT_PCREL)
2602 	    opjp->reloctype = BFD_RELOC_SH_GOT_LOW16;
2603 	  else if (opjp->reloctype == BFD_RELOC_SH_GOTPLT32)
2604 	    opjp->reloctype = BFD_RELOC_SH_GOTPLT_LOW16;
2605 
2606 	  if ((opjp->reloctype == BFD_RELOC_NONE
2607 	       || opjp->reloctype == BFD_RELOC_32_GOTOFF
2608 	       || opjp->reloctype == BFD_RELOC_32_PLT_PCREL
2609 	       || opjp->reloctype == BFD_RELOC_SH_GOTPC)
2610 	      && opcode->opcode_base == SHMEDIA_MOVI_OPC
2611 	      && (opjp->immediate.X_op != O_constant
2612 		  || opjp->immediate.X_add_number < -32768
2613 		  || opjp->immediate.X_add_number > 32767)
2614 	      && (sh64_expand
2615 		  || opjp->reloctype == BFD_RELOC_32_GOTOFF
2616 		  || opjp->reloctype == BFD_RELOC_32_PLT_PCREL
2617 		  || opjp->reloctype == BFD_RELOC_SH_GOTPC))
2618 	    {
2619 	      int what = sh64_abi == sh64_abi_64 ? MOVI_IMM_64 : MOVI_IMM_32;
2620 	      offsetT max = sh64_abi == sh64_abi_64 ? MOVI_64 : MOVI_32;
2621 	      offsetT min = MOVI_16;
2622 	      offsetT init = UNDEF_MOVI;
2623 	      valueT addvalue
2624 		= opjp->immediate.X_op_symbol != NULL
2625 		? 0 : opjp->immediate.X_add_number;
2626 	      symbolS *sym
2627 		= opjp->immediate.X_op_symbol != NULL
2628 		? make_expr_symbol (&opjp->immediate)
2629 		: opjp->immediate.X_add_symbol;
2630 
2631 	      if (opjp->reloctype == BFD_RELOC_32_GOTOFF)
2632 		init = max = min = MOVI_GOTOFF;
2633 	      else if (opjp->reloctype == BFD_RELOC_32_PLT_PCREL)
2634 		{
2635 		  init = max = min = MOVI_PLT;
2636 		  what = (sh64_abi == sh64_abi_64
2637 			  ? MOVI_IMM_64_PCREL
2638 			  : MOVI_IMM_32_PCREL);
2639 		}
2640 	      else if (opjp->reloctype == BFD_RELOC_SH_GOTPC)
2641 		{
2642 		  init = max = min = MOVI_GOTPC;
2643 		  what = (sh64_abi == sh64_abi_64
2644 			  ? MOVI_IMM_64_PCREL
2645 			  : MOVI_IMM_32_PCREL);
2646 		}
2647 
2648 	      frag_var (rs_machine_dependent,
2649 			md_relax_table[C (what, max)].rlx_length,
2650 			md_relax_table[C (what, min)].rlx_length,
2651 			C (what, init), sym, addvalue, insn_loc);
2652 	    }
2653 	  else
2654 	    insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2655 					  (opjp->reloctype
2656 					   == BFD_RELOC_NONE)
2657 					  ? BFD_RELOC_SH_IMMS16
2658 					  : opjp->reloctype);
2659 	  j++;
2660 	  break;
2661 
2662 	case A_PCIMMS16BY4:
2663 	  {
2664 	    int what
2665 	      = ((sh64_abi == sh64_abi_64 && ! sh64_pt32)
2666 		 ? SH64PCREL16_64 : SH64PCREL16_32);
2667 	    offsetT max
2668 	      = ((sh64_abi == sh64_abi_64 && ! sh64_pt32)
2669 		 ? SH64PCREL64 : SH64PCREL32);
2670 	    offsetT min = SH64PCREL16;
2671 	    offsetT init = UNDEF_SH64PCREL;
2672 
2673 	    /* Don't allow complex expressions here.  */
2674 	    if (opjp->immediate.X_op_symbol != NULL)
2675 	      return 0;
2676 
2677 	    if (opjp->reloctype == BFD_RELOC_32_PLT_PCREL)
2678 	      init = max = min = SH64PCRELPLT;
2679 
2680 	    /* If we're not expanding, then just emit a fixup.  */
2681 	    if (sh64_expand || opjp->reloctype != BFD_RELOC_NONE)
2682 	      frag_var (rs_machine_dependent,
2683 			md_relax_table[C (what, max)].rlx_length,
2684 			md_relax_table[C (what, min)].rlx_length,
2685 			C (what, init),
2686 			opjp->immediate.X_add_symbol,
2687 			opjp->immediate.X_add_number,
2688 			insn_loc);
2689 	    else
2690 	      insn |= shmedia_immediate_op (insn_loc, opjp, 1,
2691 					    opjp->reloctype == BFD_RELOC_NONE
2692 					    ? BFD_RELOC_SH_PT_16
2693 					    : opjp->reloctype);
2694 
2695 	    j++;
2696 	    break;
2697 	  }
2698 
2699 	case A_PCIMMS16BY4_PT:
2700 	  {
2701 	    int what
2702 	      = ((sh64_abi == sh64_abi_64 && ! sh64_pt32)
2703 		 ? SH64PCREL16PT_64 : SH64PCREL16PT_32);
2704 	    offsetT max
2705 	      = ((sh64_abi == sh64_abi_64 && ! sh64_pt32)
2706 		 ? SH64PCREL64 : SH64PCREL32);
2707 	    offsetT min = SH64PCREL16;
2708 	    offsetT init = UNDEF_SH64PCREL;
2709 
2710 	    /* Don't allow complex expressions here.  */
2711 	    if (opjp->immediate.X_op_symbol != NULL)
2712 	      return 0;
2713 
2714 	    if (opjp->reloctype == BFD_RELOC_32_PLT_PCREL)
2715 	      init = max = min = SH64PCRELPLT;
2716 
2717 	    /* If we're not expanding, then just emit a fixup.  */
2718 	    if (sh64_expand || opjp->reloctype != BFD_RELOC_NONE)
2719 	      frag_var (rs_machine_dependent,
2720 			md_relax_table[C (what, max)].rlx_length,
2721 			md_relax_table[C (what, min)].rlx_length,
2722 			C (what, init),
2723 			opjp->immediate.X_add_symbol,
2724 			opjp->immediate.X_add_number,
2725 			insn_loc);
2726 	    else
2727 	      /* This reloc-type is just temporary, so we can distinguish
2728 		 PTA from PT.  It is changed in shmedia_md_apply_fix3 to
2729 		 BFD_RELOC_SH_PT_16.  */
2730 	      insn |= shmedia_immediate_op (insn_loc, opjp, 1,
2731 					    opjp->reloctype == BFD_RELOC_NONE
2732 					    ? SHMEDIA_BFD_RELOC_PT
2733 					    : opjp->reloctype);
2734 
2735 	    j++;
2736 	    break;
2737 	  }
2738 
2739 	case A_IMMU5:
2740 	  insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2741 					BFD_RELOC_SH_IMMU5);
2742 	  j++;
2743 	  break;
2744 
2745 	case A_IMMU6:
2746 	  insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2747 					BFD_RELOC_SH_IMMU6);
2748 	  j++;
2749 	  break;
2750 
2751 	case A_IMMU16:
2752 	  insn |= shmedia_immediate_op (insn_loc, opjp, 0,
2753 					(opjp->reloctype
2754 					 == BFD_RELOC_NONE)
2755 					? BFD_RELOC_SH_IMMU16
2756 					: opjp->reloctype);
2757 	  j++;
2758 	  break;
2759 
2760 	default:
2761 	  BAD_CASE (argtype);
2762 	}
2763     }
2764 
2765   md_number_to_chars (insn_loc, insn, 4);
2766   return 4;
2767 }
2768 
2769 /* Assemble a SHmedia instruction.  */
2770 
2771 static void
2772 shmedia_md_assemble (char *str)
2773 {
2774   char *op_end;
2775   shmedia_opcode_info *opcode;
2776   shmedia_operands_info operands;
2777   int size;
2778 
2779   opcode = shmedia_find_cooked_opcode (&str);
2780   op_end = str;
2781 
2782   if (opcode == NULL)
2783     {
2784       as_bad (_("unknown opcode"));
2785       return;
2786     }
2787 
2788   /* Start a SHmedia code region, if there has been pseudoinsns or similar
2789      seen since the last one.  */
2790   if (!seen_insn)
2791     {
2792       sh64_update_contents_mark (TRUE);
2793       sh64_set_contents_type (CRT_SH5_ISA32);
2794       seen_insn = TRUE;
2795     }
2796 
2797   op_end = shmedia_get_operands (opcode, op_end, &operands);
2798 
2799   if (op_end == NULL)
2800     {
2801       as_bad (_("invalid operands to %s"), opcode->name);
2802       return;
2803     }
2804 
2805   if (*op_end)
2806     {
2807       as_bad (_("excess operands to %s"), opcode->name);
2808       return;
2809     }
2810 
2811   size = shmedia_build_Mytes (opcode, &operands);
2812   if (size == 0)
2813     return;
2814 }
2815 
2816 /* Hook called from md_begin in tc-sh.c.  */
2817 
2818 void
2819 shmedia_md_begin (void)
2820 {
2821   const shmedia_opcode_info *shmedia_opcode;
2822   shmedia_opcode_hash_control = hash_new ();
2823 
2824   /* Create opcode table for SHmedia mnemonics.  */
2825   for (shmedia_opcode = shmedia_table;
2826        shmedia_opcode->name;
2827        shmedia_opcode++)
2828     hash_insert (shmedia_opcode_hash_control, shmedia_opcode->name,
2829 		 (char *) shmedia_opcode);
2830 }
2831 
2832 /* Switch instruction set.  Only valid if one of the --isa or --abi
2833    options was specified.  */
2834 
2835 static void
2836 s_sh64_mode (int ignore ATTRIBUTE_UNUSED)
2837 {
2838   char *name = input_line_pointer, ch;
2839 
2840   /* Make sure data up to this location is handled according to the
2841      previous ISA.  */
2842   sh64_update_contents_mark (TRUE);
2843 
2844   while (!is_end_of_line[(unsigned char) *input_line_pointer])
2845     input_line_pointer++;
2846   ch = *input_line_pointer;
2847   *input_line_pointer = '\0';
2848 
2849   /* If the mode was not set before, explicitly or implicitly, then we're
2850      not emitting SH64 code, so this pseudo is invalid.  */
2851   if (sh64_isa_mode == sh64_isa_unspecified)
2852     as_bad (_("The `.mode %s' directive is not valid with this architecture"),
2853 	    name);
2854 
2855   if (strcasecmp (name, "shcompact") == 0)
2856     sh64_isa_mode = sh64_isa_shcompact;
2857   else if (strcasecmp (name, "shmedia") == 0)
2858     sh64_isa_mode = sh64_isa_shmedia;
2859   else
2860     as_bad (_("Invalid argument to .mode: %s"), name);
2861 
2862   /* Make a new frag, marking it with the supposedly-changed ISA.  */
2863   frag_wane (frag_now);
2864   frag_new (0);
2865 
2866   /* Contents type up to this new point is the same as before; don't add a
2867      data region just because the new frag we created.  */
2868   sh64_update_contents_mark (FALSE);
2869 
2870   *input_line_pointer = ch;
2871   demand_empty_rest_of_line ();
2872 }
2873 
2874 /* Check that the right ABI is used.  Only valid if one of the --isa or
2875    --abi options was specified.  */
2876 
2877 static void
2878 s_sh64_abi (int ignore ATTRIBUTE_UNUSED)
2879 {
2880   char *name = input_line_pointer, ch;
2881 
2882   while (!is_end_of_line[(unsigned char) *input_line_pointer])
2883     input_line_pointer++;
2884   ch = *input_line_pointer;
2885   *input_line_pointer = '\0';
2886 
2887   /* If the mode was not set before, explicitly or implicitly, then we're
2888      not emitting SH64 code, so this pseudo is invalid.  */
2889   if (sh64_abi == sh64_abi_unspecified)
2890     as_bad (_("The `.abi %s' directive is not valid with this architecture"),
2891 	    name);
2892 
2893   if (strcmp (name, "64") == 0)
2894     {
2895       if (sh64_abi != sh64_abi_64)
2896 	as_bad (_("`.abi 64' but command-line options do not specify 64-bit ABI"));
2897     }
2898   else if (strcmp (name, "32") == 0)
2899     {
2900       if (sh64_abi != sh64_abi_32)
2901 	as_bad (_("`.abi 32' but command-line options do not specify 32-bit ABI"));
2902     }
2903   else
2904     as_bad (_("Invalid argument to .abi: %s"), name);
2905 
2906   *input_line_pointer = ch;
2907   demand_empty_rest_of_line ();
2908 }
2909 
2910 /* This function is the first target-specific function called after
2911    parsing command-line options.  Therefore we set default values from
2912    command-line options here and do some sanity checking we couldn't do
2913    when options were being parsed.  */
2914 
2915 const char *
2916 sh64_target_format (void)
2917 {
2918 #ifdef TE_NetBSD
2919   /* For NetBSD, if the ISA is unspecified, always use SHmedia.  */
2920   if (sh64_isa_mode == sh64_isa_unspecified)
2921     sh64_isa_mode = sh64_isa_shmedia;
2922 
2923   /* If the ABI is unspecified, select a default: based on how
2924      we were configured: sh64 == sh64_abi_64, else sh64_abi_32.  */
2925   if (sh64_abi == sh64_abi_unspecified)
2926     {
2927       if (sh64_isa_mode == sh64_isa_shcompact)
2928 	sh64_abi = sh64_abi_32;
2929       else if (strncmp (TARGET_CPU, "sh64", 4) == 0)
2930         sh64_abi = sh64_abi_64;
2931       else
2932         sh64_abi = sh64_abi_32;
2933     }
2934 #endif
2935 
2936 #ifdef TE_LINUX
2937   if (sh64_isa_mode == sh64_isa_unspecified)
2938     sh64_isa_mode = sh64_isa_shmedia;
2939 
2940   if (sh64_abi == sh64_abi_unspecified)
2941     sh64_abi = sh64_abi_32;
2942 #endif
2943 
2944   if (sh64_abi == sh64_abi_64 && sh64_isa_mode == sh64_isa_unspecified)
2945     sh64_isa_mode = sh64_isa_shmedia;
2946 
2947   if (sh64_abi == sh64_abi_32 && sh64_isa_mode == sh64_isa_unspecified)
2948     sh64_isa_mode = sh64_isa_shcompact;
2949 
2950   if (sh64_isa_mode == sh64_isa_shcompact
2951       && sh64_abi == sh64_abi_unspecified)
2952     sh64_abi = sh64_abi_32;
2953 
2954   if (sh64_isa_mode == sh64_isa_shmedia
2955       && sh64_abi == sh64_abi_unspecified)
2956     sh64_abi = sh64_abi_64;
2957 
2958   if (sh64_isa_mode == sh64_isa_unspecified && ! sh64_mix)
2959     as_bad (_("-no-mix is invalid without specifying SHcompact or SHmedia"));
2960 
2961   if ((sh64_isa_mode == sh64_isa_unspecified
2962        || sh64_isa_mode == sh64_isa_shmedia)
2963       && sh64_shcompact_const_crange)
2964     as_bad (_("-shcompact-const-crange is invalid without SHcompact"));
2965 
2966   if (sh64_pt32 && sh64_abi != sh64_abi_64)
2967     as_bad (_("-expand-pt32 only valid with -abi=64"));
2968 
2969   if (! sh64_expand && sh64_isa_mode == sh64_isa_unspecified)
2970     as_bad (_("-no-expand only valid with SHcompact or SHmedia"));
2971 
2972   if (sh64_pt32 && ! sh64_expand)
2973     as_bad (_("-expand-pt32 invalid together with -no-expand"));
2974 
2975 #ifdef TE_NetBSD
2976   if (sh64_abi == sh64_abi_64)
2977     return (target_big_endian ? "elf64-sh64-nbsd" : "elf64-sh64l-nbsd");
2978   else
2979     return (target_big_endian ? "elf32-sh64-nbsd" : "elf32-sh64l-nbsd");
2980 #elif defined (TE_LINUX)
2981   if (sh64_abi == sh64_abi_64)
2982     return (target_big_endian ? "elf64-sh64big-linux" : "elf64-sh64-linux");
2983   else
2984     return (target_big_endian ? "elf32-sh64big-linux" : "elf32-sh64-linux");
2985 #else
2986   /* When the ISA is not one of SHmedia or SHcompact, use the old SH
2987      object format.  */
2988   if (sh64_isa_mode == sh64_isa_unspecified)
2989     return (target_big_endian ? "elf32-sh" : "elf32-shl");
2990   else if (sh64_abi == sh64_abi_64)
2991     return (target_big_endian ? "elf64-sh64" : "elf64-sh64l");
2992   else
2993     return (target_big_endian ? "elf32-sh64" : "elf32-sh64l");
2994 #endif
2995 }
2996 
2997 /* The worker function of TARGET_MACH.  */
2998 
2999 int
3000 sh64_target_mach (void)
3001 {
3002   /* We need to explicitly set bfd_mach_sh5 instead of the default 0.  But
3003      we only do this for the 64-bit ABI: if we do it for the 32-bit ABI,
3004      the SH5 info in the bfd_arch_info structure will be selected.
3005      However correct, as the machine has 64-bit addresses, functions
3006      expected to emit 32-bit data for addresses will start failing.  For
3007      example, the dwarf2dbg.c functions will emit 64-bit debugging format,
3008      and we don't want that in the 32-bit ABI.
3009 
3010      We could have two bfd_arch_info structures for SH64; one for the
3011      32-bit ABI and one for the rest (64-bit ABI).  But that would be a
3012      bigger kludge: it's a flaw in the BFD design, and we need to just
3013      work around it by having the default machine set here in the
3014      assembler.  For everything else but the assembler, the various bfd
3015      functions will set the machine type right to bfd_mach_sh5 from object
3016      file header flags regardless of the 0 here.  */
3017 
3018   return (sh64_abi == sh64_abi_64) ? bfd_mach_sh5 : 0;
3019 }
3020 
3021 /* This is MD_PCREL_FROM_SECTION, we we define so it is called instead of
3022    md_pcrel_from (in tc-sh.c).  */
3023 
3024 valueT
3025 shmedia_md_pcrel_from_section (struct fix *fixP, segT sec ATTRIBUTE_UNUSED)
3026 {
3027   know (fixP->fx_frag->fr_type == rs_machine_dependent);
3028 
3029   /* Use the ISA for the instruction to decide which offset to use.  We
3030      can glean it from the fisup type.  */
3031   switch (fixP->fx_r_type)
3032     {
3033     case BFD_RELOC_SH_IMM_LOW16:
3034     case BFD_RELOC_SH_IMM_MEDLOW16:
3035     case BFD_RELOC_SH_IMM_MEDHI16:
3036     case BFD_RELOC_SH_IMM_HI16:
3037     case BFD_RELOC_SH_IMM_LOW16_PCREL:
3038     case BFD_RELOC_SH_IMM_MEDLOW16_PCREL:
3039     case BFD_RELOC_SH_IMM_MEDHI16_PCREL:
3040     case BFD_RELOC_SH_IMM_HI16_PCREL:
3041     case BFD_RELOC_SH_IMMU5:
3042     case BFD_RELOC_SH_IMMU6:
3043     case BFD_RELOC_SH_IMMS6:
3044     case BFD_RELOC_SH_IMMS10:
3045     case BFD_RELOC_SH_IMMS10BY2:
3046     case BFD_RELOC_SH_IMMS10BY4:
3047     case BFD_RELOC_SH_IMMS10BY8:
3048     case BFD_RELOC_SH_IMMS16:
3049     case BFD_RELOC_SH_IMMU16:
3050     case BFD_RELOC_SH_PT_16:
3051     case SHMEDIA_BFD_RELOC_PT:
3052       /* PC-relative relocs are relative to the address of the last generated
3053 	 instruction, i.e. fx_size - 4.  */
3054       return SHMEDIA_MD_PCREL_FROM_FIX (fixP);
3055 
3056     case BFD_RELOC_64:
3057     case BFD_RELOC_64_PCREL:
3058       know (0 /* Shouldn't get here.  */);
3059       break;
3060 
3061     default:
3062       /* If section was SHcompact, use its function.  */
3063       return (valueT) md_pcrel_from_section (fixP, sec);
3064     }
3065 
3066   know (0 /* Shouldn't get here.  */);
3067   return 0;
3068 }
3069 
3070 /* Create one .cranges descriptor from two symbols, STARTSYM marking begin
3071    and ENDSYM marking end, and CR_TYPE specifying the type.  */
3072 
3073 static void
3074 sh64_emit_crange (symbolS *startsym, symbolS *endsym,
3075 		  enum sh64_elf_cr_type cr_type)
3076 {
3077   expressionS exp;
3078   segT current_seg = now_seg;
3079   subsegT current_subseg = now_subseg;
3080 
3081   asection *cranges
3082     = bfd_make_section_old_way (stdoutput,
3083 				SH64_CRANGES_SECTION_NAME);
3084 
3085   /* Temporarily change to the .cranges section.  */
3086   subseg_set (cranges, 0);
3087 
3088   /* Emit the cr_addr part.  */
3089   exp.X_op = O_symbol;
3090   exp.X_add_number = 0;
3091   exp.X_op_symbol = NULL;
3092   exp.X_add_symbol = startsym;
3093   emit_expr (&exp, 4);
3094 
3095   /* Emit the cr_size part.  */
3096   exp.X_op = O_subtract;
3097   exp.X_add_number = 0;
3098   exp.X_add_symbol = endsym;
3099   exp.X_op_symbol = startsym;
3100   emit_expr (&exp, 4);
3101 
3102   /* Emit the cr_size part.  */
3103   exp.X_op = O_constant;
3104   exp.X_add_number = cr_type;
3105   exp.X_add_symbol = NULL;
3106   exp.X_op_symbol = NULL;
3107   emit_expr (&exp, 2);
3108 
3109   /* Now back to our regular program.  */
3110   subseg_set (current_seg, current_subseg);
3111 }
3112 
3113 /* Called when the assembler is about to emit contents of some type into
3114    SEG, so it is *known* that the type of that new contents is in
3115    NEW_CONTENTS_TYPE.  If just switching back and forth between different
3116    contents types (for example, with consecutive .mode pseudos), then this
3117    function isn't called.  */
3118 
3119 static void
3120 sh64_set_contents_type (enum sh64_elf_cr_type new_contents_type)
3121 {
3122   segment_info_type *seginfo;
3123 
3124   /* We will not be called when emitting .cranges output, since callers
3125      stop that.  Validize that assumption.  */
3126   know (!emitting_crange);
3127 
3128   seginfo = seg_info (now_seg);
3129 
3130   if (seginfo)
3131     {
3132       symbolS *symp = seginfo->tc_segment_info_data.last_contents_mark;
3133 
3134       enum sh64_elf_cr_type contents_type
3135 	= seginfo->tc_segment_info_data.contents_type;
3136 
3137       /* If it was just SHcompact switching between code and constant
3138 	 pool, don't change contents type.  Just make sure we don't set
3139 	 the contents type to data, as that would join with a data-region
3140 	 in SHmedia mode.  */
3141       if (sh64_isa_mode == sh64_isa_shcompact
3142 	  && ! sh64_shcompact_const_crange)
3143 	new_contents_type = CRT_SH5_ISA16;
3144 
3145       /* If nothing changed, stop here.  */
3146       if (contents_type == new_contents_type)
3147 	return;
3148 
3149       /* If we're in 64-bit ABI mode, we do not emit .cranges, as it is
3150 	 only specified for 32-bit addresses.  It could presumably be
3151 	 extended, but in 64-bit ABI mode we don't have SHcompact code, so
3152 	 we would only use it to mark code and data.  */
3153       if (sh64_abi == sh64_abi_64)
3154 	{
3155 	  /* Make the code type "sticky".  We don't want to set the
3156 	     sections contents type to data if there's any code in it as
3157 	     we don't have .cranges in 64-bit mode to notice the
3158 	     difference.  */
3159 	  seginfo->tc_segment_info_data.contents_type
3160 	    = (new_contents_type == CRT_SH5_ISA32
3161 	       || contents_type == CRT_SH5_ISA32)
3162 	    ? CRT_SH5_ISA32 : new_contents_type;
3163 	  return;
3164 	}
3165 
3166       /* If none was marked, create a start symbol for this range and
3167 	 perhaps as a closing symbol for the old one.  */
3168       if (symp == NULL)
3169 	symp = symbol_new (FAKE_LABEL_NAME, now_seg, (valueT) frag_now_fix (),
3170 			   frag_now);
3171 
3172       /* We will use this symbol, so don't leave a pointer behind.  */
3173       seginfo->tc_segment_info_data.last_contents_mark = NULL;
3174 
3175       /* We'll be making only datalabel references to it, if we emit a
3176 	 .cranges descriptor, so remove any code flag.  */
3177       S_SET_OTHER (symp, S_GET_OTHER (symp) & ~STO_SH5_ISA32);
3178 
3179       /* If we have already marked the start of a range, we need to close
3180 	 and emit it before marking a new one, so emit a new .cranges
3181 	 descriptor into the .cranges section.  */
3182       if (seginfo->tc_segment_info_data.mode_start_symbol)
3183 	{
3184 	  /* If we're not supposed to emit mixed-mode sections, make it an
3185 	     error, but continue processing.  */
3186 	  if (! sh64_mix
3187 	      && (new_contents_type == CRT_SH5_ISA32
3188 		  || contents_type == CRT_SH5_ISA32))
3189 	    as_bad (
3190 _("SHmedia code not allowed in same section as constants and SHcompact code"));
3191 
3192 	  emitting_crange = TRUE;
3193 	  sh64_emit_crange (seginfo->tc_segment_info_data.mode_start_symbol,
3194 			    symp, contents_type);
3195 	  emitting_crange = FALSE;
3196 	  seginfo->tc_segment_info_data.emitted_ranges++;
3197 	}
3198 
3199       seginfo->tc_segment_info_data.mode_start_symbol = symp;
3200       seginfo->tc_segment_info_data.mode_start_subseg = now_subseg;
3201       seginfo->tc_segment_info_data.contents_type = new_contents_type;
3202 
3203       /* Always reset this, so the SHcompact code will emit a reloc when
3204 	 it prepares to relax.  */
3205       seginfo->tc_segment_info_data.in_code = 0;
3206     }
3207   else
3208     as_bad (_("No segment info for current section"));
3209 }
3210 
3211 /* Hook when defining symbols and labels.  We set the ST_OTHER field if
3212    the symbol is "shmedia" (with "bitor 1" automatically applied).  Simple
3213    semantics for a label being "shmedia" : It was defined when .mode
3214    SHmedia was in effect, and it was defined in a code section.  It
3215    doesn't matter whether or not an assembled opcode is nearby.  */
3216 
3217 void
3218 sh64_frob_label (symbolS *symp)
3219 {
3220   segT seg = S_GET_SEGMENT (symp);
3221   static const symbolS *null = NULL;
3222 
3223   /* Reset the tc marker for all newly created symbols.  */
3224   symbol_set_tc (symp, (symbolS **) &null);
3225 
3226   if (seg != NULL && sh64_isa_mode == sh64_isa_shmedia && subseg_text_p (seg))
3227     S_SET_OTHER (symp, S_GET_OTHER (symp) | STO_SH5_ISA32);
3228 }
3229 
3230 /* Handle the "datalabel" qualifier.  We need to call "operand", but it's
3231    static, so a function pointer is passed here instead.  FIXME: A target
3232    hook for qualifiers is needed; we currently use the md_parse_name
3233    symbol hook.  */
3234 
3235 int
3236 sh64_consume_datalabel (const char *name, expressionS *exp, char *cp,
3237 			segT (*operandf) (expressionS *))
3238 {
3239   static int parsing_datalabel = 0;
3240 
3241   if (strcasecmp (name, "datalabel") == 0)
3242     {
3243       int save_parsing_datalabel = parsing_datalabel;
3244 
3245       if (parsing_datalabel)
3246 	as_bad (_("duplicate datalabel operator ignored"));
3247 
3248       *input_line_pointer = *cp;
3249       parsing_datalabel = 1;
3250       (*operandf) (exp);
3251       parsing_datalabel = save_parsing_datalabel;
3252 
3253       if (exp->X_op == O_symbol || exp->X_op == O_PIC_reloc)
3254 	{
3255 	  symbolS *symp = exp->X_add_symbol;
3256 	  segT symseg = S_GET_SEGMENT (symp);
3257 
3258 	  /* If the symbol is defined to something that is already a
3259 	     datalabel, we don't need to bother with any special handling.  */
3260 	  if (symseg != undefined_section
3261 	      && S_GET_OTHER (symp) != STO_SH5_ISA32)
3262 	    /* Do nothing.  */
3263 	    ;
3264 	  else
3265 	    {
3266 	      symbolS *dl_symp;
3267 	      const char *name = S_GET_NAME (symp);
3268 	      char *dl_name
3269 		= xmalloc (strlen (name) + sizeof (DATALABEL_SUFFIX));
3270 
3271 	      /* Now we copy the datalabel-qualified symbol into a symbol
3272 		 with the same name, but with " DL" appended.  We mark the
3273 		 symbol using the TC_SYMFIELD_TYPE field with a pointer to
3274 		 the main symbol, so we don't have to inspect all symbol
3275 		 names.  Note that use of "datalabel" is not expected to
3276 		 be a common case.  */
3277 	      strcpy (dl_name, name);
3278 	      strcat (dl_name, DATALABEL_SUFFIX);
3279 
3280 	      /* A FAKE_LABEL_NAME marks "$" or ".".  There can be any
3281 		 number of them and all have the same (faked) name; we
3282 		 must make a new one each time.  */
3283 	      if (strcmp (name, FAKE_LABEL_NAME) == 0)
3284 		dl_symp = symbol_make (dl_name);
3285 	      else
3286 		dl_symp = symbol_find_or_make (dl_name);
3287 
3288 	      free (dl_name);
3289 	      symbol_set_value_expression (dl_symp,
3290 					   symbol_get_value_expression (symp));
3291 	      S_SET_SEGMENT (dl_symp, symseg);
3292 	      symbol_set_frag (dl_symp, symbol_get_frag (symp));
3293 	      symbol_set_tc (dl_symp, &symp);
3294 	      copy_symbol_attributes (dl_symp, symp);
3295 	      exp->X_add_symbol = dl_symp;
3296 
3297 	      /* Unset the BranchTarget mark that can be set at symbol
3298 		 creation or attributes copying.  */
3299 	      S_SET_OTHER (dl_symp, S_GET_OTHER (dl_symp) & ~STO_SH5_ISA32);
3300 
3301 	      /* The GLOBAL and WEAK attributes are not copied over by
3302 		 copy_symbol_attributes.  Do it here.  */
3303 	      if (S_IS_WEAK (symp))
3304 		S_SET_WEAK (dl_symp);
3305 	      else if (S_IS_EXTERNAL (symp))
3306 		S_SET_EXTERNAL (dl_symp);
3307 	    }
3308 	}
3309       /* Complain about other types of operands than symbol, unless they
3310 	 have already been complained about.  A constant is always a
3311 	 datalabel.  Removing the low bit would therefore be wrong.
3312 	 Complaining about it would also be wrong.  */
3313       else if (exp->X_op != O_illegal
3314 	       && exp->X_op != O_absent
3315 	       && exp->X_op != O_constant)
3316 	as_bad (_("Invalid DataLabel expression"));
3317 
3318       *cp = *input_line_pointer;
3319 
3320       return 1;
3321     }
3322 
3323   return sh_parse_name (name, exp, cp);
3324 }
3325 
3326 /* This function is called just before symbols are being output.  It
3327    returns zero when a symbol must be output, non-zero otherwise.
3328    Datalabel references that were fully resolved to local symbols are not
3329    necessary to output.  We also do not want to output undefined symbols
3330    that are not used in relocs.  For symbols that are used in a reloc, it
3331    does not matter what we set here.  If it is *not* used in a reloc, then
3332    it was probably the datalabel counterpart that was used in a reloc;
3333    then we need not output the main symbol.  */
3334 
3335 int
3336 sh64_exclude_symbol (symbolS *symp)
3337 {
3338   symbolS *main_symbol = *symbol_get_tc (symp);
3339 
3340   return main_symbol != NULL || ! S_IS_DEFINED (symp);
3341 }
3342 
3343 /* If we haven't seen an insn since the last update, and location
3344    indicators have moved (a new frag, new location within frag) we have
3345    emitted data, so change contents type to data.  Forget that we have
3346    seen a sequence of insns and store the current location so we can mark
3347    a new region if needed.  */
3348 
3349 static void
3350 sh64_update_contents_mark (bfd_boolean update_type)
3351 {
3352   segment_info_type *seginfo;
3353   seginfo = seg_info (now_seg);
3354 
3355   if (seginfo != NULL)
3356     {
3357       symbolS *symp = seginfo->tc_segment_info_data.last_contents_mark;
3358 
3359       if (symp == NULL)
3360 	{
3361 	  symp = symbol_new (FAKE_LABEL_NAME, now_seg,
3362 			     (valueT) frag_now_fix (), frag_now);
3363 	  seginfo->tc_segment_info_data.last_contents_mark = symp;
3364 	}
3365       else
3366 	{
3367 	  /* If we have moved location since last flush, we need to emit a
3368 	     data range.  The previous contents type ended at the location
3369 	     of the last update.  */
3370 	  if ((S_GET_VALUE (symp) != frag_now_fix ()
3371 	       || symbol_get_frag (symp) != frag_now))
3372 	    {
3373 	      enum sh64_elf_cr_type contents_type
3374 		= seginfo->tc_segment_info_data.contents_type;
3375 
3376 	      if (update_type
3377 		  && contents_type != CRT_DATA
3378 		  && contents_type != CRT_NONE
3379 		  && ! seen_insn)
3380 		{
3381 		  sh64_set_contents_type (CRT_DATA);
3382 		  symp = seginfo->tc_segment_info_data.last_contents_mark;
3383 		}
3384 
3385 	      /* If the symbol wasn't used up to make up a new range
3386 		 descriptor, update it to this new location.  */
3387 	      if (symp)
3388 		{
3389 		  S_SET_VALUE (symp, (valueT) frag_now_fix ());
3390 		  symbol_set_frag (symp, frag_now);
3391 		}
3392 	    }
3393 	}
3394     }
3395 
3396   seen_insn = FALSE;
3397 }
3398 
3399 /* Called when the assembler is about to output some data, or maybe it's
3400    just switching segments.  */
3401 
3402 void
3403 sh64_flush_pending_output (void)
3404 {
3405   sh64_update_contents_mark (TRUE);
3406   sh_flush_pending_output ();
3407 }
3408 
3409 /* Flush out the last crange descriptor after all insns have been emitted.  */
3410 
3411 static void
3412 sh64_flush_last_crange (bfd *abfd ATTRIBUTE_UNUSED, asection *seg,
3413 			void *countparg ATTRIBUTE_UNUSED)
3414 {
3415   segment_info_type *seginfo;
3416 
3417   seginfo = seg_info (seg);
3418 
3419   if (seginfo
3420       /* Only emit .cranges descriptors if we would make it more than one.  */
3421       && seginfo->tc_segment_info_data.emitted_ranges != 0)
3422     {
3423       symbolS *symp;
3424 
3425       /* We need a closing symbol, so switch to the indicated section and
3426 	 emit it.  */
3427 
3428       /* Change to the section we're about to handle.  */
3429       subseg_set (seg, seginfo->tc_segment_info_data.mode_start_subseg);
3430 
3431       symp = symbol_new (FAKE_LABEL_NAME, now_seg, (valueT) frag_now_fix (),
3432 			 frag_now);
3433 
3434       /* We'll be making a datalabel reference to it, so remove any code
3435          flag.  */
3436       S_SET_OTHER (symp, S_GET_OTHER (symp) & ~STO_SH5_ISA32);
3437 
3438       sh64_emit_crange (seginfo->tc_segment_info_data.mode_start_symbol,
3439 			symp,
3440 			seginfo->tc_segment_info_data.contents_type);
3441     }
3442 }
3443 
3444 /* If and only if we see a call to md_number_to_chars without flagging the
3445    start of an insn, we set the contents type to CRT_DATA, and only when
3446    in SHmedia mode.  Note that by default we don't bother changing when
3447    going from SHcompact to data, as the constant pools in GCC-generated
3448    SHcompact code would create an inordinate amount of .cranges
3449    descriptors.  */
3450 
3451 static void
3452 sh64_flag_output (void)
3453 {
3454   if (sh64_isa_mode != sh64_isa_unspecified
3455       && !seen_insn
3456       && !sh64_end_of_assembly
3457       && !emitting_crange)
3458     {
3459       md_flush_pending_output ();
3460       sh64_set_contents_type (CRT_DATA);
3461     }
3462 }
3463 
3464 /* Vtables don't need "datalabel" but we allow it by simply deleting
3465    any we find.  */
3466 
3467 static char *
3468 strip_datalabels (void)
3469 {
3470   char *src, *dest, *start=input_line_pointer;
3471 
3472   for (src=input_line_pointer, dest=input_line_pointer; *src != '\n'; )
3473     {
3474       if (strncasecmp (src, "datalabel", 9) == 0
3475 	  && ISSPACE (src[9])
3476 	  && (src == start || !(ISALNUM (src[-1])) || src[-1] == '_'))
3477 	src += 10;
3478       else
3479 	*dest++ = *src++;
3480     }
3481 
3482   if (dest < src)
3483     *dest = '\n';
3484   return src + 1;
3485 }
3486 
3487 static void
3488 sh64_vtable_entry (int ignore ATTRIBUTE_UNUSED)
3489 {
3490   char *eol = strip_datalabels ();
3491 
3492   obj_elf_vtable_entry (0);
3493   input_line_pointer = eol;
3494 }
3495 
3496 static void
3497 sh64_vtable_inherit (int ignore ATTRIBUTE_UNUSED)
3498 {
3499   char *eol = strip_datalabels ();
3500 
3501   obj_elf_vtable_inherit (0);
3502   input_line_pointer = eol;
3503 }
3504 
3505