#define HAVE_SH64
#include <stdio.h>
#include "as.h"
#include "safe-ctype.h"
#include "opcodes/sh64-opc.h"
#ifndef OBJ_ELF
#error This file assumes object output is in the ELF format
#endif
#define DATALABEL_SUFFIX " DL"
#define SHMEDIA_MD_PCREL_FROM_FIX(FIXP) \
((FIXP)->fx_size + (FIXP)->fx_where + (FIXP)->fx_frag->fr_address - 4)
#define SHMEDIA_BFD_RELOC_PT BFD_RELOC_12_PCREL
typedef struct
{
shmedia_arg_type type;
int reg;
expressionS immediate;
bfd_reloc_code_real_type reloctype;
} shmedia_operand_info;
fragS *sh64_last_insn_frag = NULL;
typedef struct
{
shmedia_operand_info operands[3];
unsigned long ops_val;
} shmedia_operands_info;
enum sh64_abi_values
{ sh64_abi_unspecified, sh64_abi_32, sh64_abi_64 };
enum sh64_isa_values sh64_isa_mode = sh64_isa_unspecified;
static enum sh64_abi_values sh64_abi = sh64_abi_unspecified;
static bfd_boolean seen_insn = FALSE;
static bfd_boolean sh64_end_of_assembly = FALSE;
static bfd_boolean sh64_mix = TRUE;
static bfd_boolean sh64_shcompact_const_crange = FALSE;
static bfd_boolean sh64_expand = TRUE;
static bfd_boolean sh64_pt32 = FALSE;
static bfd_boolean emitting_crange = FALSE;
static struct hash_control *shmedia_opcode_hash_control = NULL;
static const unsigned char shmedia_big_nop_pattern[4] =
{
(SHMEDIA_NOP_OPC >> 24) & 255, (SHMEDIA_NOP_OPC >> 16) & 255,
(SHMEDIA_NOP_OPC >> 8) & 255, SHMEDIA_NOP_OPC & 255
};
static const unsigned char shmedia_little_nop_pattern[4] =
{
SHMEDIA_NOP_OPC & 255, (SHMEDIA_NOP_OPC >> 8) & 255,
(SHMEDIA_NOP_OPC >> 16) & 255, (SHMEDIA_NOP_OPC >> 24) & 255
};
static void shmedia_md_begin (void);
static int shmedia_parse_reg (char *, int *, int *, shmedia_arg_type);
static void shmedia_md_assemble (char *);
static void shmedia_md_apply_fix3 (fixS *, valueT *);
static int shmedia_md_estimate_size_before_relax (fragS *, segT);
static int shmedia_init_reloc (arelent *, fixS *);
static char *shmedia_get_operands (shmedia_opcode_info *, char *,
shmedia_operands_info *);
static void s_sh64_mode (int);
static void s_sh64_abi (int);
static void shmedia_md_convert_frag (bfd *, segT, fragS *, bfd_boolean);
static void shmedia_check_limits (offsetT *, bfd_reloc_code_real_type,
fixS *);
static void sh64_set_contents_type (enum sh64_elf_cr_type);
static void shmedia_get_operand (char **, shmedia_operand_info *,
shmedia_arg_type);
static unsigned long shmedia_immediate_op (char *, shmedia_operand_info *,
int, bfd_reloc_code_real_type);
static char *shmedia_parse_exp (char *, shmedia_operand_info *);
static void shmedia_frob_file_before_adjust (void);
static void sh64_emit_crange (symbolS *, symbolS *, enum sh64_elf_cr_type);
static void sh64_flush_last_crange (bfd *, asection *, void *);
static void sh64_flag_output (void);
static void sh64_update_contents_mark (bfd_boolean);
static void sh64_vtable_entry (int);
static void sh64_vtable_inherit (int);
static char *strip_datalabels (void);
static int shmedia_build_Mytes (shmedia_opcode_info *,
shmedia_operands_info *);
static shmedia_opcode_info *shmedia_find_cooked_opcode (char **);
static unsigned long shmedia_mask_number (unsigned long,
bfd_reloc_code_real_type);
#include "tc-sh.c"
void
shmedia_md_end (void)
{
symbolS *symp;
sh64_update_contents_mark (TRUE);
if (sh64_isa_mode != sh64_isa_unspecified)
sh64_isa_mode = sh64_isa_sh5_guard;
sh64_end_of_assembly = TRUE;
bfd_map_over_sections (stdoutput, sh64_flush_last_crange, NULL);
for (symp = symbol_rootP; symp != NULL; symp = symp->sy_next)
{
symbolS *mainsym = *symbol_get_tc (symp);
if (mainsym != NULL)
{
if (S_GET_SEGMENT (symp) == undefined_section)
{
segT symseg;
symseg = S_GET_SEGMENT (mainsym);
if (symseg != undefined_section)
{
if (S_GET_OTHER (mainsym) != STO_SH5_ISA32)
{
symp->sy_value.X_op = O_symbol;
symp->sy_value.X_add_symbol = mainsym;
symp->sy_value.X_op_symbol = NULL;
symp->sy_value.X_add_number = 0;
S_SET_SEGMENT (symp, S_GET_SEGMENT (mainsym));
symbol_set_frag (symp, &zero_address_frag);
copy_symbol_attributes (symp, mainsym);
}
else
{
symbol_set_value_expression (symp,
symbol_get_value_expression
(mainsym));
S_SET_SEGMENT (symp, symseg);
symbol_set_frag (symp, symbol_get_frag (mainsym));
copy_symbol_attributes (symp, mainsym);
S_SET_OTHER (symp,
S_GET_OTHER (symp) & ~STO_SH5_ISA32);
if (S_IS_WEAK (mainsym))
S_SET_WEAK (symp);
else if (S_IS_EXTERNAL (mainsym))
S_SET_EXTERNAL (symp);
}
}
else
{
if (S_IS_WEAK (mainsym))
S_SET_WEAK (symp);
else if (S_IS_EXTERNAL (mainsym))
S_SET_EXTERNAL (symp);
}
}
}
}
for (symp = symbol_rootP; symp != NULL; symp = symp->sy_next)
if (S_GET_OTHER (symp) & STO_SH5_ISA32)
symp->sy_value.X_add_number++;
}
static void
shmedia_frob_file_before_adjust (void)
{
symbolS *symp;
for (symp = symbol_rootP; symp != NULL; symp = symp->sy_next)
{
symbolS *mainsym = *symbol_get_tc (symp);
if (mainsym != NULL
&& S_GET_OTHER (mainsym) != STO_SH5_ISA32
&& (S_IS_EXTERN (mainsym) || S_IS_WEAK (mainsym)))
{
symp->sy_value.X_op = O_symbol;
symp->sy_value.X_add_symbol = mainsym;
symp->sy_value.X_op_symbol = NULL;
symp->sy_value.X_add_number = 0;
S_SET_SEGMENT (symp, undefined_section);
symbol_set_frag (symp, &zero_address_frag);
copy_symbol_attributes (symp, mainsym);
S_SET_OTHER (symp, S_GET_OTHER (symp) & ~STO_SH5_ISA32);
}
}
}
void
sh64_do_align (int n, const char *fill, int len, int max)
{
sh64_update_contents_mark (TRUE);
if (n != 0 && !need_pass_2)
{
if (fill == NULL)
{
if (subseg_text_p (now_seg))
frag_align_code (n, max);
else
frag_align (n, 0, max);
}
else if (len <= 1)
frag_align (n, *fill, max);
else
frag_align_pattern (n, fill, len, max);
}
sh64_update_contents_mark (FALSE);
}
int
sh64_max_mem_for_rs_align_code (void)
{
segment_info_type *seginfo;
fragS *mode_start_frag;
seginfo = seg_info (now_seg);
return
(sh64_isa_mode == sh64_isa_shmedia
|| (sh64_isa_mode != sh64_isa_unspecified
&& seginfo != NULL
&& seginfo->tc_segment_info_data.mode_start_symbol != NULL
&& ((mode_start_frag
= (symbol_get_frag
(seginfo->tc_segment_info_data.mode_start_symbol)))
!= NULL)
&& mode_start_frag->tc_frag_data.isa == sh64_isa_shmedia))
? (3 + 4) : (2 + 1);
}
void
sh64_handle_align (fragS * frag)
{
int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
char * p = frag->fr_literal + frag->fr_fix;
if (frag->tc_frag_data.isa == sh64_isa_shmedia
&& frag->fr_type == rs_align_code)
{
while (bytes & 3)
{
*p++ = 0;
bytes--;
frag->fr_fix += 1;
}
if (target_big_endian)
{
memcpy (p, shmedia_big_nop_pattern,
sizeof shmedia_big_nop_pattern);
frag->fr_var = sizeof shmedia_big_nop_pattern;
}
else
{
memcpy (p, shmedia_little_nop_pattern,
sizeof shmedia_little_nop_pattern);
frag->fr_var = sizeof shmedia_little_nop_pattern;
}
}
else
sh_handle_align (frag);
}
void
shmedia_frob_section_type (asection *sec)
{
segment_info_type *seginfo;
seginfo = seg_info (sec);
if (seginfo && sh64_abi == sh64_abi_32)
{
struct sh64_section_data *sec_elf_data;
flagword sec_type = 0;
if (seginfo->tc_segment_info_data.emitted_ranges != 0)
sec_type = SHF_SH5_ISA32_MIXED;
else if (seginfo->tc_segment_info_data.contents_type == CRT_SH5_ISA32)
sec_type = SHF_SH5_ISA32;
sec_elf_data = sh64_elf_section_data (sec)->sh64_info;
if (sec_elf_data == NULL)
{
sec_elf_data = xcalloc (1, sizeof (*sec_elf_data));
sh64_elf_section_data (sec)->sh64_info = sec_elf_data;
}
sec_elf_data->contents_flags = sec_type;
}
}
void
sh64_adjust_symtab (void)
{
symbolS *symp;
for (symp = symbol_rootP; symp; symp = symbol_next (symp))
{
symbolS *main_symbol = *symbol_get_tc (symp);
if (main_symbol)
{
char *sym_name = (char *) S_GET_NAME (symp);
sym_name[strlen (sym_name) - strlen (DATALABEL_SUFFIX)] = 0;
elf_symbol (symbol_get_bfdsym (symp))->internal_elf_sym.st_info
= STT_DATALABEL;
S_SET_SEGMENT (symp, undefined_section);
}
else if (S_GET_OTHER (symp) & STO_SH5_ISA32)
{
symp->bsym->value--;
}
}
}
static int
shmedia_init_reloc (arelent *rel, fixS *fixP)
{
switch (fixP->fx_r_type)
{
case BFD_RELOC_64:
case BFD_RELOC_64_PCREL:
case BFD_RELOC_SH_IMM_LOW16:
case BFD_RELOC_SH_IMM_MEDLOW16:
case BFD_RELOC_SH_IMM_MEDHI16:
case BFD_RELOC_SH_IMM_HI16:
case BFD_RELOC_SH_IMM_LOW16_PCREL:
case BFD_RELOC_SH_IMM_MEDLOW16_PCREL:
case BFD_RELOC_SH_IMM_MEDHI16_PCREL:
case BFD_RELOC_SH_IMM_HI16_PCREL:
case BFD_RELOC_SH_IMMU5:
case BFD_RELOC_SH_IMMU6:
case BFD_RELOC_SH_IMMS6:
case BFD_RELOC_SH_IMMS10:
case BFD_RELOC_SH_IMMS10BY2:
case BFD_RELOC_SH_IMMS10BY4:
case BFD_RELOC_SH_IMMS10BY8:
case BFD_RELOC_SH_IMMS16:
case BFD_RELOC_SH_IMMU16:
case BFD_RELOC_SH_PT_16:
case BFD_RELOC_SH_GOT_LOW16:
case BFD_RELOC_SH_GOT_MEDLOW16:
case BFD_RELOC_SH_GOT_MEDHI16:
case BFD_RELOC_SH_GOT_HI16:
case BFD_RELOC_SH_GOT10BY4:
case BFD_RELOC_SH_GOT10BY8:
case BFD_RELOC_SH_GOTPLT_LOW16:
case BFD_RELOC_SH_GOTPLT_MEDLOW16:
case BFD_RELOC_SH_GOTPLT_MEDHI16:
case BFD_RELOC_SH_GOTPLT_HI16:
case BFD_RELOC_SH_GOTPLT10BY4:
case BFD_RELOC_SH_GOTPLT10BY8:
case BFD_RELOC_SH_GOTOFF_LOW16:
case BFD_RELOC_SH_GOTOFF_MEDLOW16:
case BFD_RELOC_SH_GOTOFF_MEDHI16:
case BFD_RELOC_SH_GOTOFF_HI16:
case BFD_RELOC_SH_GOTPC_LOW16:
case BFD_RELOC_SH_GOTPC_MEDLOW16:
case BFD_RELOC_SH_GOTPC_MEDHI16:
case BFD_RELOC_SH_GOTPC_HI16:
case BFD_RELOC_SH_PLT_LOW16:
case BFD_RELOC_SH_PLT_MEDLOW16:
case BFD_RELOC_SH_PLT_MEDHI16:
case BFD_RELOC_SH_PLT_HI16:
rel->addend = fixP->fx_addnumber + fixP->fx_offset;
return 1;
case BFD_RELOC_SH_IMMS6BY32:
as_bad_where (fixP->fx_file, fixP->fx_line,
_("This operand must be constant at assembly time"));
break;
default:
;
}
return 0;
}
static void
shmedia_md_apply_fix3 (fixS *fixP, valueT *valp)
{
offsetT val = *valp;
char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
unsigned long insn
= target_big_endian ? bfd_getb32 (buf) : bfd_getl32 (buf);
bfd_reloc_code_real_type orig_fx_r_type = fixP->fx_r_type;
if (fixP->fx_pcrel)
{
switch (orig_fx_r_type)
{
case BFD_RELOC_64:
case BFD_RELOC_SH_IMM_LOW16:
case BFD_RELOC_SH_IMM_MEDLOW16:
case BFD_RELOC_SH_IMM_MEDHI16:
case BFD_RELOC_SH_IMM_HI16:
fixP->fx_offset
= *valp + SHMEDIA_MD_PCREL_FROM_FIX (fixP);
break;
case BFD_RELOC_SH_PLT_LOW16:
case BFD_RELOC_SH_PLT_MEDLOW16:
case BFD_RELOC_SH_PLT_MEDHI16:
case BFD_RELOC_SH_PLT_HI16:
case BFD_RELOC_SH_GOTPC_LOW16:
case BFD_RELOC_SH_GOTPC_MEDLOW16:
case BFD_RELOC_SH_GOTPC_MEDHI16:
case BFD_RELOC_SH_GOTPC_HI16:
*valp = 0;
return;
default:
;
}
switch (orig_fx_r_type)
{
case BFD_RELOC_64:
fixP->fx_r_type = BFD_RELOC_64_PCREL;
break;
case BFD_RELOC_SH_IMM_LOW16:
fixP->fx_r_type = BFD_RELOC_SH_IMM_LOW16_PCREL;
break;
case BFD_RELOC_SH_IMM_MEDLOW16:
fixP->fx_r_type = BFD_RELOC_SH_IMM_MEDLOW16_PCREL;
break;
case BFD_RELOC_SH_IMM_MEDHI16:
fixP->fx_r_type = BFD_RELOC_SH_IMM_MEDHI16_PCREL;
break;
case BFD_RELOC_SH_IMM_HI16:
fixP->fx_r_type = BFD_RELOC_SH_IMM_HI16_PCREL;
break;
case SHMEDIA_BFD_RELOC_PT:
fixP->fx_r_type = BFD_RELOC_SH_PT_16;
break;
case BFD_RELOC_SH_PT_16:
md_number_to_chars (buf, insn | (1 << 10), 4);
break;
case BFD_RELOC_64_PCREL:
case BFD_RELOC_SH_IMM_LOW16_PCREL:
case BFD_RELOC_SH_IMM_MEDLOW16_PCREL:
case BFD_RELOC_SH_IMM_MEDHI16_PCREL:
case BFD_RELOC_SH_IMM_HI16_PCREL:
break;
default:
as_bad_where (fixP->fx_file, fixP->fx_line,
_("Invalid operand expression"));
break;
}
return;
}
if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
{
shmedia_check_limits ((offsetT *) valp, fixP->fx_r_type, fixP);
switch (fixP->fx_r_type)
{
case BFD_RELOC_SH_IMM_LOW16:
md_number_to_chars (buf, insn | ((val & 65535) << 10), 4);
break;
case BFD_RELOC_SH_IMM_MEDLOW16:
md_number_to_chars (buf,
insn
| ((valueT) (val & ((valueT) 65535 << 16))
>> (16 - 10)), 4);
break;
case BFD_RELOC_SH_IMM_MEDHI16:
md_number_to_chars (buf,
insn
| ((valueT) (val & ((valueT) 65535 << 32))
>> (32 - 10)), 4);
break;
case BFD_RELOC_SH_IMM_HI16:
md_number_to_chars (buf,
insn
| ((valueT) (val & ((valueT) 65535 << 48))
>> (48 - 10)), 4);
break;
case BFD_RELOC_SH_IMMS16:
case BFD_RELOC_SH_IMMU16:
md_number_to_chars (buf, insn | ((val & 65535) << 10), 4);
break;
case BFD_RELOC_SH_IMMS10:
md_number_to_chars (buf, insn | ((val & 0x3ff) << 10), 4);
break;
case BFD_RELOC_SH_IMMS10BY2:
md_number_to_chars (buf,
insn | ((val & (0x3ff << 1)) << (10 - 1)), 4);
break;
case BFD_RELOC_SH_IMMS10BY4:
md_number_to_chars (buf,
insn | ((val & (0x3ff << 2)) << (10 - 2)), 4);
break;
case BFD_RELOC_SH_IMMS10BY8:
md_number_to_chars (buf,
insn | ((val & (0x3ff << 3)) << (10 - 3)), 4);
break;
case BFD_RELOC_SH_SHMEDIA_CODE:
break;
case BFD_RELOC_64:
md_number_to_chars (buf, val, 8);
break;
case SHMEDIA_BFD_RELOC_PT:
if ((val & 1) == 0)
insn |= SHMEDIA_PTB_BIT;
case BFD_RELOC_SH_PT_16:
if (! sh64_expand || sh_relax)
{
if (orig_fx_r_type != SHMEDIA_BFD_RELOC_PT)
{
if ((insn & SHMEDIA_PTB_BIT) != 0 && (val & 1) != 0)
as_bad_where (fixP->fx_file, fixP->fx_line,
_("PTB operand is a SHmedia symbol"));
else if ((insn & SHMEDIA_PTB_BIT) == 0 && (val & 1) == 0)
as_bad_where (fixP->fx_file, fixP->fx_line,
_("PTA operand is a SHcompact symbol"));
}
md_number_to_chars (buf,
insn | ((val & (0xffff << 2))
<< (10 - 2)),
4);
break;
}
default:
as_bad_where (fixP->fx_file, fixP->fx_line,
_("invalid expression in operand"));
}
fixP->fx_done = 1;
}
}
static void
shmedia_md_convert_frag (bfd *output_bfd ATTRIBUTE_UNUSED,
segT seg ATTRIBUTE_UNUSED, fragS *fragP,
bfd_boolean final)
{
char *var_partp;
char *opcodep;
fragS *opc_fragP = fragP->tc_frag_data.opc_frag;
int var_part_size = 0;
symbolS *symbolP = fragP->fr_symbol;
bfd_boolean reloc_needed
= (! final
|| sh_relax
|| symbolP == NULL
|| ! S_IS_DEFINED (symbolP)
|| S_IS_EXTERN (symbolP)
|| S_IS_WEAK (symbolP)
|| (S_GET_SEGMENT (fragP->fr_symbol) != absolute_section
&& S_GET_SEGMENT (fragP->fr_symbol) != seg));
bfd_reloc_code_real_type reloctype = BFD_RELOC_NONE;
unsigned long var_part_offset;
bfd_vma target_address;
bfd_vma opcode_address;
unsigned long insn;
know (fragP->fr_type == rs_machine_dependent);
var_part_offset = fragP->fr_fix;
var_partp = fragP->fr_literal + var_part_offset;
opcodep = fragP->fr_opcode;
insn = target_big_endian ? bfd_getb32 (opcodep) : bfd_getl32 (opcodep);
target_address
= ((symbolP && final && ! sh_relax ? S_GET_VALUE (symbolP) : 0)
+ fragP->fr_offset);
opcode_address = fragP->fr_address + fragP->fr_fix - 4;
switch (fragP->fr_subtype)
{
case C (SH64PCREL16PT_64, SH64PCREL16):
case C (SH64PCREL16PT_32, SH64PCREL16):
if ((target_address & 1) == 0)
insn |= SHMEDIA_PTB_BIT;
case C (SH64PCREL16_32, SH64PCREL16):
case C (SH64PCREL16_64, SH64PCREL16):
if (((target_address & 1) == 0) && ((insn & SHMEDIA_PTB_BIT) == 0))
as_bad_where (fragP->fr_file, fragP->fr_line,
_("PTA operand is a SHcompact symbol"));
if (((target_address & 1) != 0) && ((insn & SHMEDIA_PTB_BIT) != 0))
as_bad_where (fragP->fr_file, fragP->fr_line,
_("PTB operand is a SHmedia symbol"));
md_number_to_chars (opcodep,
insn
| (((sh_relax
? 1 : ((target_address - opcode_address) / 4))
& ((1 << 16) - 1)) << 10),
4);
if (sh_relax)
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset, 1, BFD_RELOC_SH_PT_16);
var_part_size = 0;
break;
case C (SH64PCREL16_32, SH64PCRELPLT):
case C (SH64PCREL16PT_32, SH64PCRELPLT):
reloctype = BFD_RELOC_32_PLT_PCREL;
reloc_needed = 1;
case C (SH64PCREL16_32, SH64PCREL32):
case C (SH64PCREL16_64, SH64PCREL32):
case C (SH64PCREL16PT_32, SH64PCREL32):
case C (SH64PCREL16PT_64, SH64PCREL32):
md_number_to_chars (opcodep,
SHMEDIA_MOVI_OPC
| (SHMEDIA_TEMP_REG << 4)
| ((((reloc_needed
? 0 : (target_address - (opcode_address + 8))
) >> 16) & 65535) << 10),
4);
md_number_to_chars (var_partp,
SHMEDIA_SHORI_OPC
| (SHMEDIA_TEMP_REG << 4)
| (((reloc_needed
? 0 : (target_address - (opcode_address + 8)))
& 65535) << 10),
4);
md_number_to_chars (var_partp + 4,
SHMEDIA_PTREL_OPC | (insn & SHMEDIA_LIKELY_BIT)
| (SHMEDIA_TEMP_REG << 10)
| (insn & (7 << 4)),
4);
if (reloc_needed)
{
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset - 8, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDLOW16
: BFD_RELOC_SH_IMM_MEDLOW16_PCREL);
fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
fragP->fr_offset - 4, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_LOW16
: BFD_RELOC_SH_IMM_LOW16_PCREL);
}
var_part_size = 8;
break;
case C (SH64PCREL16_64, SH64PCREL48):
case C (SH64PCREL16PT_64, SH64PCREL48):
md_number_to_chars (opcodep,
SHMEDIA_MOVI_OPC
| (SHMEDIA_TEMP_REG << 4)
| ((((reloc_needed
? 0 : (target_address - (opcode_address + 12))
) >> 32) & 65535) << 10),
4);
md_number_to_chars (var_partp,
SHMEDIA_SHORI_OPC
| (SHMEDIA_TEMP_REG << 4)
| ((((reloc_needed
? 0 : (target_address - (opcode_address + 12))
) >> 16) & 65535) << 10),
4);
md_number_to_chars (var_partp + 4,
SHMEDIA_SHORI_OPC
| (SHMEDIA_TEMP_REG << 4)
| (((reloc_needed
? 0 : (target_address - (opcode_address + 12)))
& 65535) << 10),
4);
md_number_to_chars (var_partp + 8,
SHMEDIA_PTREL_OPC | (insn & SHMEDIA_LIKELY_BIT)
| (SHMEDIA_TEMP_REG << 10)
| (insn & (7 << 4)),
4);
if (reloc_needed)
{
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset - 12, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDHI16
: BFD_RELOC_SH_IMM_MEDHI16_PCREL);
fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
fragP->fr_offset - 8, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDLOW16
: BFD_RELOC_SH_IMM_MEDLOW16_PCREL);
fix_new (fragP, var_partp - fragP->fr_literal + 4, 4, fragP->fr_symbol,
fragP->fr_offset - 4, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_LOW16
: BFD_RELOC_SH_IMM_LOW16_PCREL);
}
var_part_size = 12;
break;
case C (SH64PCREL16_64, SH64PCRELPLT):
case C (SH64PCREL16PT_64, SH64PCRELPLT):
reloctype = BFD_RELOC_32_PLT_PCREL;
reloc_needed = 1;
case C (SH64PCREL16_64, SH64PCREL64):
case C (SH64PCREL16PT_64, SH64PCREL64):
md_number_to_chars (opcodep,
SHMEDIA_MOVI_OPC
| (SHMEDIA_TEMP_REG << 4)
| ((((reloc_needed
? 0 : (target_address - (opcode_address + 16))
) >> 48) & 65535) << 10),
4);
md_number_to_chars (var_partp,
SHMEDIA_SHORI_OPC
| (SHMEDIA_TEMP_REG << 4)
| ((((reloc_needed
? 0 : (target_address - (opcode_address + 16))
) >> 32) & 65535) << 10),
4);
md_number_to_chars (var_partp + 4,
SHMEDIA_SHORI_OPC
| (SHMEDIA_TEMP_REG << 4)
| ((((reloc_needed
? 0 : (target_address - (opcode_address + 16))
) >> 16) & 65535) << 10),
4);
md_number_to_chars (var_partp + 8,
SHMEDIA_SHORI_OPC
| (SHMEDIA_TEMP_REG << 4)
| (((reloc_needed
? 0 : (target_address - (opcode_address + 16)))
& 65535) << 10),
4);
md_number_to_chars (var_partp + 12,
SHMEDIA_PTREL_OPC | (insn & SHMEDIA_LIKELY_BIT)
| (SHMEDIA_TEMP_REG << 10)
| (insn & (7 << 4)),
4);
if (reloc_needed)
{
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset - 16, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_HI16
: BFD_RELOC_SH_IMM_HI16_PCREL);
fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
fragP->fr_offset - 12, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDHI16
: BFD_RELOC_SH_IMM_MEDHI16_PCREL);
fix_new (fragP, var_partp - fragP->fr_literal + 4, 4, fragP->fr_symbol,
fragP->fr_offset - 8, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDLOW16
: BFD_RELOC_SH_IMM_MEDLOW16_PCREL);
fix_new (fragP, var_partp - fragP->fr_literal + 8, 4, fragP->fr_symbol,
fragP->fr_offset - 4, 1,
reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_LOW16
: BFD_RELOC_SH_IMM_LOW16_PCREL);
}
var_part_size = 16;
break;
case C (MOVI_IMM_64, MOVI_GOTOFF):
reloctype = BFD_RELOC_32_GOTOFF;
reloc_needed = 1;
case C (MOVI_IMM_64, UNDEF_MOVI):
case C (MOVI_IMM_64, MOVI_64):
{
int reg = (insn >> 4) & 0x3f;
md_number_to_chars (opcodep, SHMEDIA_MOVI_OPC | (reg << 4), 4);
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset, 0,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_HI16
: reloctype == BFD_RELOC_32_GOTOFF
? BFD_RELOC_SH_GOTOFF_HI16
: (abort (), BFD_RELOC_SH_IMM_HI16));
md_number_to_chars (var_partp, SHMEDIA_SHORI_OPC | (reg << 4), 4);
fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
fragP->fr_offset, 0,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_MEDHI16
: reloctype == BFD_RELOC_32_GOTOFF
? BFD_RELOC_SH_GOTOFF_MEDHI16
: (abort (), BFD_RELOC_SH_IMM_MEDHI16));
md_number_to_chars (var_partp + 4,
SHMEDIA_SHORI_OPC | (reg << 4), 4);
fix_new (fragP, var_partp - fragP->fr_literal + 4, 4, fragP->fr_symbol,
fragP->fr_offset, 0,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_MEDLOW16
: reloctype == BFD_RELOC_32_GOTOFF
? BFD_RELOC_SH_GOTOFF_MEDLOW16
: (abort (), BFD_RELOC_SH_IMM_MEDLOW16));
md_number_to_chars (var_partp + 8,
SHMEDIA_SHORI_OPC | (reg << 4), 4);
fix_new (fragP, var_partp - fragP->fr_literal + 8, 4, fragP->fr_symbol,
fragP->fr_offset, 0,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_LOW16
: reloctype == BFD_RELOC_32_GOTOFF
? BFD_RELOC_SH_GOTOFF_LOW16
: (abort (), BFD_RELOC_SH_IMM_LOW16));
var_part_size = 12;
break;
}
case C (MOVI_IMM_32, MOVI_GOTOFF):
reloctype = BFD_RELOC_32_GOTOFF;
reloc_needed = 1;
case C (MOVI_IMM_32, UNDEF_MOVI):
case C (MOVI_IMM_32, MOVI_32):
{
int reg = (insn >> 4) & 0x3f;
md_number_to_chars (opcodep, SHMEDIA_MOVI_OPC | (reg << 4), 4);
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset, 0,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_MEDLOW16
: reloctype == BFD_RELOC_32_GOTOFF
? BFD_RELOC_SH_GOTOFF_MEDLOW16
: reloctype == BFD_RELOC_SH_GOTPC
? BFD_RELOC_SH_GOTPC_MEDLOW16
: reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDLOW16
: (abort (), BFD_RELOC_SH_IMM_MEDLOW16));
md_number_to_chars (var_partp,
SHMEDIA_SHORI_OPC | (reg << 4), 4);
fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
fragP->fr_offset, 0,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_LOW16
: reloctype == BFD_RELOC_32_GOTOFF
? BFD_RELOC_SH_GOTOFF_LOW16
: reloctype == BFD_RELOC_SH_GOTPC
? BFD_RELOC_SH_GOTPC_LOW16
: reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_LOW16
: (abort (), BFD_RELOC_SH_IMM_LOW16));
var_part_size = 4;
break;
}
case C (MOVI_IMM_32_PCREL, MOVI_16):
case C (MOVI_IMM_64_PCREL, MOVI_16):
md_number_to_chars (opcodep,
insn
| (((reloc_needed
? 0 : (target_address - opcode_address))
& 65535) << 10),
4);
if (reloc_needed)
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset, 1,
BFD_RELOC_SH_IMM_LOW16_PCREL);
var_part_size = 0;
break;
case C (MOVI_IMM_32, MOVI_16):
case C (MOVI_IMM_64, MOVI_16):
md_number_to_chars (opcodep,
insn
| (((reloc_needed ? 0 : target_address)
& 65535) << 10),
4);
if (reloc_needed)
abort ();
var_part_size = 0;
break;
case C (MOVI_IMM_32_PCREL, MOVI_PLT):
reloctype = BFD_RELOC_32_PLT_PCREL;
goto movi_imm_32_pcrel_reloc_needed;
case C (MOVI_IMM_32_PCREL, MOVI_GOTPC):
reloctype = BFD_RELOC_SH_GOTPC;
movi_imm_32_pcrel_reloc_needed:
reloc_needed = 1;
case C (MOVI_IMM_32_PCREL, MOVI_32):
case C (MOVI_IMM_64_PCREL, MOVI_32):
{
int reg = (insn >> 4) & 0x3f;
md_number_to_chars (opcodep,
insn
| (((((reloc_needed
? 0 : (target_address - opcode_address)))
>> 16) & 65535) << 10), 4);
md_number_to_chars (var_partp,
SHMEDIA_SHORI_OPC
| (reg << 4)
| (((reloc_needed
? 0 : (target_address - opcode_address))
& 65535) << 10), 4);
if (reloc_needed)
{
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset, 1,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_MEDLOW16_PCREL
: reloctype == BFD_RELOC_SH_GOTPC
? BFD_RELOC_SH_GOTPC_MEDLOW16
: reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDLOW16
: (abort (), BFD_RELOC_SH_IMM_MEDLOW16_PCREL));
fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
fragP->fr_offset + 4, 1,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_LOW16_PCREL
: reloctype == BFD_RELOC_SH_GOTPC
? BFD_RELOC_SH_GOTPC_LOW16
: reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_LOW16
: (abort (), BFD_RELOC_SH_IMM_LOW16_PCREL));
}
var_part_size = 4;
}
break;
case C (MOVI_IMM_32_PCREL, MOVI_48):
case C (MOVI_IMM_64_PCREL, MOVI_48):
{
int reg = (insn >> 4) & 0x3f;
md_number_to_chars (opcodep,
insn
| (((((reloc_needed
? 0 : (target_address - opcode_address)))
>> 32) & 65535) << 10), 4);
md_number_to_chars (var_partp,
SHMEDIA_SHORI_OPC
| (reg << 4)
| ((((reloc_needed
? 0 : (target_address - opcode_address))
>> 16) & 65535) << 10), 4);
md_number_to_chars (var_partp + 4,
SHMEDIA_SHORI_OPC
| (reg << 4)
| (((reloc_needed
? 0 : (target_address - opcode_address))
& 65535) << 10), 4);
if (reloc_needed)
{
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset, 1,
BFD_RELOC_SH_IMM_MEDHI16_PCREL);
fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
fragP->fr_offset + 4, 1, BFD_RELOC_SH_IMM_MEDLOW16_PCREL);
fix_new (fragP, var_partp - fragP->fr_literal + 4, 4, fragP->fr_symbol,
fragP->fr_offset + 8, 1, BFD_RELOC_SH_IMM_LOW16_PCREL);
}
var_part_size = 8;
}
break;
case C (MOVI_IMM_64_PCREL, MOVI_PLT):
reloctype = BFD_RELOC_32_PLT_PCREL;
goto movi_imm_64_pcrel_reloc_needed;
case C (MOVI_IMM_64_PCREL, MOVI_GOTPC):
reloctype = BFD_RELOC_SH_GOTPC;
movi_imm_64_pcrel_reloc_needed:
reloc_needed = 1;
case C (MOVI_IMM_32_PCREL, MOVI_64):
case C (MOVI_IMM_64_PCREL, MOVI_64):
{
int reg = (insn >> 4) & 0x3f;
md_number_to_chars (opcodep,
insn
| (((((reloc_needed
? 0 : (target_address - opcode_address)))
>> 48) & 65535) << 10), 4);
md_number_to_chars (var_partp,
SHMEDIA_SHORI_OPC
| (reg << 4)
| ((((reloc_needed
? 0 : (target_address - opcode_address))
>> 32) & 65535) << 10), 4);
md_number_to_chars (var_partp + 4,
SHMEDIA_SHORI_OPC
| (reg << 4)
| ((((reloc_needed
? 0 : (target_address - opcode_address))
>> 16) & 65535) << 10), 4);
md_number_to_chars (var_partp + 8,
SHMEDIA_SHORI_OPC
| (reg << 4)
| (((reloc_needed
? 0 : (target_address - opcode_address))
& 65535) << 10), 4);
if (reloc_needed)
{
fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
fragP->fr_symbol, fragP->fr_offset, 1,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_HI16_PCREL
: reloctype == BFD_RELOC_SH_GOTPC
? BFD_RELOC_SH_GOTPC_HI16
: reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_HI16
: (abort (), BFD_RELOC_SH_IMM_HI16_PCREL));
fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
fragP->fr_offset + 4, 1,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_MEDHI16_PCREL
: reloctype == BFD_RELOC_SH_GOTPC
? BFD_RELOC_SH_GOTPC_MEDHI16
: reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDHI16
: (abort (), BFD_RELOC_SH_IMM_MEDHI16_PCREL));
fix_new (fragP, var_partp - fragP->fr_literal + 4, 4,
fragP->fr_symbol,
fragP->fr_offset + 8, 1,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_MEDLOW16_PCREL
: reloctype == BFD_RELOC_SH_GOTPC
? BFD_RELOC_SH_GOTPC_MEDLOW16
: reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_MEDLOW16
: (abort (), BFD_RELOC_SH_IMM_MEDLOW16_PCREL));
fix_new (fragP, var_partp - fragP->fr_literal + 8, 4,
fragP->fr_symbol,
fragP->fr_offset + 12, 1,
reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_IMM_LOW16_PCREL
: reloctype == BFD_RELOC_SH_GOTPC
? BFD_RELOC_SH_GOTPC_LOW16
: reloctype == BFD_RELOC_32_PLT_PCREL
? BFD_RELOC_SH_PLT_LOW16
: (abort (), BFD_RELOC_SH_IMM_LOW16_PCREL));
}
var_part_size = 12;
}
break;
default:
BAD_CASE (fragP->fr_subtype);
}
fragP->fr_fix += var_part_size;
fragP->fr_var = 0;
}
static unsigned long
shmedia_mask_number (unsigned long number, bfd_reloc_code_real_type how)
{
switch (how)
{
case BFD_RELOC_SH_IMMU5:
number &= (1 << 5) - 1;
break;
case BFD_RELOC_SH_IMMS6:
case BFD_RELOC_SH_IMMU6:
number &= (1 << 6) - 1;
break;
case BFD_RELOC_SH_IMMS6BY32:
number = (number & ((1 << (6 + 5)) - 1)) >> 5;
break;
case BFD_RELOC_SH_IMMS10:
number &= (1 << 10) - 1;
break;
case BFD_RELOC_SH_IMMS10BY2:
number = (number & ((1 << (10 + 1)) - 1)) >> 1;
break;
case BFD_RELOC_SH_IMMS10BY4:
number = (number & ((1 << (10 + 2)) - 1)) >> 2;
break;
case BFD_RELOC_SH_IMMS10BY8:
number = (number & ((1 << (10 + 3)) - 1)) >> 3;
break;
case BFD_RELOC_SH_IMMS16:
case BFD_RELOC_SH_IMMU16:
number &= (1 << 16) - 1;
break;
default:
BAD_CASE (how);
}
return number;
}
static void
shmedia_check_limits (offsetT *valp, bfd_reloc_code_real_type reloc,
fixS *fixp)
{
offsetT val = *valp;
char *msg = NULL;
switch (reloc)
{
case BFD_RELOC_SH_IMMU5:
if (val < 0 || val > (1 << 5) - 1)
msg = _("invalid operand, not a 5-bit unsigned value: %d");
break;
case BFD_RELOC_SH_IMMS6:
if (val < -(1 << 5) || val > (1 << 5) - 1)
msg = _("invalid operand, not a 6-bit signed value: %d");
break;
case BFD_RELOC_SH_IMMU6:
if (val < 0 || val > (1 << 6) - 1)
msg = _("invalid operand, not a 6-bit unsigned value: %d");
break;
case BFD_RELOC_SH_IMMS6BY32:
if (val < -(1 << 10) || val > (1 << 10) - 1)
msg = _("invalid operand, not a 11-bit signed value: %d");
else if (val & 31)
msg = _("invalid operand, not a multiple of 32: %d");
break;
case BFD_RELOC_SH_IMMS10:
if (val < -(1 << 9) || val > (1 << 9) - 1)
msg = _("invalid operand, not a 10-bit signed value: %d");
break;
case BFD_RELOC_SH_IMMS10BY2:
if (val < -(1 << 10) || val > (1 << 10) - 1)
msg = _("invalid operand, not a 11-bit signed value: %d");
else if (val & 1)
msg = _("invalid operand, not an even value: %d");
break;
case BFD_RELOC_SH_IMMS10BY4:
if (val < -(1 << 11) || val > (1 << 11) - 1)
msg = _("invalid operand, not a 12-bit signed value: %d");
else if (val & 3)
msg = _("invalid operand, not a multiple of 4: %d");
break;
case BFD_RELOC_SH_IMMS10BY8:
if (val < -(1 << 12) || val > (1 << 12) - 1)
msg = _("invalid operand, not a 13-bit signed value: %d");
else if (val & 7)
msg = _("invalid operand, not a multiple of 8: %d");
break;
case BFD_RELOC_SH_IMMS16:
if (val < -(1 << 15) || val > (1 << 15) - 1)
msg = _("invalid operand, not a 16-bit signed value: %d");
break;
case BFD_RELOC_SH_IMMU16:
if (val < 0 || val > (1 << 16) - 1)
msg = _("invalid operand, not an 16-bit unsigned value: %d");
break;
case BFD_RELOC_SH_PT_16:
case SHMEDIA_BFD_RELOC_PT:
if (val < -(1 << 15) * 4 || val > ((1 << 15) - 1) * 4 + 1)
msg = _("operand out of range for PT, PTA and PTB");
else if ((val % 4) != 0 && ((val - 1) % 4) != 0)
msg = _("operand not a multiple of 4 for PT, PTA or PTB: %d");
break;
case BFD_RELOC_SH_IMM_HI16:
case BFD_RELOC_SH_IMM_MEDHI16:
case BFD_RELOC_SH_IMM_MEDLOW16:
case BFD_RELOC_SH_IMM_LOW16:
case BFD_RELOC_SH_IMM_HI16_PCREL:
case BFD_RELOC_SH_IMM_MEDHI16_PCREL:
case BFD_RELOC_SH_IMM_MEDLOW16_PCREL:
case BFD_RELOC_SH_IMM_LOW16_PCREL:
case BFD_RELOC_SH_SHMEDIA_CODE:
break;
case BFD_RELOC_64:
break;
default:
BAD_CASE (reloc);
}
if (msg)
{
if (fixp)
as_bad_where (fixp->fx_file, fixp->fx_line, msg, val);
else
as_bad (msg, val);
}
}
static unsigned long
shmedia_immediate_op (char *where, shmedia_operand_info *op, int pcrel,
bfd_reloc_code_real_type how)
{
unsigned long retval = 0;
if (op->immediate.X_op != O_constant || pcrel)
fix_new_exp (frag_now,
where - frag_now->fr_literal,
4,
&op->immediate,
pcrel,
how);
else
{
shmedia_check_limits (&op->immediate.X_add_number, how, NULL);
retval
= shmedia_mask_number ((unsigned long) op->immediate.X_add_number,
how);
}
return retval << 10;
}
static int
shmedia_parse_reg (char *src, int *mode, int *reg, shmedia_arg_type argtype)
{
int l0 = TOLOWER (src[0]);
int l1 = l0 ? TOLOWER (src[1]) : 0;
if (l0 == 'r')
{
if (src[1] >= '1' && src[1] <= '5')
{
if (src[2] >= '0' && src[2] <= '9'
&& ! IDENT_CHAR ((unsigned char) src[3]))
{
*mode = A_GREG_M;
*reg = 10 * (src[1] - '0') + src[2] - '0';
return 3;
}
}
if (src[1] == '6')
{
if (src[2] >= '0' && src[2] <= '3'
&& ! IDENT_CHAR ((unsigned char) src[3]))
{
*mode = A_GREG_M;
*reg = 60 + src[2] - '0';
return 3;
}
}
if (src[1] >= '0' && src[1] <= '9'
&& ! IDENT_CHAR ((unsigned char) src[2]))
{
*mode = A_GREG_M;
*reg = (src[1] - '0');
return 2;
}
}
if (l0 == 't' && l1 == 'r')
{
if (src[2] >= '0' && src[2] <= '7'
&& ! IDENT_CHAR ((unsigned char) src[3]))
{
*mode = A_TREG_B;
*reg = (src[2] - '0');
return 3;
}
}
if (l0 == 'f' && l1 == 'r')
{
if (src[2] >= '1' && src[2] <= '5')
{
if (src[3] >= '0' && src[3] <= '9'
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_FREG_G;
*reg = 10 * (src[2] - '0') + src[3] - '0';
return 4;
}
}
if (src[2] == '6')
{
if (src[3] >= '0' && src[3] <= '3'
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_FREG_G;
*reg = 60 + src[3] - '0';
return 4;
}
}
if (src[2] >= '0' && src[2] <= '9'
&& ! IDENT_CHAR ((unsigned char) src[3]))
{
*mode = A_FREG_G;
*reg = (src[2] - '0');
return 3;
}
}
if (l0 == 'f' && l1 == 'v')
{
if (src[2] >= '1' && src[2] <= '5')
{
if (src[3] >= '0' && src[3] <= '9'
&& ((10 * (src[2] - '0') + src[3] - '0') % 4) == 0
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_FVREG_G;
*reg = 10 * (src[2] - '0') + src[3] - '0';
return 4;
}
}
if (src[2] == '6')
{
if (src[3] == '0'
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_FVREG_G;
*reg = 60 + src[3] - '0';
return 4;
}
}
if (src[2] >= '0' && src[2] <= '9'
&& ((src[2] - '0') % 4) == 0
&& ! IDENT_CHAR ((unsigned char) src[3]))
{
*mode = A_FVREG_G;
*reg = (src[2] - '0');
return 3;
}
}
if (l0 == 'd' && l1 == 'r')
{
if (src[2] >= '1' && src[2] <= '5')
{
if (src[3] >= '0' && src[3] <= '9'
&& ((src[3] - '0') % 2) == 0
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_DREG_G;
*reg = 10 * (src[2] - '0') + src[3] - '0';
return 4;
}
}
if (src[2] == '6')
{
if ((src[3] == '0' || src[3] == '2')
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_DREG_G;
*reg = 60 + src[3] - '0';
return 4;
}
}
if (src[2] >= '0' && src[2] <= '9'
&& ((src[2] - '0') % 2) == 0
&& ! IDENT_CHAR ((unsigned char) src[3]))
{
*mode = A_DREG_G;
*reg = (src[2] - '0');
return 3;
}
}
if (l0 == 'f' && l1 == 'p')
{
if (src[2] >= '1' && src[2] <= '5')
{
if (src[3] >= '0' && src[3] <= '9'
&& ((src[3] - '0') % 2) == 0
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_FPREG_G;
*reg = 10 * (src[2] - '0') + src[3] - '0';
return 4;
}
}
if (src[2] == '6')
{
if ((src[3] == '0' || src[3] == '2')
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_FPREG_G;
*reg = 60 + src[3] - '0';
return 4;
}
}
if (src[2] >= '0' && src[2] <= '9'
&& ((src[2] - '0') % 2) == 0
&& ! IDENT_CHAR ((unsigned char) src[3]))
{
*mode = A_FPREG_G;
*reg = (src[2] - '0');
return 3;
}
}
if (l0 == 'm' && strncasecmp (src, "mtrx", 4) == 0)
{
if (src[4] == '0' && ! IDENT_CHAR ((unsigned char) src[5]))
{
*mode = A_FMREG_G;
*reg = 0;
return 5;
}
if (src[4] == '1' && src[5] == '6'
&& ! IDENT_CHAR ((unsigned char) src[6]))
{
*mode = A_FMREG_G;
*reg = 16;
return 6;
}
if (src[4] == '3' && src[5] == '2'
&& ! IDENT_CHAR ((unsigned char) src[6]))
{
*mode = A_FMREG_G;
*reg = 32;
return 6;
}
if (src[4] == '4' && src[5] == '8'
&& ! IDENT_CHAR ((unsigned char) src[6]))
{
*mode = A_FMREG_G;
*reg = 48;
return 6;
}
}
if (l0 == 'c' && l1 == 'r')
{
if (src[2] >= '1' && src[2] <= '5')
{
if (src[3] >= '0' && src[3] <= '9'
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_CREG_K;
*reg = 10 * (src[2] - '0') + src[3] - '0';
return 4;
}
}
if (src[2] == '6')
{
if (src[3] >= '0' && src[3] <= '3'
&& ! IDENT_CHAR ((unsigned char) src[4]))
{
*mode = A_CREG_K;
*reg = 60 + src[3] - '0';
return 4;
}
}
if (src[2] >= '0' && src[2] <= '9'
&& ! IDENT_CHAR ((unsigned char) src[3]))
{
*mode = A_CREG_K;
*reg = (src[2] - '0');
return 3;
}
}
if (argtype == A_CREG_J || argtype == A_CREG_K)
{
const shmedia_creg_info *cregp;
int len = 0;
for (cregp = shmedia_creg_table; cregp->name != NULL; cregp++)
{
len = strlen (cregp->name);
if (strncasecmp (cregp->name, src, len) == 0
&& ! IDENT_CHAR (src[len]))
break;
}
if (cregp->name != NULL)
{
*mode = A_CREG_K;
*reg = cregp->cregno;
return len;
}
}
return 0;
}
static int
shmedia_md_estimate_size_before_relax (fragS *fragP,
segT segment_type ATTRIBUTE_UNUSED)
{
int old_fr_fix;
expressionS *exp;
bfd_boolean sym_relaxable
= (fragP->fr_symbol
&& S_GET_SEGMENT (fragP->fr_symbol) == segment_type
&& ! S_IS_EXTERNAL (fragP->fr_symbol)
&& ! S_IS_WEAK (fragP->fr_symbol));
old_fr_fix = fragP->fr_fix;
switch (fragP->fr_subtype)
{
case C (SH64PCREL16_32, UNDEF_SH64PCREL):
case C (SH64PCREL16PT_32, UNDEF_SH64PCREL):
if (sym_relaxable)
{
int what = GET_WHAT (fragP->fr_subtype);
fragP->fr_subtype = C (what, SH64PCREL16);
}
else
{
int what = GET_WHAT (fragP->fr_subtype);
fragP->fr_subtype = C (what, SH64PCREL32);
}
fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
break;
case C (SH64PCREL16_64, UNDEF_SH64PCREL):
case C (SH64PCREL16PT_64, UNDEF_SH64PCREL):
if (sym_relaxable)
{
int what = GET_WHAT (fragP->fr_subtype);
fragP->fr_subtype = C (what, SH64PCREL16);
}
else
{
int what = GET_WHAT (fragP->fr_subtype);
fragP->fr_subtype = C (what, SH64PCREL64);
}
fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
break;
case C (MOVI_IMM_64, UNDEF_MOVI):
case C (MOVI_IMM_32, UNDEF_MOVI):
exp = NULL;
if (fragP->fr_symbol != NULL
&& (exp = symbol_get_value_expression (fragP->fr_symbol)) != NULL
&& exp->X_op == O_subtract
&& exp->X_op_symbol != NULL
&& S_GET_SEGMENT (exp->X_op_symbol) == segment_type)
{
int what = GET_WHAT (fragP->fr_subtype);
int what_high = what == MOVI_IMM_32 ? MOVI_32 : MOVI_64;
expressionS *opexp
= symbol_get_value_expression (exp->X_op_symbol);
expressionS *addexp
= symbol_get_value_expression (exp->X_add_symbol);
if (opexp != NULL
&& addexp != NULL
&& opexp->X_op == O_constant
&& fragP == symbol_get_frag (exp->X_op_symbol))
{
fragP->fr_offset
= (exp->X_add_number
- (opexp->X_add_number - (fragP->fr_fix - 4)));
fragP->fr_symbol = exp->X_add_symbol;
what = what == MOVI_IMM_32
? MOVI_IMM_32_PCREL : MOVI_IMM_64_PCREL;
if (S_GET_SEGMENT (exp->X_add_symbol) == segment_type
&& ! S_IS_EXTERNAL (exp->X_add_symbol)
&& ! S_IS_WEAK (exp->X_add_symbol))
fragP->fr_subtype = C (what, MOVI_16);
else
fragP->fr_subtype = C (what, what_high);
}
else
fragP->fr_subtype = C (what, what_high);
}
else if (fragP->fr_symbol == NULL
|| (S_GET_SEGMENT (fragP->fr_symbol) == absolute_section
&& exp->X_op == O_constant))
{
unsigned long insn
= (target_big_endian
? bfd_getb32 (fragP->fr_opcode)
: bfd_getl32 (fragP->fr_opcode));
offsetT one = (offsetT) 1;
offsetT value = fragP->fr_offset
+ (fragP->fr_symbol == NULL ? 0 : S_GET_VALUE (fragP->fr_symbol));
if (value >= ((offsetT) -1 << 15) && value < ((offsetT) 1 << 15))
{
int what = GET_WHAT (fragP->fr_subtype);
fragP->fr_subtype = C (what, MOVI_16);
md_number_to_chars (fragP->fr_opcode,
insn | ((value & ((1 << 16) - 1)) << 10),
4);
}
else if (value >= -(one << 31)
&& (value < (one << 31)
|| (sh64_abi == sh64_abi_32 && value < (one << 32))))
{
int reg = (insn >> 4) & 0x3f;
md_number_to_chars (fragP->fr_opcode,
insn
| (((value >> 16) & ((1 << 16) - 1)) << 10),
4);
md_number_to_chars (fragP->fr_literal + old_fr_fix,
SHMEDIA_SHORI_OPC
| (reg << 4)
| ((value & ((1 << 16) - 1)) << 10),
4);
fragP->fr_fix += 4;
}
else if (GET_WHAT (fragP->fr_subtype) == MOVI_IMM_32)
{
as_bad_where (fragP->fr_file, fragP->fr_line,
_("MOVI operand is not a 32-bit signed value: 0x%8x%08x"),
((unsigned int) (value >> 32)
& (unsigned int) 0xffffffff),
(unsigned int) value & (unsigned int) 0xffffffff);
fragP->fr_fix += 4;
}
else if (value >= -(one << 47) && value < (one << 47))
{
int reg = (insn >> 4) & 0x3f;
md_number_to_chars (fragP->fr_opcode,
insn
| (((value >> 32) & ((1 << 16) - 1)) << 10),
4);
md_number_to_chars (fragP->fr_literal + old_fr_fix,
SHMEDIA_SHORI_OPC
| (reg << 4)
| (((value >> 16) & ((1 << 16) - 1)) << 10),
4);
md_number_to_chars (fragP->fr_literal + old_fr_fix + 4,
SHMEDIA_SHORI_OPC
| (reg << 4)
| ((value & ((1 << 16) - 1)) << 10),
4);
fragP->fr_fix += 8;
}
else
{
int reg = (insn >> 4) & 0x3f;
md_number_to_chars (fragP->fr_opcode,
insn
| (((value >> 48) & ((1 << 16) - 1)) << 10),
4);
md_number_to_chars (fragP->fr_literal + old_fr_fix,
SHMEDIA_SHORI_OPC
| (reg << 4)
| (((value >> 32) & ((1 << 16) - 1)) << 10),
4);
md_number_to_chars (fragP->fr_literal + old_fr_fix + 4,
SHMEDIA_SHORI_OPC
| (reg << 4)
| (((value >> 16) & ((1 << 16) - 1)) << 10),
4);
md_number_to_chars (fragP->fr_literal + old_fr_fix + 8,
SHMEDIA_SHORI_OPC
| (reg << 4)
| ((value & ((1 << 16) - 1)) << 10), 4);
fragP->fr_fix += 12;
}
frag_wane (fragP);
break;
}
fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
break;
case C (SH64PCREL16_32, SH64PCREL16):
case C (SH64PCREL16PT_32, SH64PCREL16):
case C (SH64PCREL16_32, SH64PCREL32):
case C (SH64PCREL16PT_32, SH64PCREL32):
case C (SH64PCREL16_32, SH64PCRELPLT):
case C (SH64PCREL16PT_32, SH64PCRELPLT):
case C (SH64PCREL16_64, SH64PCREL16):
case C (SH64PCREL16PT_64, SH64PCREL16):
case C (SH64PCREL16_64, SH64PCREL32):
case C (SH64PCREL16PT_64, SH64PCREL32):
case C (SH64PCREL16_64, SH64PCREL48):
case C (SH64PCREL16PT_64, SH64PCREL48):
case C (SH64PCREL16_64, SH64PCREL64):
case C (SH64PCREL16PT_64, SH64PCREL64):
case C (SH64PCREL16_64, SH64PCRELPLT):
case C (SH64PCREL16PT_64, SH64PCRELPLT):
case C (MOVI_IMM_32, MOVI_16):
case C (MOVI_IMM_32, MOVI_32):
case C (MOVI_IMM_32, MOVI_GOTOFF):
case C (MOVI_IMM_32_PCREL, MOVI_16):
case C (MOVI_IMM_32_PCREL, MOVI_32):
case C (MOVI_IMM_32_PCREL, MOVI_PLT):
case C (MOVI_IMM_32_PCREL, MOVI_GOTPC):
case C (MOVI_IMM_64, MOVI_16):
case C (MOVI_IMM_64, MOVI_32):
case C (MOVI_IMM_64, MOVI_48):
case C (MOVI_IMM_64, MOVI_64):
case C (MOVI_IMM_64, MOVI_GOTOFF):
case C (MOVI_IMM_64_PCREL, MOVI_16):
case C (MOVI_IMM_64_PCREL, MOVI_32):
case C (MOVI_IMM_64_PCREL, MOVI_48):
case C (MOVI_IMM_64_PCREL, MOVI_64):
case C (MOVI_IMM_64_PCREL, MOVI_PLT):
case C (MOVI_IMM_64_PCREL, MOVI_GOTPC):
fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
break;
default:
abort ();
}
return fragP->fr_var + (fragP->fr_fix - old_fr_fix);
}
static char *
shmedia_parse_exp (char *s, shmedia_operand_info *op)
{
char *save;
char *new;
save = input_line_pointer;
input_line_pointer = s;
expression (&op->immediate);
if (op->immediate.X_op == O_absent)
as_bad (_("missing operand"));
new = input_line_pointer;
input_line_pointer = save;
return new;
}
static void
shmedia_get_operand (char **ptr, shmedia_operand_info *op,
shmedia_arg_type argtype)
{
char *src = *ptr;
int mode = -1;
unsigned int len;
len = shmedia_parse_reg (src, &mode, &(op->reg), argtype);
if (len)
{
*ptr = src + len;
op->type = mode;
}
else
{
*ptr = shmedia_parse_exp (src, op);
op->type = A_IMMM;
op->reloctype = BFD_RELOC_NONE;
}
}
static char *
shmedia_get_operands (shmedia_opcode_info *info, char *args,
shmedia_operands_info *operands)
{
char *ptr = args;
int i;
if (*ptr == ' ')
ptr++;
for (i = 0; info->arg[i] != 0; i++)
{
memset (operands->operands + i, 0, sizeof (operands->operands[0]));
if (info->arg[i] == A_REUSE_PREV)
continue;
shmedia_get_operand (&ptr, &operands->operands[i], info->arg[i]);
switch (info->arg[i])
{
case A_GREG_M:
case A_GREG_N:
case A_GREG_D:
if (operands->operands[i].type != A_GREG_M)
return NULL;
break;
case A_FREG_G:
case A_FREG_H:
case A_FREG_F:
if (operands->operands[i].type != A_FREG_G)
return NULL;
break;
case A_FVREG_G:
case A_FVREG_H:
case A_FVREG_F:
if (operands->operands[i].type != A_FVREG_G)
return NULL;
break;
case A_FMREG_G:
case A_FMREG_H:
case A_FMREG_F:
if (operands->operands[i].type != A_FMREG_G)
return NULL;
break;
case A_FPREG_G:
case A_FPREG_H:
case A_FPREG_F:
if (operands->operands[i].type != A_FPREG_G)
return NULL;
break;
case A_DREG_G:
case A_DREG_H:
case A_DREG_F:
if (operands->operands[i].type != A_DREG_G)
return NULL;
break;
case A_TREG_A:
case A_TREG_B:
if (operands->operands[i].type != A_TREG_B)
return NULL;
break;
case A_CREG_J:
case A_CREG_K:
if (operands->operands[i].type != A_CREG_K)
return NULL;
break;
case A_IMMS16:
case A_IMMU16:
{
expressionS *imm_expr = &operands->operands[i].immediate;
expressionS *right_expr;
if (operands->operands[i].type == A_IMMM
&& imm_expr->X_op == O_bit_and
&& imm_expr->X_op_symbol != NULL
&& ((right_expr
= symbol_get_value_expression (imm_expr->X_op_symbol))
->X_op == O_constant)
&& right_expr->X_add_number == 0xffff)
{
symbolS *inner = imm_expr->X_add_symbol;
bfd_reloc_code_real_type reloctype = BFD_RELOC_SH_IMM_LOW16;
expressionS *inner_expr
= symbol_get_value_expression (inner);
if (inner_expr->X_op == O_right_shift)
{
expressionS *inner_right;
if (inner_expr->X_op_symbol != NULL
&& ((inner_right
= symbol_get_value_expression (inner_expr
->X_op_symbol))
->X_op == O_constant))
{
offsetT addnum
= inner_right->X_add_number;
if (addnum == 0 || addnum == 16 || addnum == 32
|| addnum == 48)
{
reloctype
= (addnum == 0
? BFD_RELOC_SH_IMM_LOW16
: (addnum == 16
? BFD_RELOC_SH_IMM_MEDLOW16
: (addnum == 32
? BFD_RELOC_SH_IMM_MEDHI16
: BFD_RELOC_SH_IMM_HI16)));
inner = inner_expr->X_add_symbol;
inner_expr = symbol_get_value_expression (inner);
}
}
}
if (inner_expr->X_op == O_constant
&& S_GET_SEGMENT (inner) != absolute_section)
{
operands->operands[i].immediate.X_op = O_symbol;
operands->operands[i].immediate.X_add_symbol = inner;
operands->operands[i].immediate.X_add_number = 0;
}
else
operands->operands[i].immediate
= *symbol_get_value_expression (inner);
operands->operands[i].reloctype = reloctype;
}
}
case A_IMMS6:
case A_IMMS6BY32:
case A_IMMS10:
case A_IMMS10BY1:
case A_IMMS10BY2:
case A_IMMS10BY4:
case A_IMMS10BY8:
case A_PCIMMS16BY4:
case A_PCIMMS16BY4_PT:
case A_IMMU5:
case A_IMMU6:
if (operands->operands[i].type != A_IMMM)
return NULL;
if (sh_check_fixup (&operands->operands[i].immediate,
&operands->operands[i].reloctype))
{
as_bad (_("invalid PIC reference"));
return NULL;
}
break;
default:
BAD_CASE (info->arg[i]);
}
if (*ptr == ',' && info->arg[i + 1])
ptr++;
}
return ptr;
}
static shmedia_opcode_info *
shmedia_find_cooked_opcode (char **str_p)
{
char *str = *str_p;
char *op_start;
char *op_end;
char name[20];
unsigned int nlen = 0;
while (*str == ' ')
str++;
for (op_start = op_end = str;
*op_end
&& nlen < sizeof (name) - 1
&& ! is_end_of_line[(unsigned char) *op_end]
&& ! ISSPACE ((unsigned char) *op_end);
op_end++)
{
unsigned char c = op_start[nlen];
c = TOLOWER (c);
name[nlen] = c;
nlen++;
}
name[nlen] = 0;
*str_p = op_end;
if (nlen == 0)
as_bad (_("can't find opcode"));
return
(shmedia_opcode_info *) hash_find (shmedia_opcode_hash_control, name);
}
static int
shmedia_build_Mytes (shmedia_opcode_info *opcode,
shmedia_operands_info *operands)
{
unsigned long insn = opcode->opcode_base;
int i, j;
char *insn_loc = frag_more (4);
dwarf2_emit_insn (3);
sh64_last_insn_frag = frag_now;
for (i = 0, j = 0; opcode->arg[i]; i++)
{
shmedia_arg_type argtype = opcode->arg[i];
shmedia_operand_info *opjp = &operands->operands[j];
switch (argtype)
{
case A_TREG_A:
case A_TREG_B:
case A_GREG_M:
case A_GREG_N:
case A_GREG_D:
case A_FREG_G:
case A_FREG_H:
case A_FREG_F:
case A_FVREG_G:
case A_FVREG_H:
case A_FVREG_F:
case A_FMREG_G:
case A_FMREG_H:
case A_FMREG_F:
case A_FPREG_G:
case A_FPREG_H:
case A_FPREG_F:
case A_DREG_G:
case A_DREG_H:
case A_DREG_F:
case A_CREG_J:
case A_CREG_K:
insn |= (opjp->reg << opcode->nibbles[i]);
j++;
break;
case A_REUSE_PREV:
insn |= (operands->operands[j - 1].reg << opcode->nibbles[i]);
j++;
break;
case A_IMMS6:
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_IMMS6);
j++;
break;
case A_IMMS6BY32:
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_IMMS6BY32);
j++;
break;
case A_IMMS10BY1:
case A_IMMS10:
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_IMMS10);
j++;
break;
case A_IMMS10BY2:
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_IMMS10BY2);
j++;
break;
case A_IMMS10BY4:
if (opjp->reloctype == BFD_RELOC_NONE)
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_IMMS10BY4);
else if (opjp->reloctype == BFD_RELOC_SH_GOTPLT32)
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_GOTPLT10BY4);
else if (opjp->reloctype == BFD_RELOC_32_GOT_PCREL)
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_GOT10BY4);
else
as_bad (_("invalid PIC reference"));
j++;
break;
case A_IMMS10BY8:
if (opjp->reloctype == BFD_RELOC_NONE)
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_IMMS10BY8);
else if (opjp->reloctype == BFD_RELOC_SH_GOTPLT32)
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_GOTPLT10BY8);
else if (opjp->reloctype == BFD_RELOC_32_GOT_PCREL)
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_GOT10BY8);
else
as_bad (_("invalid PIC reference"));
j++;
break;
case A_IMMS16:
if (opjp->reloctype == BFD_RELOC_32_GOT_PCREL)
opjp->reloctype = BFD_RELOC_SH_GOT_LOW16;
else if (opjp->reloctype == BFD_RELOC_SH_GOTPLT32)
opjp->reloctype = BFD_RELOC_SH_GOTPLT_LOW16;
if ((opjp->reloctype == BFD_RELOC_NONE
|| opjp->reloctype == BFD_RELOC_32_GOTOFF
|| opjp->reloctype == BFD_RELOC_32_PLT_PCREL
|| opjp->reloctype == BFD_RELOC_SH_GOTPC)
&& opcode->opcode_base == SHMEDIA_MOVI_OPC
&& (opjp->immediate.X_op != O_constant
|| opjp->immediate.X_add_number < -32768
|| opjp->immediate.X_add_number > 32767)
&& (sh64_expand
|| opjp->reloctype == BFD_RELOC_32_GOTOFF
|| opjp->reloctype == BFD_RELOC_32_PLT_PCREL
|| opjp->reloctype == BFD_RELOC_SH_GOTPC))
{
int what = sh64_abi == sh64_abi_64 ? MOVI_IMM_64 : MOVI_IMM_32;
offsetT max = sh64_abi == sh64_abi_64 ? MOVI_64 : MOVI_32;
offsetT min = MOVI_16;
offsetT init = UNDEF_MOVI;
valueT addvalue
= opjp->immediate.X_op_symbol != NULL
? 0 : opjp->immediate.X_add_number;
symbolS *sym
= opjp->immediate.X_op_symbol != NULL
? make_expr_symbol (&opjp->immediate)
: opjp->immediate.X_add_symbol;
if (opjp->reloctype == BFD_RELOC_32_GOTOFF)
init = max = min = MOVI_GOTOFF;
else if (opjp->reloctype == BFD_RELOC_32_PLT_PCREL)
{
init = max = min = MOVI_PLT;
what = (sh64_abi == sh64_abi_64
? MOVI_IMM_64_PCREL
: MOVI_IMM_32_PCREL);
}
else if (opjp->reloctype == BFD_RELOC_SH_GOTPC)
{
init = max = min = MOVI_GOTPC;
what = (sh64_abi == sh64_abi_64
? MOVI_IMM_64_PCREL
: MOVI_IMM_32_PCREL);
}
frag_var (rs_machine_dependent,
md_relax_table[C (what, max)].rlx_length,
md_relax_table[C (what, min)].rlx_length,
C (what, init), sym, addvalue, insn_loc);
}
else
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
(opjp->reloctype
== BFD_RELOC_NONE)
? BFD_RELOC_SH_IMMS16
: opjp->reloctype);
j++;
break;
case A_PCIMMS16BY4:
{
int what
= ((sh64_abi == sh64_abi_64 && ! sh64_pt32)
? SH64PCREL16_64 : SH64PCREL16_32);
offsetT max
= ((sh64_abi == sh64_abi_64 && ! sh64_pt32)
? SH64PCREL64 : SH64PCREL32);
offsetT min = SH64PCREL16;
offsetT init = UNDEF_SH64PCREL;
if (opjp->immediate.X_op_symbol != NULL)
{
as_bad(_("invalid operand: expression in PT target"));
return 0;
}
if (opjp->reloctype == BFD_RELOC_32_PLT_PCREL)
init = max = min = SH64PCRELPLT;
if (sh64_expand || opjp->reloctype != BFD_RELOC_NONE)
frag_var (rs_machine_dependent,
md_relax_table[C (what, max)].rlx_length,
md_relax_table[C (what, min)].rlx_length,
C (what, init),
opjp->immediate.X_add_symbol,
opjp->immediate.X_add_number,
insn_loc);
else
insn |= shmedia_immediate_op (insn_loc, opjp, 1,
opjp->reloctype == BFD_RELOC_NONE
? BFD_RELOC_SH_PT_16
: opjp->reloctype);
j++;
break;
}
case A_PCIMMS16BY4_PT:
{
int what
= ((sh64_abi == sh64_abi_64 && ! sh64_pt32)
? SH64PCREL16PT_64 : SH64PCREL16PT_32);
offsetT max
= ((sh64_abi == sh64_abi_64 && ! sh64_pt32)
? SH64PCREL64 : SH64PCREL32);
offsetT min = SH64PCREL16;
offsetT init = UNDEF_SH64PCREL;
if (opjp->immediate.X_op_symbol != NULL)
{
as_bad(_("invalid operand: expression in PT target"));
return 0;
}
if (opjp->reloctype == BFD_RELOC_32_PLT_PCREL)
init = max = min = SH64PCRELPLT;
if (sh64_expand || opjp->reloctype != BFD_RELOC_NONE)
frag_var (rs_machine_dependent,
md_relax_table[C (what, max)].rlx_length,
md_relax_table[C (what, min)].rlx_length,
C (what, init),
opjp->immediate.X_add_symbol,
opjp->immediate.X_add_number,
insn_loc);
else
insn |= shmedia_immediate_op (insn_loc, opjp, 1,
opjp->reloctype == BFD_RELOC_NONE
? SHMEDIA_BFD_RELOC_PT
: opjp->reloctype);
j++;
break;
}
case A_IMMU5:
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_IMMU5);
j++;
break;
case A_IMMU6:
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
BFD_RELOC_SH_IMMU6);
j++;
break;
case A_IMMU16:
insn |= shmedia_immediate_op (insn_loc, opjp, 0,
(opjp->reloctype
== BFD_RELOC_NONE)
? BFD_RELOC_SH_IMMU16
: opjp->reloctype);
j++;
break;
default:
BAD_CASE (argtype);
}
}
md_number_to_chars (insn_loc, insn, 4);
return 4;
}
static void
shmedia_md_assemble (char *str)
{
char *op_end;
shmedia_opcode_info *opcode;
shmedia_operands_info operands;
int size;
opcode = shmedia_find_cooked_opcode (&str);
op_end = str;
if (opcode == NULL)
{
as_bad (_("unknown opcode"));
return;
}
if (!seen_insn)
{
sh64_update_contents_mark (TRUE);
sh64_set_contents_type (CRT_SH5_ISA32);
seen_insn = TRUE;
}
op_end = shmedia_get_operands (opcode, op_end, &operands);
if (op_end == NULL)
{
as_bad (_("invalid operands to %s"), opcode->name);
return;
}
if (*op_end)
{
as_bad (_("excess operands to %s"), opcode->name);
return;
}
size = shmedia_build_Mytes (opcode, &operands);
if (size == 0)
return;
}
void
shmedia_md_begin (void)
{
const shmedia_opcode_info *shmedia_opcode;
shmedia_opcode_hash_control = hash_new ();
for (shmedia_opcode = shmedia_table;
shmedia_opcode->name;
shmedia_opcode++)
hash_insert (shmedia_opcode_hash_control, shmedia_opcode->name,
(char *) shmedia_opcode);
}
static void
s_sh64_mode (int ignore ATTRIBUTE_UNUSED)
{
char *name = input_line_pointer, ch;
sh64_update_contents_mark (TRUE);
while (!is_end_of_line[(unsigned char) *input_line_pointer])
input_line_pointer++;
ch = *input_line_pointer;
*input_line_pointer = '\0';
if (sh64_isa_mode == sh64_isa_unspecified)
as_bad (_("The `.mode %s' directive is not valid with this architecture"),
name);
if (strcasecmp (name, "shcompact") == 0)
sh64_isa_mode = sh64_isa_shcompact;
else if (strcasecmp (name, "shmedia") == 0)
sh64_isa_mode = sh64_isa_shmedia;
else
as_bad (_("Invalid argument to .mode: %s"), name);
frag_wane (frag_now);
frag_new (0);
sh64_update_contents_mark (FALSE);
*input_line_pointer = ch;
demand_empty_rest_of_line ();
}
static void
s_sh64_abi (int ignore ATTRIBUTE_UNUSED)
{
char *name = input_line_pointer, ch;
while (!is_end_of_line[(unsigned char) *input_line_pointer])
input_line_pointer++;
ch = *input_line_pointer;
*input_line_pointer = '\0';
if (sh64_abi == sh64_abi_unspecified)
as_bad (_("The `.abi %s' directive is not valid with this architecture"),
name);
if (strcmp (name, "64") == 0)
{
if (sh64_abi != sh64_abi_64)
as_bad (_("`.abi 64' but command-line options do not specify 64-bit ABI"));
}
else if (strcmp (name, "32") == 0)
{
if (sh64_abi != sh64_abi_32)
as_bad (_("`.abi 32' but command-line options do not specify 32-bit ABI"));
}
else
as_bad (_("Invalid argument to .abi: %s"), name);
*input_line_pointer = ch;
demand_empty_rest_of_line ();
}
const char *
sh64_target_format (void)
{
#ifdef TE_NetBSD
if (sh64_isa_mode == sh64_isa_unspecified)
sh64_isa_mode = sh64_isa_shmedia;
if (sh64_abi == sh64_abi_unspecified)
{
if (sh64_isa_mode == sh64_isa_shcompact)
sh64_abi = sh64_abi_32;
else if (strncmp (TARGET_CPU, "sh64", 4) == 0)
sh64_abi = sh64_abi_64;
else
sh64_abi = sh64_abi_32;
}
#endif
#ifdef TE_LINUX
if (sh64_isa_mode == sh64_isa_unspecified)
sh64_isa_mode = sh64_isa_shmedia;
if (sh64_abi == sh64_abi_unspecified)
sh64_abi = sh64_abi_32;
#endif
if (sh64_abi == sh64_abi_64 && sh64_isa_mode == sh64_isa_unspecified)
sh64_isa_mode = sh64_isa_shmedia;
if (sh64_abi == sh64_abi_32 && sh64_isa_mode == sh64_isa_unspecified)
sh64_isa_mode = sh64_isa_shcompact;
if (sh64_isa_mode == sh64_isa_shcompact
&& sh64_abi == sh64_abi_unspecified)
sh64_abi = sh64_abi_32;
if (sh64_isa_mode == sh64_isa_shmedia
&& sh64_abi == sh64_abi_unspecified)
sh64_abi = sh64_abi_64;
if (sh64_isa_mode == sh64_isa_unspecified && ! sh64_mix)
as_bad (_("-no-mix is invalid without specifying SHcompact or SHmedia"));
if ((sh64_isa_mode == sh64_isa_unspecified
|| sh64_isa_mode == sh64_isa_shmedia)
&& sh64_shcompact_const_crange)
as_bad (_("-shcompact-const-crange is invalid without SHcompact"));
if (sh64_pt32 && sh64_abi != sh64_abi_64)
as_bad (_("-expand-pt32 only valid with -abi=64"));
if (! sh64_expand && sh64_isa_mode == sh64_isa_unspecified)
as_bad (_("-no-expand only valid with SHcompact or SHmedia"));
if (sh64_pt32 && ! sh64_expand)
as_bad (_("-expand-pt32 invalid together with -no-expand"));
#ifdef TE_NetBSD
if (sh64_abi == sh64_abi_64)
return (target_big_endian ? "elf64-sh64-nbsd" : "elf64-sh64l-nbsd");
else
return (target_big_endian ? "elf32-sh64-nbsd" : "elf32-sh64l-nbsd");
#elif defined (TE_LINUX)
if (sh64_abi == sh64_abi_64)
return (target_big_endian ? "elf64-sh64big-linux" : "elf64-sh64-linux");
else
return (target_big_endian ? "elf32-sh64big-linux" : "elf32-sh64-linux");
#else
if (sh64_isa_mode == sh64_isa_unspecified)
return (target_big_endian ? "elf32-sh" : "elf32-shl");
else if (sh64_abi == sh64_abi_64)
return (target_big_endian ? "elf64-sh64" : "elf64-sh64l");
else
return (target_big_endian ? "elf32-sh64" : "elf32-sh64l");
#endif
}
int
sh64_target_mach (void)
{
return (sh64_abi == sh64_abi_64) ? bfd_mach_sh5 : 0;
}
valueT
shmedia_md_pcrel_from_section (struct fix *fixP, segT sec ATTRIBUTE_UNUSED)
{
know (fixP->fx_frag->fr_type == rs_machine_dependent);
switch (fixP->fx_r_type)
{
case BFD_RELOC_SH_IMM_LOW16:
case BFD_RELOC_SH_IMM_MEDLOW16:
case BFD_RELOC_SH_IMM_MEDHI16:
case BFD_RELOC_SH_IMM_HI16:
case BFD_RELOC_SH_IMM_LOW16_PCREL:
case BFD_RELOC_SH_IMM_MEDLOW16_PCREL:
case BFD_RELOC_SH_IMM_MEDHI16_PCREL:
case BFD_RELOC_SH_IMM_HI16_PCREL:
case BFD_RELOC_SH_IMMU5:
case BFD_RELOC_SH_IMMU6:
case BFD_RELOC_SH_IMMS6:
case BFD_RELOC_SH_IMMS10:
case BFD_RELOC_SH_IMMS10BY2:
case BFD_RELOC_SH_IMMS10BY4:
case BFD_RELOC_SH_IMMS10BY8:
case BFD_RELOC_SH_IMMS16:
case BFD_RELOC_SH_IMMU16:
case BFD_RELOC_SH_PT_16:
case SHMEDIA_BFD_RELOC_PT:
return SHMEDIA_MD_PCREL_FROM_FIX (fixP);
case BFD_RELOC_64:
case BFD_RELOC_64_PCREL:
know (0 );
break;
default:
return (valueT) md_pcrel_from_section (fixP, sec);
}
know (0 );
return 0;
}
static void
sh64_emit_crange (symbolS *startsym, symbolS *endsym,
enum sh64_elf_cr_type cr_type)
{
expressionS exp;
segT current_seg = now_seg;
subsegT current_subseg = now_subseg;
asection *cranges
= bfd_make_section_old_way (stdoutput,
SH64_CRANGES_SECTION_NAME);
subseg_set (cranges, 0);
exp.X_op = O_symbol;
exp.X_add_number = 0;
exp.X_op_symbol = NULL;
exp.X_add_symbol = startsym;
emit_expr (&exp, 4);
exp.X_op = O_subtract;
exp.X_add_number = 0;
exp.X_add_symbol = endsym;
exp.X_op_symbol = startsym;
emit_expr (&exp, 4);
exp.X_op = O_constant;
exp.X_add_number = cr_type;
exp.X_add_symbol = NULL;
exp.X_op_symbol = NULL;
emit_expr (&exp, 2);
subseg_set (current_seg, current_subseg);
}
static void
sh64_set_contents_type (enum sh64_elf_cr_type new_contents_type)
{
segment_info_type *seginfo;
know (!emitting_crange);
seginfo = seg_info (now_seg);
if (seginfo)
{
symbolS *symp = seginfo->tc_segment_info_data.last_contents_mark;
enum sh64_elf_cr_type contents_type
= seginfo->tc_segment_info_data.contents_type;
if (sh64_isa_mode == sh64_isa_shcompact
&& ! sh64_shcompact_const_crange)
new_contents_type = CRT_SH5_ISA16;
if (contents_type == new_contents_type)
return;
if (sh64_abi == sh64_abi_64)
{
seginfo->tc_segment_info_data.contents_type
= (new_contents_type == CRT_SH5_ISA32
|| contents_type == CRT_SH5_ISA32)
? CRT_SH5_ISA32 : new_contents_type;
return;
}
if (symp == NULL)
symp = symbol_new (FAKE_LABEL_NAME, now_seg, (valueT) frag_now_fix (),
frag_now);
seginfo->tc_segment_info_data.last_contents_mark = NULL;
S_SET_OTHER (symp, S_GET_OTHER (symp) & ~STO_SH5_ISA32);
if (seginfo->tc_segment_info_data.mode_start_symbol)
{
if (! sh64_mix
&& (new_contents_type == CRT_SH5_ISA32
|| contents_type == CRT_SH5_ISA32))
as_bad (
_("SHmedia code not allowed in same section as constants and SHcompact code"));
emitting_crange = TRUE;
sh64_emit_crange (seginfo->tc_segment_info_data.mode_start_symbol,
symp, contents_type);
emitting_crange = FALSE;
seginfo->tc_segment_info_data.emitted_ranges++;
}
seginfo->tc_segment_info_data.mode_start_symbol = symp;
seginfo->tc_segment_info_data.mode_start_subseg = now_subseg;
seginfo->tc_segment_info_data.contents_type = new_contents_type;
seginfo->tc_segment_info_data.in_code = 0;
}
else
as_bad (_("No segment info for current section"));
}
void
sh64_frob_label (symbolS *symp)
{
segT seg = S_GET_SEGMENT (symp);
static const symbolS *null = NULL;
symbol_set_tc (symp, (symbolS **) &null);
if (seg != NULL && sh64_isa_mode == sh64_isa_shmedia && subseg_text_p (seg))
S_SET_OTHER (symp, S_GET_OTHER (symp) | STO_SH5_ISA32);
}
int
sh64_consume_datalabel (const char *name, expressionS *exp, char *cp,
segT (*operandf) (expressionS *))
{
static int parsing_datalabel = 0;
if (strcasecmp (name, "datalabel") == 0)
{
int save_parsing_datalabel = parsing_datalabel;
if (parsing_datalabel)
as_bad (_("duplicate datalabel operator ignored"));
*input_line_pointer = *cp;
parsing_datalabel = 1;
(*operandf) (exp);
parsing_datalabel = save_parsing_datalabel;
if (exp->X_op == O_symbol || exp->X_op == O_PIC_reloc)
{
symbolS *symp = exp->X_add_symbol;
segT symseg = S_GET_SEGMENT (symp);
if (symseg != undefined_section
&& S_GET_OTHER (symp) != STO_SH5_ISA32)
;
else
{
symbolS *dl_symp;
const char *name = S_GET_NAME (symp);
char *dl_name
= xmalloc (strlen (name) + sizeof (DATALABEL_SUFFIX));
strcpy (dl_name, name);
strcat (dl_name, DATALABEL_SUFFIX);
if (strcmp (name, FAKE_LABEL_NAME) == 0)
dl_symp = symbol_make (dl_name);
else
dl_symp = symbol_find_or_make (dl_name);
free (dl_name);
symbol_set_value_expression (dl_symp,
symbol_get_value_expression (symp));
S_SET_SEGMENT (dl_symp, symseg);
symbol_set_frag (dl_symp, symbol_get_frag (symp));
symbol_set_tc (dl_symp, &symp);
copy_symbol_attributes (dl_symp, symp);
exp->X_add_symbol = dl_symp;
S_SET_OTHER (dl_symp, S_GET_OTHER (dl_symp) & ~STO_SH5_ISA32);
if (S_IS_WEAK (symp))
S_SET_WEAK (dl_symp);
else if (S_IS_EXTERNAL (symp))
S_SET_EXTERNAL (dl_symp);
}
}
else if (exp->X_op != O_illegal
&& exp->X_op != O_absent
&& exp->X_op != O_constant)
as_bad (_("Invalid DataLabel expression"));
*cp = *input_line_pointer;
return 1;
}
return sh_parse_name (name, exp, cp);
}
int
sh64_exclude_symbol (symbolS *symp)
{
symbolS *main_symbol = *symbol_get_tc (symp);
return main_symbol != NULL || ! S_IS_DEFINED (symp);
}
static void
sh64_update_contents_mark (bfd_boolean update_type)
{
segment_info_type *seginfo;
seginfo = seg_info (now_seg);
if (seginfo != NULL)
{
symbolS *symp = seginfo->tc_segment_info_data.last_contents_mark;
if (symp == NULL)
{
symp = symbol_new (FAKE_LABEL_NAME, now_seg,
(valueT) frag_now_fix (), frag_now);
seginfo->tc_segment_info_data.last_contents_mark = symp;
}
else
{
if ((S_GET_VALUE (symp) != frag_now_fix ()
|| symbol_get_frag (symp) != frag_now))
{
enum sh64_elf_cr_type contents_type
= seginfo->tc_segment_info_data.contents_type;
if (update_type
&& contents_type != CRT_DATA
&& contents_type != CRT_NONE
&& ! seen_insn)
{
sh64_set_contents_type (CRT_DATA);
symp = seginfo->tc_segment_info_data.last_contents_mark;
}
if (symp)
{
S_SET_VALUE (symp, (valueT) frag_now_fix ());
symbol_set_frag (symp, frag_now);
}
}
}
}
seen_insn = FALSE;
}
void
sh64_flush_pending_output (void)
{
sh64_update_contents_mark (TRUE);
sh_flush_pending_output ();
}
static void
sh64_flush_last_crange (bfd *abfd ATTRIBUTE_UNUSED, asection *seg,
void *countparg ATTRIBUTE_UNUSED)
{
segment_info_type *seginfo;
seginfo = seg_info (seg);
if (seginfo
&& seginfo->tc_segment_info_data.emitted_ranges != 0)
{
symbolS *symp;
subseg_set (seg, seginfo->tc_segment_info_data.mode_start_subseg);
symp = symbol_new (FAKE_LABEL_NAME, now_seg, (valueT) frag_now_fix (),
frag_now);
S_SET_OTHER (symp, S_GET_OTHER (symp) & ~STO_SH5_ISA32);
sh64_emit_crange (seginfo->tc_segment_info_data.mode_start_symbol,
symp,
seginfo->tc_segment_info_data.contents_type);
}
}
static void
sh64_flag_output (void)
{
if (sh64_isa_mode != sh64_isa_unspecified
&& !seen_insn
&& !sh64_end_of_assembly
&& !emitting_crange)
{
md_flush_pending_output ();
sh64_set_contents_type (CRT_DATA);
}
}
static char *
strip_datalabels (void)
{
char *src, *dest, *start=input_line_pointer;
for (src=input_line_pointer, dest=input_line_pointer; *src != '\n'; )
{
if (strncasecmp (src, "datalabel", 9) == 0
&& ISSPACE (src[9])
&& (src == start || !(ISALNUM (src[-1])) || src[-1] == '_'))
src += 10;
else
*dest++ = *src++;
}
if (dest < src)
*dest = '\n';
return src + 1;
}
static void
sh64_vtable_entry (int ignore ATTRIBUTE_UNUSED)
{
char *eol = strip_datalabels ();
obj_elf_vtable_entry (0);
input_line_pointer = eol;
}
static void
sh64_vtable_inherit (int ignore ATTRIBUTE_UNUSED)
{
char *eol = strip_datalabels ();
obj_elf_vtable_inherit (0);
input_line_pointer = eol;
}