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