Generate warning if the same destination register is used in parallel
[binutils.git] / gas / config / tc-sh.c
blob61a38fddf88624607063073bbb2fd3f3b38e5252
1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Written By Steve Chamberlain <sac@cygnus.com> */
24 #include <stdio.h>
25 #include "as.h"
26 #include "bfd.h"
27 #include "subsegs.h"
28 #define DEFINE_TABLE
29 #include "opcodes/sh-opc.h"
30 #include "safe-ctype.h"
31 #include "struc-symbol.h"
33 #ifdef OBJ_ELF
34 #include "elf/sh.h"
35 #endif
37 #include "dwarf2dbg.h"
39 typedef struct
41 sh_arg_type type;
42 int reg;
43 expressionS immediate;
45 sh_operand_info;
47 const char comment_chars[] = "!";
48 const char line_separator_chars[] = ";";
49 const char line_comment_chars[] = "!#";
51 static void s_uses PARAMS ((int));
53 static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
54 static void sh_frob_section PARAMS ((bfd *, segT, PTR));
56 static void s_uacons PARAMS ((int));
57 static sh_opcode_info *find_cooked_opcode PARAMS ((char **));
58 static unsigned int assemble_ppi PARAMS ((char *, sh_opcode_info *));
59 static void little PARAMS ((int));
60 static void big PARAMS ((int));
61 static int parse_reg PARAMS ((char *, int *, int *));
62 static char *parse_exp PARAMS ((char *, sh_operand_info *));
63 static char *parse_at PARAMS ((char *, sh_operand_info *));
64 static void get_operand PARAMS ((char **, sh_operand_info *));
65 static char *get_operands
66 PARAMS ((sh_opcode_info *, char *, sh_operand_info *));
67 static sh_opcode_info *get_specific
68 PARAMS ((sh_opcode_info *, sh_operand_info *));
69 static void insert PARAMS ((char *, int, int, sh_operand_info *));
70 static void build_relax PARAMS ((sh_opcode_info *, sh_operand_info *));
71 static char *insert_loop_bounds PARAMS ((char *, sh_operand_info *));
72 static unsigned int build_Mytes
73 PARAMS ((sh_opcode_info *, sh_operand_info *));
75 #ifdef OBJ_ELF
76 static void sh_elf_cons PARAMS ((int));
78 inline static int sh_PIC_related_p PARAMS ((symbolS *));
79 static int sh_check_fixup PARAMS ((expressionS *, bfd_reloc_code_real_type *));
80 inline static char *sh_end_of_match PARAMS ((char *, char *));
82 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
83 #endif
85 static void
86 big (ignore)
87 int ignore ATTRIBUTE_UNUSED;
89 if (! target_big_endian)
90 as_bad (_("directive .big encountered when option -big required"));
92 /* Stop further messages. */
93 target_big_endian = 1;
96 static void
97 little (ignore)
98 int ignore ATTRIBUTE_UNUSED;
100 if (target_big_endian)
101 as_bad (_("directive .little encountered when option -little required"));
103 /* Stop further messages. */
104 target_big_endian = 0;
107 /* This table describes all the machine specific pseudo-ops the assembler
108 has to support. The fields are:
109 pseudo-op name without dot
110 function to call to execute this pseudo-op
111 Integer arg to pass to the function. */
113 const pseudo_typeS md_pseudo_table[] =
115 #ifdef OBJ_ELF
116 {"long", sh_elf_cons, 4},
117 {"int", sh_elf_cons, 4},
118 {"word", sh_elf_cons, 2},
119 {"short", sh_elf_cons, 2},
120 #else
121 {"int", cons, 4},
122 {"word", cons, 2},
123 #endif /* OBJ_ELF */
124 {"big", big, 0},
125 {"form", listing_psize, 0},
126 {"little", little, 0},
127 {"heading", listing_title, 0},
128 {"import", s_ignore, 0},
129 {"page", listing_eject, 0},
130 {"program", s_ignore, 0},
131 {"uses", s_uses, 0},
132 {"uaword", s_uacons, 2},
133 {"ualong", s_uacons, 4},
134 {"uaquad", s_uacons, 8},
135 {"2byte", s_uacons, 2},
136 {"4byte", s_uacons, 4},
137 {"8byte", s_uacons, 8},
138 #ifdef BFD_ASSEMBLER
139 {"file", dwarf2_directive_file, 0 },
140 {"loc", dwarf2_directive_loc, 0 },
141 #endif
142 #ifdef HAVE_SH64
143 {"mode", s_sh64_mode, 0 },
145 /* Have the old name too. */
146 {"isa", s_sh64_mode, 0 },
148 /* Assert that the right ABI is used. */
149 {"abi", s_sh64_abi, 0 },
151 { "vtable_inherit", sh64_vtable_inherit, 0 },
152 { "vtable_entry", sh64_vtable_entry, 0 },
153 #endif /* HAVE_SH64 */
154 {0, 0, 0}
157 /*int md_reloc_size; */
159 int sh_relax; /* set if -relax seen */
161 /* Whether -small was seen. */
163 int sh_small;
165 /* Whether -dsp was seen. */
167 static int sh_dsp;
169 /* The bit mask of architectures that could
170 accomodate the insns seen so far. */
171 static int valid_arch;
173 const char EXP_CHARS[] = "eE";
175 /* Chars that mean this number is a floating point constant. */
176 /* As in 0f12.456 */
177 /* or 0d1.2345e12 */
178 const char FLT_CHARS[] = "rRsSfFdDxXpP";
180 #define C(a,b) ENCODE_RELAX(a,b)
182 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
183 #define GET_WHAT(x) ((x>>4))
185 /* These are the three types of relaxable instrction. */
186 /* These are the types of relaxable instructions; except for END which is
187 a marker. */
188 #define COND_JUMP 1
189 #define COND_JUMP_DELAY 2
190 #define UNCOND_JUMP 3
192 #ifdef HAVE_SH64
194 /* A 16-bit (times four) pc-relative operand, at most expanded to 32 bits. */
195 #define SH64PCREL16_32 4
196 /* A 16-bit (times four) pc-relative operand, at most expanded to 64 bits. */
197 #define SH64PCREL16_64 5
199 /* Variants of the above for adjusting the insn to PTA or PTB according to
200 the label. */
201 #define SH64PCREL16PT_32 6
202 #define SH64PCREL16PT_64 7
204 /* A MOVI expansion, expanding to at most 32 or 64 bits. */
205 #define MOVI_IMM_32 8
206 #define MOVI_IMM_32_PCREL 9
207 #define MOVI_IMM_64 10
208 #define MOVI_IMM_64_PCREL 11
209 #define END 12
211 #else /* HAVE_SH64 */
213 #define END 4
215 #endif /* HAVE_SH64 */
217 #define UNDEF_DISP 0
218 #define COND8 1
219 #define COND12 2
220 #define COND32 3
221 #define UNDEF_WORD_DISP 4
223 #define UNCOND12 1
224 #define UNCOND32 2
226 #ifdef HAVE_SH64
227 #define UNDEF_SH64PCREL 0
228 #define SH64PCREL16 1
229 #define SH64PCREL32 2
230 #define SH64PCREL48 3
231 #define SH64PCREL64 4
232 #define SH64PCRELPLT 5
234 #define UNDEF_MOVI 0
235 #define MOVI_16 1
236 #define MOVI_32 2
237 #define MOVI_48 3
238 #define MOVI_64 4
239 #define MOVI_PLT 5
240 #define MOVI_GOTOFF 6
241 #define MOVI_GOTPC 7
242 #endif /* HAVE_SH64 */
244 /* Branch displacements are from the address of the branch plus
245 four, thus all minimum and maximum values have 4 added to them. */
246 #define COND8_F 258
247 #define COND8_M -252
248 #define COND8_LENGTH 2
250 /* There is one extra instruction before the branch, so we must add
251 two more bytes to account for it. */
252 #define COND12_F 4100
253 #define COND12_M -4090
254 #define COND12_LENGTH 6
256 #define COND12_DELAY_LENGTH 4
258 /* ??? The minimum and maximum values are wrong, but this does not matter
259 since this relocation type is not supported yet. */
260 #define COND32_F (1<<30)
261 #define COND32_M -(1<<30)
262 #define COND32_LENGTH 14
264 #define UNCOND12_F 4098
265 #define UNCOND12_M -4092
266 #define UNCOND12_LENGTH 2
268 /* ??? The minimum and maximum values are wrong, but this does not matter
269 since this relocation type is not supported yet. */
270 #define UNCOND32_F (1<<30)
271 #define UNCOND32_M -(1<<30)
272 #define UNCOND32_LENGTH 14
274 #ifdef HAVE_SH64
275 /* The trivial expansion of a SH64PCREL16 relaxation is just a "PT label,
276 TRd" as is the current insn, so no extra length. Note that the "reach"
277 is calculated from the address *after* that insn, but the offset in the
278 insn is calculated from the beginning of the insn. We also need to
279 take into account the implicit 1 coded as the "A" in PTA when counting
280 forward. If PTB reaches an odd address, we trap that as an error
281 elsewhere, so we don't have to have different relaxation entries. We
282 don't add a one to the negative range, since PTB would then have the
283 farthest backward-reaching value skipped, not generated at relaxation. */
284 #define SH64PCREL16_F (32767 * 4 - 4 + 1)
285 #define SH64PCREL16_M (-32768 * 4 - 4)
286 #define SH64PCREL16_LENGTH 0
288 /* The next step is to change that PT insn into
289 MOVI ((label - datalabel Ln) >> 16) & 65535, R25
290 SHORI (label - datalabel Ln) & 65535, R25
292 PTREL R25,TRd
293 which means two extra insns, 8 extra bytes. This is the limit for the
294 32-bit ABI.
296 The expressions look a bit bad since we have to adjust this to avoid overflow on a
297 32-bit host. */
298 #define SH64PCREL32_F ((((long) 1 << 30) - 1) * 2 + 1 - 4)
299 #define SH64PCREL32_LENGTH (2 * 4)
301 /* Similarly, we just change the MOVI and add a SHORI for the 48-bit
302 expansion. */
303 #if BFD_HOST_64BIT_LONG
304 /* The "reach" type is long, so we can only do this for a 64-bit-long
305 host. */
306 #define SH64PCREL32_M (((long) -1 << 30) * 2 - 4)
307 #define SH64PCREL48_F ((((long) 1 << 47) - 1) - 4)
308 #define SH64PCREL48_M (((long) -1 << 47) - 4)
309 #define SH64PCREL48_LENGTH (3 * 4)
310 #else
311 /* If the host does not have 64-bit longs, just make this state identical
312 in reach to the 32-bit state. Note that we have a slightly incorrect
313 reach, but the correct one above will overflow a 32-bit number. */
314 #define SH64PCREL32_M (((long) -1 << 30) * 2)
315 #define SH64PCREL48_F SH64PCREL32_F
316 #define SH64PCREL48_M SH64PCREL32_M
317 #define SH64PCREL48_LENGTH (3 * 4)
318 #endif /* BFD_HOST_64BIT_LONG */
320 /* And similarly for the 64-bit expansion; a MOVI + SHORI + SHORI + SHORI
321 + PTREL sequence. */
322 #define SH64PCREL64_LENGTH (4 * 4)
324 /* For MOVI, we make the MOVI + SHORI... expansion you can see in the
325 SH64PCREL expansions. The PCREL one is similar, but the other has no
326 pc-relative reach; it must be fully expanded in
327 shmedia_md_estimate_size_before_relax. */
328 #define MOVI_16_LENGTH 0
329 #define MOVI_16_F (32767 - 4)
330 #define MOVI_16_M (-32768 - 4)
331 #define MOVI_32_LENGTH 4
332 #define MOVI_32_F ((((long) 1 << 30) - 1) * 2 + 1 - 4)
333 #define MOVI_48_LENGTH 8
335 #if BFD_HOST_64BIT_LONG
336 /* The "reach" type is long, so we can only do this for a 64-bit-long
337 host. */
338 #define MOVI_32_M (((long) -1 << 30) * 2 - 4)
339 #define MOVI_48_F ((((long) 1 << 47) - 1) - 4)
340 #define MOVI_48_M (((long) -1 << 47) - 4)
341 #else
342 /* If the host does not have 64-bit longs, just make this state identical
343 in reach to the 32-bit state. Note that we have a slightly incorrect
344 reach, but the correct one above will overflow a 32-bit number. */
345 #define MOVI_32_M (((long) -1 << 30) * 2)
346 #define MOVI_48_F MOVI_32_F
347 #define MOVI_48_M MOVI_32_M
348 #endif /* BFD_HOST_64BIT_LONG */
350 #define MOVI_64_LENGTH 12
351 #endif /* HAVE_SH64 */
353 #define EMPTY { 0, 0, 0, 0 }
355 const relax_typeS md_relax_table[C (END, 0)] = {
356 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
357 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
359 EMPTY,
360 /* C (COND_JUMP, COND8) */
361 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
362 /* C (COND_JUMP, COND12) */
363 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
364 /* C (COND_JUMP, COND32) */
365 { COND32_F, COND32_M, COND32_LENGTH, 0, },
366 /* C (COND_JUMP, UNDEF_WORD_DISP) */
367 { 0, 0, COND32_LENGTH, 0, },
368 EMPTY, EMPTY, EMPTY,
369 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
371 EMPTY,
372 /* C (COND_JUMP_DELAY, COND8) */
373 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
374 /* C (COND_JUMP_DELAY, COND12) */
375 { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
376 /* C (COND_JUMP_DELAY, COND32) */
377 { COND32_F, COND32_M, COND32_LENGTH, 0, },
378 /* C (COND_JUMP_DELAY, UNDEF_WORD_DISP) */
379 { 0, 0, COND32_LENGTH, 0, },
380 EMPTY, EMPTY, EMPTY,
381 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
383 EMPTY,
384 /* C (UNCOND_JUMP, UNCOND12) */
385 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
386 /* C (UNCOND_JUMP, UNCOND32) */
387 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
388 EMPTY,
389 /* C (UNCOND_JUMP, UNDEF_WORD_DISP) */
390 { 0, 0, UNCOND32_LENGTH, 0, },
391 EMPTY, EMPTY, EMPTY,
392 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
394 #ifdef HAVE_SH64
395 /* C (SH64PCREL16_32, SH64PCREL16) */
396 EMPTY,
397 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16_32, SH64PCREL32) },
398 /* C (SH64PCREL16_32, SH64PCREL32) */
399 { 0, 0, SH64PCREL32_LENGTH, 0 },
400 EMPTY, EMPTY,
401 /* C (SH64PCREL16_32, SH64PCRELPLT) */
402 { 0, 0, SH64PCREL32_LENGTH, 0 },
403 EMPTY, EMPTY,
404 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
406 /* C (SH64PCREL16_64, SH64PCREL16) */
407 EMPTY,
408 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16_64, SH64PCREL32) },
409 /* C (SH64PCREL16_64, SH64PCREL32) */
410 { SH64PCREL32_F, SH64PCREL32_M, SH64PCREL32_LENGTH, C (SH64PCREL16_64, SH64PCREL48) },
411 /* C (SH64PCREL16_64, SH64PCREL48) */
412 { SH64PCREL48_F, SH64PCREL48_M, SH64PCREL48_LENGTH, C (SH64PCREL16_64, SH64PCREL64) },
413 /* C (SH64PCREL16_64, SH64PCREL64) */
414 { 0, 0, SH64PCREL64_LENGTH, 0 },
415 /* C (SH64PCREL16_64, SH64PCRELPLT) */
416 { 0, 0, SH64PCREL64_LENGTH, 0 },
417 EMPTY, EMPTY,
418 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
420 /* C (SH64PCREL16PT_32, SH64PCREL16) */
421 EMPTY,
422 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16PT_32, SH64PCREL32) },
423 /* C (SH64PCREL16PT_32, SH64PCREL32) */
424 { 0, 0, SH64PCREL32_LENGTH, 0 },
425 EMPTY, EMPTY,
426 /* C (SH64PCREL16PT_32, SH64PCRELPLT) */
427 { 0, 0, SH64PCREL32_LENGTH, 0 },
428 EMPTY, EMPTY,
429 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
431 /* C (SH64PCREL16PT_64, SH64PCREL16) */
432 EMPTY,
433 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16PT_64, SH64PCREL32) },
434 /* C (SH64PCREL16PT_64, SH64PCREL32) */
435 { SH64PCREL32_F,
436 SH64PCREL32_M,
437 SH64PCREL32_LENGTH,
438 C (SH64PCREL16PT_64, SH64PCREL48) },
439 /* C (SH64PCREL16PT_64, SH64PCREL48) */
440 { SH64PCREL48_F, SH64PCREL48_M, SH64PCREL48_LENGTH, C (SH64PCREL16PT_64, SH64PCREL64) },
441 /* C (SH64PCREL16PT_64, SH64PCREL64) */
442 { 0, 0, SH64PCREL64_LENGTH, 0 },
443 /* C (SH64PCREL16PT_64, SH64PCRELPLT) */
444 { 0, 0, SH64PCREL64_LENGTH, 0},
445 EMPTY, EMPTY,
446 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
448 /* C (MOVI_IMM_32, UNDEF_MOVI) */
449 { 0, 0, MOVI_32_LENGTH, 0 },
450 /* C (MOVI_IMM_32, MOVI_16) */
451 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_32, MOVI_32) },
452 /* C (MOVI_IMM_32, MOVI_32) */
453 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, 0 },
454 EMPTY, EMPTY, EMPTY,
455 /* C (MOVI_IMM_32, MOVI_GOTOFF) */
456 { 0, 0, MOVI_32_LENGTH, 0 },
457 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
459 /* C (MOVI_IMM_32_PCREL, MOVI_16) */
460 EMPTY,
461 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_32_PCREL, MOVI_32) },
462 /* C (MOVI_IMM_32_PCREL, MOVI_32) */
463 { 0, 0, MOVI_32_LENGTH, 0 },
464 EMPTY, EMPTY,
465 /* C (MOVI_IMM_32_PCREL, MOVI_PLT) */
466 { 0, 0, MOVI_32_LENGTH, 0 },
467 EMPTY,
468 /* C (MOVI_IMM_32_PCREL, MOVI_GOTPC) */
469 { 0, 0, MOVI_32_LENGTH, 0 },
470 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
472 /* C (MOVI_IMM_64, UNDEF_MOVI) */
473 { 0, 0, MOVI_64_LENGTH, 0 },
474 /* C (MOVI_IMM_64, MOVI_16) */
475 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_64, MOVI_32) },
476 /* C (MOVI_IMM_64, MOVI_32) */
477 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, C (MOVI_IMM_64, MOVI_48) },
478 /* C (MOVI_IMM_64, MOVI_48) */
479 { MOVI_48_F, MOVI_48_M, MOVI_48_LENGTH, C (MOVI_IMM_64, MOVI_64) },
480 /* C (MOVI_IMM_64, MOVI_64) */
481 { 0, 0, MOVI_64_LENGTH, 0 },
482 EMPTY,
483 /* C (MOVI_IMM_64, MOVI_GOTOFF) */
484 { 0, 0, MOVI_64_LENGTH, 0 },
485 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
487 /* C (MOVI_IMM_64_PCREL, MOVI_16) */
488 EMPTY,
489 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_32) },
490 /* C (MOVI_IMM_64_PCREL, MOVI_32) */
491 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_48) },
492 /* C (MOVI_IMM_64_PCREL, MOVI_48) */
493 { MOVI_48_F, MOVI_48_M, MOVI_48_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_64) },
494 /* C (MOVI_IMM_64_PCREL, MOVI_64) */
495 { 0, 0, MOVI_64_LENGTH, 0 },
496 /* C (MOVI_IMM_64_PCREL, MOVI_PLT) */
497 { 0, 0, MOVI_64_LENGTH, 0 },
498 EMPTY,
499 /* C (MOVI_IMM_64_PCREL, MOVI_GOTPC) */
500 { 0, 0, MOVI_64_LENGTH, 0 },
501 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
503 #endif /* HAVE_SH64 */
507 #undef EMPTY
509 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
512 #ifdef OBJ_ELF
513 /* Determinet whether the symbol needs any kind of PIC relocation. */
515 inline static int
516 sh_PIC_related_p (sym)
517 symbolS *sym;
519 expressionS *exp;
521 if (! sym)
522 return 0;
524 if (sym == GOT_symbol)
525 return 1;
527 #ifdef HAVE_SH64
528 if (sh_PIC_related_p (*symbol_get_tc (sym)))
529 return 1;
530 #endif
532 exp = symbol_get_value_expression (sym);
534 return (exp->X_op == O_PIC_reloc
535 || sh_PIC_related_p (exp->X_add_symbol)
536 || sh_PIC_related_p (exp->X_op_symbol));
539 /* Determine the relocation type to be used to represent the
540 expression, that may be rearranged. */
542 static int
543 sh_check_fixup (main_exp, r_type_p)
544 expressionS *main_exp;
545 bfd_reloc_code_real_type *r_type_p;
547 expressionS *exp = main_exp;
549 /* This is here for backward-compatibility only. GCC used to generated:
551 f@PLT + . - (.LPCS# + 2)
553 but we'd rather be able to handle this as a PIC-related reference
554 plus/minus a symbol. However, gas' parser gives us:
556 O_subtract (O_add (f@PLT, .), .LPCS#+2)
558 so we attempt to transform this into:
560 O_subtract (f@PLT, O_subtract (.LPCS#+2, .))
562 which we can handle simply below. */
563 if (exp->X_op == O_subtract)
565 if (sh_PIC_related_p (exp->X_op_symbol))
566 return 1;
568 exp = symbol_get_value_expression (exp->X_add_symbol);
570 if (exp && sh_PIC_related_p (exp->X_op_symbol))
571 return 1;
573 if (exp && exp->X_op == O_add
574 && sh_PIC_related_p (exp->X_add_symbol))
576 symbolS *sym = exp->X_add_symbol;
578 exp->X_op = O_subtract;
579 exp->X_add_symbol = main_exp->X_op_symbol;
581 main_exp->X_op_symbol = main_exp->X_add_symbol;
582 main_exp->X_add_symbol = sym;
584 main_exp->X_add_number += exp->X_add_number;
585 exp->X_add_number = 0;
588 exp = main_exp;
590 else if (exp->X_op == O_add && sh_PIC_related_p (exp->X_op_symbol))
591 return 1;
593 if (exp->X_op == O_symbol || exp->X_op == O_add || exp->X_op == O_subtract)
595 #ifdef HAVE_SH64
596 if (exp->X_add_symbol
597 && (exp->X_add_symbol == GOT_symbol
598 || (GOT_symbol
599 && *symbol_get_tc (exp->X_add_symbol) == GOT_symbol)))
601 switch (*r_type_p)
603 case BFD_RELOC_SH_IMM_LOW16:
604 *r_type_p = BFD_RELOC_SH_GOTPC_LOW16;
605 break;
607 case BFD_RELOC_SH_IMM_MEDLOW16:
608 *r_type_p = BFD_RELOC_SH_GOTPC_MEDLOW16;
609 break;
611 case BFD_RELOC_SH_IMM_MEDHI16:
612 *r_type_p = BFD_RELOC_SH_GOTPC_MEDHI16;
613 break;
615 case BFD_RELOC_SH_IMM_HI16:
616 *r_type_p = BFD_RELOC_SH_GOTPC_HI16;
617 break;
619 case BFD_RELOC_NONE:
620 case BFD_RELOC_UNUSED:
621 *r_type_p = BFD_RELOC_SH_GOTPC;
622 break;
624 default:
625 abort ();
627 return 0;
629 #else
630 if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
632 *r_type_p = BFD_RELOC_SH_GOTPC;
633 return 0;
635 #endif
636 exp = symbol_get_value_expression (exp->X_add_symbol);
637 if (! exp)
638 return 0;
641 if (exp->X_op == O_PIC_reloc)
643 #ifdef HAVE_SH64
644 switch (*r_type_p)
646 case BFD_RELOC_NONE:
647 case BFD_RELOC_UNUSED:
648 *r_type_p = exp->X_md;
649 break;
651 case BFD_RELOC_SH_IMM_LOW16:
652 switch (exp->X_md)
654 case BFD_RELOC_32_GOTOFF:
655 *r_type_p = BFD_RELOC_SH_GOTOFF_LOW16;
656 break;
658 case BFD_RELOC_SH_GOTPLT32:
659 *r_type_p = BFD_RELOC_SH_GOTPLT_LOW16;
660 break;
662 case BFD_RELOC_32_GOT_PCREL:
663 *r_type_p = BFD_RELOC_SH_GOT_LOW16;
664 break;
666 case BFD_RELOC_32_PLT_PCREL:
667 *r_type_p = BFD_RELOC_SH_PLT_LOW16;
668 break;
670 default:
671 abort ();
673 break;
675 case BFD_RELOC_SH_IMM_MEDLOW16:
676 switch (exp->X_md)
678 case BFD_RELOC_32_GOTOFF:
679 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDLOW16;
680 break;
682 case BFD_RELOC_SH_GOTPLT32:
683 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDLOW16;
684 break;
686 case BFD_RELOC_32_GOT_PCREL:
687 *r_type_p = BFD_RELOC_SH_GOT_MEDLOW16;
688 break;
690 case BFD_RELOC_32_PLT_PCREL:
691 *r_type_p = BFD_RELOC_SH_PLT_MEDLOW16;
692 break;
694 default:
695 abort ();
697 break;
699 case BFD_RELOC_SH_IMM_MEDHI16:
700 switch (exp->X_md)
702 case BFD_RELOC_32_GOTOFF:
703 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDHI16;
704 break;
706 case BFD_RELOC_SH_GOTPLT32:
707 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDHI16;
708 break;
710 case BFD_RELOC_32_GOT_PCREL:
711 *r_type_p = BFD_RELOC_SH_GOT_MEDHI16;
712 break;
714 case BFD_RELOC_32_PLT_PCREL:
715 *r_type_p = BFD_RELOC_SH_PLT_MEDHI16;
716 break;
718 default:
719 abort ();
721 break;
723 case BFD_RELOC_SH_IMM_HI16:
724 switch (exp->X_md)
726 case BFD_RELOC_32_GOTOFF:
727 *r_type_p = BFD_RELOC_SH_GOTOFF_HI16;
728 break;
730 case BFD_RELOC_SH_GOTPLT32:
731 *r_type_p = BFD_RELOC_SH_GOTPLT_HI16;
732 break;
734 case BFD_RELOC_32_GOT_PCREL:
735 *r_type_p = BFD_RELOC_SH_GOT_HI16;
736 break;
738 case BFD_RELOC_32_PLT_PCREL:
739 *r_type_p = BFD_RELOC_SH_PLT_HI16;
740 break;
742 default:
743 abort ();
745 break;
747 default:
748 abort ();
750 #else
751 *r_type_p = exp->X_md;
752 #endif
753 if (exp == main_exp)
754 exp->X_op = O_symbol;
755 else
757 main_exp->X_add_symbol = exp->X_add_symbol;
758 main_exp->X_add_number += exp->X_add_number;
761 else
762 return (sh_PIC_related_p (exp->X_add_symbol)
763 || sh_PIC_related_p (exp->X_op_symbol));
765 return 0;
768 /* Add expression EXP of SIZE bytes to offset OFF of fragment FRAG. */
770 void
771 sh_cons_fix_new (frag, off, size, exp)
772 fragS *frag;
773 int off, size;
774 expressionS *exp;
776 bfd_reloc_code_real_type r_type = BFD_RELOC_UNUSED;
778 if (sh_check_fixup (exp, &r_type))
779 as_bad (_("Invalid PIC expression."));
781 if (r_type == BFD_RELOC_UNUSED)
782 switch (size)
784 case 1:
785 r_type = BFD_RELOC_8;
786 break;
788 case 2:
789 r_type = BFD_RELOC_16;
790 break;
792 case 4:
793 r_type = BFD_RELOC_32;
794 break;
796 #ifdef HAVE_SH64
797 case 8:
798 r_type = BFD_RELOC_64;
799 break;
800 #endif
802 default:
803 goto error;
805 else if (size != 4)
807 error:
808 as_bad (_("unsupported BFD relocation size %u"), size);
809 r_type = BFD_RELOC_UNUSED;
812 fix_new_exp (frag, off, size, exp, 0, r_type);
815 /* The regular cons() function, that reads constants, doesn't support
816 suffixes such as @GOT, @GOTOFF and @PLT, that generate
817 machine-specific relocation types. So we must define it here. */
818 /* Clobbers input_line_pointer, checks end-of-line. */
819 static void
820 sh_elf_cons (nbytes)
821 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
823 expressionS exp;
825 #ifdef HAVE_SH64
827 /* Update existing range to include a previous insn, if there was one. */
828 sh64_update_contents_mark (true);
830 /* We need to make sure the contents type is set to data. */
831 sh64_flag_output ();
833 #endif /* HAVE_SH64 */
835 if (is_it_end_of_statement ())
837 demand_empty_rest_of_line ();
838 return;
843 expression (&exp);
844 emit_expr (&exp, (unsigned int) nbytes);
846 while (*input_line_pointer++ == ',');
848 input_line_pointer--; /* Put terminator back into stream. */
849 if (*input_line_pointer == '#' || *input_line_pointer == '!')
851 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
853 else
854 demand_empty_rest_of_line ();
856 #endif /* OBJ_ELF */
859 /* This function is called once, at assembler startup time. This should
860 set up all the tables, etc that the MD part of the assembler needs. */
862 void
863 md_begin ()
865 sh_opcode_info *opcode;
866 char *prev_name = "";
867 int target_arch;
869 target_arch = arch_sh1_up & ~(sh_dsp ? arch_sh3e_up : arch_sh_dsp_up);
870 valid_arch = target_arch;
872 #ifdef HAVE_SH64
873 shmedia_md_begin ();
874 #endif
876 opcode_hash_control = hash_new ();
878 /* Insert unique names into hash table. */
879 for (opcode = sh_table; opcode->name; opcode++)
881 if (strcmp (prev_name, opcode->name))
883 if (! (opcode->arch & target_arch))
884 continue;
885 prev_name = opcode->name;
886 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
888 else
890 /* Make all the opcodes with the same name point to the same
891 string. */
892 opcode->name = prev_name;
897 static int reg_m;
898 static int reg_n;
899 static int reg_x, reg_y;
900 static int reg_efg;
901 static int reg_b;
903 #define IDENT_CHAR(c) (ISALNUM (c) || (c) == '_')
905 /* Try to parse a reg name. Return the number of chars consumed. */
907 static int
908 parse_reg (src, mode, reg)
909 char *src;
910 int *mode;
911 int *reg;
913 char l0 = TOLOWER (src[0]);
914 char l1 = l0 ? TOLOWER (src[1]) : 0;
916 /* We use ! IDENT_CHAR for the next character after the register name, to
917 make sure that we won't accidentally recognize a symbol name such as
918 'sram' or sr_ram as being a reference to the register 'sr'. */
920 if (l0 == 'r')
922 if (l1 == '1')
924 if (src[2] >= '0' && src[2] <= '5'
925 && ! IDENT_CHAR ((unsigned char) src[3]))
927 *mode = A_REG_N;
928 *reg = 10 + src[2] - '0';
929 return 3;
932 if (l1 >= '0' && l1 <= '9'
933 && ! IDENT_CHAR ((unsigned char) src[2]))
935 *mode = A_REG_N;
936 *reg = (l1 - '0');
937 return 2;
939 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
940 && ! IDENT_CHAR ((unsigned char) src[7]))
942 *mode = A_REG_B;
943 *reg = (l1 - '0');
944 return 7;
947 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
949 *mode = A_RE;
950 return 2;
952 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
954 *mode = A_RS;
955 return 2;
959 if (l0 == 'a')
961 if (l1 == '0')
963 if (! IDENT_CHAR ((unsigned char) src[2]))
965 *mode = DSP_REG_N;
966 *reg = A_A0_NUM;
967 return 2;
969 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
971 *mode = DSP_REG_N;
972 *reg = A_A0G_NUM;
973 return 3;
976 if (l1 == '1')
978 if (! IDENT_CHAR ((unsigned char) src[2]))
980 *mode = DSP_REG_N;
981 *reg = A_A1_NUM;
982 return 2;
984 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
986 *mode = DSP_REG_N;
987 *reg = A_A1G_NUM;
988 return 3;
992 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
993 && ! IDENT_CHAR ((unsigned char) src[3]))
995 *mode = A_REG_N;
996 *reg = 4 + (l1 - '0');
997 return 3;
999 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
1000 && ! IDENT_CHAR ((unsigned char) src[3]))
1002 *mode = A_REG_N;
1003 *reg = 6 + (l1 - '0');
1004 return 3;
1006 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
1007 && ! IDENT_CHAR ((unsigned char) src[3]))
1009 int n = l1 - '0';
1011 *mode = A_REG_N;
1012 *reg = n | ((~n & 2) << 1);
1013 return 3;
1017 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[2]))
1019 if (l1 == 's')
1021 *mode = A_REG_N;
1022 *reg = 8;
1023 return 2;
1025 if (l1 == 'x')
1027 *mode = A_REG_N;
1028 *reg = 8;
1029 return 2;
1031 if (l1 == 'y')
1033 *mode = A_REG_N;
1034 *reg = 9;
1035 return 2;
1039 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
1040 && ! IDENT_CHAR ((unsigned char) src[2]))
1042 *mode = DSP_REG_N;
1043 *reg = A_X0_NUM + l1 - '0';
1044 return 2;
1047 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
1048 && ! IDENT_CHAR ((unsigned char) src[2]))
1050 *mode = DSP_REG_N;
1051 *reg = A_Y0_NUM + l1 - '0';
1052 return 2;
1055 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
1056 && ! IDENT_CHAR ((unsigned char) src[2]))
1058 *mode = DSP_REG_N;
1059 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
1060 return 2;
1063 if (l0 == 's'
1064 && l1 == 's'
1065 && TOLOWER (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
1067 *mode = A_SSR;
1068 return 3;
1071 if (l0 == 's' && l1 == 'p' && TOLOWER (src[2]) == 'c'
1072 && ! IDENT_CHAR ((unsigned char) src[3]))
1074 *mode = A_SPC;
1075 return 3;
1078 if (l0 == 's' && l1 == 'g' && TOLOWER (src[2]) == 'r'
1079 && ! IDENT_CHAR ((unsigned char) src[3]))
1081 *mode = A_SGR;
1082 return 3;
1085 if (l0 == 'd' && l1 == 's' && TOLOWER (src[2]) == 'r'
1086 && ! IDENT_CHAR ((unsigned char) src[3]))
1088 *mode = A_DSR;
1089 return 3;
1092 if (l0 == 'd' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1093 && ! IDENT_CHAR ((unsigned char) src[3]))
1095 *mode = A_DBR;
1096 return 3;
1099 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
1101 *mode = A_SR;
1102 return 2;
1105 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
1107 *mode = A_REG_N;
1108 *reg = 15;
1109 return 2;
1112 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
1114 *mode = A_PR;
1115 return 2;
1117 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
1119 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
1120 and use an uninitialized immediate. */
1121 *mode = A_PC;
1122 return 2;
1124 if (l0 == 'g' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1125 && ! IDENT_CHAR ((unsigned char) src[3]))
1127 *mode = A_GBR;
1128 return 3;
1130 if (l0 == 'v' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1131 && ! IDENT_CHAR ((unsigned char) src[3]))
1133 *mode = A_VBR;
1134 return 3;
1137 if (l0 == 'm' && l1 == 'a' && TOLOWER (src[2]) == 'c'
1138 && ! IDENT_CHAR ((unsigned char) src[4]))
1140 if (TOLOWER (src[3]) == 'l')
1142 *mode = A_MACL;
1143 return 4;
1145 if (TOLOWER (src[3]) == 'h')
1147 *mode = A_MACH;
1148 return 4;
1151 if (l0 == 'm' && l1 == 'o' && TOLOWER (src[2]) == 'd'
1152 && ! IDENT_CHAR ((unsigned char) src[3]))
1154 *mode = A_MOD;
1155 return 3;
1157 if (l0 == 'f' && l1 == 'r')
1159 if (src[2] == '1')
1161 if (src[3] >= '0' && src[3] <= '5'
1162 && ! IDENT_CHAR ((unsigned char) src[4]))
1164 *mode = F_REG_N;
1165 *reg = 10 + src[3] - '0';
1166 return 4;
1169 if (src[2] >= '0' && src[2] <= '9'
1170 && ! IDENT_CHAR ((unsigned char) src[3]))
1172 *mode = F_REG_N;
1173 *reg = (src[2] - '0');
1174 return 3;
1177 if (l0 == 'd' && l1 == 'r')
1179 if (src[2] == '1')
1181 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
1182 && ! IDENT_CHAR ((unsigned char) src[4]))
1184 *mode = D_REG_N;
1185 *reg = 10 + src[3] - '0';
1186 return 4;
1189 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
1190 && ! IDENT_CHAR ((unsigned char) src[3]))
1192 *mode = D_REG_N;
1193 *reg = (src[2] - '0');
1194 return 3;
1197 if (l0 == 'x' && l1 == 'd')
1199 if (src[2] == '1')
1201 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
1202 && ! IDENT_CHAR ((unsigned char) src[4]))
1204 *mode = X_REG_N;
1205 *reg = 11 + src[3] - '0';
1206 return 4;
1209 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
1210 && ! IDENT_CHAR ((unsigned char) src[3]))
1212 *mode = X_REG_N;
1213 *reg = (src[2] - '0') + 1;
1214 return 3;
1217 if (l0 == 'f' && l1 == 'v')
1219 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
1221 *mode = V_REG_N;
1222 *reg = 12;
1223 return 4;
1225 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
1226 && ! IDENT_CHAR ((unsigned char) src[3]))
1228 *mode = V_REG_N;
1229 *reg = (src[2] - '0');
1230 return 3;
1233 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 'u'
1234 && TOLOWER (src[3]) == 'l'
1235 && ! IDENT_CHAR ((unsigned char) src[4]))
1237 *mode = FPUL_N;
1238 return 4;
1241 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 's'
1242 && TOLOWER (src[3]) == 'c'
1243 && TOLOWER (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
1245 *mode = FPSCR_N;
1246 return 5;
1249 if (l0 == 'x' && l1 == 'm' && TOLOWER (src[2]) == 't'
1250 && TOLOWER (src[3]) == 'r'
1251 && TOLOWER (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
1253 *mode = XMTRX_M4;
1254 return 5;
1257 return 0;
1260 static char *
1261 parse_exp (s, op)
1262 char *s;
1263 sh_operand_info *op;
1265 char *save;
1266 char *new;
1268 save = input_line_pointer;
1269 input_line_pointer = s;
1270 expression (&op->immediate);
1271 if (op->immediate.X_op == O_absent)
1272 as_bad (_("missing operand"));
1273 #ifdef OBJ_ELF
1274 else if (op->immediate.X_op == O_PIC_reloc
1275 || sh_PIC_related_p (op->immediate.X_add_symbol)
1276 || sh_PIC_related_p (op->immediate.X_op_symbol))
1277 as_bad (_("misplaced PIC operand"));
1278 #endif
1279 new = input_line_pointer;
1280 input_line_pointer = save;
1281 return new;
1284 /* The many forms of operand:
1286 Rn Register direct
1287 @Rn Register indirect
1288 @Rn+ Autoincrement
1289 @-Rn Autodecrement
1290 @(disp:4,Rn)
1291 @(disp:8,GBR)
1292 @(disp:8,PC)
1294 @(R0,Rn)
1295 @(R0,GBR)
1297 disp:8
1298 disp:12
1299 #imm8
1300 pr, gbr, vbr, macl, mach
1303 static char *
1304 parse_at (src, op)
1305 char *src;
1306 sh_operand_info *op;
1308 int len;
1309 int mode;
1310 src++;
1311 if (src[0] == '-')
1313 /* Must be predecrement. */
1314 src++;
1316 len = parse_reg (src, &mode, &(op->reg));
1317 if (mode != A_REG_N)
1318 as_bad (_("illegal register after @-"));
1320 op->type = A_DEC_N;
1321 src += len;
1323 else if (src[0] == '(')
1325 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
1326 @(r0, rn). */
1327 src++;
1328 len = parse_reg (src, &mode, &(op->reg));
1329 if (len && mode == A_REG_N)
1331 src += len;
1332 if (op->reg != 0)
1334 as_bad (_("must be @(r0,...)"));
1336 if (src[0] == ',')
1338 src++;
1339 /* Now can be rn or gbr. */
1340 len = parse_reg (src, &mode, &(op->reg));
1342 else
1344 len = 0;
1346 if (len)
1348 if (mode == A_GBR)
1350 op->type = A_R0_GBR;
1352 else if (mode == A_REG_N)
1354 op->type = A_IND_R0_REG_N;
1356 else
1358 as_bad (_("syntax error in @(r0,...)"));
1361 else
1363 as_bad (_("syntax error in @(r0...)"));
1366 else
1368 /* Must be an @(disp,.. thing). */
1369 src = parse_exp (src, op);
1370 if (src[0] == ',')
1371 src++;
1372 /* Now can be rn, gbr or pc. */
1373 len = parse_reg (src, &mode, &op->reg);
1374 if (len)
1376 if (mode == A_REG_N)
1378 op->type = A_DISP_REG_N;
1380 else if (mode == A_GBR)
1382 op->type = A_DISP_GBR;
1384 else if (mode == A_PC)
1386 op->type = A_DISP_PC_ABS;
1387 /* Such operands don't get corrected for PC==.+4, so
1388 make the correction here. */
1389 op->immediate.X_add_number -= 4;
1391 else
1393 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1396 else
1398 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1401 src += len;
1402 if (src[0] != ')')
1403 as_bad (_("expecting )"));
1404 else
1405 src++;
1407 else
1409 src += parse_reg (src, &mode, &(op->reg));
1410 if (mode != A_REG_N)
1411 as_bad (_("illegal register after @"));
1413 if (src[0] == '+')
1415 char l0, l1;
1417 src++;
1418 l0 = TOLOWER (src[0]);
1419 l1 = TOLOWER (src[1]);
1421 if ((l0 == 'r' && l1 == '8')
1422 || (l0 == 'i' && (l1 == 'x' || l1 == 's')))
1424 src += 2;
1425 op->type = A_PMOD_N;
1427 else if ( (l0 == 'r' && l1 == '9')
1428 || (l0 == 'i' && l1 == 'y'))
1430 src += 2;
1431 op->type = A_PMODY_N;
1433 else
1434 op->type = A_INC_N;
1436 else
1437 op->type = A_IND_N;
1439 return src;
1442 static void
1443 get_operand (ptr, op)
1444 char **ptr;
1445 sh_operand_info *op;
1447 char *src = *ptr;
1448 int mode = -1;
1449 unsigned int len;
1451 if (src[0] == '#')
1453 src++;
1454 *ptr = parse_exp (src, op);
1455 op->type = A_IMM;
1456 return;
1459 else if (src[0] == '@')
1461 *ptr = parse_at (src, op);
1462 return;
1464 len = parse_reg (src, &mode, &(op->reg));
1465 if (len)
1467 *ptr = src + len;
1468 op->type = mode;
1469 return;
1471 else
1473 /* Not a reg, the only thing left is a displacement. */
1474 *ptr = parse_exp (src, op);
1475 op->type = A_DISP_PC;
1476 return;
1480 static char *
1481 get_operands (info, args, operand)
1482 sh_opcode_info *info;
1483 char *args;
1484 sh_operand_info *operand;
1486 char *ptr = args;
1487 if (info->arg[0])
1489 /* The pre-processor will eliminate whitespace in front of '@'
1490 after the first argument; we may be called multiple times
1491 from assemble_ppi, so don't insist on finding whitespace here. */
1492 if (*ptr == ' ')
1493 ptr++;
1495 get_operand (&ptr, operand + 0);
1496 if (info->arg[1])
1498 if (*ptr == ',')
1500 ptr++;
1502 get_operand (&ptr, operand + 1);
1503 /* ??? Hack: psha/pshl have a varying operand number depending on
1504 the type of the first operand. We handle this by having the
1505 three-operand version first and reducing the number of operands
1506 parsed to two if we see that the first operand is an immediate.
1507 This works because no insn with three operands has an immediate
1508 as first operand. */
1509 if (info->arg[2] && operand[0].type != A_IMM)
1511 if (*ptr == ',')
1513 ptr++;
1515 get_operand (&ptr, operand + 2);
1517 else
1519 operand[2].type = 0;
1522 else
1524 operand[1].type = 0;
1525 operand[2].type = 0;
1528 else
1530 operand[0].type = 0;
1531 operand[1].type = 0;
1532 operand[2].type = 0;
1534 return ptr;
1537 /* Passed a pointer to a list of opcodes which use different
1538 addressing modes, return the opcode which matches the opcodes
1539 provided. */
1541 static sh_opcode_info *
1542 get_specific (opcode, operands)
1543 sh_opcode_info *opcode;
1544 sh_operand_info *operands;
1546 sh_opcode_info *this_try = opcode;
1547 char *name = opcode->name;
1548 int n = 0;
1550 while (opcode->name)
1552 this_try = opcode++;
1553 if (this_try->name != name)
1555 /* We've looked so far down the table that we've run out of
1556 opcodes with the same name. */
1557 return 0;
1560 /* Look at both operands needed by the opcodes and provided by
1561 the user - since an arg test will often fail on the same arg
1562 again and again, we'll try and test the last failing arg the
1563 first on each opcode try. */
1564 for (n = 0; this_try->arg[n]; n++)
1566 sh_operand_info *user = operands + n;
1567 sh_arg_type arg = this_try->arg[n];
1569 /* If this is a parallel insn check to see if both
1570 parts have the same destination register. */
1571 if ((n == 2) && (this_try->nibbles[0] == PPI))
1573 static boolean bIsPPI = false;
1574 static int nLastDestReg;
1576 if (!bIsPPI)
1578 bIsPPI = true;
1579 nLastDestReg = user->reg;
1581 else /* Second insn. */
1583 if (nLastDestReg == user->reg)
1584 as_warn (_("destination register is same for parallel insns"));
1586 bIsPPI = false;
1590 switch (arg)
1592 case A_DISP_PC:
1593 if (user->type == A_DISP_PC_ABS)
1594 break;
1595 /* Fall through. */
1596 case A_IMM:
1597 case A_BDISP12:
1598 case A_BDISP8:
1599 case A_DISP_GBR:
1600 case A_MACH:
1601 case A_PR:
1602 case A_MACL:
1603 if (user->type != arg)
1604 goto fail;
1605 break;
1606 case A_R0:
1607 /* opcode needs r0 */
1608 if (user->type != A_REG_N || user->reg != 0)
1609 goto fail;
1610 break;
1611 case A_R0_GBR:
1612 if (user->type != A_R0_GBR || user->reg != 0)
1613 goto fail;
1614 break;
1615 case F_FR0:
1616 if (user->type != F_REG_N || user->reg != 0)
1617 goto fail;
1618 break;
1620 case A_REG_N:
1621 case A_INC_N:
1622 case A_DEC_N:
1623 case A_IND_N:
1624 case A_IND_R0_REG_N:
1625 case A_DISP_REG_N:
1626 case F_REG_N:
1627 case D_REG_N:
1628 case X_REG_N:
1629 case V_REG_N:
1630 case FPUL_N:
1631 case FPSCR_N:
1632 case A_PMOD_N:
1633 case A_PMODY_N:
1634 case DSP_REG_N:
1635 /* Opcode needs rn */
1636 if (user->type != arg)
1637 goto fail;
1638 reg_n = user->reg;
1639 break;
1640 case DX_REG_N:
1641 if (user->type != D_REG_N && user->type != X_REG_N)
1642 goto fail;
1643 reg_n = user->reg;
1644 break;
1645 case A_GBR:
1646 case A_SR:
1647 case A_VBR:
1648 case A_DSR:
1649 case A_MOD:
1650 case A_RE:
1651 case A_RS:
1652 case A_SSR:
1653 case A_SPC:
1654 case A_SGR:
1655 case A_DBR:
1656 if (user->type != arg)
1657 goto fail;
1658 break;
1660 case A_REG_B:
1661 if (user->type != arg)
1662 goto fail;
1663 reg_b = user->reg;
1664 break;
1666 case A_REG_M:
1667 case A_INC_M:
1668 case A_DEC_M:
1669 case A_IND_M:
1670 case A_IND_R0_REG_M:
1671 case A_DISP_REG_M:
1672 case DSP_REG_M:
1673 /* Opcode needs rn */
1674 if (user->type != arg - A_REG_M + A_REG_N)
1675 goto fail;
1676 reg_m = user->reg;
1677 break;
1679 case DSP_REG_X:
1680 if (user->type != DSP_REG_N)
1681 goto fail;
1682 switch (user->reg)
1684 case A_X0_NUM:
1685 reg_x = 0;
1686 break;
1687 case A_X1_NUM:
1688 reg_x = 1;
1689 break;
1690 case A_A0_NUM:
1691 reg_x = 2;
1692 break;
1693 case A_A1_NUM:
1694 reg_x = 3;
1695 break;
1696 default:
1697 goto fail;
1699 break;
1701 case DSP_REG_Y:
1702 if (user->type != DSP_REG_N)
1703 goto fail;
1704 switch (user->reg)
1706 case A_Y0_NUM:
1707 reg_y = 0;
1708 break;
1709 case A_Y1_NUM:
1710 reg_y = 1;
1711 break;
1712 case A_M0_NUM:
1713 reg_y = 2;
1714 break;
1715 case A_M1_NUM:
1716 reg_y = 3;
1717 break;
1718 default:
1719 goto fail;
1721 break;
1723 case DSP_REG_E:
1724 if (user->type != DSP_REG_N)
1725 goto fail;
1726 switch (user->reg)
1728 case A_X0_NUM:
1729 reg_efg = 0 << 10;
1730 break;
1731 case A_X1_NUM:
1732 reg_efg = 1 << 10;
1733 break;
1734 case A_Y0_NUM:
1735 reg_efg = 2 << 10;
1736 break;
1737 case A_A1_NUM:
1738 reg_efg = 3 << 10;
1739 break;
1740 default:
1741 goto fail;
1743 break;
1745 case DSP_REG_F:
1746 if (user->type != DSP_REG_N)
1747 goto fail;
1748 switch (user->reg)
1750 case A_Y0_NUM:
1751 reg_efg |= 0 << 8;
1752 break;
1753 case A_Y1_NUM:
1754 reg_efg |= 1 << 8;
1755 break;
1756 case A_X0_NUM:
1757 reg_efg |= 2 << 8;
1758 break;
1759 case A_A1_NUM:
1760 reg_efg |= 3 << 8;
1761 break;
1762 default:
1763 goto fail;
1765 break;
1767 case DSP_REG_G:
1768 if (user->type != DSP_REG_N)
1769 goto fail;
1770 switch (user->reg)
1772 case A_M0_NUM:
1773 reg_efg |= 0 << 2;
1774 break;
1775 case A_M1_NUM:
1776 reg_efg |= 1 << 2;
1777 break;
1778 case A_A0_NUM:
1779 reg_efg |= 2 << 2;
1780 break;
1781 case A_A1_NUM:
1782 reg_efg |= 3 << 2;
1783 break;
1784 default:
1785 goto fail;
1787 break;
1789 case A_A0:
1790 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1791 goto fail;
1792 break;
1793 case A_X0:
1794 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1795 goto fail;
1796 break;
1797 case A_X1:
1798 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1799 goto fail;
1800 break;
1801 case A_Y0:
1802 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1803 goto fail;
1804 break;
1805 case A_Y1:
1806 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1807 goto fail;
1808 break;
1810 case F_REG_M:
1811 case D_REG_M:
1812 case X_REG_M:
1813 case V_REG_M:
1814 case FPUL_M:
1815 case FPSCR_M:
1816 /* Opcode needs rn */
1817 if (user->type != arg - F_REG_M + F_REG_N)
1818 goto fail;
1819 reg_m = user->reg;
1820 break;
1821 case DX_REG_M:
1822 if (user->type != D_REG_N && user->type != X_REG_N)
1823 goto fail;
1824 reg_m = user->reg;
1825 break;
1826 case XMTRX_M4:
1827 if (user->type != XMTRX_M4)
1828 goto fail;
1829 reg_m = 4;
1830 break;
1832 default:
1833 printf (_("unhandled %d\n"), arg);
1834 goto fail;
1837 if ( !(valid_arch & this_try->arch))
1838 goto fail;
1839 valid_arch &= this_try->arch;
1840 return this_try;
1841 fail:
1845 return 0;
1848 static void
1849 insert (where, how, pcrel, op)
1850 char *where;
1851 int how;
1852 int pcrel;
1853 sh_operand_info *op;
1855 fix_new_exp (frag_now,
1856 where - frag_now->fr_literal,
1858 &op->immediate,
1859 pcrel,
1860 how);
1863 static void
1864 build_relax (opcode, op)
1865 sh_opcode_info *opcode;
1866 sh_operand_info *op;
1868 int high_byte = target_big_endian ? 0 : 1;
1869 char *p;
1871 if (opcode->arg[0] == A_BDISP8)
1873 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
1874 p = frag_var (rs_machine_dependent,
1875 md_relax_table[C (what, COND32)].rlx_length,
1876 md_relax_table[C (what, COND8)].rlx_length,
1877 C (what, 0),
1878 op->immediate.X_add_symbol,
1879 op->immediate.X_add_number,
1881 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
1883 else if (opcode->arg[0] == A_BDISP12)
1885 p = frag_var (rs_machine_dependent,
1886 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
1887 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
1888 C (UNCOND_JUMP, 0),
1889 op->immediate.X_add_symbol,
1890 op->immediate.X_add_number,
1892 p[high_byte] = (opcode->nibbles[0] << 4);
1897 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
1899 static char *
1900 insert_loop_bounds (output, operand)
1901 char *output;
1902 sh_operand_info *operand;
1904 char *name;
1905 symbolS *end_sym;
1907 /* Since the low byte of the opcode will be overwritten by the reloc, we
1908 can just stash the high byte into both bytes and ignore endianness. */
1909 output[0] = 0x8c;
1910 output[1] = 0x8c;
1911 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1912 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1914 if (sh_relax)
1916 static int count = 0;
1918 /* If the last loop insn is a two-byte-insn, it is in danger of being
1919 swapped with the insn after it. To prevent this, create a new
1920 symbol - complete with SH_LABEL reloc - after the last loop insn.
1921 If the last loop insn is four bytes long, the symbol will be
1922 right in the middle, but four byte insns are not swapped anyways. */
1923 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
1924 Hence a 9 digit number should be enough to count all REPEATs. */
1925 name = alloca (11);
1926 sprintf (name, "_R%x", count++ & 0x3fffffff);
1927 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
1928 /* Make this a local symbol. */
1929 #ifdef OBJ_COFF
1930 SF_SET_LOCAL (end_sym);
1931 #endif /* OBJ_COFF */
1932 symbol_table_insert (end_sym);
1933 end_sym->sy_value = operand[1].immediate;
1934 end_sym->sy_value.X_add_number += 2;
1935 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
1938 output = frag_more (2);
1939 output[0] = 0x8e;
1940 output[1] = 0x8e;
1941 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1942 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1944 return frag_more (2);
1947 /* Now we know what sort of opcodes it is, let's build the bytes. */
1949 static unsigned int
1950 build_Mytes (opcode, operand)
1951 sh_opcode_info *opcode;
1952 sh_operand_info *operand;
1954 int index;
1955 char nbuf[4];
1956 char *output = frag_more (2);
1957 unsigned int size = 2;
1958 int low_byte = target_big_endian ? 1 : 0;
1959 nbuf[0] = 0;
1960 nbuf[1] = 0;
1961 nbuf[2] = 0;
1962 nbuf[3] = 0;
1964 for (index = 0; index < 4; index++)
1966 sh_nibble_type i = opcode->nibbles[index];
1967 if (i < 16)
1969 nbuf[index] = i;
1971 else
1973 switch (i)
1975 case REG_N:
1976 nbuf[index] = reg_n;
1977 break;
1978 case REG_M:
1979 nbuf[index] = reg_m;
1980 break;
1981 case SDT_REG_N:
1982 if (reg_n < 2 || reg_n > 5)
1983 as_bad (_("Invalid register: 'r%d'"), reg_n);
1984 nbuf[index] = (reg_n & 3) | 4;
1985 break;
1986 case REG_NM:
1987 nbuf[index] = reg_n | (reg_m >> 2);
1988 break;
1989 case REG_B:
1990 nbuf[index] = reg_b | 0x08;
1991 break;
1992 case IMM0_4BY4:
1993 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
1994 break;
1995 case IMM0_4BY2:
1996 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
1997 break;
1998 case IMM0_4:
1999 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
2000 break;
2001 case IMM1_4BY4:
2002 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
2003 break;
2004 case IMM1_4BY2:
2005 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
2006 break;
2007 case IMM1_4:
2008 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
2009 break;
2010 case IMM0_8BY4:
2011 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
2012 break;
2013 case IMM0_8BY2:
2014 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
2015 break;
2016 case IMM0_8:
2017 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
2018 break;
2019 case IMM1_8BY4:
2020 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
2021 break;
2022 case IMM1_8BY2:
2023 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
2024 break;
2025 case IMM1_8:
2026 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
2027 break;
2028 case PCRELIMM_8BY4:
2029 insert (output, BFD_RELOC_SH_PCRELIMM8BY4,
2030 operand->type != A_DISP_PC_ABS, operand);
2031 break;
2032 case PCRELIMM_8BY2:
2033 insert (output, BFD_RELOC_SH_PCRELIMM8BY2,
2034 operand->type != A_DISP_PC_ABS, operand);
2035 break;
2036 case REPEAT:
2037 output = insert_loop_bounds (output, operand);
2038 nbuf[index] = opcode->nibbles[3];
2039 operand += 2;
2040 break;
2041 default:
2042 printf (_("failed for %d\n"), i);
2046 if (!target_big_endian)
2048 output[1] = (nbuf[0] << 4) | (nbuf[1]);
2049 output[0] = (nbuf[2] << 4) | (nbuf[3]);
2051 else
2053 output[0] = (nbuf[0] << 4) | (nbuf[1]);
2054 output[1] = (nbuf[2] << 4) | (nbuf[3]);
2056 return size;
2059 /* Find an opcode at the start of *STR_P in the hash table, and set
2060 *STR_P to the first character after the last one read. */
2062 static sh_opcode_info *
2063 find_cooked_opcode (str_p)
2064 char **str_p;
2066 char *str = *str_p;
2067 unsigned char *op_start;
2068 unsigned char *op_end;
2069 char name[20];
2070 int nlen = 0;
2072 /* Drop leading whitespace. */
2073 while (*str == ' ')
2074 str++;
2076 /* Find the op code end.
2077 The pre-processor will eliminate whitespace in front of
2078 any '@' after the first argument; we may be called from
2079 assemble_ppi, so the opcode might be terminated by an '@'. */
2080 for (op_start = op_end = (unsigned char *) (str);
2081 *op_end
2082 && nlen < 20
2083 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
2084 op_end++)
2086 unsigned char c = op_start[nlen];
2088 /* The machine independent code will convert CMP/EQ into cmp/EQ
2089 because it thinks the '/' is the end of the symbol. Moreover,
2090 all but the first sub-insn is a parallel processing insn won't
2091 be capitalized. Instead of hacking up the machine independent
2092 code, we just deal with it here. */
2093 c = TOLOWER (c);
2094 name[nlen] = c;
2095 nlen++;
2098 name[nlen] = 0;
2099 *str_p = op_end;
2101 if (nlen == 0)
2102 as_bad (_("can't find opcode "));
2104 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
2107 /* Assemble a parallel processing insn. */
2108 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
2110 static unsigned int
2111 assemble_ppi (op_end, opcode)
2112 char *op_end;
2113 sh_opcode_info *opcode;
2115 int movx = 0;
2116 int movy = 0;
2117 int cond = 0;
2118 int field_b = 0;
2119 char *output;
2120 int move_code;
2121 unsigned int size;
2123 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
2124 Make sure we encode a defined insn pattern. */
2125 reg_x = 0;
2126 reg_y = 0;
2128 for (;;)
2130 sh_operand_info operand[3];
2132 if (opcode->arg[0] != A_END)
2133 op_end = get_operands (opcode, op_end, operand);
2134 opcode = get_specific (opcode, operand);
2135 if (opcode == 0)
2137 /* Couldn't find an opcode which matched the operands. */
2138 char *where = frag_more (2);
2139 size = 2;
2141 where[0] = 0x0;
2142 where[1] = 0x0;
2143 as_bad (_("invalid operands for opcode"));
2144 return size;
2147 if (opcode->nibbles[0] != PPI)
2148 as_bad (_("insn can't be combined with parallel processing insn"));
2150 switch (opcode->nibbles[1])
2153 case NOPX:
2154 if (movx)
2155 as_bad (_("multiple movx specifications"));
2156 movx = DDT_BASE;
2157 break;
2158 case NOPY:
2159 if (movy)
2160 as_bad (_("multiple movy specifications"));
2161 movy = DDT_BASE;
2162 break;
2164 case MOVX:
2165 if (movx)
2166 as_bad (_("multiple movx specifications"));
2167 if (reg_n < 4 || reg_n > 5)
2168 as_bad (_("invalid movx address register"));
2169 if (opcode->nibbles[2] & 8)
2171 if (reg_m == A_A1_NUM)
2172 movx = 1 << 7;
2173 else if (reg_m != A_A0_NUM)
2174 as_bad (_("invalid movx dsp register"));
2176 else
2178 if (reg_x > 1)
2179 as_bad (_("invalid movx dsp register"));
2180 movx = reg_x << 7;
2182 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
2183 break;
2185 case MOVY:
2186 if (movy)
2187 as_bad (_("multiple movy specifications"));
2188 if (opcode->nibbles[2] & 8)
2190 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
2191 so add 8 more. */
2192 movy = 8;
2193 if (reg_m == A_A1_NUM)
2194 movy += 1 << 6;
2195 else if (reg_m != A_A0_NUM)
2196 as_bad (_("invalid movy dsp register"));
2198 else
2200 if (reg_y > 1)
2201 as_bad (_("invalid movy dsp register"));
2202 movy = reg_y << 6;
2204 if (reg_n < 6 || reg_n > 7)
2205 as_bad (_("invalid movy address register"));
2206 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
2207 break;
2209 case PSH:
2210 if (operand[0].immediate.X_op != O_constant)
2211 as_bad (_("dsp immediate shift value not constant"));
2212 field_b = ((opcode->nibbles[2] << 12)
2213 | (operand[0].immediate.X_add_number & 127) << 4
2214 | reg_n);
2215 break;
2216 case PPI3:
2217 if (field_b)
2218 as_bad (_("multiple parallel processing specifications"));
2219 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2220 + (reg_x << 6) + (reg_y << 4) + reg_n);
2221 break;
2222 case PDC:
2223 if (cond)
2224 as_bad (_("multiple condition specifications"));
2225 cond = opcode->nibbles[2] << 8;
2226 if (*op_end)
2227 goto skip_cond_check;
2228 break;
2229 case PPIC:
2230 if (field_b)
2231 as_bad (_("multiple parallel processing specifications"));
2232 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2233 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
2234 cond = 0;
2235 break;
2236 case PMUL:
2237 if (field_b)
2239 if ((field_b & 0xef00) != 0xa100)
2240 as_bad (_("insn cannot be combined with pmuls"));
2241 field_b -= 0x8100;
2242 switch (field_b & 0xf)
2244 case A_X0_NUM:
2245 field_b += 0 - A_X0_NUM;
2246 break;
2247 case A_Y0_NUM:
2248 field_b += 1 - A_Y0_NUM;
2249 break;
2250 case A_A0_NUM:
2251 field_b += 2 - A_A0_NUM;
2252 break;
2253 case A_A1_NUM:
2254 field_b += 3 - A_A1_NUM;
2255 break;
2256 default:
2257 as_bad (_("bad padd / psub pmuls output operand"));
2260 field_b += 0x4000 + reg_efg;
2261 break;
2262 default:
2263 abort ();
2265 if (cond)
2267 as_bad (_("condition not followed by conditionalizable insn"));
2268 cond = 0;
2270 if (! *op_end)
2271 break;
2272 skip_cond_check:
2273 opcode = find_cooked_opcode (&op_end);
2274 if (opcode == NULL)
2276 (as_bad
2277 (_("unrecognized characters at end of parallel processing insn")));
2278 break;
2282 move_code = movx | movy;
2283 if (field_b)
2285 /* Parallel processing insn. */
2286 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
2288 output = frag_more (4);
2289 size = 4;
2290 if (! target_big_endian)
2292 output[3] = ppi_code >> 8;
2293 output[2] = ppi_code;
2295 else
2297 output[2] = ppi_code >> 8;
2298 output[3] = ppi_code;
2300 move_code |= 0xf800;
2302 else
2304 /* Just a double data transfer. */
2305 output = frag_more (2);
2306 size = 2;
2308 if (! target_big_endian)
2310 output[1] = move_code >> 8;
2311 output[0] = move_code;
2313 else
2315 output[0] = move_code >> 8;
2316 output[1] = move_code;
2318 return size;
2321 /* This is the guts of the machine-dependent assembler. STR points to a
2322 machine dependent instruction. This function is supposed to emit
2323 the frags/bytes it assembles to. */
2325 void
2326 md_assemble (str)
2327 char *str;
2329 unsigned char *op_end;
2330 sh_operand_info operand[3];
2331 sh_opcode_info *opcode;
2332 unsigned int size = 0;
2334 #ifdef HAVE_SH64
2335 if (sh64_isa_mode == sh64_isa_shmedia)
2337 shmedia_md_assemble (str);
2338 return;
2340 else
2342 /* If we've seen pseudo-directives, make sure any emitted data or
2343 frags are marked as data. */
2344 if (seen_insn == false)
2346 sh64_update_contents_mark (true);
2347 sh64_set_contents_type (CRT_SH5_ISA16);
2350 seen_insn = true;
2352 #endif /* HAVE_SH64 */
2354 opcode = find_cooked_opcode (&str);
2355 op_end = str;
2357 if (opcode == NULL)
2359 as_bad (_("unknown opcode"));
2360 return;
2363 if (sh_relax
2364 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
2366 /* Output a CODE reloc to tell the linker that the following
2367 bytes are instructions, not data. */
2368 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2369 BFD_RELOC_SH_CODE);
2370 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
2373 if (opcode->nibbles[0] == PPI)
2375 size = assemble_ppi (op_end, opcode);
2377 else
2379 if (opcode->arg[0] == A_BDISP12
2380 || opcode->arg[0] == A_BDISP8)
2382 parse_exp (op_end + 1, &operand[0]);
2383 build_relax (opcode, &operand[0]);
2385 else
2387 if (opcode->arg[0] == A_END)
2389 /* Ignore trailing whitespace. If there is any, it has already
2390 been compressed to a single space. */
2391 if (*op_end == ' ')
2392 op_end++;
2394 else
2396 op_end = get_operands (opcode, op_end, operand);
2398 opcode = get_specific (opcode, operand);
2400 if (opcode == 0)
2402 /* Couldn't find an opcode which matched the operands. */
2403 char *where = frag_more (2);
2404 size = 2;
2406 where[0] = 0x0;
2407 where[1] = 0x0;
2408 as_bad (_("invalid operands for opcode"));
2410 else
2412 if (*op_end)
2413 as_bad (_("excess operands: '%s'"), op_end);
2415 size = build_Mytes (opcode, operand);
2420 #ifdef BFD_ASSEMBLER
2421 dwarf2_emit_insn (size);
2422 #endif
2425 /* This routine is called each time a label definition is seen. It
2426 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
2428 void
2429 sh_frob_label ()
2431 static fragS *last_label_frag;
2432 static int last_label_offset;
2434 if (sh_relax
2435 && seg_info (now_seg)->tc_segment_info_data.in_code)
2437 int offset;
2439 offset = frag_now_fix ();
2440 if (frag_now != last_label_frag
2441 || offset != last_label_offset)
2443 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
2444 last_label_frag = frag_now;
2445 last_label_offset = offset;
2450 /* This routine is called when the assembler is about to output some
2451 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
2453 void
2454 sh_flush_pending_output ()
2456 if (sh_relax
2457 && seg_info (now_seg)->tc_segment_info_data.in_code)
2459 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2460 BFD_RELOC_SH_DATA);
2461 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
2465 symbolS *
2466 md_undefined_symbol (name)
2467 char *name ATTRIBUTE_UNUSED;
2469 return 0;
2472 #ifdef OBJ_COFF
2473 #ifndef BFD_ASSEMBLER
2475 void
2476 tc_crawl_symbol_chain (headers)
2477 object_headers *headers;
2479 printf (_("call to tc_crawl_symbol_chain \n"));
2482 void
2483 tc_headers_hook (headers)
2484 object_headers *headers;
2486 printf (_("call to tc_headers_hook \n"));
2489 #endif
2490 #endif
2492 /* Various routines to kill one day. */
2493 /* Equal to MAX_PRECISION in atof-ieee.c. */
2494 #define MAX_LITTLENUMS 6
2496 /* Turn a string in input_line_pointer into a floating point constant
2497 of type TYPE, and store the appropriate bytes in *LITP. The number
2498 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2499 returned, or NULL on OK. */
2501 char *
2502 md_atof (type, litP, sizeP)
2503 int type;
2504 char *litP;
2505 int *sizeP;
2507 int prec;
2508 LITTLENUM_TYPE words[4];
2509 char *t;
2510 int i;
2512 switch (type)
2514 case 'f':
2515 prec = 2;
2516 break;
2518 case 'd':
2519 prec = 4;
2520 break;
2522 default:
2523 *sizeP = 0;
2524 return _("bad call to md_atof");
2527 t = atof_ieee (input_line_pointer, type, words);
2528 if (t)
2529 input_line_pointer = t;
2531 *sizeP = prec * 2;
2533 if (! target_big_endian)
2535 for (i = prec - 1; i >= 0; i--)
2537 md_number_to_chars (litP, (valueT) words[i], 2);
2538 litP += 2;
2541 else
2543 for (i = 0; i < prec; i++)
2545 md_number_to_chars (litP, (valueT) words[i], 2);
2546 litP += 2;
2550 return NULL;
2553 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
2554 call instruction. It refers to a label of the instruction which
2555 loads the register which the call uses. We use it to generate a
2556 special reloc for the linker. */
2558 static void
2559 s_uses (ignore)
2560 int ignore ATTRIBUTE_UNUSED;
2562 expressionS ex;
2564 if (! sh_relax)
2565 as_warn (_(".uses pseudo-op seen when not relaxing"));
2567 expression (&ex);
2569 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2571 as_bad (_("bad .uses format"));
2572 ignore_rest_of_line ();
2573 return;
2576 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
2578 demand_empty_rest_of_line ();
2581 CONST char *md_shortopts = "";
2582 struct option md_longopts[] =
2584 #define OPTION_RELAX (OPTION_MD_BASE)
2585 #define OPTION_BIG (OPTION_MD_BASE + 1)
2586 #define OPTION_LITTLE (OPTION_BIG + 1)
2587 #define OPTION_SMALL (OPTION_LITTLE + 1)
2588 #define OPTION_DSP (OPTION_SMALL + 1)
2590 {"relax", no_argument, NULL, OPTION_RELAX},
2591 {"big", no_argument, NULL, OPTION_BIG},
2592 {"little", no_argument, NULL, OPTION_LITTLE},
2593 {"small", no_argument, NULL, OPTION_SMALL},
2594 {"dsp", no_argument, NULL, OPTION_DSP},
2595 #ifdef HAVE_SH64
2596 #define OPTION_ISA (OPTION_DSP + 1)
2597 #define OPTION_ABI (OPTION_ISA + 1)
2598 #define OPTION_NO_MIX (OPTION_ABI + 1)
2599 #define OPTION_SHCOMPACT_CONST_CRANGE (OPTION_NO_MIX + 1)
2600 #define OPTION_NO_EXPAND (OPTION_SHCOMPACT_CONST_CRANGE + 1)
2601 #define OPTION_PT32 (OPTION_NO_EXPAND + 1)
2602 {"isa", required_argument, NULL, OPTION_ISA},
2603 {"abi", required_argument, NULL, OPTION_ABI},
2604 {"no-mix", no_argument, NULL, OPTION_NO_MIX},
2605 {"shcompact-const-crange", no_argument, NULL, OPTION_SHCOMPACT_CONST_CRANGE},
2606 {"no-expand", no_argument, NULL, OPTION_NO_EXPAND},
2607 {"expand-pt32", no_argument, NULL, OPTION_PT32},
2608 #endif /* HAVE_SH64 */
2610 {NULL, no_argument, NULL, 0}
2612 size_t md_longopts_size = sizeof (md_longopts);
2615 md_parse_option (c, arg)
2616 int c;
2617 char *arg ATTRIBUTE_UNUSED;
2619 switch (c)
2621 case OPTION_RELAX:
2622 sh_relax = 1;
2623 break;
2625 case OPTION_BIG:
2626 target_big_endian = 1;
2627 break;
2629 case OPTION_LITTLE:
2630 target_big_endian = 0;
2631 break;
2633 case OPTION_SMALL:
2634 sh_small = 1;
2635 break;
2637 case OPTION_DSP:
2638 sh_dsp = 1;
2639 break;
2641 #ifdef HAVE_SH64
2642 case OPTION_ISA:
2643 if (strcasecmp (arg, "shmedia") == 0)
2645 if (sh64_isa_mode == sh64_isa_shcompact)
2646 as_bad (_("Invalid combination: --isa=SHcompact with --isa=SHmedia"));
2647 sh64_isa_mode = sh64_isa_shmedia;
2649 else if (strcasecmp (arg, "shcompact") == 0)
2651 if (sh64_isa_mode == sh64_isa_shmedia)
2652 as_bad (_("Invalid combination: --isa=SHmedia with --isa=SHcompact"));
2653 if (sh64_abi == sh64_abi_64)
2654 as_bad (_("Invalid combination: --abi=64 with --isa=SHcompact"));
2655 sh64_isa_mode = sh64_isa_shcompact;
2657 else
2658 as_bad ("Invalid argument to --isa option: %s", arg);
2659 break;
2661 case OPTION_ABI:
2662 if (strcmp (arg, "32") == 0)
2664 if (sh64_abi == sh64_abi_64)
2665 as_bad (_("Invalid combination: --abi=32 with --abi=64"));
2666 sh64_abi = sh64_abi_32;
2668 else if (strcmp (arg, "64") == 0)
2670 if (sh64_abi == sh64_abi_32)
2671 as_bad (_("Invalid combination: --abi=64 with --abi=32"));
2672 if (sh64_isa_mode == sh64_isa_shcompact)
2673 as_bad (_("Invalid combination: --isa=SHcompact with --abi=64"));
2674 sh64_abi = sh64_abi_64;
2676 else
2677 as_bad ("Invalid argument to --abi option: %s", arg);
2678 break;
2680 case OPTION_NO_MIX:
2681 sh64_mix = false;
2682 break;
2684 case OPTION_SHCOMPACT_CONST_CRANGE:
2685 sh64_shcompact_const_crange = true;
2686 break;
2688 case OPTION_NO_EXPAND:
2689 sh64_expand = false;
2690 break;
2692 case OPTION_PT32:
2693 sh64_pt32 = true;
2694 break;
2695 #endif /* HAVE_SH64 */
2697 default:
2698 return 0;
2701 return 1;
2704 void
2705 md_show_usage (stream)
2706 FILE *stream;
2708 fprintf (stream, _("\
2709 SH options:\n\
2710 -little generate little endian code\n\
2711 -big generate big endian code\n\
2712 -relax alter jump instructions for long displacements\n\
2713 -small align sections to 4 byte boundaries, not 16\n\
2714 -dsp enable sh-dsp insns, and disable sh3e / sh4 insns.\n"));
2715 #ifdef HAVE_SH64
2716 fprintf (stream, _("\
2717 -isa=[shmedia set default instruction set for SH64\n\
2718 | SHmedia\n\
2719 | shcompact\n\
2720 | SHcompact]\n\
2721 -abi=[32|64] set size of expanded SHmedia operands and object\n\
2722 file type\n\
2723 -shcompact-const-crange emit code-range descriptors for constants in\n\
2724 SHcompact code sections\n\
2725 -no-mix disallow SHmedia code in the same section as\n\
2726 constants and SHcompact code\n\
2727 -no-expand do not expand MOVI, PT, PTA or PTB instructions\n\
2728 -expand-pt32 with -abi=64, expand PT, PTA and PTB instructions\n\
2729 to 32 bits only"));
2730 #endif /* HAVE_SH64 */
2733 /* This struct is used to pass arguments to sh_count_relocs through
2734 bfd_map_over_sections. */
2736 struct sh_count_relocs
2738 /* Symbol we are looking for. */
2739 symbolS *sym;
2740 /* Count of relocs found. */
2741 int count;
2744 /* Count the number of fixups in a section which refer to a particular
2745 symbol. When using BFD_ASSEMBLER, this is called via
2746 bfd_map_over_sections. */
2748 static void
2749 sh_count_relocs (abfd, sec, data)
2750 bfd *abfd ATTRIBUTE_UNUSED;
2751 segT sec;
2752 PTR data;
2754 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
2755 segment_info_type *seginfo;
2756 symbolS *sym;
2757 fixS *fix;
2759 seginfo = seg_info (sec);
2760 if (seginfo == NULL)
2761 return;
2763 sym = info->sym;
2764 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2766 if (fix->fx_addsy == sym)
2768 ++info->count;
2769 fix->fx_tcbit = 1;
2774 /* Handle the count relocs for a particular section. When using
2775 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
2777 static void
2778 sh_frob_section (abfd, sec, ignore)
2779 bfd *abfd ATTRIBUTE_UNUSED;
2780 segT sec;
2781 PTR ignore ATTRIBUTE_UNUSED;
2783 segment_info_type *seginfo;
2784 fixS *fix;
2786 seginfo = seg_info (sec);
2787 if (seginfo == NULL)
2788 return;
2790 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2792 symbolS *sym;
2793 bfd_vma val;
2794 fixS *fscan;
2795 struct sh_count_relocs info;
2797 if (fix->fx_r_type != BFD_RELOC_SH_USES)
2798 continue;
2800 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2801 symbol in the same section. */
2802 sym = fix->fx_addsy;
2803 if (sym == NULL
2804 || fix->fx_subsy != NULL
2805 || fix->fx_addnumber != 0
2806 || S_GET_SEGMENT (sym) != sec
2807 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2808 || S_GET_STORAGE_CLASS (sym) == C_EXT
2809 #endif
2810 || S_IS_EXTERNAL (sym))
2812 as_warn_where (fix->fx_file, fix->fx_line,
2813 _(".uses does not refer to a local symbol in the same section"));
2814 continue;
2817 /* Look through the fixups again, this time looking for one
2818 at the same location as sym. */
2819 val = S_GET_VALUE (sym);
2820 for (fscan = seginfo->fix_root;
2821 fscan != NULL;
2822 fscan = fscan->fx_next)
2823 if (val == fscan->fx_frag->fr_address + fscan->fx_where
2824 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
2825 && fscan->fx_r_type != BFD_RELOC_SH_CODE
2826 && fscan->fx_r_type != BFD_RELOC_SH_DATA
2827 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
2828 break;
2829 if (fscan == NULL)
2831 as_warn_where (fix->fx_file, fix->fx_line,
2832 _("can't find fixup pointed to by .uses"));
2833 continue;
2836 if (fscan->fx_tcbit)
2838 /* We've already done this one. */
2839 continue;
2842 /* The variable fscan should also be a fixup to a local symbol
2843 in the same section. */
2844 sym = fscan->fx_addsy;
2845 if (sym == NULL
2846 || fscan->fx_subsy != NULL
2847 || fscan->fx_addnumber != 0
2848 || S_GET_SEGMENT (sym) != sec
2849 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2850 || S_GET_STORAGE_CLASS (sym) == C_EXT
2851 #endif
2852 || S_IS_EXTERNAL (sym))
2854 as_warn_where (fix->fx_file, fix->fx_line,
2855 _(".uses target does not refer to a local symbol in the same section"));
2856 continue;
2859 /* Now we look through all the fixups of all the sections,
2860 counting the number of times we find a reference to sym. */
2861 info.sym = sym;
2862 info.count = 0;
2863 #ifdef BFD_ASSEMBLER
2864 bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
2865 #else
2867 int iscan;
2869 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
2870 sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
2872 #endif
2874 if (info.count < 1)
2875 abort ();
2877 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
2878 We have already adjusted the value of sym to include the
2879 fragment address, so we undo that adjustment here. */
2880 subseg_change (sec, 0);
2881 fix_new (fscan->fx_frag,
2882 S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
2883 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
2887 /* This function is called after the symbol table has been completed,
2888 but before the relocs or section contents have been written out.
2889 If we have seen any .uses pseudo-ops, they point to an instruction
2890 which loads a register with the address of a function. We look
2891 through the fixups to find where the function address is being
2892 loaded from. We then generate a COUNT reloc giving the number of
2893 times that function address is referred to. The linker uses this
2894 information when doing relaxing, to decide when it can eliminate
2895 the stored function address entirely. */
2897 void
2898 sh_frob_file ()
2900 #ifdef HAVE_SH64
2901 shmedia_frob_file_before_adjust ();
2902 #endif
2904 if (! sh_relax)
2905 return;
2907 #ifdef BFD_ASSEMBLER
2908 bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
2909 #else
2911 int iseg;
2913 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
2914 sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
2916 #endif
2919 /* Called after relaxing. Set the correct sizes of the fragments, and
2920 create relocs so that md_apply_fix3 will fill in the correct values. */
2922 void
2923 md_convert_frag (headers, seg, fragP)
2924 #ifdef BFD_ASSEMBLER
2925 bfd *headers ATTRIBUTE_UNUSED;
2926 #else
2927 object_headers *headers;
2928 #endif
2929 segT seg;
2930 fragS *fragP;
2932 int donerelax = 0;
2934 switch (fragP->fr_subtype)
2936 case C (COND_JUMP, COND8):
2937 case C (COND_JUMP_DELAY, COND8):
2938 subseg_change (seg, 0);
2939 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2940 1, BFD_RELOC_SH_PCDISP8BY2);
2941 fragP->fr_fix += 2;
2942 fragP->fr_var = 0;
2943 break;
2945 case C (UNCOND_JUMP, UNCOND12):
2946 subseg_change (seg, 0);
2947 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2948 1, BFD_RELOC_SH_PCDISP12BY2);
2949 fragP->fr_fix += 2;
2950 fragP->fr_var = 0;
2951 break;
2953 case C (UNCOND_JUMP, UNCOND32):
2954 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
2955 if (fragP->fr_symbol == NULL)
2956 as_bad_where (fragP->fr_file, fragP->fr_line,
2957 _("displacement overflows 12-bit field"));
2958 else if (S_IS_DEFINED (fragP->fr_symbol))
2959 as_bad_where (fragP->fr_file, fragP->fr_line,
2960 _("displacement to defined symbol %s overflows 12-bit field"),
2961 S_GET_NAME (fragP->fr_symbol));
2962 else
2963 as_bad_where (fragP->fr_file, fragP->fr_line,
2964 _("displacement to undefined symbol %s overflows 12-bit field"),
2965 S_GET_NAME (fragP->fr_symbol));
2966 /* Stabilize this frag, so we don't trip an assert. */
2967 fragP->fr_fix += fragP->fr_var;
2968 fragP->fr_var = 0;
2969 break;
2971 case C (COND_JUMP, COND12):
2972 case C (COND_JUMP_DELAY, COND12):
2973 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
2974 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
2975 was due to gas incorrectly relaxing an out-of-range conditional
2976 branch with delay slot. It turned:
2977 bf.s L6 (slot mov.l r12,@(44,r0))
2978 into:
2980 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
2981 30: 00 09 nop
2982 32: 10 cb mov.l r12,@(44,r0)
2983 Therefore, branches with delay slots have to be handled
2984 differently from ones without delay slots. */
2986 unsigned char *buffer =
2987 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
2988 int highbyte = target_big_endian ? 0 : 1;
2989 int lowbyte = target_big_endian ? 1 : 0;
2990 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
2992 /* Toggle the true/false bit of the bcond. */
2993 buffer[highbyte] ^= 0x2;
2995 /* If this is a delayed branch, we may not put the bra in the
2996 slot. So we change it to a non-delayed branch, like that:
2997 b! cond slot_label; bra disp; slot_label: slot_insn
2998 ??? We should try if swapping the conditional branch and
2999 its delay-slot insn already makes the branch reach. */
3001 /* Build a relocation to six / four bytes farther on. */
3002 subseg_change (seg, 0);
3003 fix_new (fragP, fragP->fr_fix, 2,
3004 #ifdef BFD_ASSEMBLER
3005 section_symbol (seg),
3006 #else
3007 seg_info (seg)->dot,
3008 #endif
3009 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
3010 1, BFD_RELOC_SH_PCDISP8BY2);
3012 /* Set up a jump instruction. */
3013 buffer[highbyte + 2] = 0xa0;
3014 buffer[lowbyte + 2] = 0;
3015 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
3016 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
3018 if (delay)
3020 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
3021 fragP->fr_fix += 4;
3023 else
3025 /* Fill in a NOP instruction. */
3026 buffer[highbyte + 4] = 0x0;
3027 buffer[lowbyte + 4] = 0x9;
3029 fragP->fr_fix += 6;
3031 fragP->fr_var = 0;
3032 donerelax = 1;
3034 break;
3036 case C (COND_JUMP, COND32):
3037 case C (COND_JUMP_DELAY, COND32):
3038 case C (COND_JUMP, UNDEF_WORD_DISP):
3039 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3040 if (fragP->fr_symbol == NULL)
3041 as_bad_where (fragP->fr_file, fragP->fr_line,
3042 _("displacement overflows 8-bit field"));
3043 else if (S_IS_DEFINED (fragP->fr_symbol))
3044 as_bad_where (fragP->fr_file, fragP->fr_line,
3045 _("displacement to defined symbol %s overflows 8-bit field"),
3046 S_GET_NAME (fragP->fr_symbol));
3047 else
3048 as_bad_where (fragP->fr_file, fragP->fr_line,
3049 _("displacement to undefined symbol %s overflows 8-bit field "),
3050 S_GET_NAME (fragP->fr_symbol));
3051 /* Stabilize this frag, so we don't trip an assert. */
3052 fragP->fr_fix += fragP->fr_var;
3053 fragP->fr_var = 0;
3054 break;
3056 default:
3057 #ifdef HAVE_SH64
3058 shmedia_md_convert_frag (headers, seg, fragP, true);
3059 #else
3060 abort ();
3061 #endif
3064 if (donerelax && !sh_relax)
3065 as_warn_where (fragP->fr_file, fragP->fr_line,
3066 _("overflow in branch to %s; converted into longer instruction sequence"),
3067 (fragP->fr_symbol != NULL
3068 ? S_GET_NAME (fragP->fr_symbol)
3069 : ""));
3072 valueT
3073 md_section_align (seg, size)
3074 segT seg ATTRIBUTE_UNUSED;
3075 valueT size;
3077 #ifdef BFD_ASSEMBLER
3078 #ifdef OBJ_ELF
3079 return size;
3080 #else /* ! OBJ_ELF */
3081 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
3082 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
3083 #endif /* ! OBJ_ELF */
3084 #else /* ! BFD_ASSEMBLER */
3085 return ((size + (1 << section_alignment[(int) seg]) - 1)
3086 & (-1 << section_alignment[(int) seg]));
3087 #endif /* ! BFD_ASSEMBLER */
3090 /* This static variable is set by s_uacons to tell sh_cons_align that
3091 the expession does not need to be aligned. */
3093 static int sh_no_align_cons = 0;
3095 /* This handles the unaligned space allocation pseudo-ops, such as
3096 .uaword. .uaword is just like .word, but the value does not need
3097 to be aligned. */
3099 static void
3100 s_uacons (bytes)
3101 int bytes;
3103 /* Tell sh_cons_align not to align this value. */
3104 sh_no_align_cons = 1;
3105 cons (bytes);
3108 /* If a .word, et. al., pseud-op is seen, warn if the value is not
3109 aligned correctly. Note that this can cause warnings to be issued
3110 when assembling initialized structured which were declared with the
3111 packed attribute. FIXME: Perhaps we should require an option to
3112 enable this warning? */
3114 void
3115 sh_cons_align (nbytes)
3116 int nbytes;
3118 int nalign;
3119 char *p;
3121 if (sh_no_align_cons)
3123 /* This is an unaligned pseudo-op. */
3124 sh_no_align_cons = 0;
3125 return;
3128 nalign = 0;
3129 while ((nbytes & 1) == 0)
3131 ++nalign;
3132 nbytes >>= 1;
3135 if (nalign == 0)
3136 return;
3138 if (now_seg == absolute_section)
3140 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3141 as_warn (_("misaligned data"));
3142 return;
3145 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
3146 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3148 record_alignment (now_seg, nalign);
3151 /* When relaxing, we need to output a reloc for any .align directive
3152 that requests alignment to a four byte boundary or larger. This is
3153 also where we check for misaligned data. */
3155 void
3156 sh_handle_align (frag)
3157 fragS *frag;
3159 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
3161 if (frag->fr_type == rs_align_code)
3163 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
3164 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
3166 char *p = frag->fr_literal + frag->fr_fix;
3168 if (bytes & 1)
3170 *p++ = 0;
3171 bytes--;
3172 frag->fr_fix += 1;
3175 if (target_big_endian)
3177 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
3178 frag->fr_var = sizeof big_nop_pattern;
3180 else
3182 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
3183 frag->fr_var = sizeof little_nop_pattern;
3186 else if (frag->fr_type == rs_align_test)
3188 if (bytes != 0)
3189 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
3192 if (sh_relax
3193 && (frag->fr_type == rs_align
3194 || frag->fr_type == rs_align_code)
3195 && frag->fr_address + frag->fr_fix > 0
3196 && frag->fr_offset > 1
3197 && now_seg != bss_section)
3198 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
3199 BFD_RELOC_SH_ALIGN);
3202 /* This macro decides whether a particular reloc is an entry in a
3203 switch table. It is used when relaxing, because the linker needs
3204 to know about all such entries so that it can adjust them if
3205 necessary. */
3207 #ifdef BFD_ASSEMBLER
3208 #define SWITCH_TABLE_CONS(fix) (0)
3209 #else
3210 #define SWITCH_TABLE_CONS(fix) \
3211 ((fix)->fx_r_type == 0 \
3212 && ((fix)->fx_size == 2 \
3213 || (fix)->fx_size == 1 \
3214 || (fix)->fx_size == 4))
3215 #endif
3217 #define SWITCH_TABLE(fix) \
3218 ((fix)->fx_addsy != NULL \
3219 && (fix)->fx_subsy != NULL \
3220 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
3221 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
3222 && ((fix)->fx_r_type == BFD_RELOC_32 \
3223 || (fix)->fx_r_type == BFD_RELOC_16 \
3224 || (fix)->fx_r_type == BFD_RELOC_8 \
3225 || SWITCH_TABLE_CONS (fix)))
3227 /* See whether we need to force a relocation into the output file.
3228 This is used to force out switch and PC relative relocations when
3229 relaxing. */
3232 sh_force_relocation (fix)
3233 fixS *fix;
3236 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3237 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY
3238 || fix->fx_r_type == BFD_RELOC_SH_LOOP_START
3239 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END)
3240 return 1;
3242 if (! sh_relax)
3243 return 0;
3245 return (fix->fx_pcrel
3246 || SWITCH_TABLE (fix)
3247 || fix->fx_r_type == BFD_RELOC_SH_COUNT
3248 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
3249 || fix->fx_r_type == BFD_RELOC_SH_CODE
3250 || fix->fx_r_type == BFD_RELOC_SH_DATA
3251 #ifdef HAVE_SH64
3252 || fix->fx_r_type == BFD_RELOC_SH_SHMEDIA_CODE
3253 #endif
3254 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
3257 #ifdef OBJ_ELF
3258 boolean
3259 sh_fix_adjustable (fixP)
3260 fixS *fixP;
3263 if (fixP->fx_addsy == NULL)
3264 return 1;
3266 if (fixP->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
3267 || fixP->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
3268 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
3269 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
3270 || fixP->fx_r_type == BFD_RELOC_8_PCREL
3271 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH16
3272 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH32)
3273 return 1;
3275 if (! TC_RELOC_RTSYM_LOC_FIXUP (fixP)
3276 || fixP->fx_r_type == BFD_RELOC_RVA)
3277 return 0;
3279 /* We need the symbol name for the VTABLE entries */
3280 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3281 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3282 return 0;
3284 return 1;
3287 void
3288 sh_elf_final_processing ()
3290 int val;
3292 /* Set file-specific flags to indicate if this code needs
3293 a processor with the sh-dsp / sh3e ISA to execute. */
3294 #ifdef HAVE_SH64
3295 /* SH5 and above don't know about the valid_arch arch_sh* bits defined
3296 in sh-opc.h, so check SH64 mode before checking valid_arch. */
3297 if (sh64_isa_mode != sh64_isa_unspecified)
3298 val = EF_SH5;
3299 else
3300 #endif /* HAVE_SH64 */
3301 if (valid_arch & arch_sh1)
3302 val = EF_SH1;
3303 else if (valid_arch & arch_sh2)
3304 val = EF_SH2;
3305 else if (valid_arch & arch_sh_dsp)
3306 val = EF_SH_DSP;
3307 else if (valid_arch & arch_sh3)
3308 val = EF_SH3;
3309 else if (valid_arch & arch_sh3_dsp)
3310 val = EF_SH_DSP;
3311 else if (valid_arch & arch_sh3e)
3312 val = EF_SH3E;
3313 else if (valid_arch & arch_sh4)
3314 val = EF_SH4;
3315 else
3316 abort ();
3318 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
3319 elf_elfheader (stdoutput)->e_flags |= val;
3321 #endif
3323 /* Apply a fixup to the object file. */
3325 void
3326 md_apply_fix3 (fixP, valP, seg)
3327 fixS * fixP;
3328 valueT * valP;
3329 segT seg ATTRIBUTE_UNUSED;
3331 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3332 int lowbyte = target_big_endian ? 1 : 0;
3333 int highbyte = target_big_endian ? 0 : 1;
3334 long val = (long) *valP;
3335 long max, min;
3336 int shift;
3338 #ifdef BFD_ASSEMBLER
3339 /* A difference between two symbols, the second of which is in the
3340 current section, is transformed in a PC-relative relocation to
3341 the other symbol. We have to adjust the relocation type here. */
3342 if (fixP->fx_pcrel)
3344 switch (fixP->fx_r_type)
3346 default:
3347 break;
3349 case BFD_RELOC_32:
3350 fixP->fx_r_type = BFD_RELOC_32_PCREL;
3351 break;
3353 /* Currently, we only support 32-bit PCREL relocations.
3354 We'd need a new reloc type to handle 16_PCREL, and
3355 8_PCREL is already taken for R_SH_SWITCH8, which
3356 apparently does something completely different than what
3357 we need. FIXME. */
3358 case BFD_RELOC_16:
3359 bfd_set_error (bfd_error_bad_value);
3360 return;
3362 case BFD_RELOC_8:
3363 bfd_set_error (bfd_error_bad_value);
3364 return;
3368 /* The function adjust_reloc_syms won't convert a reloc against a weak
3369 symbol into a reloc against a section, but bfd_install_relocation
3370 will screw up if the symbol is defined, so we have to adjust val here
3371 to avoid the screw up later.
3373 For ordinary relocs, this does not happen for ELF, since for ELF,
3374 bfd_install_relocation uses the "special function" field of the
3375 howto, and does not execute the code that needs to be undone, as long
3376 as the special function does not return bfd_reloc_continue.
3377 It can happen for GOT- and PLT-type relocs the way they are
3378 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
3379 doesn't matter here since those relocs don't use VAL; see below. */
3380 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3381 && fixP->fx_addsy != NULL
3382 && S_IS_WEAK (fixP->fx_addsy))
3383 val -= S_GET_VALUE (fixP->fx_addsy);
3384 #endif
3386 #ifndef BFD_ASSEMBLER
3387 if (fixP->fx_r_type == 0)
3389 if (fixP->fx_size == 2)
3390 fixP->fx_r_type = BFD_RELOC_16;
3391 else if (fixP->fx_size == 4)
3392 fixP->fx_r_type = BFD_RELOC_32;
3393 else if (fixP->fx_size == 1)
3394 fixP->fx_r_type = BFD_RELOC_8;
3395 else
3396 abort ();
3398 #endif
3400 max = min = 0;
3401 shift = 0;
3402 switch (fixP->fx_r_type)
3404 case BFD_RELOC_SH_IMM4:
3405 max = 0xf;
3406 *buf = (*buf & 0xf0) | (val & 0xf);
3407 break;
3409 case BFD_RELOC_SH_IMM4BY2:
3410 max = 0xf;
3411 shift = 1;
3412 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
3413 break;
3415 case BFD_RELOC_SH_IMM4BY4:
3416 max = 0xf;
3417 shift = 2;
3418 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
3419 break;
3421 case BFD_RELOC_SH_IMM8BY2:
3422 max = 0xff;
3423 shift = 1;
3424 *buf = val >> 1;
3425 break;
3427 case BFD_RELOC_SH_IMM8BY4:
3428 max = 0xff;
3429 shift = 2;
3430 *buf = val >> 2;
3431 break;
3433 case BFD_RELOC_8:
3434 case BFD_RELOC_SH_IMM8:
3435 /* Sometimes the 8 bit value is sign extended (e.g., add) and
3436 sometimes it is not (e.g., and). We permit any 8 bit value.
3437 Note that adding further restrictions may invalidate
3438 reasonable looking assembly code, such as ``and -0x1,r0''. */
3439 max = 0xff;
3440 min = -0xff;
3441 *buf++ = val;
3442 break;
3444 case BFD_RELOC_SH_PCRELIMM8BY4:
3445 /* The lower two bits of the PC are cleared before the
3446 displacement is added in. We can assume that the destination
3447 is on a 4 byte bounday. If this instruction is also on a 4
3448 byte boundary, then we want
3449 (target - here) / 4
3450 and target - here is a multiple of 4.
3451 Otherwise, we are on a 2 byte boundary, and we want
3452 (target - (here - 2)) / 4
3453 and target - here is not a multiple of 4. Computing
3454 (target - (here - 2)) / 4 == (target - here + 2) / 4
3455 works for both cases, since in the first case the addition of
3456 2 will be removed by the division. target - here is in the
3457 variable val. */
3458 val = (val + 2) / 4;
3459 if (val & ~0xff)
3460 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3461 buf[lowbyte] = val;
3462 break;
3464 case BFD_RELOC_SH_PCRELIMM8BY2:
3465 val /= 2;
3466 if (val & ~0xff)
3467 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3468 buf[lowbyte] = val;
3469 break;
3471 case BFD_RELOC_SH_PCDISP8BY2:
3472 val /= 2;
3473 if (val < -0x80 || val > 0x7f)
3474 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3475 buf[lowbyte] = val;
3476 break;
3478 case BFD_RELOC_SH_PCDISP12BY2:
3479 val /= 2;
3480 if (val < -0x800 || val > 0x7ff)
3481 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3482 buf[lowbyte] = val & 0xff;
3483 buf[highbyte] |= (val >> 8) & 0xf;
3484 break;
3486 case BFD_RELOC_32:
3487 case BFD_RELOC_32_PCREL:
3488 md_number_to_chars (buf, val, 4);
3489 break;
3491 case BFD_RELOC_16:
3492 md_number_to_chars (buf, val, 2);
3493 break;
3495 case BFD_RELOC_SH_USES:
3496 /* Pass the value into sh_coff_reloc_mangle. */
3497 fixP->fx_addnumber = val;
3498 break;
3500 case BFD_RELOC_SH_COUNT:
3501 case BFD_RELOC_SH_ALIGN:
3502 case BFD_RELOC_SH_CODE:
3503 case BFD_RELOC_SH_DATA:
3504 case BFD_RELOC_SH_LABEL:
3505 /* Nothing to do here. */
3506 break;
3508 case BFD_RELOC_SH_LOOP_START:
3509 case BFD_RELOC_SH_LOOP_END:
3511 case BFD_RELOC_VTABLE_INHERIT:
3512 case BFD_RELOC_VTABLE_ENTRY:
3513 fixP->fx_done = 0;
3514 return;
3516 #ifdef OBJ_ELF
3517 case BFD_RELOC_32_PLT_PCREL:
3518 /* Make the jump instruction point to the address of the operand. At
3519 runtime we merely add the offset to the actual PLT entry. */
3520 * valP = 0xfffffffc;
3521 val = fixP->fx_addnumber;
3522 if (fixP->fx_subsy)
3523 val -= S_GET_VALUE (fixP->fx_subsy);
3524 md_number_to_chars (buf, val, 4);
3525 break;
3527 case BFD_RELOC_SH_GOTPC:
3528 /* This is tough to explain. We end up with this one if we have
3529 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
3530 The goal here is to obtain the absolute address of the GOT,
3531 and it is strongly preferable from a performance point of
3532 view to avoid using a runtime relocation for this. There are
3533 cases where you have something like:
3535 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
3537 and here no correction would be required. Internally in the
3538 assembler we treat operands of this form as not being pcrel
3539 since the '.' is explicitly mentioned, and I wonder whether
3540 it would simplify matters to do it this way. Who knows. In
3541 earlier versions of the PIC patches, the pcrel_adjust field
3542 was used to store the correction, but since the expression is
3543 not pcrel, I felt it would be confusing to do it this way. */
3544 * valP -= 1;
3545 md_number_to_chars (buf, val, 4);
3546 break;
3548 case BFD_RELOC_32_GOT_PCREL:
3549 case BFD_RELOC_SH_GOTPLT32:
3550 * valP = 0; /* Fully resolved at runtime. No addend. */
3551 md_number_to_chars (buf, 0, 4);
3552 break;
3554 case BFD_RELOC_32_GOTOFF:
3555 md_number_to_chars (buf, val, 4);
3556 break;
3557 #endif
3559 default:
3560 #ifdef HAVE_SH64
3561 shmedia_md_apply_fix3 (fixP, valP);
3562 return;
3563 #else
3564 abort ();
3565 #endif
3568 if (shift != 0)
3570 if ((val & ((1 << shift) - 1)) != 0)
3571 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
3572 if (val >= 0)
3573 val >>= shift;
3574 else
3575 val = ((val >> shift)
3576 | ((long) -1 & ~ ((long) -1 >> shift)));
3578 if (max != 0 && (val < min || val > max))
3579 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
3581 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
3582 fixP->fx_done = 1;
3585 /* Called just before address relaxation. Return the length
3586 by which a fragment must grow to reach it's destination. */
3589 md_estimate_size_before_relax (fragP, segment_type)
3590 register fragS *fragP;
3591 register segT segment_type;
3593 int what;
3595 switch (fragP->fr_subtype)
3597 default:
3598 #ifdef HAVE_SH64
3599 return shmedia_md_estimate_size_before_relax (fragP, segment_type);
3600 #else
3601 abort ();
3602 #endif
3605 case C (UNCOND_JUMP, UNDEF_DISP):
3606 /* Used to be a branch to somewhere which was unknown. */
3607 if (!fragP->fr_symbol)
3609 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3611 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3613 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3615 else
3617 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
3619 break;
3621 case C (COND_JUMP, UNDEF_DISP):
3622 case C (COND_JUMP_DELAY, UNDEF_DISP):
3623 what = GET_WHAT (fragP->fr_subtype);
3624 /* Used to be a branch to somewhere which was unknown. */
3625 if (fragP->fr_symbol
3626 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3628 /* Got a symbol and it's defined in this segment, become byte
3629 sized - maybe it will fix up. */
3630 fragP->fr_subtype = C (what, COND8);
3632 else if (fragP->fr_symbol)
3634 /* Its got a segment, but its not ours, so it will always be long. */
3635 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
3637 else
3639 /* We know the abs value. */
3640 fragP->fr_subtype = C (what, COND8);
3642 break;
3644 case C (UNCOND_JUMP, UNCOND12):
3645 case C (UNCOND_JUMP, UNCOND32):
3646 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3647 case C (COND_JUMP, COND8):
3648 case C (COND_JUMP, COND12):
3649 case C (COND_JUMP, COND32):
3650 case C (COND_JUMP, UNDEF_WORD_DISP):
3651 case C (COND_JUMP_DELAY, COND8):
3652 case C (COND_JUMP_DELAY, COND12):
3653 case C (COND_JUMP_DELAY, COND32):
3654 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3655 /* When relaxing a section for the second time, we don't need to
3656 do anything besides return the current size. */
3657 break;
3660 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
3661 return fragP->fr_var;
3664 /* Put number into target byte order. */
3666 void
3667 md_number_to_chars (ptr, use, nbytes)
3668 char *ptr;
3669 valueT use;
3670 int nbytes;
3672 #ifdef HAVE_SH64
3673 /* We might need to set the contents type to data. */
3674 sh64_flag_output ();
3675 #endif
3677 if (! target_big_endian)
3678 number_to_chars_littleendian (ptr, use, nbytes);
3679 else
3680 number_to_chars_bigendian (ptr, use, nbytes);
3683 long
3684 md_pcrel_from_section (fixP, sec)
3685 fixS *fixP;
3686 segT sec;
3688 if (fixP->fx_addsy != (symbolS *) NULL
3689 && (! S_IS_DEFINED (fixP->fx_addsy)
3690 || S_IS_EXTERN (fixP->fx_addsy)
3691 || S_IS_WEAK (fixP->fx_addsy)
3692 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3694 /* The symbol is undefined (or is defined but not in this section,
3695 or we're not sure about it being the final definition). Let the
3696 linker figure it out. We need to adjust the subtraction of a
3697 symbol to the position of the relocated data, though. */
3698 return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
3701 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
3704 #ifdef OBJ_COFF
3707 tc_coff_sizemachdep (frag)
3708 fragS *frag;
3710 return md_relax_table[frag->fr_subtype].rlx_length;
3713 #endif /* OBJ_COFF */
3715 #ifndef BFD_ASSEMBLER
3716 #ifdef OBJ_COFF
3718 /* Map BFD relocs to SH COFF relocs. */
3720 struct reloc_map
3722 bfd_reloc_code_real_type bfd_reloc;
3723 int sh_reloc;
3726 static const struct reloc_map coff_reloc_map[] =
3728 { BFD_RELOC_32, R_SH_IMM32 },
3729 { BFD_RELOC_16, R_SH_IMM16 },
3730 { BFD_RELOC_8, R_SH_IMM8 },
3731 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
3732 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
3733 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
3734 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
3735 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
3736 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
3737 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
3738 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
3739 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
3740 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
3741 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
3742 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
3743 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
3744 { BFD_RELOC_SH_USES, R_SH_USES },
3745 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
3746 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
3747 { BFD_RELOC_SH_CODE, R_SH_CODE },
3748 { BFD_RELOC_SH_DATA, R_SH_DATA },
3749 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
3750 { BFD_RELOC_UNUSED, 0 }
3753 /* Adjust a reloc for the SH. This is similar to the generic code,
3754 but does some minor tweaking. */
3756 void
3757 sh_coff_reloc_mangle (seg, fix, intr, paddr)
3758 segment_info_type *seg;
3759 fixS *fix;
3760 struct internal_reloc *intr;
3761 unsigned int paddr;
3763 symbolS *symbol_ptr = fix->fx_addsy;
3764 symbolS *dot;
3766 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
3768 if (! SWITCH_TABLE (fix))
3770 const struct reloc_map *rm;
3772 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
3773 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
3774 break;
3775 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
3776 as_bad_where (fix->fx_file, fix->fx_line,
3777 _("Can not represent %s relocation in this object file format"),
3778 bfd_get_reloc_code_name (fix->fx_r_type));
3779 intr->r_type = rm->sh_reloc;
3780 intr->r_offset = 0;
3782 else
3784 know (sh_relax);
3786 if (fix->fx_r_type == BFD_RELOC_16)
3787 intr->r_type = R_SH_SWITCH16;
3788 else if (fix->fx_r_type == BFD_RELOC_8)
3789 intr->r_type = R_SH_SWITCH8;
3790 else if (fix->fx_r_type == BFD_RELOC_32)
3791 intr->r_type = R_SH_SWITCH32;
3792 else
3793 abort ();
3795 /* For a switch reloc, we set r_offset to the difference between
3796 the reloc address and the subtrahend. When the linker is
3797 doing relaxing, it can use the determine the starting and
3798 ending points of the switch difference expression. */
3799 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
3802 /* PC relative relocs are always against the current section. */
3803 if (symbol_ptr == NULL)
3805 switch (fix->fx_r_type)
3807 case BFD_RELOC_SH_PCRELIMM8BY2:
3808 case BFD_RELOC_SH_PCRELIMM8BY4:
3809 case BFD_RELOC_SH_PCDISP8BY2:
3810 case BFD_RELOC_SH_PCDISP12BY2:
3811 case BFD_RELOC_SH_USES:
3812 symbol_ptr = seg->dot;
3813 break;
3814 default:
3815 break;
3819 if (fix->fx_r_type == BFD_RELOC_SH_USES)
3821 /* We can't store the offset in the object file, since this
3822 reloc does not take up any space, so we store it in r_offset.
3823 The fx_addnumber field was set in md_apply_fix3. */
3824 intr->r_offset = fix->fx_addnumber;
3826 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
3828 /* We can't store the count in the object file, since this reloc
3829 does not take up any space, so we store it in r_offset. The
3830 fx_offset field was set when the fixup was created in
3831 sh_coff_frob_file. */
3832 intr->r_offset = fix->fx_offset;
3833 /* This reloc is always absolute. */
3834 symbol_ptr = NULL;
3836 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
3838 /* Store the alignment in the r_offset field. */
3839 intr->r_offset = fix->fx_offset;
3840 /* This reloc is always absolute. */
3841 symbol_ptr = NULL;
3843 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
3844 || fix->fx_r_type == BFD_RELOC_SH_DATA
3845 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
3847 /* These relocs are always absolute. */
3848 symbol_ptr = NULL;
3851 /* Turn the segment of the symbol into an offset. */
3852 if (symbol_ptr != NULL)
3854 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
3855 if (dot != NULL)
3856 intr->r_symndx = dot->sy_number;
3857 else
3858 intr->r_symndx = symbol_ptr->sy_number;
3860 else
3861 intr->r_symndx = -1;
3864 #endif /* OBJ_COFF */
3865 #endif /* ! BFD_ASSEMBLER */
3867 #ifdef BFD_ASSEMBLER
3869 /* Create a reloc. */
3871 arelent *
3872 tc_gen_reloc (section, fixp)
3873 asection *section ATTRIBUTE_UNUSED;
3874 fixS *fixp;
3876 arelent *rel;
3877 bfd_reloc_code_real_type r_type;
3879 rel = (arelent *) xmalloc (sizeof (arelent));
3880 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3881 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3882 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3884 if (fixp->fx_subsy
3885 && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
3887 fixp->fx_addnumber -= S_GET_VALUE (fixp->fx_subsy);
3888 fixp->fx_subsy = 0;
3891 r_type = fixp->fx_r_type;
3893 if (SWITCH_TABLE (fixp))
3895 rel->addend = rel->address - S_GET_VALUE (fixp->fx_subsy);
3896 if (r_type == BFD_RELOC_16)
3897 r_type = BFD_RELOC_SH_SWITCH16;
3898 else if (r_type == BFD_RELOC_8)
3899 r_type = BFD_RELOC_8_PCREL;
3900 else if (r_type == BFD_RELOC_32)
3901 r_type = BFD_RELOC_SH_SWITCH32;
3902 else
3903 abort ();
3905 else if (r_type == BFD_RELOC_SH_USES)
3906 rel->addend = fixp->fx_addnumber;
3907 else if (r_type == BFD_RELOC_SH_COUNT)
3908 rel->addend = fixp->fx_offset;
3909 else if (r_type == BFD_RELOC_SH_ALIGN)
3910 rel->addend = fixp->fx_offset;
3911 else if (r_type == BFD_RELOC_VTABLE_INHERIT
3912 || r_type == BFD_RELOC_VTABLE_ENTRY)
3913 rel->addend = fixp->fx_offset;
3914 else if (r_type == BFD_RELOC_SH_LOOP_START
3915 || r_type == BFD_RELOC_SH_LOOP_END)
3916 rel->addend = fixp->fx_offset;
3917 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
3919 rel->addend = 0;
3920 rel->address = rel->addend = fixp->fx_offset;
3922 #ifdef HAVE_SH64
3923 else if (shmedia_init_reloc (rel, fixp))
3925 #endif
3926 else if (fixp->fx_pcrel)
3927 rel->addend = fixp->fx_addnumber;
3928 else if (r_type == BFD_RELOC_32 || r_type == BFD_RELOC_32_GOTOFF)
3929 rel->addend = fixp->fx_addnumber;
3930 else
3931 rel->addend = 0;
3933 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
3934 if (rel->howto == NULL || fixp->fx_subsy)
3936 as_bad_where (fixp->fx_file, fixp->fx_line,
3937 _("Cannot represent relocation type %s"),
3938 bfd_get_reloc_code_name (r_type));
3939 /* Set howto to a garbage value so that we can keep going. */
3940 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3941 assert (rel->howto != NULL);
3944 return rel;
3947 #ifdef OBJ_ELF
3948 inline static char *
3949 sh_end_of_match (cont, what)
3950 char *cont, *what;
3952 int len = strlen (what);
3954 if (strncasecmp (cont, what, strlen (what)) == 0
3955 && ! is_part_of_name (cont[len]))
3956 return cont + len;
3958 return NULL;
3962 sh_parse_name (name, exprP, nextcharP)
3963 char const *name;
3964 expressionS *exprP;
3965 char *nextcharP;
3967 char *next = input_line_pointer;
3968 char *next_end;
3969 int reloc_type;
3970 segT segment;
3972 exprP->X_op_symbol = NULL;
3974 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
3976 if (! GOT_symbol)
3977 GOT_symbol = symbol_find_or_make (name);
3979 exprP->X_add_symbol = GOT_symbol;
3980 no_suffix:
3981 /* If we have an absolute symbol or a reg, then we know its
3982 value now. */
3983 segment = S_GET_SEGMENT (exprP->X_add_symbol);
3984 if (segment == absolute_section)
3986 exprP->X_op = O_constant;
3987 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
3988 exprP->X_add_symbol = NULL;
3990 else if (segment == reg_section)
3992 exprP->X_op = O_register;
3993 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
3994 exprP->X_add_symbol = NULL;
3996 else
3998 exprP->X_op = O_symbol;
3999 exprP->X_add_number = 0;
4002 return 1;
4005 exprP->X_add_symbol = symbol_find_or_make (name);
4007 if (*nextcharP != '@')
4008 goto no_suffix;
4009 else if ((next_end = sh_end_of_match (next + 1, "GOTOFF")))
4010 reloc_type = BFD_RELOC_32_GOTOFF;
4011 else if ((next_end = sh_end_of_match (next + 1, "GOTPLT")))
4012 reloc_type = BFD_RELOC_SH_GOTPLT32;
4013 else if ((next_end = sh_end_of_match (next + 1, "GOT")))
4014 reloc_type = BFD_RELOC_32_GOT_PCREL;
4015 else if ((next_end = sh_end_of_match (next + 1, "PLT")))
4016 reloc_type = BFD_RELOC_32_PLT_PCREL;
4017 else
4018 goto no_suffix;
4020 *input_line_pointer = *nextcharP;
4021 input_line_pointer = next_end;
4022 *nextcharP = *input_line_pointer;
4023 *input_line_pointer = '\0';
4025 exprP->X_op = O_PIC_reloc;
4026 exprP->X_add_number = 0;
4027 exprP->X_md = reloc_type;
4029 return 1;
4031 #endif
4032 #endif /* BFD_ASSEMBLER */