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