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