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