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