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