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