tc-sh.c revision 1.1.1.2 1 /* tc-sh.c -- Assemble code for the Renesas / SuperH SH
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* Written By Steve Chamberlain <sac (at) cygnus.com> */
24
25 #include "as.h"
26 #include "subsegs.h"
27 #define DEFINE_TABLE
28 #include "opcodes/sh-opc.h"
29 #include "safe-ctype.h"
30 #include "struc-symbol.h"
31
32 #ifdef OBJ_ELF
33 #include "elf/sh.h"
34 #endif
35
36 #include "dwarf2dbg.h"
37 #include "dw2gencfi.h"
38
39 typedef struct
40 {
41 sh_arg_type type;
42 int reg;
43 expressionS immediate;
44 }
45 sh_operand_info;
46
47 const char comment_chars[] = "!";
48 const char line_separator_chars[] = ";";
49 const char line_comment_chars[] = "!#";
50
51 static void s_uses (int);
52 static void s_uacons (int);
53
54 #ifdef OBJ_ELF
55 static void sh_elf_cons (int);
56
57 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
58 #endif
59
60 static void
61 big (int ignore ATTRIBUTE_UNUSED)
62 {
63 if (! target_big_endian)
64 as_bad (_("directive .big encountered when option -big required"));
65
66 /* Stop further messages. */
67 target_big_endian = 1;
68 }
69
70 static void
71 little (int ignore ATTRIBUTE_UNUSED)
72 {
73 if (target_big_endian)
74 as_bad (_("directive .little encountered when option -little required"));
75
76 /* Stop further messages. */
77 target_big_endian = 0;
78 }
79
80 /* This table describes all the machine specific pseudo-ops the assembler
81 has to support. The fields are:
82 pseudo-op name without dot
83 function to call to execute this pseudo-op
84 Integer arg to pass to the function. */
85
86 const pseudo_typeS md_pseudo_table[] =
87 {
88 #ifdef OBJ_ELF
89 {"long", sh_elf_cons, 4},
90 {"int", sh_elf_cons, 4},
91 {"word", sh_elf_cons, 2},
92 {"short", sh_elf_cons, 2},
93 #else
94 {"int", cons, 4},
95 {"word", cons, 2},
96 #endif /* OBJ_ELF */
97 {"big", big, 0},
98 {"form", listing_psize, 0},
99 {"little", little, 0},
100 {"heading", listing_title, 0},
101 {"import", s_ignore, 0},
102 {"page", listing_eject, 0},
103 {"program", s_ignore, 0},
104 {"uses", s_uses, 0},
105 {"uaword", s_uacons, 2},
106 {"ualong", s_uacons, 4},
107 {"uaquad", s_uacons, 8},
108 {"2byte", s_uacons, 2},
109 {"4byte", s_uacons, 4},
110 {"8byte", s_uacons, 8},
111 #ifdef HAVE_SH64
112 {"mode", s_sh64_mode, 0 },
113
114 /* Have the old name too. */
115 {"isa", s_sh64_mode, 0 },
116
117 /* Assert that the right ABI is used. */
118 {"abi", s_sh64_abi, 0 },
119
120 { "vtable_inherit", sh64_vtable_inherit, 0 },
121 { "vtable_entry", sh64_vtable_entry, 0 },
122 #endif /* HAVE_SH64 */
123 {0, 0, 0}
124 };
125
126 int sh_relax; /* set if -relax seen */
127
128 /* Whether -small was seen. */
129
130 int sh_small;
131
132 /* Flag to generate relocations against symbol values for local symbols. */
133
134 static int dont_adjust_reloc_32;
135
136 /* Flag to indicate that '$' is allowed as a register prefix. */
137
138 static int allow_dollar_register_prefix;
139
140 /* Preset architecture set, if given; zero otherwise. */
141
142 static unsigned int preset_target_arch;
143
144 /* The bit mask of architectures that could
145 accommodate the insns seen so far. */
146 static unsigned int valid_arch;
147
148 #ifdef OBJ_ELF
149 /* Whether --fdpic was given. */
150 static int sh_fdpic;
151 #endif
152
153 const char EXP_CHARS[] = "eE";
154
155 /* Chars that mean this number is a floating point constant. */
156 /* As in 0f12.456 */
157 /* or 0d1.2345e12 */
158 const char FLT_CHARS[] = "rRsSfFdDxXpP";
159
160 #define C(a,b) ENCODE_RELAX(a,b)
161
162 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
163 #define GET_WHAT(x) ((x>>4))
164
165 /* These are the three types of relaxable instruction. */
166 /* These are the types of relaxable instructions; except for END which is
167 a marker. */
168 #define COND_JUMP 1
169 #define COND_JUMP_DELAY 2
170 #define UNCOND_JUMP 3
171
172 #ifdef HAVE_SH64
173
174 /* A 16-bit (times four) pc-relative operand, at most expanded to 32 bits. */
175 #define SH64PCREL16_32 4
176 /* A 16-bit (times four) pc-relative operand, at most expanded to 64 bits. */
177 #define SH64PCREL16_64 5
178
179 /* Variants of the above for adjusting the insn to PTA or PTB according to
180 the label. */
181 #define SH64PCREL16PT_32 6
182 #define SH64PCREL16PT_64 7
183
184 /* A MOVI expansion, expanding to at most 32 or 64 bits. */
185 #define MOVI_IMM_32 8
186 #define MOVI_IMM_32_PCREL 9
187 #define MOVI_IMM_64 10
188 #define MOVI_IMM_64_PCREL 11
189 #define END 12
190
191 #else /* HAVE_SH64 */
192
193 #define END 4
194
195 #endif /* HAVE_SH64 */
196
197 #define UNDEF_DISP 0
198 #define COND8 1
199 #define COND12 2
200 #define COND32 3
201 #define UNDEF_WORD_DISP 4
202
203 #define UNCOND12 1
204 #define UNCOND32 2
205
206 #ifdef HAVE_SH64
207 #define UNDEF_SH64PCREL 0
208 #define SH64PCREL16 1
209 #define SH64PCREL32 2
210 #define SH64PCREL48 3
211 #define SH64PCREL64 4
212 #define SH64PCRELPLT 5
213
214 #define UNDEF_MOVI 0
215 #define MOVI_16 1
216 #define MOVI_32 2
217 #define MOVI_48 3
218 #define MOVI_64 4
219 #define MOVI_PLT 5
220 #define MOVI_GOTOFF 6
221 #define MOVI_GOTPC 7
222 #endif /* HAVE_SH64 */
223
224 /* Branch displacements are from the address of the branch plus
225 four, thus all minimum and maximum values have 4 added to them. */
226 #define COND8_F 258
227 #define COND8_M -252
228 #define COND8_LENGTH 2
229
230 /* There is one extra instruction before the branch, so we must add
231 two more bytes to account for it. */
232 #define COND12_F 4100
233 #define COND12_M -4090
234 #define COND12_LENGTH 6
235
236 #define COND12_DELAY_LENGTH 4
237
238 /* ??? The minimum and maximum values are wrong, but this does not matter
239 since this relocation type is not supported yet. */
240 #define COND32_F (1<<30)
241 #define COND32_M -(1<<30)
242 #define COND32_LENGTH 14
243
244 #define UNCOND12_F 4098
245 #define UNCOND12_M -4092
246 #define UNCOND12_LENGTH 2
247
248 /* ??? The minimum and maximum values are wrong, but this does not matter
249 since this relocation type is not supported yet. */
250 #define UNCOND32_F (1<<30)
251 #define UNCOND32_M -(1<<30)
252 #define UNCOND32_LENGTH 14
253
254 #ifdef HAVE_SH64
255 /* The trivial expansion of a SH64PCREL16 relaxation is just a "PT label,
256 TRd" as is the current insn, so no extra length. Note that the "reach"
257 is calculated from the address *after* that insn, but the offset in the
258 insn is calculated from the beginning of the insn. We also need to
259 take into account the implicit 1 coded as the "A" in PTA when counting
260 forward. If PTB reaches an odd address, we trap that as an error
261 elsewhere, so we don't have to have different relaxation entries. We
262 don't add a one to the negative range, since PTB would then have the
263 farthest backward-reaching value skipped, not generated at relaxation. */
264 #define SH64PCREL16_F (32767 * 4 - 4 + 1)
265 #define SH64PCREL16_M (-32768 * 4 - 4)
266 #define SH64PCREL16_LENGTH 0
267
268 /* The next step is to change that PT insn into
269 MOVI ((label - datalabel Ln) >> 16) & 65535, R25
270 SHORI (label - datalabel Ln) & 65535, R25
271 Ln:
272 PTREL R25,TRd
273 which means two extra insns, 8 extra bytes. This is the limit for the
274 32-bit ABI.
275
276 The expressions look a bit bad since we have to adjust this to avoid overflow on a
277 32-bit host. */
278 #define SH64PCREL32_F ((((long) 1 << 30) - 1) * 2 + 1 - 4)
279 #define SH64PCREL32_LENGTH (2 * 4)
280
281 /* Similarly, we just change the MOVI and add a SHORI for the 48-bit
282 expansion. */
283 #if BFD_HOST_64BIT_LONG
284 /* The "reach" type is long, so we can only do this for a 64-bit-long
285 host. */
286 #define SH64PCREL32_M (((long) -1 << 30) * 2 - 4)
287 #define SH64PCREL48_F ((((long) 1 << 47) - 1) - 4)
288 #define SH64PCREL48_M (((long) -1 << 47) - 4)
289 #define SH64PCREL48_LENGTH (3 * 4)
290 #else
291 /* If the host does not have 64-bit longs, just make this state identical
292 in reach to the 32-bit state. Note that we have a slightly incorrect
293 reach, but the correct one above will overflow a 32-bit number. */
294 #define SH64PCREL32_M (((long) -1 << 30) * 2)
295 #define SH64PCREL48_F SH64PCREL32_F
296 #define SH64PCREL48_M SH64PCREL32_M
297 #define SH64PCREL48_LENGTH (3 * 4)
298 #endif /* BFD_HOST_64BIT_LONG */
299
300 /* And similarly for the 64-bit expansion; a MOVI + SHORI + SHORI + SHORI
301 + PTREL sequence. */
302 #define SH64PCREL64_LENGTH (4 * 4)
303
304 /* For MOVI, we make the MOVI + SHORI... expansion you can see in the
305 SH64PCREL expansions. The PCREL one is similar, but the other has no
306 pc-relative reach; it must be fully expanded in
307 shmedia_md_estimate_size_before_relax. */
308 #define MOVI_16_LENGTH 0
309 #define MOVI_16_F (32767 - 4)
310 #define MOVI_16_M (-32768 - 4)
311 #define MOVI_32_LENGTH 4
312 #define MOVI_32_F ((((long) 1 << 30) - 1) * 2 + 1 - 4)
313 #define MOVI_48_LENGTH 8
314
315 #if BFD_HOST_64BIT_LONG
316 /* The "reach" type is long, so we can only do this for a 64-bit-long
317 host. */
318 #define MOVI_32_M (((long) -1 << 30) * 2 - 4)
319 #define MOVI_48_F ((((long) 1 << 47) - 1) - 4)
320 #define MOVI_48_M (((long) -1 << 47) - 4)
321 #else
322 /* If the host does not have 64-bit longs, just make this state identical
323 in reach to the 32-bit state. Note that we have a slightly incorrect
324 reach, but the correct one above will overflow a 32-bit number. */
325 #define MOVI_32_M (((long) -1 << 30) * 2)
326 #define MOVI_48_F MOVI_32_F
327 #define MOVI_48_M MOVI_32_M
328 #endif /* BFD_HOST_64BIT_LONG */
329
330 #define MOVI_64_LENGTH 12
331 #endif /* HAVE_SH64 */
332
333 #define EMPTY { 0, 0, 0, 0 }
334
335 const relax_typeS md_relax_table[C (END, 0)] = {
336 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
337 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
338
339 EMPTY,
340 /* C (COND_JUMP, COND8) */
341 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
342 /* C (COND_JUMP, COND12) */
343 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
344 /* C (COND_JUMP, COND32) */
345 { COND32_F, COND32_M, COND32_LENGTH, 0, },
346 /* C (COND_JUMP, UNDEF_WORD_DISP) */
347 { 0, 0, COND32_LENGTH, 0, },
348 EMPTY, EMPTY, EMPTY,
349 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
350
351 EMPTY,
352 /* C (COND_JUMP_DELAY, COND8) */
353 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
354 /* C (COND_JUMP_DELAY, COND12) */
355 { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
356 /* C (COND_JUMP_DELAY, COND32) */
357 { COND32_F, COND32_M, COND32_LENGTH, 0, },
358 /* C (COND_JUMP_DELAY, UNDEF_WORD_DISP) */
359 { 0, 0, COND32_LENGTH, 0, },
360 EMPTY, EMPTY, EMPTY,
361 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
362
363 EMPTY,
364 /* C (UNCOND_JUMP, UNCOND12) */
365 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
366 /* C (UNCOND_JUMP, UNCOND32) */
367 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
368 EMPTY,
369 /* C (UNCOND_JUMP, UNDEF_WORD_DISP) */
370 { 0, 0, UNCOND32_LENGTH, 0, },
371 EMPTY, EMPTY, EMPTY,
372 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
373
374 #ifdef HAVE_SH64
375 /* C (SH64PCREL16_32, SH64PCREL16) */
376 EMPTY,
377 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16_32, SH64PCREL32) },
378 /* C (SH64PCREL16_32, SH64PCREL32) */
379 { 0, 0, SH64PCREL32_LENGTH, 0 },
380 EMPTY, EMPTY,
381 /* C (SH64PCREL16_32, SH64PCRELPLT) */
382 { 0, 0, SH64PCREL32_LENGTH, 0 },
383 EMPTY, EMPTY,
384 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
385
386 /* C (SH64PCREL16_64, SH64PCREL16) */
387 EMPTY,
388 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16_64, SH64PCREL32) },
389 /* C (SH64PCREL16_64, SH64PCREL32) */
390 { SH64PCREL32_F, SH64PCREL32_M, SH64PCREL32_LENGTH, C (SH64PCREL16_64, SH64PCREL48) },
391 /* C (SH64PCREL16_64, SH64PCREL48) */
392 { SH64PCREL48_F, SH64PCREL48_M, SH64PCREL48_LENGTH, C (SH64PCREL16_64, SH64PCREL64) },
393 /* C (SH64PCREL16_64, SH64PCREL64) */
394 { 0, 0, SH64PCREL64_LENGTH, 0 },
395 /* C (SH64PCREL16_64, SH64PCRELPLT) */
396 { 0, 0, SH64PCREL64_LENGTH, 0 },
397 EMPTY, EMPTY,
398 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
399
400 /* C (SH64PCREL16PT_32, SH64PCREL16) */
401 EMPTY,
402 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16PT_32, SH64PCREL32) },
403 /* C (SH64PCREL16PT_32, SH64PCREL32) */
404 { 0, 0, SH64PCREL32_LENGTH, 0 },
405 EMPTY, EMPTY,
406 /* C (SH64PCREL16PT_32, SH64PCRELPLT) */
407 { 0, 0, SH64PCREL32_LENGTH, 0 },
408 EMPTY, EMPTY,
409 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
410
411 /* C (SH64PCREL16PT_64, SH64PCREL16) */
412 EMPTY,
413 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16PT_64, SH64PCREL32) },
414 /* C (SH64PCREL16PT_64, SH64PCREL32) */
415 { SH64PCREL32_F,
416 SH64PCREL32_M,
417 SH64PCREL32_LENGTH,
418 C (SH64PCREL16PT_64, SH64PCREL48) },
419 /* C (SH64PCREL16PT_64, SH64PCREL48) */
420 { SH64PCREL48_F, SH64PCREL48_M, SH64PCREL48_LENGTH, C (SH64PCREL16PT_64, SH64PCREL64) },
421 /* C (SH64PCREL16PT_64, SH64PCREL64) */
422 { 0, 0, SH64PCREL64_LENGTH, 0 },
423 /* C (SH64PCREL16PT_64, SH64PCRELPLT) */
424 { 0, 0, SH64PCREL64_LENGTH, 0},
425 EMPTY, EMPTY,
426 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
427
428 /* C (MOVI_IMM_32, UNDEF_MOVI) */
429 { 0, 0, MOVI_32_LENGTH, 0 },
430 /* C (MOVI_IMM_32, MOVI_16) */
431 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_32, MOVI_32) },
432 /* C (MOVI_IMM_32, MOVI_32) */
433 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, 0 },
434 EMPTY, EMPTY, EMPTY,
435 /* C (MOVI_IMM_32, MOVI_GOTOFF) */
436 { 0, 0, MOVI_32_LENGTH, 0 },
437 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
438
439 /* C (MOVI_IMM_32_PCREL, MOVI_16) */
440 EMPTY,
441 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_32_PCREL, MOVI_32) },
442 /* C (MOVI_IMM_32_PCREL, MOVI_32) */
443 { 0, 0, MOVI_32_LENGTH, 0 },
444 EMPTY, EMPTY,
445 /* C (MOVI_IMM_32_PCREL, MOVI_PLT) */
446 { 0, 0, MOVI_32_LENGTH, 0 },
447 EMPTY,
448 /* C (MOVI_IMM_32_PCREL, MOVI_GOTPC) */
449 { 0, 0, MOVI_32_LENGTH, 0 },
450 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
451
452 /* C (MOVI_IMM_64, UNDEF_MOVI) */
453 { 0, 0, MOVI_64_LENGTH, 0 },
454 /* C (MOVI_IMM_64, MOVI_16) */
455 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_64, MOVI_32) },
456 /* C (MOVI_IMM_64, MOVI_32) */
457 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, C (MOVI_IMM_64, MOVI_48) },
458 /* C (MOVI_IMM_64, MOVI_48) */
459 { MOVI_48_F, MOVI_48_M, MOVI_48_LENGTH, C (MOVI_IMM_64, MOVI_64) },
460 /* C (MOVI_IMM_64, MOVI_64) */
461 { 0, 0, MOVI_64_LENGTH, 0 },
462 EMPTY,
463 /* C (MOVI_IMM_64, MOVI_GOTOFF) */
464 { 0, 0, MOVI_64_LENGTH, 0 },
465 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
466
467 /* C (MOVI_IMM_64_PCREL, MOVI_16) */
468 EMPTY,
469 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_32) },
470 /* C (MOVI_IMM_64_PCREL, MOVI_32) */
471 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_48) },
472 /* C (MOVI_IMM_64_PCREL, MOVI_48) */
473 { MOVI_48_F, MOVI_48_M, MOVI_48_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_64) },
474 /* C (MOVI_IMM_64_PCREL, MOVI_64) */
475 { 0, 0, MOVI_64_LENGTH, 0 },
476 /* C (MOVI_IMM_64_PCREL, MOVI_PLT) */
477 { 0, 0, MOVI_64_LENGTH, 0 },
478 EMPTY,
479 /* C (MOVI_IMM_64_PCREL, MOVI_GOTPC) */
480 { 0, 0, MOVI_64_LENGTH, 0 },
481 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
482
483 #endif /* HAVE_SH64 */
484
485 };
486
487 #undef EMPTY
488
489 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
490
491
492 #ifdef OBJ_ELF
494 /* Determinet whether the symbol needs any kind of PIC relocation. */
495
496 inline static int
497 sh_PIC_related_p (symbolS *sym)
498 {
499 expressionS *exp;
500
501 if (! sym)
502 return 0;
503
504 if (sym == GOT_symbol)
505 return 1;
506
507 #ifdef HAVE_SH64
508 if (sh_PIC_related_p (*symbol_get_tc (sym)))
509 return 1;
510 #endif
511
512 exp = symbol_get_value_expression (sym);
513
514 return (exp->X_op == O_PIC_reloc
515 || sh_PIC_related_p (exp->X_add_symbol)
516 || sh_PIC_related_p (exp->X_op_symbol));
517 }
518
519 /* Determine the relocation type to be used to represent the
520 expression, that may be rearranged. */
521
522 static int
523 sh_check_fixup (expressionS *main_exp, bfd_reloc_code_real_type *r_type_p)
524 {
525 expressionS *exp = main_exp;
526
527 /* This is here for backward-compatibility only. GCC used to generated:
528
529 f@PLT + . - (.LPCS# + 2)
530
531 but we'd rather be able to handle this as a PIC-related reference
532 plus/minus a symbol. However, gas' parser gives us:
533
534 O_subtract (O_add (f@PLT, .), .LPCS#+2)
535
536 so we attempt to transform this into:
537
538 O_subtract (f@PLT, O_subtract (.LPCS#+2, .))
539
540 which we can handle simply below. */
541 if (exp->X_op == O_subtract)
542 {
543 if (sh_PIC_related_p (exp->X_op_symbol))
544 return 1;
545
546 exp = symbol_get_value_expression (exp->X_add_symbol);
547
548 if (exp && sh_PIC_related_p (exp->X_op_symbol))
549 return 1;
550
551 if (exp && exp->X_op == O_add
552 && sh_PIC_related_p (exp->X_add_symbol))
553 {
554 symbolS *sym = exp->X_add_symbol;
555
556 exp->X_op = O_subtract;
557 exp->X_add_symbol = main_exp->X_op_symbol;
558
559 main_exp->X_op_symbol = main_exp->X_add_symbol;
560 main_exp->X_add_symbol = sym;
561
562 main_exp->X_add_number += exp->X_add_number;
563 exp->X_add_number = 0;
564 }
565
566 exp = main_exp;
567 }
568 else if (exp->X_op == O_add && sh_PIC_related_p (exp->X_op_symbol))
569 return 1;
570
571 if (exp->X_op == O_symbol || exp->X_op == O_add || exp->X_op == O_subtract)
572 {
573 #ifdef HAVE_SH64
574 if (exp->X_add_symbol
575 && (exp->X_add_symbol == GOT_symbol
576 || (GOT_symbol
577 && *symbol_get_tc (exp->X_add_symbol) == GOT_symbol)))
578 {
579 switch (*r_type_p)
580 {
581 case BFD_RELOC_SH_IMM_LOW16:
582 *r_type_p = BFD_RELOC_SH_GOTPC_LOW16;
583 break;
584
585 case BFD_RELOC_SH_IMM_MEDLOW16:
586 *r_type_p = BFD_RELOC_SH_GOTPC_MEDLOW16;
587 break;
588
589 case BFD_RELOC_SH_IMM_MEDHI16:
590 *r_type_p = BFD_RELOC_SH_GOTPC_MEDHI16;
591 break;
592
593 case BFD_RELOC_SH_IMM_HI16:
594 *r_type_p = BFD_RELOC_SH_GOTPC_HI16;
595 break;
596
597 case BFD_RELOC_NONE:
598 case BFD_RELOC_UNUSED:
599 *r_type_p = BFD_RELOC_SH_GOTPC;
600 break;
601
602 default:
603 abort ();
604 }
605 return 0;
606 }
607 #else
608 if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
609 {
610 *r_type_p = BFD_RELOC_SH_GOTPC;
611 return 0;
612 }
613 #endif
614 exp = symbol_get_value_expression (exp->X_add_symbol);
615 if (! exp)
616 return 0;
617 }
618
619 if (exp->X_op == O_PIC_reloc)
620 {
621 switch (*r_type_p)
622 {
623 case BFD_RELOC_NONE:
624 case BFD_RELOC_UNUSED:
625 *r_type_p = exp->X_md;
626 break;
627
628 case BFD_RELOC_SH_DISP20:
629 switch (exp->X_md)
630 {
631 case BFD_RELOC_32_GOT_PCREL:
632 *r_type_p = BFD_RELOC_SH_GOT20;
633 break;
634
635 case BFD_RELOC_32_GOTOFF:
636 *r_type_p = BFD_RELOC_SH_GOTOFF20;
637 break;
638
639 case BFD_RELOC_SH_GOTFUNCDESC:
640 *r_type_p = BFD_RELOC_SH_GOTFUNCDESC20;
641 break;
642
643 case BFD_RELOC_SH_GOTOFFFUNCDESC:
644 *r_type_p = BFD_RELOC_SH_GOTOFFFUNCDESC20;
645 break;
646
647 default:
648 abort ();
649 }
650 break;
651
652 #ifdef HAVE_SH64
653 case BFD_RELOC_SH_IMM_LOW16:
654 switch (exp->X_md)
655 {
656 case BFD_RELOC_32_GOTOFF:
657 *r_type_p = BFD_RELOC_SH_GOTOFF_LOW16;
658 break;
659
660 case BFD_RELOC_SH_GOTPLT32:
661 *r_type_p = BFD_RELOC_SH_GOTPLT_LOW16;
662 break;
663
664 case BFD_RELOC_32_GOT_PCREL:
665 *r_type_p = BFD_RELOC_SH_GOT_LOW16;
666 break;
667
668 case BFD_RELOC_32_PLT_PCREL:
669 *r_type_p = BFD_RELOC_SH_PLT_LOW16;
670 break;
671
672 default:
673 abort ();
674 }
675 break;
676
677 case BFD_RELOC_SH_IMM_MEDLOW16:
678 switch (exp->X_md)
679 {
680 case BFD_RELOC_32_GOTOFF:
681 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDLOW16;
682 break;
683
684 case BFD_RELOC_SH_GOTPLT32:
685 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDLOW16;
686 break;
687
688 case BFD_RELOC_32_GOT_PCREL:
689 *r_type_p = BFD_RELOC_SH_GOT_MEDLOW16;
690 break;
691
692 case BFD_RELOC_32_PLT_PCREL:
693 *r_type_p = BFD_RELOC_SH_PLT_MEDLOW16;
694 break;
695
696 default:
697 abort ();
698 }
699 break;
700
701 case BFD_RELOC_SH_IMM_MEDHI16:
702 switch (exp->X_md)
703 {
704 case BFD_RELOC_32_GOTOFF:
705 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDHI16;
706 break;
707
708 case BFD_RELOC_SH_GOTPLT32:
709 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDHI16;
710 break;
711
712 case BFD_RELOC_32_GOT_PCREL:
713 *r_type_p = BFD_RELOC_SH_GOT_MEDHI16;
714 break;
715
716 case BFD_RELOC_32_PLT_PCREL:
717 *r_type_p = BFD_RELOC_SH_PLT_MEDHI16;
718 break;
719
720 default:
721 abort ();
722 }
723 break;
724
725 case BFD_RELOC_SH_IMM_HI16:
726 switch (exp->X_md)
727 {
728 case BFD_RELOC_32_GOTOFF:
729 *r_type_p = BFD_RELOC_SH_GOTOFF_HI16;
730 break;
731
732 case BFD_RELOC_SH_GOTPLT32:
733 *r_type_p = BFD_RELOC_SH_GOTPLT_HI16;
734 break;
735
736 case BFD_RELOC_32_GOT_PCREL:
737 *r_type_p = BFD_RELOC_SH_GOT_HI16;
738 break;
739
740 case BFD_RELOC_32_PLT_PCREL:
741 *r_type_p = BFD_RELOC_SH_PLT_HI16;
742 break;
743
744 default:
745 abort ();
746 }
747 break;
748 #endif
749
750 default:
751 abort ();
752 }
753 if (exp == main_exp)
754 exp->X_op = O_symbol;
755 else
756 {
757 main_exp->X_add_symbol = exp->X_add_symbol;
758 main_exp->X_add_number += exp->X_add_number;
759 }
760 }
761 else
762 return (sh_PIC_related_p (exp->X_add_symbol)
763 || sh_PIC_related_p (exp->X_op_symbol));
764
765 return 0;
766 }
767
768 /* Add expression EXP of SIZE bytes to offset OFF of fragment FRAG. */
769
770 void
771 sh_cons_fix_new (fragS *frag, int off, int size, expressionS *exp)
772 {
773 bfd_reloc_code_real_type r_type = BFD_RELOC_UNUSED;
774
775 if (sh_check_fixup (exp, &r_type))
776 as_bad (_("Invalid PIC expression."));
777
778 if (r_type == BFD_RELOC_UNUSED)
779 switch (size)
780 {
781 case 1:
782 r_type = BFD_RELOC_8;
783 break;
784
785 case 2:
786 r_type = BFD_RELOC_16;
787 break;
788
789 case 4:
790 r_type = BFD_RELOC_32;
791 break;
792
793 #ifdef HAVE_SH64
794 case 8:
795 r_type = BFD_RELOC_64;
796 break;
797 #endif
798
799 default:
800 goto error;
801 }
802 else if (size != 4)
803 {
804 error:
805 as_bad (_("unsupported BFD relocation size %u"), size);
806 r_type = BFD_RELOC_UNUSED;
807 }
808
809 fix_new_exp (frag, off, size, exp, 0, r_type);
810 }
811
812 /* The regular cons() function, that reads constants, doesn't support
813 suffixes such as @GOT, @GOTOFF and @PLT, that generate
814 machine-specific relocation types. So we must define it here. */
815 /* Clobbers input_line_pointer, checks end-of-line. */
816 /* NBYTES 1=.byte, 2=.word, 4=.long */
817 static void
818 sh_elf_cons (register int nbytes)
819 {
820 expressionS exp;
821
822 #ifdef HAVE_SH64
823
824 /* Update existing range to include a previous insn, if there was one. */
825 sh64_update_contents_mark (TRUE);
826
827 /* We need to make sure the contents type is set to data. */
828 sh64_flag_output ();
829
830 #endif /* HAVE_SH64 */
831
832 if (is_it_end_of_statement ())
833 {
834 demand_empty_rest_of_line ();
835 return;
836 }
837
838 #ifdef md_cons_align
839 md_cons_align (nbytes);
840 #endif
841
842 do
843 {
844 expression (&exp);
845 emit_expr (&exp, (unsigned int) nbytes);
846 }
847 while (*input_line_pointer++ == ',');
848
849 input_line_pointer--; /* Put terminator back into stream. */
850 if (*input_line_pointer == '#' || *input_line_pointer == '!')
851 {
852 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
853 }
854 else
855 demand_empty_rest_of_line ();
856 }
857
858 /* The regular frag_offset_fixed_p doesn't work for rs_align_test
859 frags. */
860
861 static bfd_boolean
862 align_test_frag_offset_fixed_p (const fragS *frag1, const fragS *frag2,
863 bfd_vma *offset)
864 {
865 const fragS *frag;
866 bfd_vma off;
867
868 /* Start with offset initialised to difference between the two frags.
869 Prior to assigning frag addresses this will be zero. */
870 off = frag1->fr_address - frag2->fr_address;
871 if (frag1 == frag2)
872 {
873 *offset = off;
874 return TRUE;
875 }
876
877 /* Maybe frag2 is after frag1. */
878 frag = frag1;
879 while (frag->fr_type == rs_fill
880 || frag->fr_type == rs_align_test)
881 {
882 if (frag->fr_type == rs_fill)
883 off += frag->fr_fix + frag->fr_offset * frag->fr_var;
884 else
885 off += frag->fr_fix;
886 frag = frag->fr_next;
887 if (frag == NULL)
888 break;
889 if (frag == frag2)
890 {
891 *offset = off;
892 return TRUE;
893 }
894 }
895
896 /* Maybe frag1 is after frag2. */
897 off = frag1->fr_address - frag2->fr_address;
898 frag = frag2;
899 while (frag->fr_type == rs_fill
900 || frag->fr_type == rs_align_test)
901 {
902 if (frag->fr_type == rs_fill)
903 off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
904 else
905 off -= frag->fr_fix;
906 frag = frag->fr_next;
907 if (frag == NULL)
908 break;
909 if (frag == frag1)
910 {
911 *offset = off;
912 return TRUE;
913 }
914 }
915
916 return FALSE;
917 }
918
919 /* Optimize a difference of symbols which have rs_align_test frag if
920 possible. */
921
922 int
923 sh_optimize_expr (expressionS *l, operatorT op, expressionS *r)
924 {
925 bfd_vma frag_off;
926
927 if (op == O_subtract
928 && l->X_op == O_symbol
929 && r->X_op == O_symbol
930 && S_GET_SEGMENT (l->X_add_symbol) == S_GET_SEGMENT (r->X_add_symbol)
931 && (SEG_NORMAL (S_GET_SEGMENT (l->X_add_symbol))
932 || r->X_add_symbol == l->X_add_symbol)
933 && align_test_frag_offset_fixed_p (symbol_get_frag (l->X_add_symbol),
934 symbol_get_frag (r->X_add_symbol),
935 &frag_off))
936 {
937 l->X_add_number -= r->X_add_number;
938 l->X_add_number -= frag_off / OCTETS_PER_BYTE;
939 l->X_add_number += (S_GET_VALUE (l->X_add_symbol)
940 - S_GET_VALUE (r->X_add_symbol));
941 l->X_op = O_constant;
942 l->X_add_symbol = 0;
943 return 1;
944 }
945 return 0;
946 }
947 #endif /* OBJ_ELF */
948
949 /* This function is called once, at assembler startup time. This should
951 set up all the tables, etc that the MD part of the assembler needs. */
952
953 void
954 md_begin (void)
955 {
956 const sh_opcode_info *opcode;
957 char *prev_name = "";
958 unsigned int target_arch;
959
960 target_arch
961 = preset_target_arch ? preset_target_arch : arch_sh_up & ~arch_sh_has_dsp;
962 valid_arch = target_arch;
963
964 #ifdef HAVE_SH64
965 shmedia_md_begin ();
966 #endif
967
968 opcode_hash_control = hash_new ();
969
970 /* Insert unique names into hash table. */
971 for (opcode = sh_table; opcode->name; opcode++)
972 {
973 if (strcmp (prev_name, opcode->name) != 0)
974 {
975 if (!SH_MERGE_ARCH_SET_VALID (opcode->arch, target_arch))
976 continue;
977 prev_name = opcode->name;
978 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
979 }
980 }
981 }
982
983 static int reg_m;
984 static int reg_n;
985 static int reg_x, reg_y;
986 static int reg_efg;
987 static int reg_b;
988
989 #define IDENT_CHAR(c) (ISALNUM (c) || (c) == '_')
990
991 /* Try to parse a reg name. Return the number of chars consumed. */
992
993 static unsigned int
994 parse_reg_without_prefix (char *src, int *mode, int *reg)
995 {
996 char l0 = TOLOWER (src[0]);
997 char l1 = l0 ? TOLOWER (src[1]) : 0;
998
999 /* We use ! IDENT_CHAR for the next character after the register name, to
1000 make sure that we won't accidentally recognize a symbol name such as
1001 'sram' or sr_ram as being a reference to the register 'sr'. */
1002
1003 if (l0 == 'r')
1004 {
1005 if (l1 == '1')
1006 {
1007 if (src[2] >= '0' && src[2] <= '5'
1008 && ! IDENT_CHAR ((unsigned char) src[3]))
1009 {
1010 *mode = A_REG_N;
1011 *reg = 10 + src[2] - '0';
1012 return 3;
1013 }
1014 }
1015 if (l1 >= '0' && l1 <= '9'
1016 && ! IDENT_CHAR ((unsigned char) src[2]))
1017 {
1018 *mode = A_REG_N;
1019 *reg = (l1 - '0');
1020 return 2;
1021 }
1022 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
1023 && ! IDENT_CHAR ((unsigned char) src[7]))
1024 {
1025 *mode = A_REG_B;
1026 *reg = (l1 - '0');
1027 return 7;
1028 }
1029
1030 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
1031 {
1032 *mode = A_RE;
1033 return 2;
1034 }
1035 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
1036 {
1037 *mode = A_RS;
1038 return 2;
1039 }
1040 }
1041
1042 if (l0 == 'a')
1043 {
1044 if (l1 == '0')
1045 {
1046 if (! IDENT_CHAR ((unsigned char) src[2]))
1047 {
1048 *mode = DSP_REG_N;
1049 *reg = A_A0_NUM;
1050 return 2;
1051 }
1052 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
1053 {
1054 *mode = DSP_REG_N;
1055 *reg = A_A0G_NUM;
1056 return 3;
1057 }
1058 }
1059 if (l1 == '1')
1060 {
1061 if (! IDENT_CHAR ((unsigned char) src[2]))
1062 {
1063 *mode = DSP_REG_N;
1064 *reg = A_A1_NUM;
1065 return 2;
1066 }
1067 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
1068 {
1069 *mode = DSP_REG_N;
1070 *reg = A_A1G_NUM;
1071 return 3;
1072 }
1073 }
1074
1075 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
1076 && ! IDENT_CHAR ((unsigned char) src[3]))
1077 {
1078 *mode = A_REG_N;
1079 *reg = 4 + (l1 - '0');
1080 return 3;
1081 }
1082 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
1083 && ! IDENT_CHAR ((unsigned char) src[3]))
1084 {
1085 *mode = A_REG_N;
1086 *reg = 6 + (l1 - '0');
1087 return 3;
1088 }
1089 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
1090 && ! IDENT_CHAR ((unsigned char) src[3]))
1091 {
1092 int n = l1 - '0';
1093
1094 *mode = A_REG_N;
1095 *reg = n | ((~n & 2) << 1);
1096 return 3;
1097 }
1098 }
1099
1100 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[2]))
1101 {
1102 if (l1 == 's')
1103 {
1104 *mode = A_REG_N;
1105 *reg = 8;
1106 return 2;
1107 }
1108 if (l1 == 'x')
1109 {
1110 *mode = A_REG_N;
1111 *reg = 8;
1112 return 2;
1113 }
1114 if (l1 == 'y')
1115 {
1116 *mode = A_REG_N;
1117 *reg = 9;
1118 return 2;
1119 }
1120 }
1121
1122 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
1123 && ! IDENT_CHAR ((unsigned char) src[2]))
1124 {
1125 *mode = DSP_REG_N;
1126 *reg = A_X0_NUM + l1 - '0';
1127 return 2;
1128 }
1129
1130 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
1131 && ! IDENT_CHAR ((unsigned char) src[2]))
1132 {
1133 *mode = DSP_REG_N;
1134 *reg = A_Y0_NUM + l1 - '0';
1135 return 2;
1136 }
1137
1138 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
1139 && ! IDENT_CHAR ((unsigned char) src[2]))
1140 {
1141 *mode = DSP_REG_N;
1142 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
1143 return 2;
1144 }
1145
1146 if (l0 == 's'
1147 && l1 == 's'
1148 && TOLOWER (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
1149 {
1150 *mode = A_SSR;
1151 return 3;
1152 }
1153
1154 if (l0 == 's' && l1 == 'p' && TOLOWER (src[2]) == 'c'
1155 && ! IDENT_CHAR ((unsigned char) src[3]))
1156 {
1157 *mode = A_SPC;
1158 return 3;
1159 }
1160
1161 if (l0 == 's' && l1 == 'g' && TOLOWER (src[2]) == 'r'
1162 && ! IDENT_CHAR ((unsigned char) src[3]))
1163 {
1164 *mode = A_SGR;
1165 return 3;
1166 }
1167
1168 if (l0 == 'd' && l1 == 's' && TOLOWER (src[2]) == 'r'
1169 && ! IDENT_CHAR ((unsigned char) src[3]))
1170 {
1171 *mode = A_DSR;
1172 return 3;
1173 }
1174
1175 if (l0 == 'd' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1176 && ! IDENT_CHAR ((unsigned char) src[3]))
1177 {
1178 *mode = A_DBR;
1179 return 3;
1180 }
1181
1182 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
1183 {
1184 *mode = A_SR;
1185 return 2;
1186 }
1187
1188 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
1189 {
1190 *mode = A_REG_N;
1191 *reg = 15;
1192 return 2;
1193 }
1194
1195 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
1196 {
1197 *mode = A_PR;
1198 return 2;
1199 }
1200 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
1201 {
1202 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
1203 and use an uninitialized immediate. */
1204 *mode = A_PC;
1205 return 2;
1206 }
1207 if (l0 == 'g' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1208 && ! IDENT_CHAR ((unsigned char) src[3]))
1209 {
1210 *mode = A_GBR;
1211 return 3;
1212 }
1213 if (l0 == 'v' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1214 && ! IDENT_CHAR ((unsigned char) src[3]))
1215 {
1216 *mode = A_VBR;
1217 return 3;
1218 }
1219
1220 if (l0 == 't' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1221 && ! IDENT_CHAR ((unsigned char) src[3]))
1222 {
1223 *mode = A_TBR;
1224 return 3;
1225 }
1226 if (l0 == 'm' && l1 == 'a' && TOLOWER (src[2]) == 'c'
1227 && ! IDENT_CHAR ((unsigned char) src[4]))
1228 {
1229 if (TOLOWER (src[3]) == 'l')
1230 {
1231 *mode = A_MACL;
1232 return 4;
1233 }
1234 if (TOLOWER (src[3]) == 'h')
1235 {
1236 *mode = A_MACH;
1237 return 4;
1238 }
1239 }
1240 if (l0 == 'm' && l1 == 'o' && TOLOWER (src[2]) == 'd'
1241 && ! IDENT_CHAR ((unsigned char) src[3]))
1242 {
1243 *mode = A_MOD;
1244 return 3;
1245 }
1246 if (l0 == 'f' && l1 == 'r')
1247 {
1248 if (src[2] == '1')
1249 {
1250 if (src[3] >= '0' && src[3] <= '5'
1251 && ! IDENT_CHAR ((unsigned char) src[4]))
1252 {
1253 *mode = F_REG_N;
1254 *reg = 10 + src[3] - '0';
1255 return 4;
1256 }
1257 }
1258 if (src[2] >= '0' && src[2] <= '9'
1259 && ! IDENT_CHAR ((unsigned char) src[3]))
1260 {
1261 *mode = F_REG_N;
1262 *reg = (src[2] - '0');
1263 return 3;
1264 }
1265 }
1266 if (l0 == 'd' && l1 == 'r')
1267 {
1268 if (src[2] == '1')
1269 {
1270 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
1271 && ! IDENT_CHAR ((unsigned char) src[4]))
1272 {
1273 *mode = D_REG_N;
1274 *reg = 10 + src[3] - '0';
1275 return 4;
1276 }
1277 }
1278 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
1279 && ! IDENT_CHAR ((unsigned char) src[3]))
1280 {
1281 *mode = D_REG_N;
1282 *reg = (src[2] - '0');
1283 return 3;
1284 }
1285 }
1286 if (l0 == 'x' && l1 == 'd')
1287 {
1288 if (src[2] == '1')
1289 {
1290 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
1291 && ! IDENT_CHAR ((unsigned char) src[4]))
1292 {
1293 *mode = X_REG_N;
1294 *reg = 11 + src[3] - '0';
1295 return 4;
1296 }
1297 }
1298 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
1299 && ! IDENT_CHAR ((unsigned char) src[3]))
1300 {
1301 *mode = X_REG_N;
1302 *reg = (src[2] - '0') + 1;
1303 return 3;
1304 }
1305 }
1306 if (l0 == 'f' && l1 == 'v')
1307 {
1308 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
1309 {
1310 *mode = V_REG_N;
1311 *reg = 12;
1312 return 4;
1313 }
1314 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
1315 && ! IDENT_CHAR ((unsigned char) src[3]))
1316 {
1317 *mode = V_REG_N;
1318 *reg = (src[2] - '0');
1319 return 3;
1320 }
1321 }
1322 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 'u'
1323 && TOLOWER (src[3]) == 'l'
1324 && ! IDENT_CHAR ((unsigned char) src[4]))
1325 {
1326 *mode = FPUL_N;
1327 return 4;
1328 }
1329
1330 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 's'
1331 && TOLOWER (src[3]) == 'c'
1332 && TOLOWER (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
1333 {
1334 *mode = FPSCR_N;
1335 return 5;
1336 }
1337
1338 if (l0 == 'x' && l1 == 'm' && TOLOWER (src[2]) == 't'
1339 && TOLOWER (src[3]) == 'r'
1340 && TOLOWER (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
1341 {
1342 *mode = XMTRX_M4;
1343 return 5;
1344 }
1345
1346 return 0;
1347 }
1348
1349 /* Like parse_reg_without_prefix, but this version supports
1350 $-prefixed register names if enabled by the user. */
1351
1352 static unsigned int
1353 parse_reg (char *src, int *mode, int *reg)
1354 {
1355 unsigned int prefix;
1356 unsigned int consumed;
1357
1358 if (src[0] == '$')
1359 {
1360 if (allow_dollar_register_prefix)
1361 {
1362 src ++;
1363 prefix = 1;
1364 }
1365 else
1366 return 0;
1367 }
1368 else
1369 prefix = 0;
1370
1371 consumed = parse_reg_without_prefix (src, mode, reg);
1372
1373 if (consumed == 0)
1374 return 0;
1375
1376 return consumed + prefix;
1377 }
1378
1379 static char *
1380 parse_exp (char *s, sh_operand_info *op)
1381 {
1382 char *save;
1383 char *new_pointer;
1384
1385 save = input_line_pointer;
1386 input_line_pointer = s;
1387 expression (&op->immediate);
1388 if (op->immediate.X_op == O_absent)
1389 as_bad (_("missing operand"));
1390 new_pointer = input_line_pointer;
1391 input_line_pointer = save;
1392 return new_pointer;
1393 }
1394
1395 /* The many forms of operand:
1396
1397 Rn Register direct
1398 @Rn Register indirect
1399 @Rn+ Autoincrement
1400 @-Rn Autodecrement
1401 @(disp:4,Rn)
1402 @(disp:8,GBR)
1403 @(disp:8,PC)
1404
1405 @(R0,Rn)
1406 @(R0,GBR)
1407
1408 disp:8
1409 disp:12
1410 #imm8
1411 pr, gbr, vbr, macl, mach
1412 */
1413
1414 static char *
1415 parse_at (char *src, sh_operand_info *op)
1416 {
1417 int len;
1418 int mode;
1419 src++;
1420 if (src[0] == '@')
1421 {
1422 src = parse_at (src, op);
1423 if (op->type == A_DISP_TBR)
1424 op->type = A_DISP2_TBR;
1425 else
1426 as_bad (_("illegal double indirection"));
1427 }
1428 else if (src[0] == '-')
1429 {
1430 /* Must be predecrement. */
1431 src++;
1432
1433 len = parse_reg (src, &mode, &(op->reg));
1434 if (mode != A_REG_N)
1435 as_bad (_("illegal register after @-"));
1436
1437 op->type = A_DEC_N;
1438 src += len;
1439 }
1440 else if (src[0] == '(')
1441 {
1442 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
1443 @(r0, rn). */
1444 src++;
1445 len = parse_reg (src, &mode, &(op->reg));
1446 if (len && mode == A_REG_N)
1447 {
1448 src += len;
1449 if (op->reg != 0)
1450 {
1451 as_bad (_("must be @(r0,...)"));
1452 }
1453 if (src[0] == ',')
1454 {
1455 src++;
1456 /* Now can be rn or gbr. */
1457 len = parse_reg (src, &mode, &(op->reg));
1458 }
1459 else
1460 {
1461 len = 0;
1462 }
1463 if (len)
1464 {
1465 if (mode == A_GBR)
1466 {
1467 op->type = A_R0_GBR;
1468 }
1469 else if (mode == A_REG_N)
1470 {
1471 op->type = A_IND_R0_REG_N;
1472 }
1473 else
1474 {
1475 as_bad (_("syntax error in @(r0,...)"));
1476 }
1477 }
1478 else
1479 {
1480 as_bad (_("syntax error in @(r0...)"));
1481 }
1482 }
1483 else
1484 {
1485 /* Must be an @(disp,.. thing). */
1486 src = parse_exp (src, op);
1487 if (src[0] == ',')
1488 src++;
1489 /* Now can be rn, gbr or pc. */
1490 len = parse_reg (src, &mode, &op->reg);
1491 if (len)
1492 {
1493 if (mode == A_REG_N)
1494 {
1495 op->type = A_DISP_REG_N;
1496 }
1497 else if (mode == A_GBR)
1498 {
1499 op->type = A_DISP_GBR;
1500 }
1501 else if (mode == A_TBR)
1502 {
1503 op->type = A_DISP_TBR;
1504 }
1505 else if (mode == A_PC)
1506 {
1507 /* We want @(expr, pc) to uniformly address . + expr,
1508 no matter if expr is a constant, or a more complex
1509 expression, e.g. sym-. or sym1-sym2.
1510 However, we also used to accept @(sym,pc)
1511 as addressing sym, i.e. meaning the same as plain sym.
1512 Some existing code does use the @(sym,pc) syntax, so
1513 we give it the old semantics for now, but warn about
1514 its use, so that users have some time to fix their code.
1515
1516 Note that due to this backward compatibility hack,
1517 we'll get unexpected results when @(offset, pc) is used,
1518 and offset is a symbol that is set later to an an address
1519 difference, or an external symbol that is set to an
1520 address difference in another source file, so we want to
1521 eventually remove it. */
1522 if (op->immediate.X_op == O_symbol)
1523 {
1524 op->type = A_DISP_PC;
1525 as_warn (_("Deprecated syntax."));
1526 }
1527 else
1528 {
1529 op->type = A_DISP_PC_ABS;
1530 /* Such operands don't get corrected for PC==.+4, so
1531 make the correction here. */
1532 op->immediate.X_add_number -= 4;
1533 }
1534 }
1535 else
1536 {
1537 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1538 }
1539 }
1540 else
1541 {
1542 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1543 }
1544 }
1545 src += len;
1546 if (src[0] != ')')
1547 as_bad (_("expecting )"));
1548 else
1549 src++;
1550 }
1551 else
1552 {
1553 src += parse_reg (src, &mode, &(op->reg));
1554 if (mode != A_REG_N)
1555 as_bad (_("illegal register after @"));
1556
1557 if (src[0] == '+')
1558 {
1559 char l0, l1;
1560
1561 src++;
1562 l0 = TOLOWER (src[0]);
1563 l1 = TOLOWER (src[1]);
1564
1565 if ((l0 == 'r' && l1 == '8')
1566 || (l0 == 'i' && (l1 == 'x' || l1 == 's')))
1567 {
1568 src += 2;
1569 op->type = AX_PMOD_N;
1570 }
1571 else if ( (l0 == 'r' && l1 == '9')
1572 || (l0 == 'i' && l1 == 'y'))
1573 {
1574 src += 2;
1575 op->type = AY_PMOD_N;
1576 }
1577 else
1578 op->type = A_INC_N;
1579 }
1580 else
1581 op->type = A_IND_N;
1582 }
1583 return src;
1584 }
1585
1586 static void
1587 get_operand (char **ptr, sh_operand_info *op)
1588 {
1589 char *src = *ptr;
1590 int mode = -1;
1591 unsigned int len;
1592
1593 if (src[0] == '#')
1594 {
1595 src++;
1596 *ptr = parse_exp (src, op);
1597 op->type = A_IMM;
1598 return;
1599 }
1600
1601 else if (src[0] == '@')
1602 {
1603 *ptr = parse_at (src, op);
1604 return;
1605 }
1606 len = parse_reg (src, &mode, &(op->reg));
1607 if (len)
1608 {
1609 *ptr = src + len;
1610 op->type = mode;
1611 return;
1612 }
1613 else
1614 {
1615 /* Not a reg, the only thing left is a displacement. */
1616 *ptr = parse_exp (src, op);
1617 op->type = A_DISP_PC;
1618 return;
1619 }
1620 }
1621
1622 static char *
1623 get_operands (sh_opcode_info *info, char *args, sh_operand_info *operand)
1624 {
1625 char *ptr = args;
1626 if (info->arg[0])
1627 {
1628 /* The pre-processor will eliminate whitespace in front of '@'
1629 after the first argument; we may be called multiple times
1630 from assemble_ppi, so don't insist on finding whitespace here. */
1631 if (*ptr == ' ')
1632 ptr++;
1633
1634 get_operand (&ptr, operand + 0);
1635 if (info->arg[1])
1636 {
1637 if (*ptr == ',')
1638 {
1639 ptr++;
1640 }
1641 get_operand (&ptr, operand + 1);
1642 /* ??? Hack: psha/pshl have a varying operand number depending on
1643 the type of the first operand. We handle this by having the
1644 three-operand version first and reducing the number of operands
1645 parsed to two if we see that the first operand is an immediate.
1646 This works because no insn with three operands has an immediate
1647 as first operand. */
1648 if (info->arg[2] && operand[0].type != A_IMM)
1649 {
1650 if (*ptr == ',')
1651 {
1652 ptr++;
1653 }
1654 get_operand (&ptr, operand + 2);
1655 }
1656 else
1657 {
1658 operand[2].type = 0;
1659 }
1660 }
1661 else
1662 {
1663 operand[1].type = 0;
1664 operand[2].type = 0;
1665 }
1666 }
1667 else
1668 {
1669 operand[0].type = 0;
1670 operand[1].type = 0;
1671 operand[2].type = 0;
1672 }
1673 return ptr;
1674 }
1675
1676 /* Passed a pointer to a list of opcodes which use different
1677 addressing modes, return the opcode which matches the opcodes
1678 provided. */
1679
1680 static sh_opcode_info *
1681 get_specific (sh_opcode_info *opcode, sh_operand_info *operands)
1682 {
1683 sh_opcode_info *this_try = opcode;
1684 char *name = opcode->name;
1685 int n = 0;
1686
1687 while (opcode->name)
1688 {
1689 this_try = opcode++;
1690 if ((this_try->name != name) && (strcmp (this_try->name, name) != 0))
1691 {
1692 /* We've looked so far down the table that we've run out of
1693 opcodes with the same name. */
1694 return 0;
1695 }
1696
1697 /* Look at both operands needed by the opcodes and provided by
1698 the user - since an arg test will often fail on the same arg
1699 again and again, we'll try and test the last failing arg the
1700 first on each opcode try. */
1701 for (n = 0; this_try->arg[n]; n++)
1702 {
1703 sh_operand_info *user = operands + n;
1704 sh_arg_type arg = this_try->arg[n];
1705
1706 switch (arg)
1707 {
1708 case A_DISP_PC:
1709 if (user->type == A_DISP_PC_ABS)
1710 break;
1711 /* Fall through. */
1712 case A_IMM:
1713 case A_BDISP12:
1714 case A_BDISP8:
1715 case A_DISP_GBR:
1716 case A_DISP2_TBR:
1717 case A_MACH:
1718 case A_PR:
1719 case A_MACL:
1720 if (user->type != arg)
1721 goto fail;
1722 break;
1723 case A_R0:
1724 /* opcode needs r0 */
1725 if (user->type != A_REG_N || user->reg != 0)
1726 goto fail;
1727 break;
1728 case A_R0_GBR:
1729 if (user->type != A_R0_GBR || user->reg != 0)
1730 goto fail;
1731 break;
1732 case F_FR0:
1733 if (user->type != F_REG_N || user->reg != 0)
1734 goto fail;
1735 break;
1736
1737 case A_REG_N:
1738 case A_INC_N:
1739 case A_DEC_N:
1740 case A_IND_N:
1741 case A_IND_R0_REG_N:
1742 case A_DISP_REG_N:
1743 case F_REG_N:
1744 case D_REG_N:
1745 case X_REG_N:
1746 case V_REG_N:
1747 case FPUL_N:
1748 case FPSCR_N:
1749 case DSP_REG_N:
1750 /* Opcode needs rn */
1751 if (user->type != arg)
1752 goto fail;
1753 reg_n = user->reg;
1754 break;
1755 case DX_REG_N:
1756 if (user->type != D_REG_N && user->type != X_REG_N)
1757 goto fail;
1758 reg_n = user->reg;
1759 break;
1760 case A_GBR:
1761 case A_TBR:
1762 case A_SR:
1763 case A_VBR:
1764 case A_DSR:
1765 case A_MOD:
1766 case A_RE:
1767 case A_RS:
1768 case A_SSR:
1769 case A_SPC:
1770 case A_SGR:
1771 case A_DBR:
1772 if (user->type != arg)
1773 goto fail;
1774 break;
1775
1776 case A_REG_B:
1777 if (user->type != arg)
1778 goto fail;
1779 reg_b = user->reg;
1780 break;
1781
1782 case A_INC_R15:
1783 if (user->type != A_INC_N)
1784 goto fail;
1785 if (user->reg != 15)
1786 goto fail;
1787 reg_n = user->reg;
1788 break;
1789
1790 case A_DEC_R15:
1791 if (user->type != A_DEC_N)
1792 goto fail;
1793 if (user->reg != 15)
1794 goto fail;
1795 reg_n = user->reg;
1796 break;
1797
1798 case A_REG_M:
1799 case A_INC_M:
1800 case A_DEC_M:
1801 case A_IND_M:
1802 case A_IND_R0_REG_M:
1803 case A_DISP_REG_M:
1804 case DSP_REG_M:
1805 /* Opcode needs rn */
1806 if (user->type != arg - A_REG_M + A_REG_N)
1807 goto fail;
1808 reg_m = user->reg;
1809 break;
1810
1811 case AS_DEC_N:
1812 if (user->type != A_DEC_N)
1813 goto fail;
1814 if (user->reg < 2 || user->reg > 5)
1815 goto fail;
1816 reg_n = user->reg;
1817 break;
1818
1819 case AS_INC_N:
1820 if (user->type != A_INC_N)
1821 goto fail;
1822 if (user->reg < 2 || user->reg > 5)
1823 goto fail;
1824 reg_n = user->reg;
1825 break;
1826
1827 case AS_IND_N:
1828 if (user->type != A_IND_N)
1829 goto fail;
1830 if (user->reg < 2 || user->reg > 5)
1831 goto fail;
1832 reg_n = user->reg;
1833 break;
1834
1835 case AS_PMOD_N:
1836 if (user->type != AX_PMOD_N)
1837 goto fail;
1838 if (user->reg < 2 || user->reg > 5)
1839 goto fail;
1840 reg_n = user->reg;
1841 break;
1842
1843 case AX_INC_N:
1844 if (user->type != A_INC_N)
1845 goto fail;
1846 if (user->reg < 4 || user->reg > 5)
1847 goto fail;
1848 reg_n = user->reg;
1849 break;
1850
1851 case AX_IND_N:
1852 if (user->type != A_IND_N)
1853 goto fail;
1854 if (user->reg < 4 || user->reg > 5)
1855 goto fail;
1856 reg_n = user->reg;
1857 break;
1858
1859 case AX_PMOD_N:
1860 if (user->type != AX_PMOD_N)
1861 goto fail;
1862 if (user->reg < 4 || user->reg > 5)
1863 goto fail;
1864 reg_n = user->reg;
1865 break;
1866
1867 case AXY_INC_N:
1868 if (user->type != A_INC_N)
1869 goto fail;
1870 if ((user->reg < 4 || user->reg > 5)
1871 && (user->reg < 0 || user->reg > 1))
1872 goto fail;
1873 reg_n = user->reg;
1874 break;
1875
1876 case AXY_IND_N:
1877 if (user->type != A_IND_N)
1878 goto fail;
1879 if ((user->reg < 4 || user->reg > 5)
1880 && (user->reg < 0 || user->reg > 1))
1881 goto fail;
1882 reg_n = user->reg;
1883 break;
1884
1885 case AXY_PMOD_N:
1886 if (user->type != AX_PMOD_N)
1887 goto fail;
1888 if ((user->reg < 4 || user->reg > 5)
1889 && (user->reg < 0 || user->reg > 1))
1890 goto fail;
1891 reg_n = user->reg;
1892 break;
1893
1894 case AY_INC_N:
1895 if (user->type != A_INC_N)
1896 goto fail;
1897 if (user->reg < 6 || user->reg > 7)
1898 goto fail;
1899 reg_n = user->reg;
1900 break;
1901
1902 case AY_IND_N:
1903 if (user->type != A_IND_N)
1904 goto fail;
1905 if (user->reg < 6 || user->reg > 7)
1906 goto fail;
1907 reg_n = user->reg;
1908 break;
1909
1910 case AY_PMOD_N:
1911 if (user->type != AY_PMOD_N)
1912 goto fail;
1913 if (user->reg < 6 || user->reg > 7)
1914 goto fail;
1915 reg_n = user->reg;
1916 break;
1917
1918 case AYX_INC_N:
1919 if (user->type != A_INC_N)
1920 goto fail;
1921 if ((user->reg < 6 || user->reg > 7)
1922 && (user->reg < 2 || user->reg > 3))
1923 goto fail;
1924 reg_n = user->reg;
1925 break;
1926
1927 case AYX_IND_N:
1928 if (user->type != A_IND_N)
1929 goto fail;
1930 if ((user->reg < 6 || user->reg > 7)
1931 && (user->reg < 2 || user->reg > 3))
1932 goto fail;
1933 reg_n = user->reg;
1934 break;
1935
1936 case AYX_PMOD_N:
1937 if (user->type != AY_PMOD_N)
1938 goto fail;
1939 if ((user->reg < 6 || user->reg > 7)
1940 && (user->reg < 2 || user->reg > 3))
1941 goto fail;
1942 reg_n = user->reg;
1943 break;
1944
1945 case DSP_REG_A_M:
1946 if (user->type != DSP_REG_N)
1947 goto fail;
1948 if (user->reg != A_A0_NUM
1949 && user->reg != A_A1_NUM)
1950 goto fail;
1951 reg_m = user->reg;
1952 break;
1953
1954 case DSP_REG_AX:
1955 if (user->type != DSP_REG_N)
1956 goto fail;
1957 switch (user->reg)
1958 {
1959 case A_A0_NUM:
1960 reg_x = 0;
1961 break;
1962 case A_A1_NUM:
1963 reg_x = 2;
1964 break;
1965 case A_X0_NUM:
1966 reg_x = 1;
1967 break;
1968 case A_X1_NUM:
1969 reg_x = 3;
1970 break;
1971 default:
1972 goto fail;
1973 }
1974 break;
1975
1976 case DSP_REG_XY:
1977 if (user->type != DSP_REG_N)
1978 goto fail;
1979 switch (user->reg)
1980 {
1981 case A_X0_NUM:
1982 reg_x = 0;
1983 break;
1984 case A_X1_NUM:
1985 reg_x = 2;
1986 break;
1987 case A_Y0_NUM:
1988 reg_x = 1;
1989 break;
1990 case A_Y1_NUM:
1991 reg_x = 3;
1992 break;
1993 default:
1994 goto fail;
1995 }
1996 break;
1997
1998 case DSP_REG_AY:
1999 if (user->type != DSP_REG_N)
2000 goto fail;
2001 switch (user->reg)
2002 {
2003 case A_A0_NUM:
2004 reg_y = 0;
2005 break;
2006 case A_A1_NUM:
2007 reg_y = 1;
2008 break;
2009 case A_Y0_NUM:
2010 reg_y = 2;
2011 break;
2012 case A_Y1_NUM:
2013 reg_y = 3;
2014 break;
2015 default:
2016 goto fail;
2017 }
2018 break;
2019
2020 case DSP_REG_YX:
2021 if (user->type != DSP_REG_N)
2022 goto fail;
2023 switch (user->reg)
2024 {
2025 case A_Y0_NUM:
2026 reg_y = 0;
2027 break;
2028 case A_Y1_NUM:
2029 reg_y = 1;
2030 break;
2031 case A_X0_NUM:
2032 reg_y = 2;
2033 break;
2034 case A_X1_NUM:
2035 reg_y = 3;
2036 break;
2037 default:
2038 goto fail;
2039 }
2040 break;
2041
2042 case DSP_REG_X:
2043 if (user->type != DSP_REG_N)
2044 goto fail;
2045 switch (user->reg)
2046 {
2047 case A_X0_NUM:
2048 reg_x = 0;
2049 break;
2050 case A_X1_NUM:
2051 reg_x = 1;
2052 break;
2053 case A_A0_NUM:
2054 reg_x = 2;
2055 break;
2056 case A_A1_NUM:
2057 reg_x = 3;
2058 break;
2059 default:
2060 goto fail;
2061 }
2062 break;
2063
2064 case DSP_REG_Y:
2065 if (user->type != DSP_REG_N)
2066 goto fail;
2067 switch (user->reg)
2068 {
2069 case A_Y0_NUM:
2070 reg_y = 0;
2071 break;
2072 case A_Y1_NUM:
2073 reg_y = 1;
2074 break;
2075 case A_M0_NUM:
2076 reg_y = 2;
2077 break;
2078 case A_M1_NUM:
2079 reg_y = 3;
2080 break;
2081 default:
2082 goto fail;
2083 }
2084 break;
2085
2086 case DSP_REG_E:
2087 if (user->type != DSP_REG_N)
2088 goto fail;
2089 switch (user->reg)
2090 {
2091 case A_X0_NUM:
2092 reg_efg = 0 << 10;
2093 break;
2094 case A_X1_NUM:
2095 reg_efg = 1 << 10;
2096 break;
2097 case A_Y0_NUM:
2098 reg_efg = 2 << 10;
2099 break;
2100 case A_A1_NUM:
2101 reg_efg = 3 << 10;
2102 break;
2103 default:
2104 goto fail;
2105 }
2106 break;
2107
2108 case DSP_REG_F:
2109 if (user->type != DSP_REG_N)
2110 goto fail;
2111 switch (user->reg)
2112 {
2113 case A_Y0_NUM:
2114 reg_efg |= 0 << 8;
2115 break;
2116 case A_Y1_NUM:
2117 reg_efg |= 1 << 8;
2118 break;
2119 case A_X0_NUM:
2120 reg_efg |= 2 << 8;
2121 break;
2122 case A_A1_NUM:
2123 reg_efg |= 3 << 8;
2124 break;
2125 default:
2126 goto fail;
2127 }
2128 break;
2129
2130 case DSP_REG_G:
2131 if (user->type != DSP_REG_N)
2132 goto fail;
2133 switch (user->reg)
2134 {
2135 case A_M0_NUM:
2136 reg_efg |= 0 << 2;
2137 break;
2138 case A_M1_NUM:
2139 reg_efg |= 1 << 2;
2140 break;
2141 case A_A0_NUM:
2142 reg_efg |= 2 << 2;
2143 break;
2144 case A_A1_NUM:
2145 reg_efg |= 3 << 2;
2146 break;
2147 default:
2148 goto fail;
2149 }
2150 break;
2151
2152 case A_A0:
2153 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
2154 goto fail;
2155 break;
2156 case A_X0:
2157 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
2158 goto fail;
2159 break;
2160 case A_X1:
2161 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
2162 goto fail;
2163 break;
2164 case A_Y0:
2165 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
2166 goto fail;
2167 break;
2168 case A_Y1:
2169 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
2170 goto fail;
2171 break;
2172
2173 case F_REG_M:
2174 case D_REG_M:
2175 case X_REG_M:
2176 case V_REG_M:
2177 case FPUL_M:
2178 case FPSCR_M:
2179 /* Opcode needs rn */
2180 if (user->type != arg - F_REG_M + F_REG_N)
2181 goto fail;
2182 reg_m = user->reg;
2183 break;
2184 case DX_REG_M:
2185 if (user->type != D_REG_N && user->type != X_REG_N)
2186 goto fail;
2187 reg_m = user->reg;
2188 break;
2189 case XMTRX_M4:
2190 if (user->type != XMTRX_M4)
2191 goto fail;
2192 reg_m = 4;
2193 break;
2194
2195 default:
2196 printf (_("unhandled %d\n"), arg);
2197 goto fail;
2198 }
2199 if (SH_MERGE_ARCH_SET_VALID (valid_arch, arch_sh2a_nofpu_up)
2200 && ( arg == A_DISP_REG_M
2201 || arg == A_DISP_REG_N))
2202 {
2203 /* Check a few key IMM* fields for overflow. */
2204 int opf;
2205 long val = user->immediate.X_add_number;
2206
2207 for (opf = 0; opf < 4; opf ++)
2208 switch (this_try->nibbles[opf])
2209 {
2210 case IMM0_4:
2211 case IMM1_4:
2212 if (val < 0 || val > 15)
2213 goto fail;
2214 break;
2215 case IMM0_4BY2:
2216 case IMM1_4BY2:
2217 if (val < 0 || val > 15 * 2)
2218 goto fail;
2219 break;
2220 case IMM0_4BY4:
2221 case IMM1_4BY4:
2222 if (val < 0 || val > 15 * 4)
2223 goto fail;
2224 break;
2225 default:
2226 break;
2227 }
2228 }
2229 }
2230 if ( !SH_MERGE_ARCH_SET_VALID (valid_arch, this_try->arch))
2231 goto fail;
2232 valid_arch = SH_MERGE_ARCH_SET (valid_arch, this_try->arch);
2233 return this_try;
2234 fail:
2235 ;
2236 }
2237
2238 return 0;
2239 }
2240
2241 static void
2242 insert (char *where, int how, int pcrel, sh_operand_info *op)
2243 {
2244 fix_new_exp (frag_now,
2245 where - frag_now->fr_literal,
2246 2,
2247 &op->immediate,
2248 pcrel,
2249 how);
2250 }
2251
2252 static void
2253 insert4 (char * where, int how, int pcrel, sh_operand_info * op)
2254 {
2255 fix_new_exp (frag_now,
2256 where - frag_now->fr_literal,
2257 4,
2258 & op->immediate,
2259 pcrel,
2260 how);
2261 }
2262 static void
2263 build_relax (sh_opcode_info *opcode, sh_operand_info *op)
2264 {
2265 int high_byte = target_big_endian ? 0 : 1;
2266 char *p;
2267
2268 if (opcode->arg[0] == A_BDISP8)
2269 {
2270 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
2271 p = frag_var (rs_machine_dependent,
2272 md_relax_table[C (what, COND32)].rlx_length,
2273 md_relax_table[C (what, COND8)].rlx_length,
2274 C (what, 0),
2275 op->immediate.X_add_symbol,
2276 op->immediate.X_add_number,
2277 0);
2278 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
2279 }
2280 else if (opcode->arg[0] == A_BDISP12)
2281 {
2282 p = frag_var (rs_machine_dependent,
2283 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
2284 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
2285 C (UNCOND_JUMP, 0),
2286 op->immediate.X_add_symbol,
2287 op->immediate.X_add_number,
2288 0);
2289 p[high_byte] = (opcode->nibbles[0] << 4);
2290 }
2291
2292 }
2293
2294 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
2295
2296 static char *
2297 insert_loop_bounds (char *output, sh_operand_info *operand)
2298 {
2299 char *name;
2300 symbolS *end_sym;
2301
2302 /* Since the low byte of the opcode will be overwritten by the reloc, we
2303 can just stash the high byte into both bytes and ignore endianness. */
2304 output[0] = 0x8c;
2305 output[1] = 0x8c;
2306 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
2307 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
2308
2309 if (sh_relax)
2310 {
2311 static int count = 0;
2312
2313 /* If the last loop insn is a two-byte-insn, it is in danger of being
2314 swapped with the insn after it. To prevent this, create a new
2315 symbol - complete with SH_LABEL reloc - after the last loop insn.
2316 If the last loop insn is four bytes long, the symbol will be
2317 right in the middle, but four byte insns are not swapped anyways. */
2318 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
2319 Hence a 9 digit number should be enough to count all REPEATs. */
2320 name = alloca (11);
2321 sprintf (name, "_R%x", count++ & 0x3fffffff);
2322 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
2323 /* Make this a local symbol. */
2324 #ifdef OBJ_COFF
2325 SF_SET_LOCAL (end_sym);
2326 #endif /* OBJ_COFF */
2327 symbol_table_insert (end_sym);
2328 end_sym->sy_value = operand[1].immediate;
2329 end_sym->sy_value.X_add_number += 2;
2330 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
2331 }
2332
2333 output = frag_more (2);
2334 output[0] = 0x8e;
2335 output[1] = 0x8e;
2336 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
2337 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
2338
2339 return frag_more (2);
2340 }
2341
2342 /* Now we know what sort of opcodes it is, let's build the bytes. */
2343
2344 static unsigned int
2345 build_Mytes (sh_opcode_info *opcode, sh_operand_info *operand)
2346 {
2347 int indx;
2348 char nbuf[8];
2349 char *output;
2350 unsigned int size = 2;
2351 int low_byte = target_big_endian ? 1 : 0;
2352 int max_index = 4;
2353 bfd_reloc_code_real_type r_type;
2354 #ifdef OBJ_ELF
2355 int unhandled_pic = 0;
2356 #endif
2357
2358 nbuf[0] = 0;
2359 nbuf[1] = 0;
2360 nbuf[2] = 0;
2361 nbuf[3] = 0;
2362 nbuf[4] = 0;
2363 nbuf[5] = 0;
2364 nbuf[6] = 0;
2365 nbuf[7] = 0;
2366
2367 #ifdef OBJ_ELF
2368 for (indx = 0; indx < 3; indx++)
2369 if (opcode->arg[indx] == A_IMM
2370 && operand[indx].type == A_IMM
2371 && (operand[indx].immediate.X_op == O_PIC_reloc
2372 || sh_PIC_related_p (operand[indx].immediate.X_add_symbol)
2373 || sh_PIC_related_p (operand[indx].immediate.X_op_symbol)))
2374 unhandled_pic = 1;
2375 #endif
2376
2377 if (SH_MERGE_ARCH_SET (opcode->arch, arch_op32))
2378 {
2379 output = frag_more (4);
2380 size = 4;
2381 max_index = 8;
2382 }
2383 else
2384 output = frag_more (2);
2385
2386 for (indx = 0; indx < max_index; indx++)
2387 {
2388 sh_nibble_type i = opcode->nibbles[indx];
2389 if (i < 16)
2390 {
2391 nbuf[indx] = i;
2392 }
2393 else
2394 {
2395 switch (i)
2396 {
2397 case REG_N:
2398 case REG_N_D:
2399 nbuf[indx] = reg_n;
2400 break;
2401 case REG_M:
2402 nbuf[indx] = reg_m;
2403 break;
2404 case SDT_REG_N:
2405 if (reg_n < 2 || reg_n > 5)
2406 as_bad (_("Invalid register: 'r%d'"), reg_n);
2407 nbuf[indx] = (reg_n & 3) | 4;
2408 break;
2409 case REG_NM:
2410 nbuf[indx] = reg_n | (reg_m >> 2);
2411 break;
2412 case REG_B:
2413 nbuf[indx] = reg_b | 0x08;
2414 break;
2415 case REG_N_B01:
2416 nbuf[indx] = reg_n | 0x01;
2417 break;
2418 case IMM0_3s:
2419 nbuf[indx] |= 0x08;
2420 case IMM0_3c:
2421 insert (output + low_byte, BFD_RELOC_SH_IMM3, 0, operand);
2422 break;
2423 case IMM0_3Us:
2424 nbuf[indx] |= 0x80;
2425 case IMM0_3Uc:
2426 insert (output + low_byte, BFD_RELOC_SH_IMM3U, 0, operand);
2427 break;
2428 case DISP0_12:
2429 insert (output + 2, BFD_RELOC_SH_DISP12, 0, operand);
2430 break;
2431 case DISP0_12BY2:
2432 insert (output + 2, BFD_RELOC_SH_DISP12BY2, 0, operand);
2433 break;
2434 case DISP0_12BY4:
2435 insert (output + 2, BFD_RELOC_SH_DISP12BY4, 0, operand);
2436 break;
2437 case DISP0_12BY8:
2438 insert (output + 2, BFD_RELOC_SH_DISP12BY8, 0, operand);
2439 break;
2440 case DISP1_12:
2441 insert (output + 2, BFD_RELOC_SH_DISP12, 0, operand+1);
2442 break;
2443 case DISP1_12BY2:
2444 insert (output + 2, BFD_RELOC_SH_DISP12BY2, 0, operand+1);
2445 break;
2446 case DISP1_12BY4:
2447 insert (output + 2, BFD_RELOC_SH_DISP12BY4, 0, operand+1);
2448 break;
2449 case DISP1_12BY8:
2450 insert (output + 2, BFD_RELOC_SH_DISP12BY8, 0, operand+1);
2451 break;
2452 case IMM0_20_4:
2453 break;
2454 case IMM0_20:
2455 r_type = BFD_RELOC_SH_DISP20;
2456 #ifdef OBJ_ELF
2457 if (sh_check_fixup (&operand->immediate, &r_type))
2458 as_bad (_("Invalid PIC expression."));
2459 unhandled_pic = 0;
2460 #endif
2461 insert4 (output, r_type, 0, operand);
2462 break;
2463 case IMM0_20BY8:
2464 insert4 (output, BFD_RELOC_SH_DISP20BY8, 0, operand);
2465 break;
2466 case IMM0_4BY4:
2467 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
2468 break;
2469 case IMM0_4BY2:
2470 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
2471 break;
2472 case IMM0_4:
2473 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
2474 break;
2475 case IMM1_4BY4:
2476 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
2477 break;
2478 case IMM1_4BY2:
2479 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
2480 break;
2481 case IMM1_4:
2482 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
2483 break;
2484 case IMM0_8BY4:
2485 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
2486 break;
2487 case IMM0_8BY2:
2488 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
2489 break;
2490 case IMM0_8:
2491 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
2492 break;
2493 case IMM1_8BY4:
2494 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
2495 break;
2496 case IMM1_8BY2:
2497 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
2498 break;
2499 case IMM1_8:
2500 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
2501 break;
2502 case PCRELIMM_8BY4:
2503 insert (output, BFD_RELOC_SH_PCRELIMM8BY4,
2504 operand->type != A_DISP_PC_ABS, operand);
2505 break;
2506 case PCRELIMM_8BY2:
2507 insert (output, BFD_RELOC_SH_PCRELIMM8BY2,
2508 operand->type != A_DISP_PC_ABS, operand);
2509 break;
2510 case REPEAT:
2511 output = insert_loop_bounds (output, operand);
2512 nbuf[indx] = opcode->nibbles[3];
2513 operand += 2;
2514 break;
2515 default:
2516 printf (_("failed for %d\n"), i);
2517 }
2518 }
2519 }
2520 #ifdef OBJ_ELF
2521 if (unhandled_pic)
2522 as_bad (_("misplaced PIC operand"));
2523 #endif
2524 if (!target_big_endian)
2525 {
2526 output[1] = (nbuf[0] << 4) | (nbuf[1]);
2527 output[0] = (nbuf[2] << 4) | (nbuf[3]);
2528 }
2529 else
2530 {
2531 output[0] = (nbuf[0] << 4) | (nbuf[1]);
2532 output[1] = (nbuf[2] << 4) | (nbuf[3]);
2533 }
2534 if (SH_MERGE_ARCH_SET (opcode->arch, arch_op32))
2535 {
2536 if (!target_big_endian)
2537 {
2538 output[3] = (nbuf[4] << 4) | (nbuf[5]);
2539 output[2] = (nbuf[6] << 4) | (nbuf[7]);
2540 }
2541 else
2542 {
2543 output[2] = (nbuf[4] << 4) | (nbuf[5]);
2544 output[3] = (nbuf[6] << 4) | (nbuf[7]);
2545 }
2546 }
2547 return size;
2548 }
2549
2550 /* Find an opcode at the start of *STR_P in the hash table, and set
2551 *STR_P to the first character after the last one read. */
2552
2553 static sh_opcode_info *
2554 find_cooked_opcode (char **str_p)
2555 {
2556 char *str = *str_p;
2557 unsigned char *op_start;
2558 unsigned char *op_end;
2559 char name[20];
2560 unsigned int nlen = 0;
2561
2562 /* Drop leading whitespace. */
2563 while (*str == ' ')
2564 str++;
2565
2566 /* Find the op code end.
2567 The pre-processor will eliminate whitespace in front of
2568 any '@' after the first argument; we may be called from
2569 assemble_ppi, so the opcode might be terminated by an '@'. */
2570 for (op_start = op_end = (unsigned char *) str;
2571 *op_end
2572 && nlen < sizeof (name) - 1
2573 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
2574 op_end++)
2575 {
2576 unsigned char c = op_start[nlen];
2577
2578 /* The machine independent code will convert CMP/EQ into cmp/EQ
2579 because it thinks the '/' is the end of the symbol. Moreover,
2580 all but the first sub-insn is a parallel processing insn won't
2581 be capitalized. Instead of hacking up the machine independent
2582 code, we just deal with it here. */
2583 c = TOLOWER (c);
2584 name[nlen] = c;
2585 nlen++;
2586 }
2587
2588 name[nlen] = 0;
2589 *str_p = (char *) op_end;
2590
2591 if (nlen == 0)
2592 as_bad (_("can't find opcode "));
2593
2594 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
2595 }
2596
2597 /* Assemble a parallel processing insn. */
2598 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
2599
2600 static unsigned int
2601 assemble_ppi (char *op_end, sh_opcode_info *opcode)
2602 {
2603 int movx = 0;
2604 int movy = 0;
2605 int cond = 0;
2606 int field_b = 0;
2607 char *output;
2608 int move_code;
2609 unsigned int size;
2610
2611 for (;;)
2612 {
2613 sh_operand_info operand[3];
2614
2615 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
2616 Make sure we encode a defined insn pattern. */
2617 reg_x = 0;
2618 reg_y = 0;
2619 reg_n = 0;
2620
2621 if (opcode->arg[0] != A_END)
2622 op_end = get_operands (opcode, op_end, operand);
2623 try_another_opcode:
2624 opcode = get_specific (opcode, operand);
2625 if (opcode == 0)
2626 {
2627 /* Couldn't find an opcode which matched the operands. */
2628 char *where = frag_more (2);
2629 size = 2;
2630
2631 where[0] = 0x0;
2632 where[1] = 0x0;
2633 as_bad (_("invalid operands for opcode"));
2634 return size;
2635 }
2636
2637 if (opcode->nibbles[0] != PPI)
2638 as_bad (_("insn can't be combined with parallel processing insn"));
2639
2640 switch (opcode->nibbles[1])
2641 {
2642
2643 case NOPX:
2644 if (movx)
2645 as_bad (_("multiple movx specifications"));
2646 movx = DDT_BASE;
2647 break;
2648 case NOPY:
2649 if (movy)
2650 as_bad (_("multiple movy specifications"));
2651 movy = DDT_BASE;
2652 break;
2653
2654 case MOVX_NOPY:
2655 if (movx)
2656 as_bad (_("multiple movx specifications"));
2657 if ((reg_n < 4 || reg_n > 5)
2658 && (reg_n < 0 || reg_n > 1))
2659 as_bad (_("invalid movx address register"));
2660 if (movy && movy != DDT_BASE)
2661 as_bad (_("insn cannot be combined with non-nopy"));
2662 movx = ((((reg_n & 1) != 0) << 9)
2663 + (((reg_n & 4) == 0) << 8)
2664 + (reg_x << 6)
2665 + (opcode->nibbles[2] << 4)
2666 + opcode->nibbles[3]
2667 + DDT_BASE);
2668 break;
2669
2670 case MOVY_NOPX:
2671 if (movy)
2672 as_bad (_("multiple movy specifications"));
2673 if ((reg_n < 6 || reg_n > 7)
2674 && (reg_n < 2 || reg_n > 3))
2675 as_bad (_("invalid movy address register"));
2676 if (movx && movx != DDT_BASE)
2677 as_bad (_("insn cannot be combined with non-nopx"));
2678 movy = ((((reg_n & 1) != 0) << 8)
2679 + (((reg_n & 4) == 0) << 9)
2680 + (reg_y << 6)
2681 + (opcode->nibbles[2] << 4)
2682 + opcode->nibbles[3]
2683 + DDT_BASE);
2684 break;
2685
2686 case MOVX:
2687 if (movx)
2688 as_bad (_("multiple movx specifications"));
2689 if (movy & 0x2ac)
2690 as_bad (_("previous movy requires nopx"));
2691 if (reg_n < 4 || reg_n > 5)
2692 as_bad (_("invalid movx address register"));
2693 if (opcode->nibbles[2] & 8)
2694 {
2695 if (reg_m == A_A1_NUM)
2696 movx = 1 << 7;
2697 else if (reg_m != A_A0_NUM)
2698 as_bad (_("invalid movx dsp register"));
2699 }
2700 else
2701 {
2702 if (reg_x > 1)
2703 as_bad (_("invalid movx dsp register"));
2704 movx = reg_x << 7;
2705 }
2706 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
2707 break;
2708
2709 case MOVY:
2710 if (movy)
2711 as_bad (_("multiple movy specifications"));
2712 if (movx & 0x153)
2713 as_bad (_("previous movx requires nopy"));
2714 if (opcode->nibbles[2] & 8)
2715 {
2716 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
2717 so add 8 more. */
2718 movy = 8;
2719 if (reg_m == A_A1_NUM)
2720 movy += 1 << 6;
2721 else if (reg_m != A_A0_NUM)
2722 as_bad (_("invalid movy dsp register"));
2723 }
2724 else
2725 {
2726 if (reg_y > 1)
2727 as_bad (_("invalid movy dsp register"));
2728 movy = reg_y << 6;
2729 }
2730 if (reg_n < 6 || reg_n > 7)
2731 as_bad (_("invalid movy address register"));
2732 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
2733 break;
2734
2735 case PSH:
2736 if (operand[0].immediate.X_op != O_constant)
2737 as_bad (_("dsp immediate shift value not constant"));
2738 field_b = ((opcode->nibbles[2] << 12)
2739 | (operand[0].immediate.X_add_number & 127) << 4
2740 | reg_n);
2741 break;
2742 case PPI3NC:
2743 if (cond)
2744 {
2745 opcode++;
2746 goto try_another_opcode;
2747 }
2748 /* Fall through. */
2749 case PPI3:
2750 if (field_b)
2751 as_bad (_("multiple parallel processing specifications"));
2752 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2753 + (reg_x << 6) + (reg_y << 4) + reg_n);
2754 switch (opcode->nibbles[4])
2755 {
2756 case HEX_0:
2757 case HEX_XX00:
2758 case HEX_00YY:
2759 break;
2760 case HEX_1:
2761 case HEX_4:
2762 field_b += opcode->nibbles[4] << 4;
2763 break;
2764 default:
2765 abort ();
2766 }
2767 break;
2768 case PDC:
2769 if (cond)
2770 as_bad (_("multiple condition specifications"));
2771 cond = opcode->nibbles[2] << 8;
2772 if (*op_end)
2773 goto skip_cond_check;
2774 break;
2775 case PPIC:
2776 if (field_b)
2777 as_bad (_("multiple parallel processing specifications"));
2778 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2779 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
2780 cond = 0;
2781 switch (opcode->nibbles[4])
2782 {
2783 case HEX_0:
2784 case HEX_XX00:
2785 case HEX_00YY:
2786 break;
2787 case HEX_1:
2788 case HEX_4:
2789 field_b += opcode->nibbles[4] << 4;
2790 break;
2791 default:
2792 abort ();
2793 }
2794 break;
2795 case PMUL:
2796 if (field_b)
2797 {
2798 if ((field_b & 0xef00) == 0xa100)
2799 field_b -= 0x8100;
2800 /* pclr Dz pmuls Se,Sf,Dg */
2801 else if ((field_b & 0xff00) == 0x8d00
2802 && (SH_MERGE_ARCH_SET_VALID (valid_arch, arch_sh4al_dsp_up)))
2803 {
2804 valid_arch = SH_MERGE_ARCH_SET (valid_arch, arch_sh4al_dsp_up);
2805 field_b -= 0x8cf0;
2806 }
2807 else
2808 as_bad (_("insn cannot be combined with pmuls"));
2809 switch (field_b & 0xf)
2810 {
2811 case A_X0_NUM:
2812 field_b += 0 - A_X0_NUM;
2813 break;
2814 case A_Y0_NUM:
2815 field_b += 1 - A_Y0_NUM;
2816 break;
2817 case A_A0_NUM:
2818 field_b += 2 - A_A0_NUM;
2819 break;
2820 case A_A1_NUM:
2821 field_b += 3 - A_A1_NUM;
2822 break;
2823 default:
2824 as_bad (_("bad combined pmuls output operand"));
2825 }
2826 /* Generate warning if the destination register for padd / psub
2827 and pmuls is the same ( only for A0 or A1 ).
2828 If the last nibble is 1010 then A0 is used in both
2829 padd / psub and pmuls. If it is 1111 then A1 is used
2830 as destination register in both padd / psub and pmuls. */
2831
2832 if ((((field_b | reg_efg) & 0x000F) == 0x000A)
2833 || (((field_b | reg_efg) & 0x000F) == 0x000F))
2834 as_warn (_("destination register is same for parallel insns"));
2835 }
2836 field_b += 0x4000 + reg_efg;
2837 break;
2838 default:
2839 abort ();
2840 }
2841 if (cond)
2842 {
2843 as_bad (_("condition not followed by conditionalizable insn"));
2844 cond = 0;
2845 }
2846 if (! *op_end)
2847 break;
2848 skip_cond_check:
2849 opcode = find_cooked_opcode (&op_end);
2850 if (opcode == NULL)
2851 {
2852 (as_bad
2853 (_("unrecognized characters at end of parallel processing insn")));
2854 break;
2855 }
2856 }
2857
2858 move_code = movx | movy;
2859 if (field_b)
2860 {
2861 /* Parallel processing insn. */
2862 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
2863
2864 output = frag_more (4);
2865 size = 4;
2866 if (! target_big_endian)
2867 {
2868 output[3] = ppi_code >> 8;
2869 output[2] = ppi_code;
2870 }
2871 else
2872 {
2873 output[2] = ppi_code >> 8;
2874 output[3] = ppi_code;
2875 }
2876 move_code |= 0xf800;
2877 }
2878 else
2879 {
2880 /* Just a double data transfer. */
2881 output = frag_more (2);
2882 size = 2;
2883 }
2884 if (! target_big_endian)
2885 {
2886 output[1] = move_code >> 8;
2887 output[0] = move_code;
2888 }
2889 else
2890 {
2891 output[0] = move_code >> 8;
2892 output[1] = move_code;
2893 }
2894 return size;
2895 }
2896
2897 /* This is the guts of the machine-dependent assembler. STR points to a
2898 machine dependent instruction. This function is supposed to emit
2899 the frags/bytes it assembles to. */
2900
2901 void
2902 md_assemble (char *str)
2903 {
2904 char *op_end;
2905 sh_operand_info operand[3];
2906 sh_opcode_info *opcode;
2907 unsigned int size = 0;
2908 char *initial_str = str;
2909
2910 #ifdef HAVE_SH64
2911 if (sh64_isa_mode == sh64_isa_shmedia)
2912 {
2913 shmedia_md_assemble (str);
2914 return;
2915 }
2916 else
2917 {
2918 /* If we've seen pseudo-directives, make sure any emitted data or
2919 frags are marked as data. */
2920 if (!seen_insn)
2921 {
2922 sh64_update_contents_mark (TRUE);
2923 sh64_set_contents_type (CRT_SH5_ISA16);
2924 }
2925
2926 seen_insn = TRUE;
2927 }
2928 #endif /* HAVE_SH64 */
2929
2930 opcode = find_cooked_opcode (&str);
2931 op_end = str;
2932
2933 if (opcode == NULL)
2934 {
2935 /* The opcode is not in the hash table.
2936 This means we definitely have an assembly failure,
2937 but the instruction may be valid in another CPU variant.
2938 In this case emit something better than 'unknown opcode'.
2939 Search the full table in sh-opc.h to check. */
2940
2941 char *name = initial_str;
2942 int name_length = 0;
2943 const sh_opcode_info *op;
2944 int found = 0;
2945
2946 /* identify opcode in string */
2947 while (ISSPACE (*name))
2948 {
2949 name++;
2950 }
2951 while (!ISSPACE (name[name_length]))
2952 {
2953 name_length++;
2954 }
2955
2956 /* search for opcode in full list */
2957 for (op = sh_table; op->name; op++)
2958 {
2959 if (strncasecmp (op->name, name, name_length) == 0
2960 && op->name[name_length] == '\0')
2961 {
2962 found = 1;
2963 break;
2964 }
2965 }
2966
2967 if ( found )
2968 {
2969 as_bad (_("opcode not valid for this cpu variant"));
2970 }
2971 else
2972 {
2973 as_bad (_("unknown opcode"));
2974 }
2975 return;
2976 }
2977
2978 if (sh_relax
2979 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
2980 {
2981 /* Output a CODE reloc to tell the linker that the following
2982 bytes are instructions, not data. */
2983 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2984 BFD_RELOC_SH_CODE);
2985 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
2986 }
2987
2988 if (opcode->nibbles[0] == PPI)
2989 {
2990 size = assemble_ppi (op_end, opcode);
2991 }
2992 else
2993 {
2994 if (opcode->arg[0] == A_BDISP12
2995 || opcode->arg[0] == A_BDISP8)
2996 {
2997 /* Since we skip get_specific here, we have to check & update
2998 valid_arch now. */
2999 if (SH_MERGE_ARCH_SET_VALID (valid_arch, opcode->arch))
3000 valid_arch = SH_MERGE_ARCH_SET (valid_arch, opcode->arch);
3001 else
3002 as_bad (_("Delayed branches not available on SH1"));
3003 parse_exp (op_end + 1, &operand[0]);
3004 build_relax (opcode, &operand[0]);
3005
3006 /* All branches are currently 16 bit. */
3007 size = 2;
3008 }
3009 else
3010 {
3011 if (opcode->arg[0] == A_END)
3012 {
3013 /* Ignore trailing whitespace. If there is any, it has already
3014 been compressed to a single space. */
3015 if (*op_end == ' ')
3016 op_end++;
3017 }
3018 else
3019 {
3020 op_end = get_operands (opcode, op_end, operand);
3021 }
3022 opcode = get_specific (opcode, operand);
3023
3024 if (opcode == 0)
3025 {
3026 /* Couldn't find an opcode which matched the operands. */
3027 char *where = frag_more (2);
3028 size = 2;
3029
3030 where[0] = 0x0;
3031 where[1] = 0x0;
3032 as_bad (_("invalid operands for opcode"));
3033 }
3034 else
3035 {
3036 if (*op_end)
3037 as_bad (_("excess operands: '%s'"), op_end);
3038
3039 size = build_Mytes (opcode, operand);
3040 }
3041 }
3042 }
3043
3044 dwarf2_emit_insn (size);
3045 }
3046
3047 /* This routine is called each time a label definition is seen. It
3048 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
3049
3050 void
3051 sh_frob_label (symbolS *sym)
3052 {
3053 static fragS *last_label_frag;
3054 static int last_label_offset;
3055
3056 if (sh_relax
3057 && seg_info (now_seg)->tc_segment_info_data.in_code)
3058 {
3059 int offset;
3060
3061 offset = frag_now_fix ();
3062 if (frag_now != last_label_frag
3063 || offset != last_label_offset)
3064 {
3065 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
3066 last_label_frag = frag_now;
3067 last_label_offset = offset;
3068 }
3069 }
3070
3071 dwarf2_emit_label (sym);
3072 }
3073
3074 /* This routine is called when the assembler is about to output some
3075 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
3076
3077 void
3078 sh_flush_pending_output (void)
3079 {
3080 if (sh_relax
3081 && seg_info (now_seg)->tc_segment_info_data.in_code)
3082 {
3083 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
3084 BFD_RELOC_SH_DATA);
3085 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
3086 }
3087 }
3088
3089 symbolS *
3090 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
3091 {
3092 return 0;
3093 }
3094
3095 /* Various routines to kill one day. */
3096
3097 char *
3098 md_atof (int type, char *litP, int *sizeP)
3099 {
3100 return ieee_md_atof (type, litP, sizeP, target_big_endian);
3101 }
3102
3103 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
3104 call instruction. It refers to a label of the instruction which
3105 loads the register which the call uses. We use it to generate a
3106 special reloc for the linker. */
3107
3108 static void
3109 s_uses (int ignore ATTRIBUTE_UNUSED)
3110 {
3111 expressionS ex;
3112
3113 if (! sh_relax)
3114 as_warn (_(".uses pseudo-op seen when not relaxing"));
3115
3116 expression (&ex);
3117
3118 if (ex.X_op != O_symbol || ex.X_add_number != 0)
3119 {
3120 as_bad (_("bad .uses format"));
3121 ignore_rest_of_line ();
3122 return;
3123 }
3124
3125 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
3126
3127 demand_empty_rest_of_line ();
3128 }
3129
3130 enum options
3132 {
3133 OPTION_RELAX = OPTION_MD_BASE,
3134 OPTION_BIG,
3135 OPTION_LITTLE,
3136 OPTION_SMALL,
3137 OPTION_DSP,
3138 OPTION_ISA,
3139 OPTION_RENESAS,
3140 OPTION_ALLOW_REG_PREFIX,
3141 #ifdef HAVE_SH64
3142 OPTION_ABI,
3143 OPTION_NO_MIX,
3144 OPTION_SHCOMPACT_CONST_CRANGE,
3145 OPTION_NO_EXPAND,
3146 OPTION_PT32,
3147 #endif
3148 OPTION_H_TICK_HEX,
3149 #ifdef OBJ_ELF
3150 OPTION_FDPIC,
3151 #endif
3152 OPTION_DUMMY /* Not used. This is just here to make it easy to add and subtract options from this enum. */
3153 };
3154
3155 const char *md_shortopts = "";
3156 struct option md_longopts[] =
3157 {
3158 {"relax", no_argument, NULL, OPTION_RELAX},
3159 {"big", no_argument, NULL, OPTION_BIG},
3160 {"little", no_argument, NULL, OPTION_LITTLE},
3161 /* The next two switches are here because the
3162 generic parts of the linker testsuite uses them. */
3163 {"EB", no_argument, NULL, OPTION_BIG},
3164 {"EL", no_argument, NULL, OPTION_LITTLE},
3165 {"small", no_argument, NULL, OPTION_SMALL},
3166 {"dsp", no_argument, NULL, OPTION_DSP},
3167 {"isa", required_argument, NULL, OPTION_ISA},
3168 {"renesas", no_argument, NULL, OPTION_RENESAS},
3169 {"allow-reg-prefix", no_argument, NULL, OPTION_ALLOW_REG_PREFIX},
3170
3171 #ifdef HAVE_SH64
3172 {"abi", required_argument, NULL, OPTION_ABI},
3173 {"no-mix", no_argument, NULL, OPTION_NO_MIX},
3174 {"shcompact-const-crange", no_argument, NULL, OPTION_SHCOMPACT_CONST_CRANGE},
3175 {"no-expand", no_argument, NULL, OPTION_NO_EXPAND},
3176 {"expand-pt32", no_argument, NULL, OPTION_PT32},
3177 #endif /* HAVE_SH64 */
3178 { "h-tick-hex", no_argument, NULL, OPTION_H_TICK_HEX },
3179
3180 #ifdef OBJ_ELF
3181 {"fdpic", no_argument, NULL, OPTION_FDPIC},
3182 #endif
3183
3184 {NULL, no_argument, NULL, 0}
3185 };
3186 size_t md_longopts_size = sizeof (md_longopts);
3187
3188 int
3189 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
3190 {
3191 switch (c)
3192 {
3193 case OPTION_RELAX:
3194 sh_relax = 1;
3195 break;
3196
3197 case OPTION_BIG:
3198 target_big_endian = 1;
3199 break;
3200
3201 case OPTION_LITTLE:
3202 target_big_endian = 0;
3203 break;
3204
3205 case OPTION_SMALL:
3206 sh_small = 1;
3207 break;
3208
3209 case OPTION_DSP:
3210 preset_target_arch = arch_sh_up & ~(arch_sh_sp_fpu|arch_sh_dp_fpu);
3211 break;
3212
3213 case OPTION_RENESAS:
3214 dont_adjust_reloc_32 = 1;
3215 break;
3216
3217 case OPTION_ALLOW_REG_PREFIX:
3218 allow_dollar_register_prefix = 1;
3219 break;
3220
3221 case OPTION_ISA:
3222 if (strcasecmp (arg, "dsp") == 0)
3223 preset_target_arch = arch_sh_up & ~(arch_sh_sp_fpu|arch_sh_dp_fpu);
3224 else if (strcasecmp (arg, "fp") == 0)
3225 preset_target_arch = arch_sh_up & ~arch_sh_has_dsp;
3226 else if (strcasecmp (arg, "any") == 0)
3227 preset_target_arch = arch_sh_up;
3228 #ifdef HAVE_SH64
3229 else if (strcasecmp (arg, "shmedia") == 0)
3230 {
3231 if (sh64_isa_mode == sh64_isa_shcompact)
3232 as_bad (_("Invalid combination: --isa=SHcompact with --isa=SHmedia"));
3233 sh64_isa_mode = sh64_isa_shmedia;
3234 }
3235 else if (strcasecmp (arg, "shcompact") == 0)
3236 {
3237 if (sh64_isa_mode == sh64_isa_shmedia)
3238 as_bad (_("Invalid combination: --isa=SHmedia with --isa=SHcompact"));
3239 if (sh64_abi == sh64_abi_64)
3240 as_bad (_("Invalid combination: --abi=64 with --isa=SHcompact"));
3241 sh64_isa_mode = sh64_isa_shcompact;
3242 }
3243 #endif /* HAVE_SH64 */
3244 else
3245 {
3246 extern const bfd_arch_info_type bfd_sh_arch;
3247 bfd_arch_info_type const *bfd_arch = &bfd_sh_arch;
3248
3249 preset_target_arch = 0;
3250 for (; bfd_arch; bfd_arch=bfd_arch->next)
3251 {
3252 int len = strlen(bfd_arch->printable_name);
3253
3254 if (bfd_arch->mach == bfd_mach_sh5)
3255 continue;
3256
3257 if (strncasecmp (bfd_arch->printable_name, arg, len) != 0)
3258 continue;
3259
3260 if (arg[len] == '\0')
3261 preset_target_arch =
3262 sh_get_arch_from_bfd_mach (bfd_arch->mach);
3263 else if (strcasecmp(&arg[len], "-up") == 0)
3264 preset_target_arch =
3265 sh_get_arch_up_from_bfd_mach (bfd_arch->mach);
3266 else
3267 continue;
3268 break;
3269 }
3270
3271 if (!preset_target_arch)
3272 as_bad (_("Invalid argument to --isa option: %s"), arg);
3273 }
3274 break;
3275
3276 #ifdef HAVE_SH64
3277 case OPTION_ABI:
3278 if (strcmp (arg, "32") == 0)
3279 {
3280 if (sh64_abi == sh64_abi_64)
3281 as_bad (_("Invalid combination: --abi=32 with --abi=64"));
3282 sh64_abi = sh64_abi_32;
3283 }
3284 else if (strcmp (arg, "64") == 0)
3285 {
3286 if (sh64_abi == sh64_abi_32)
3287 as_bad (_("Invalid combination: --abi=64 with --abi=32"));
3288 if (sh64_isa_mode == sh64_isa_shcompact)
3289 as_bad (_("Invalid combination: --isa=SHcompact with --abi=64"));
3290 sh64_abi = sh64_abi_64;
3291 }
3292 else
3293 as_bad (_("Invalid argument to --abi option: %s"), arg);
3294 break;
3295
3296 case OPTION_NO_MIX:
3297 sh64_mix = FALSE;
3298 break;
3299
3300 case OPTION_SHCOMPACT_CONST_CRANGE:
3301 sh64_shcompact_const_crange = TRUE;
3302 break;
3303
3304 case OPTION_NO_EXPAND:
3305 sh64_expand = FALSE;
3306 break;
3307
3308 case OPTION_PT32:
3309 sh64_pt32 = TRUE;
3310 break;
3311 #endif /* HAVE_SH64 */
3312
3313 case OPTION_H_TICK_HEX:
3314 enable_h_tick_hex = 1;
3315 break;
3316
3317 #ifdef OBJ_ELF
3318 case OPTION_FDPIC:
3319 sh_fdpic = TRUE;
3320 break;
3321 #endif /* OBJ_ELF */
3322
3323 default:
3324 return 0;
3325 }
3326
3327 return 1;
3328 }
3329
3330 void
3331 md_show_usage (FILE *stream)
3332 {
3333 fprintf (stream, _("\
3334 SH options:\n\
3335 --little generate little endian code\n\
3336 --big generate big endian code\n\
3337 --relax alter jump instructions for long displacements\n\
3338 --renesas disable optimization with section symbol for\n\
3339 compatibility with Renesas assembler.\n\
3340 --small align sections to 4 byte boundaries, not 16\n\
3341 --dsp enable sh-dsp insns, and disable floating-point ISAs.\n\
3342 --allow-reg-prefix allow '$' as a register name prefix.\n\
3343 --isa=[any use most appropriate isa\n\
3344 | dsp same as '-dsp'\n\
3345 | fp"));
3346 {
3347 extern const bfd_arch_info_type bfd_sh_arch;
3348 bfd_arch_info_type const *bfd_arch = &bfd_sh_arch;
3349
3350 for (; bfd_arch; bfd_arch=bfd_arch->next)
3351 if (bfd_arch->mach != bfd_mach_sh5)
3352 {
3353 fprintf (stream, "\n | %s", bfd_arch->printable_name);
3354 fprintf (stream, "\n | %s-up", bfd_arch->printable_name);
3355 }
3356 }
3357 fprintf (stream, "]\n");
3358 #ifdef HAVE_SH64
3359 fprintf (stream, _("\
3360 --isa=[shmedia set as the default instruction set for SH64\n\
3361 | SHmedia\n\
3362 | shcompact\n\
3363 | SHcompact]\n"));
3364 fprintf (stream, _("\
3365 --abi=[32|64] set size of expanded SHmedia operands and object\n\
3366 file type\n\
3367 --shcompact-const-crange emit code-range descriptors for constants in\n\
3368 SHcompact code sections\n\
3369 --no-mix disallow SHmedia code in the same section as\n\
3370 constants and SHcompact code\n\
3371 --no-expand do not expand MOVI, PT, PTA or PTB instructions\n\
3372 --expand-pt32 with -abi=64, expand PT, PTA and PTB instructions\n\
3373 to 32 bits only\n"));
3374 #endif /* HAVE_SH64 */
3375 #ifdef OBJ_ELF
3376 fprintf (stream, _("\
3377 --fdpic generate an FDPIC object file\n"));
3378 #endif /* OBJ_ELF */
3379 }
3380
3381 /* This struct is used to pass arguments to sh_count_relocs through
3383 bfd_map_over_sections. */
3384
3385 struct sh_count_relocs
3386 {
3387 /* Symbol we are looking for. */
3388 symbolS *sym;
3389 /* Count of relocs found. */
3390 int count;
3391 };
3392
3393 /* Count the number of fixups in a section which refer to a particular
3394 symbol. This is called via bfd_map_over_sections. */
3395
3396 static void
3397 sh_count_relocs (bfd *abfd ATTRIBUTE_UNUSED, segT sec, void *data)
3398 {
3399 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
3400 segment_info_type *seginfo;
3401 symbolS *sym;
3402 fixS *fix;
3403
3404 seginfo = seg_info (sec);
3405 if (seginfo == NULL)
3406 return;
3407
3408 sym = info->sym;
3409 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3410 {
3411 if (fix->fx_addsy == sym)
3412 {
3413 ++info->count;
3414 fix->fx_tcbit = 1;
3415 }
3416 }
3417 }
3418
3419 /* Handle the count relocs for a particular section.
3420 This is called via bfd_map_over_sections. */
3421
3422 static void
3423 sh_frob_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec,
3424 void *ignore ATTRIBUTE_UNUSED)
3425 {
3426 segment_info_type *seginfo;
3427 fixS *fix;
3428
3429 seginfo = seg_info (sec);
3430 if (seginfo == NULL)
3431 return;
3432
3433 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3434 {
3435 symbolS *sym;
3436
3437 sym = fix->fx_addsy;
3438 /* Check for a local_symbol. */
3439 if (sym && sym->bsym == NULL)
3440 {
3441 struct local_symbol *ls = (struct local_symbol *)sym;
3442 /* See if it's been converted. If so, canonicalize. */
3443 if (local_symbol_converted_p (ls))
3444 fix->fx_addsy = local_symbol_get_real_symbol (ls);
3445 }
3446 }
3447
3448 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3449 {
3450 symbolS *sym;
3451 bfd_vma val;
3452 fixS *fscan;
3453 struct sh_count_relocs info;
3454
3455 if (fix->fx_r_type != BFD_RELOC_SH_USES)
3456 continue;
3457
3458 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
3459 symbol in the same section. */
3460 sym = fix->fx_addsy;
3461 if (sym == NULL
3462 || fix->fx_subsy != NULL
3463 || fix->fx_addnumber != 0
3464 || S_GET_SEGMENT (sym) != sec
3465 || S_IS_EXTERNAL (sym))
3466 {
3467 as_warn_where (fix->fx_file, fix->fx_line,
3468 _(".uses does not refer to a local symbol in the same section"));
3469 continue;
3470 }
3471
3472 /* Look through the fixups again, this time looking for one
3473 at the same location as sym. */
3474 val = S_GET_VALUE (sym);
3475 for (fscan = seginfo->fix_root;
3476 fscan != NULL;
3477 fscan = fscan->fx_next)
3478 if (val == fscan->fx_frag->fr_address + fscan->fx_where
3479 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
3480 && fscan->fx_r_type != BFD_RELOC_SH_CODE
3481 && fscan->fx_r_type != BFD_RELOC_SH_DATA
3482 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
3483 break;
3484 if (fscan == NULL)
3485 {
3486 as_warn_where (fix->fx_file, fix->fx_line,
3487 _("can't find fixup pointed to by .uses"));
3488 continue;
3489 }
3490
3491 if (fscan->fx_tcbit)
3492 {
3493 /* We've already done this one. */
3494 continue;
3495 }
3496
3497 /* The variable fscan should also be a fixup to a local symbol
3498 in the same section. */
3499 sym = fscan->fx_addsy;
3500 if (sym == NULL
3501 || fscan->fx_subsy != NULL
3502 || fscan->fx_addnumber != 0
3503 || S_GET_SEGMENT (sym) != sec
3504 || S_IS_EXTERNAL (sym))
3505 {
3506 as_warn_where (fix->fx_file, fix->fx_line,
3507 _(".uses target does not refer to a local symbol in the same section"));
3508 continue;
3509 }
3510
3511 /* Now we look through all the fixups of all the sections,
3512 counting the number of times we find a reference to sym. */
3513 info.sym = sym;
3514 info.count = 0;
3515 bfd_map_over_sections (stdoutput, sh_count_relocs, &info);
3516
3517 if (info.count < 1)
3518 abort ();
3519
3520 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
3521 We have already adjusted the value of sym to include the
3522 fragment address, so we undo that adjustment here. */
3523 subseg_change (sec, 0);
3524 fix_new (fscan->fx_frag,
3525 S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
3526 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
3527 }
3528 }
3529
3530 /* This function is called after the symbol table has been completed,
3531 but before the relocs or section contents have been written out.
3532 If we have seen any .uses pseudo-ops, they point to an instruction
3533 which loads a register with the address of a function. We look
3534 through the fixups to find where the function address is being
3535 loaded from. We then generate a COUNT reloc giving the number of
3536 times that function address is referred to. The linker uses this
3537 information when doing relaxing, to decide when it can eliminate
3538 the stored function address entirely. */
3539
3540 void
3541 sh_frob_file (void)
3542 {
3543 #ifdef HAVE_SH64
3544 shmedia_frob_file_before_adjust ();
3545 #endif
3546
3547 if (! sh_relax)
3548 return;
3549
3550 bfd_map_over_sections (stdoutput, sh_frob_section, NULL);
3551 }
3552
3553 /* Called after relaxing. Set the correct sizes of the fragments, and
3554 create relocs so that md_apply_fix will fill in the correct values. */
3555
3556 void
3557 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT seg, fragS *fragP)
3558 {
3559 int donerelax = 0;
3560
3561 switch (fragP->fr_subtype)
3562 {
3563 case C (COND_JUMP, COND8):
3564 case C (COND_JUMP_DELAY, COND8):
3565 subseg_change (seg, 0);
3566 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3567 1, BFD_RELOC_SH_PCDISP8BY2);
3568 fragP->fr_fix += 2;
3569 fragP->fr_var = 0;
3570 break;
3571
3572 case C (UNCOND_JUMP, UNCOND12):
3573 subseg_change (seg, 0);
3574 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3575 1, BFD_RELOC_SH_PCDISP12BY2);
3576 fragP->fr_fix += 2;
3577 fragP->fr_var = 0;
3578 break;
3579
3580 case C (UNCOND_JUMP, UNCOND32):
3581 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3582 if (fragP->fr_symbol == NULL)
3583 as_bad_where (fragP->fr_file, fragP->fr_line,
3584 _("displacement overflows 12-bit field"));
3585 else if (S_IS_DEFINED (fragP->fr_symbol))
3586 as_bad_where (fragP->fr_file, fragP->fr_line,
3587 _("displacement to defined symbol %s overflows 12-bit field"),
3588 S_GET_NAME (fragP->fr_symbol));
3589 else
3590 as_bad_where (fragP->fr_file, fragP->fr_line,
3591 _("displacement to undefined symbol %s overflows 12-bit field"),
3592 S_GET_NAME (fragP->fr_symbol));
3593 /* Stabilize this frag, so we don't trip an assert. */
3594 fragP->fr_fix += fragP->fr_var;
3595 fragP->fr_var = 0;
3596 break;
3597
3598 case C (COND_JUMP, COND12):
3599 case C (COND_JUMP_DELAY, COND12):
3600 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
3601 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
3602 was due to gas incorrectly relaxing an out-of-range conditional
3603 branch with delay slot. It turned:
3604 bf.s L6 (slot mov.l r12,@(44,r0))
3605 into:
3606
3607 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
3608 30: 00 09 nop
3609 32: 10 cb mov.l r12,@(44,r0)
3610 Therefore, branches with delay slots have to be handled
3611 differently from ones without delay slots. */
3612 {
3613 unsigned char *buffer =
3614 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
3615 int highbyte = target_big_endian ? 0 : 1;
3616 int lowbyte = target_big_endian ? 1 : 0;
3617 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
3618
3619 /* Toggle the true/false bit of the bcond. */
3620 buffer[highbyte] ^= 0x2;
3621
3622 /* If this is a delayed branch, we may not put the bra in the
3623 slot. So we change it to a non-delayed branch, like that:
3624 b! cond slot_label; bra disp; slot_label: slot_insn
3625 ??? We should try if swapping the conditional branch and
3626 its delay-slot insn already makes the branch reach. */
3627
3628 /* Build a relocation to six / four bytes farther on. */
3629 subseg_change (seg, 0);
3630 fix_new (fragP, fragP->fr_fix, 2, section_symbol (seg),
3631 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
3632 1, BFD_RELOC_SH_PCDISP8BY2);
3633
3634 /* Set up a jump instruction. */
3635 buffer[highbyte + 2] = 0xa0;
3636 buffer[lowbyte + 2] = 0;
3637 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
3638 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
3639
3640 if (delay)
3641 {
3642 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
3643 fragP->fr_fix += 4;
3644 }
3645 else
3646 {
3647 /* Fill in a NOP instruction. */
3648 buffer[highbyte + 4] = 0x0;
3649 buffer[lowbyte + 4] = 0x9;
3650
3651 fragP->fr_fix += 6;
3652 }
3653 fragP->fr_var = 0;
3654 donerelax = 1;
3655 }
3656 break;
3657
3658 case C (COND_JUMP, COND32):
3659 case C (COND_JUMP_DELAY, COND32):
3660 case C (COND_JUMP, UNDEF_WORD_DISP):
3661 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3662 if (fragP->fr_symbol == NULL)
3663 as_bad_where (fragP->fr_file, fragP->fr_line,
3664 _("displacement overflows 8-bit field"));
3665 else if (S_IS_DEFINED (fragP->fr_symbol))
3666 as_bad_where (fragP->fr_file, fragP->fr_line,
3667 _("displacement to defined symbol %s overflows 8-bit field"),
3668 S_GET_NAME (fragP->fr_symbol));
3669 else
3670 as_bad_where (fragP->fr_file, fragP->fr_line,
3671 _("displacement to undefined symbol %s overflows 8-bit field "),
3672 S_GET_NAME (fragP->fr_symbol));
3673 /* Stabilize this frag, so we don't trip an assert. */
3674 fragP->fr_fix += fragP->fr_var;
3675 fragP->fr_var = 0;
3676 break;
3677
3678 default:
3679 #ifdef HAVE_SH64
3680 shmedia_md_convert_frag (headers, seg, fragP, TRUE);
3681 #else
3682 abort ();
3683 #endif
3684 }
3685
3686 if (donerelax && !sh_relax)
3687 as_warn_where (fragP->fr_file, fragP->fr_line,
3688 _("overflow in branch to %s; converted into longer instruction sequence"),
3689 (fragP->fr_symbol != NULL
3690 ? S_GET_NAME (fragP->fr_symbol)
3691 : ""));
3692 }
3693
3694 valueT
3695 md_section_align (segT seg ATTRIBUTE_UNUSED, valueT size)
3696 {
3697 #ifdef OBJ_ELF
3698 return size;
3699 #else /* ! OBJ_ELF */
3700 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
3701 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
3702 #endif /* ! OBJ_ELF */
3703 }
3704
3705 /* This static variable is set by s_uacons to tell sh_cons_align that
3706 the expression does not need to be aligned. */
3707
3708 static int sh_no_align_cons = 0;
3709
3710 /* This handles the unaligned space allocation pseudo-ops, such as
3711 .uaword. .uaword is just like .word, but the value does not need
3712 to be aligned. */
3713
3714 static void
3715 s_uacons (int bytes)
3716 {
3717 /* Tell sh_cons_align not to align this value. */
3718 sh_no_align_cons = 1;
3719 cons (bytes);
3720 }
3721
3722 /* If a .word, et. al., pseud-op is seen, warn if the value is not
3723 aligned correctly. Note that this can cause warnings to be issued
3724 when assembling initialized structured which were declared with the
3725 packed attribute. FIXME: Perhaps we should require an option to
3726 enable this warning? */
3727
3728 void
3729 sh_cons_align (int nbytes)
3730 {
3731 int nalign;
3732
3733 if (sh_no_align_cons)
3734 {
3735 /* This is an unaligned pseudo-op. */
3736 sh_no_align_cons = 0;
3737 return;
3738 }
3739
3740 nalign = 0;
3741 while ((nbytes & 1) == 0)
3742 {
3743 ++nalign;
3744 nbytes >>= 1;
3745 }
3746
3747 if (nalign == 0)
3748 return;
3749
3750 if (now_seg == absolute_section)
3751 {
3752 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3753 as_warn (_("misaligned data"));
3754 return;
3755 }
3756
3757 frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
3758 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3759
3760 record_alignment (now_seg, nalign);
3761 }
3762
3763 /* When relaxing, we need to output a reloc for any .align directive
3764 that requests alignment to a four byte boundary or larger. This is
3765 also where we check for misaligned data. */
3766
3767 void
3768 sh_handle_align (fragS *frag)
3769 {
3770 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
3771
3772 if (frag->fr_type == rs_align_code)
3773 {
3774 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
3775 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
3776
3777 char *p = frag->fr_literal + frag->fr_fix;
3778
3779 if (bytes & 1)
3780 {
3781 *p++ = 0;
3782 bytes--;
3783 frag->fr_fix += 1;
3784 }
3785
3786 if (target_big_endian)
3787 {
3788 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
3789 frag->fr_var = sizeof big_nop_pattern;
3790 }
3791 else
3792 {
3793 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
3794 frag->fr_var = sizeof little_nop_pattern;
3795 }
3796 }
3797 else if (frag->fr_type == rs_align_test)
3798 {
3799 if (bytes != 0)
3800 as_bad_where (frag->fr_file, frag->fr_line, _("misaligned data"));
3801 }
3802
3803 if (sh_relax
3804 && (frag->fr_type == rs_align
3805 || frag->fr_type == rs_align_code)
3806 && frag->fr_address + frag->fr_fix > 0
3807 && frag->fr_offset > 1
3808 && now_seg != bss_section)
3809 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
3810 BFD_RELOC_SH_ALIGN);
3811 }
3812
3813 /* See whether the relocation should be resolved locally. */
3814
3815 static bfd_boolean
3816 sh_local_pcrel (fixS *fix)
3817 {
3818 return (! sh_relax
3819 && (fix->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
3820 || fix->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
3821 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
3822 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
3823 || fix->fx_r_type == BFD_RELOC_8_PCREL
3824 || fix->fx_r_type == BFD_RELOC_SH_SWITCH16
3825 || fix->fx_r_type == BFD_RELOC_SH_SWITCH32));
3826 }
3827
3828 /* See whether we need to force a relocation into the output file.
3829 This is used to force out switch and PC relative relocations when
3830 relaxing. */
3831
3832 int
3833 sh_force_relocation (fixS *fix)
3834 {
3835 /* These relocations can't make it into a DSO, so no use forcing
3836 them for global symbols. */
3837 if (sh_local_pcrel (fix))
3838 return 0;
3839
3840 /* Make sure some relocations get emitted. */
3841 if (fix->fx_r_type == BFD_RELOC_SH_LOOP_START
3842 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END
3843 || fix->fx_r_type == BFD_RELOC_SH_TLS_GD_32
3844 || fix->fx_r_type == BFD_RELOC_SH_TLS_LD_32
3845 || fix->fx_r_type == BFD_RELOC_SH_TLS_IE_32
3846 || fix->fx_r_type == BFD_RELOC_SH_TLS_LDO_32
3847 || fix->fx_r_type == BFD_RELOC_SH_TLS_LE_32
3848 || generic_force_reloc (fix))
3849 return 1;
3850
3851 if (! sh_relax)
3852 return 0;
3853
3854 return (fix->fx_pcrel
3855 || SWITCH_TABLE (fix)
3856 || fix->fx_r_type == BFD_RELOC_SH_COUNT
3857 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
3858 || fix->fx_r_type == BFD_RELOC_SH_CODE
3859 || fix->fx_r_type == BFD_RELOC_SH_DATA
3860 #ifdef HAVE_SH64
3861 || fix->fx_r_type == BFD_RELOC_SH_SHMEDIA_CODE
3862 #endif
3863 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
3864 }
3865
3866 #ifdef OBJ_ELF
3867 bfd_boolean
3868 sh_fix_adjustable (fixS *fixP)
3869 {
3870 if (fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL
3871 || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
3872 || fixP->fx_r_type == BFD_RELOC_SH_GOT20
3873 || fixP->fx_r_type == BFD_RELOC_SH_GOTPC
3874 || fixP->fx_r_type == BFD_RELOC_SH_GOTFUNCDESC
3875 || fixP->fx_r_type == BFD_RELOC_SH_GOTFUNCDESC20
3876 || fixP->fx_r_type == BFD_RELOC_SH_GOTOFFFUNCDESC
3877 || fixP->fx_r_type == BFD_RELOC_SH_GOTOFFFUNCDESC20
3878 || fixP->fx_r_type == BFD_RELOC_SH_FUNCDESC
3879 || ((fixP->fx_r_type == BFD_RELOC_32) && dont_adjust_reloc_32)
3880 || fixP->fx_r_type == BFD_RELOC_RVA)
3881 return 0;
3882
3883 /* We need the symbol name for the VTABLE entries */
3884 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3885 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3886 return 0;
3887
3888 return 1;
3889 }
3890
3891 void
3892 sh_elf_final_processing (void)
3893 {
3894 int val;
3895
3896 /* Set file-specific flags to indicate if this code needs
3897 a processor with the sh-dsp / sh2e ISA to execute. */
3898 #ifdef HAVE_SH64
3899 /* SH5 and above don't know about the valid_arch arch_sh* bits defined
3900 in sh-opc.h, so check SH64 mode before checking valid_arch. */
3901 if (sh64_isa_mode != sh64_isa_unspecified)
3902 val = EF_SH5;
3903 else
3904 #elif defined TARGET_SYMBIAN
3905 if (1)
3906 {
3907 extern int sh_symbian_find_elf_flags (unsigned int);
3908
3909 val = sh_symbian_find_elf_flags (valid_arch);
3910 }
3911 else
3912 #endif /* HAVE_SH64 */
3913 val = sh_find_elf_flags (valid_arch);
3914
3915 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
3916 elf_elfheader (stdoutput)->e_flags |= val;
3917
3918 if (sh_fdpic)
3919 elf_elfheader (stdoutput)->e_flags |= EF_SH_FDPIC;
3920 }
3921 #endif
3922
3923 #ifdef TE_UCLINUX
3924 /* Return the target format for uClinux. */
3925
3926 const char *
3927 sh_uclinux_target_format (void)
3928 {
3929 if (sh_fdpic)
3930 return (!target_big_endian ? "elf32-sh-fdpic" : "elf32-shbig-fdpic");
3931 else
3932 return (!target_big_endian ? "elf32-shl" : "elf32-sh");
3933 }
3934 #endif
3935
3936 /* Apply fixup FIXP to SIZE-byte field BUF given that VAL is its
3937 assembly-time value. If we're generating a reloc for FIXP,
3938 see whether the addend should be stored in-place or whether
3939 it should be in an ELF r_addend field. */
3940
3941 static void
3942 apply_full_field_fix (fixS *fixP, char *buf, bfd_vma val, int size)
3943 {
3944 reloc_howto_type *howto;
3945
3946 if (fixP->fx_addsy != NULL || fixP->fx_pcrel)
3947 {
3948 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
3949 if (howto && !howto->partial_inplace)
3950 {
3951 fixP->fx_addnumber = val;
3952 return;
3953 }
3954 }
3955 md_number_to_chars (buf, val, size);
3956 }
3957
3958 /* Apply a fixup to the object file. */
3959
3960 void
3961 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3962 {
3963 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3964 int lowbyte = target_big_endian ? 1 : 0;
3965 int highbyte = target_big_endian ? 0 : 1;
3966 long val = (long) *valP;
3967 long max, min;
3968 int shift;
3969
3970 /* A difference between two symbols, the second of which is in the
3971 current section, is transformed in a PC-relative relocation to
3972 the other symbol. We have to adjust the relocation type here. */
3973 if (fixP->fx_pcrel)
3974 {
3975 switch (fixP->fx_r_type)
3976 {
3977 default:
3978 break;
3979
3980 case BFD_RELOC_32:
3981 fixP->fx_r_type = BFD_RELOC_32_PCREL;
3982 break;
3983
3984 /* Currently, we only support 32-bit PCREL relocations.
3985 We'd need a new reloc type to handle 16_PCREL, and
3986 8_PCREL is already taken for R_SH_SWITCH8, which
3987 apparently does something completely different than what
3988 we need. FIXME. */
3989 case BFD_RELOC_16:
3990 bfd_set_error (bfd_error_bad_value);
3991 return;
3992
3993 case BFD_RELOC_8:
3994 bfd_set_error (bfd_error_bad_value);
3995 return;
3996 }
3997 }
3998
3999 /* The function adjust_reloc_syms won't convert a reloc against a weak
4000 symbol into a reloc against a section, but bfd_install_relocation
4001 will screw up if the symbol is defined, so we have to adjust val here
4002 to avoid the screw up later.
4003
4004 For ordinary relocs, this does not happen for ELF, since for ELF,
4005 bfd_install_relocation uses the "special function" field of the
4006 howto, and does not execute the code that needs to be undone, as long
4007 as the special function does not return bfd_reloc_continue.
4008 It can happen for GOT- and PLT-type relocs the way they are
4009 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
4010 doesn't matter here since those relocs don't use VAL; see below. */
4011 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
4012 && fixP->fx_addsy != NULL
4013 && S_IS_WEAK (fixP->fx_addsy))
4014 val -= S_GET_VALUE (fixP->fx_addsy);
4015
4016 if (SWITCH_TABLE (fixP))
4017 val -= S_GET_VALUE (fixP->fx_subsy);
4018
4019 max = min = 0;
4020 shift = 0;
4021 switch (fixP->fx_r_type)
4022 {
4023 case BFD_RELOC_SH_IMM3:
4024 max = 0x7;
4025 * buf = (* buf & 0xf8) | (val & 0x7);
4026 break;
4027 case BFD_RELOC_SH_IMM3U:
4028 max = 0x7;
4029 * buf = (* buf & 0x8f) | ((val & 0x7) << 4);
4030 break;
4031 case BFD_RELOC_SH_DISP12:
4032 max = 0xfff;
4033 buf[lowbyte] = val & 0xff;
4034 buf[highbyte] |= (val >> 8) & 0x0f;
4035 break;
4036 case BFD_RELOC_SH_DISP12BY2:
4037 max = 0xfff;
4038 shift = 1;
4039 buf[lowbyte] = (val >> 1) & 0xff;
4040 buf[highbyte] |= (val >> 9) & 0x0f;
4041 break;
4042 case BFD_RELOC_SH_DISP12BY4:
4043 max = 0xfff;
4044 shift = 2;
4045 buf[lowbyte] = (val >> 2) & 0xff;
4046 buf[highbyte] |= (val >> 10) & 0x0f;
4047 break;
4048 case BFD_RELOC_SH_DISP12BY8:
4049 max = 0xfff;
4050 shift = 3;
4051 buf[lowbyte] = (val >> 3) & 0xff;
4052 buf[highbyte] |= (val >> 11) & 0x0f;
4053 break;
4054 case BFD_RELOC_SH_DISP20:
4055 if (! target_big_endian)
4056 abort();
4057 max = 0x7ffff;
4058 min = -0x80000;
4059 buf[1] = (buf[1] & 0x0f) | ((val >> 12) & 0xf0);
4060 buf[2] = (val >> 8) & 0xff;
4061 buf[3] = val & 0xff;
4062 break;
4063 case BFD_RELOC_SH_DISP20BY8:
4064 if (!target_big_endian)
4065 abort();
4066 max = 0x7ffff;
4067 min = -0x80000;
4068 shift = 8;
4069 buf[1] = (buf[1] & 0x0f) | ((val >> 20) & 0xf0);
4070 buf[2] = (val >> 16) & 0xff;
4071 buf[3] = (val >> 8) & 0xff;
4072 break;
4073
4074 case BFD_RELOC_SH_IMM4:
4075 max = 0xf;
4076 *buf = (*buf & 0xf0) | (val & 0xf);
4077 break;
4078
4079 case BFD_RELOC_SH_IMM4BY2:
4080 max = 0xf;
4081 shift = 1;
4082 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
4083 break;
4084
4085 case BFD_RELOC_SH_IMM4BY4:
4086 max = 0xf;
4087 shift = 2;
4088 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
4089 break;
4090
4091 case BFD_RELOC_SH_IMM8BY2:
4092 max = 0xff;
4093 shift = 1;
4094 *buf = val >> 1;
4095 break;
4096
4097 case BFD_RELOC_SH_IMM8BY4:
4098 max = 0xff;
4099 shift = 2;
4100 *buf = val >> 2;
4101 break;
4102
4103 case BFD_RELOC_8:
4104 case BFD_RELOC_SH_IMM8:
4105 /* Sometimes the 8 bit value is sign extended (e.g., add) and
4106 sometimes it is not (e.g., and). We permit any 8 bit value.
4107 Note that adding further restrictions may invalidate
4108 reasonable looking assembly code, such as ``and -0x1,r0''. */
4109 max = 0xff;
4110 min = -0xff;
4111 *buf++ = val;
4112 break;
4113
4114 case BFD_RELOC_SH_PCRELIMM8BY4:
4115 /* If we are dealing with a known destination ... */
4116 if ((fixP->fx_addsy == NULL || S_IS_DEFINED (fixP->fx_addsy))
4117 && (fixP->fx_subsy == NULL || S_IS_DEFINED (fixP->fx_addsy)))
4118 {
4119 /* Don't silently move the destination due to misalignment.
4120 The absolute address is the fragment base plus the offset into
4121 the fragment plus the pc relative offset to the label. */
4122 if ((fixP->fx_frag->fr_address + fixP->fx_where + val) & 3)
4123 as_bad_where (fixP->fx_file, fixP->fx_line,
4124 _("offset to unaligned destination"));
4125
4126 /* The displacement cannot be zero or backward even if aligned.
4127 Allow -2 because val has already been adjusted somewhere. */
4128 if (val < -2)
4129 as_bad_where (fixP->fx_file, fixP->fx_line, _("negative offset"));
4130 }
4131
4132 /* The lower two bits of the PC are cleared before the
4133 displacement is added in. We can assume that the destination
4134 is on a 4 byte boundary. If this instruction is also on a 4
4135 byte boundary, then we want
4136 (target - here) / 4
4137 and target - here is a multiple of 4.
4138 Otherwise, we are on a 2 byte boundary, and we want
4139 (target - (here - 2)) / 4
4140 and target - here is not a multiple of 4. Computing
4141 (target - (here - 2)) / 4 == (target - here + 2) / 4
4142 works for both cases, since in the first case the addition of
4143 2 will be removed by the division. target - here is in the
4144 variable val. */
4145 val = (val + 2) / 4;
4146 if (val & ~0xff)
4147 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
4148 buf[lowbyte] = val;
4149 break;
4150
4151 case BFD_RELOC_SH_PCRELIMM8BY2:
4152 val /= 2;
4153 if (val & ~0xff)
4154 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
4155 buf[lowbyte] = val;
4156 break;
4157
4158 case BFD_RELOC_SH_PCDISP8BY2:
4159 val /= 2;
4160 if (val < -0x80 || val > 0x7f)
4161 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
4162 buf[lowbyte] = val;
4163 break;
4164
4165 case BFD_RELOC_SH_PCDISP12BY2:
4166 val /= 2;
4167 if (val < -0x800 || val > 0x7ff)
4168 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
4169 buf[lowbyte] = val & 0xff;
4170 buf[highbyte] |= (val >> 8) & 0xf;
4171 break;
4172
4173 case BFD_RELOC_32:
4174 case BFD_RELOC_32_PCREL:
4175 apply_full_field_fix (fixP, buf, val, 4);
4176 break;
4177
4178 case BFD_RELOC_16:
4179 apply_full_field_fix (fixP, buf, val, 2);
4180 break;
4181
4182 case BFD_RELOC_SH_USES:
4183 /* Pass the value into sh_reloc(). */
4184 fixP->fx_addnumber = val;
4185 break;
4186
4187 case BFD_RELOC_SH_COUNT:
4188 case BFD_RELOC_SH_ALIGN:
4189 case BFD_RELOC_SH_CODE:
4190 case BFD_RELOC_SH_DATA:
4191 case BFD_RELOC_SH_LABEL:
4192 /* Nothing to do here. */
4193 break;
4194
4195 case BFD_RELOC_SH_LOOP_START:
4196 case BFD_RELOC_SH_LOOP_END:
4197
4198 case BFD_RELOC_VTABLE_INHERIT:
4199 case BFD_RELOC_VTABLE_ENTRY:
4200 fixP->fx_done = 0;
4201 return;
4202
4203 #ifdef OBJ_ELF
4204 case BFD_RELOC_32_PLT_PCREL:
4205 /* Make the jump instruction point to the address of the operand. At
4206 runtime we merely add the offset to the actual PLT entry. */
4207 * valP = 0xfffffffc;
4208 val = fixP->fx_offset;
4209 if (fixP->fx_subsy)
4210 val -= S_GET_VALUE (fixP->fx_subsy);
4211 apply_full_field_fix (fixP, buf, val, 4);
4212 break;
4213
4214 case BFD_RELOC_SH_GOTPC:
4215 /* This is tough to explain. We end up with this one if we have
4216 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
4217 The goal here is to obtain the absolute address of the GOT,
4218 and it is strongly preferable from a performance point of
4219 view to avoid using a runtime relocation for this. There are
4220 cases where you have something like:
4221
4222 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
4223
4224 and here no correction would be required. Internally in the
4225 assembler we treat operands of this form as not being pcrel
4226 since the '.' is explicitly mentioned, and I wonder whether
4227 it would simplify matters to do it this way. Who knows. In
4228 earlier versions of the PIC patches, the pcrel_adjust field
4229 was used to store the correction, but since the expression is
4230 not pcrel, I felt it would be confusing to do it this way. */
4231 * valP -= 1;
4232 apply_full_field_fix (fixP, buf, val, 4);
4233 break;
4234
4235 case BFD_RELOC_SH_TLS_GD_32:
4236 case BFD_RELOC_SH_TLS_LD_32:
4237 case BFD_RELOC_SH_TLS_IE_32:
4238 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4239 /* Fallthrough */
4240 case BFD_RELOC_32_GOT_PCREL:
4241 case BFD_RELOC_SH_GOT20:
4242 case BFD_RELOC_SH_GOTPLT32:
4243 case BFD_RELOC_SH_GOTFUNCDESC:
4244 case BFD_RELOC_SH_GOTFUNCDESC20:
4245 case BFD_RELOC_SH_GOTOFFFUNCDESC:
4246 case BFD_RELOC_SH_GOTOFFFUNCDESC20:
4247 case BFD_RELOC_SH_FUNCDESC:
4248 * valP = 0; /* Fully resolved at runtime. No addend. */
4249 apply_full_field_fix (fixP, buf, 0, 4);
4250 break;
4251
4252 case BFD_RELOC_SH_TLS_LDO_32:
4253 case BFD_RELOC_SH_TLS_LE_32:
4254 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4255 /* Fallthrough */
4256 case BFD_RELOC_32_GOTOFF:
4257 case BFD_RELOC_SH_GOTOFF20:
4258 apply_full_field_fix (fixP, buf, val, 4);
4259 break;
4260 #endif
4261
4262 default:
4263 #ifdef HAVE_SH64
4264 shmedia_md_apply_fix (fixP, valP);
4265 return;
4266 #else
4267 abort ();
4268 #endif
4269 }
4270
4271 if (shift != 0)
4272 {
4273 if ((val & ((1 << shift) - 1)) != 0)
4274 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
4275 if (val >= 0)
4276 val >>= shift;
4277 else
4278 val = ((val >> shift)
4279 | ((long) -1 & ~ ((long) -1 >> shift)));
4280 }
4281
4282 /* Extend sign for 64-bit host. */
4283 val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
4284 if (max != 0 && (val < min || val > max))
4285 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
4286 else if (max != 0)
4287 /* Stop the generic code from trying to overlow check the value as well.
4288 It may not have the correct value anyway, as we do not store val back
4289 into *valP. */
4290 fixP->fx_no_overflow = 1;
4291
4292 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
4293 fixP->fx_done = 1;
4294 }
4295
4296 /* Called just before address relaxation. Return the length
4297 by which a fragment must grow to reach it's destination. */
4298
4299 int
4300 md_estimate_size_before_relax (fragS *fragP, segT segment_type)
4301 {
4302 int what;
4303
4304 switch (fragP->fr_subtype)
4305 {
4306 default:
4307 #ifdef HAVE_SH64
4308 return shmedia_md_estimate_size_before_relax (fragP, segment_type);
4309 #else
4310 abort ();
4311 #endif
4312
4313
4314 case C (UNCOND_JUMP, UNDEF_DISP):
4315 /* Used to be a branch to somewhere which was unknown. */
4316 if (!fragP->fr_symbol)
4317 {
4318 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
4319 }
4320 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
4321 {
4322 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
4323 }
4324 else
4325 {
4326 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
4327 }
4328 break;
4329
4330 case C (COND_JUMP, UNDEF_DISP):
4331 case C (COND_JUMP_DELAY, UNDEF_DISP):
4332 what = GET_WHAT (fragP->fr_subtype);
4333 /* Used to be a branch to somewhere which was unknown. */
4334 if (fragP->fr_symbol
4335 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
4336 {
4337 /* Got a symbol and it's defined in this segment, become byte
4338 sized - maybe it will fix up. */
4339 fragP->fr_subtype = C (what, COND8);
4340 }
4341 else if (fragP->fr_symbol)
4342 {
4343 /* Its got a segment, but its not ours, so it will always be long. */
4344 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
4345 }
4346 else
4347 {
4348 /* We know the abs value. */
4349 fragP->fr_subtype = C (what, COND8);
4350 }
4351 break;
4352
4353 case C (UNCOND_JUMP, UNCOND12):
4354 case C (UNCOND_JUMP, UNCOND32):
4355 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
4356 case C (COND_JUMP, COND8):
4357 case C (COND_JUMP, COND12):
4358 case C (COND_JUMP, COND32):
4359 case C (COND_JUMP, UNDEF_WORD_DISP):
4360 case C (COND_JUMP_DELAY, COND8):
4361 case C (COND_JUMP_DELAY, COND12):
4362 case C (COND_JUMP_DELAY, COND32):
4363 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
4364 /* When relaxing a section for the second time, we don't need to
4365 do anything besides return the current size. */
4366 break;
4367 }
4368
4369 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
4370 return fragP->fr_var;
4371 }
4372
4373 /* Put number into target byte order. */
4374
4375 void
4376 md_number_to_chars (char *ptr, valueT use, int nbytes)
4377 {
4378 #ifdef HAVE_SH64
4379 /* We might need to set the contents type to data. */
4380 sh64_flag_output ();
4381 #endif
4382
4383 if (! target_big_endian)
4384 number_to_chars_littleendian (ptr, use, nbytes);
4385 else
4386 number_to_chars_bigendian (ptr, use, nbytes);
4387 }
4388
4389 /* This version is used in obj-coff.c eg. for the sh-hms target. */
4390
4391 long
4392 md_pcrel_from (fixS *fixP)
4393 {
4394 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
4395 }
4396
4397 long
4398 md_pcrel_from_section (fixS *fixP, segT sec)
4399 {
4400 if (! sh_local_pcrel (fixP)
4401 && fixP->fx_addsy != (symbolS *) NULL
4402 && (generic_force_reloc (fixP)
4403 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
4404 {
4405 /* The symbol is undefined (or is defined but not in this section,
4406 or we're not sure about it being the final definition). Let the
4407 linker figure it out. We need to adjust the subtraction of a
4408 symbol to the position of the relocated data, though. */
4409 return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
4410 }
4411
4412 return md_pcrel_from (fixP);
4413 }
4414
4415 /* Create a reloc. */
4416
4417 arelent *
4418 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
4419 {
4420 arelent *rel;
4421 bfd_reloc_code_real_type r_type;
4422
4423 rel = (arelent *) xmalloc (sizeof (arelent));
4424 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4425 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4426 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
4427
4428 r_type = fixp->fx_r_type;
4429
4430 if (SWITCH_TABLE (fixp))
4431 {
4432 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
4433 rel->addend = 0;
4434 if (r_type == BFD_RELOC_16)
4435 r_type = BFD_RELOC_SH_SWITCH16;
4436 else if (r_type == BFD_RELOC_8)
4437 r_type = BFD_RELOC_8_PCREL;
4438 else if (r_type == BFD_RELOC_32)
4439 r_type = BFD_RELOC_SH_SWITCH32;
4440 else
4441 abort ();
4442 }
4443 else if (r_type == BFD_RELOC_SH_USES)
4444 rel->addend = fixp->fx_addnumber;
4445 else if (r_type == BFD_RELOC_SH_COUNT)
4446 rel->addend = fixp->fx_offset;
4447 else if (r_type == BFD_RELOC_SH_ALIGN)
4448 rel->addend = fixp->fx_offset;
4449 else if (r_type == BFD_RELOC_VTABLE_INHERIT
4450 || r_type == BFD_RELOC_VTABLE_ENTRY)
4451 rel->addend = fixp->fx_offset;
4452 else if (r_type == BFD_RELOC_SH_LOOP_START
4453 || r_type == BFD_RELOC_SH_LOOP_END)
4454 rel->addend = fixp->fx_offset;
4455 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
4456 {
4457 rel->addend = 0;
4458 rel->address = rel->addend = fixp->fx_offset;
4459 }
4460 #ifdef HAVE_SH64
4461 else if (shmedia_init_reloc (rel, fixp))
4462 ;
4463 #endif
4464 else
4465 rel->addend = fixp->fx_addnumber;
4466
4467 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
4468
4469 if (rel->howto == NULL)
4470 {
4471 as_bad_where (fixp->fx_file, fixp->fx_line,
4472 _("Cannot represent relocation type %s"),
4473 bfd_get_reloc_code_name (r_type));
4474 /* Set howto to a garbage value so that we can keep going. */
4475 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
4476 gas_assert (rel->howto != NULL);
4477 }
4478 #ifdef OBJ_ELF
4479 else if (rel->howto->type == R_SH_IND12W)
4480 rel->addend += fixp->fx_offset - 4;
4481 #endif
4482
4483 return rel;
4484 }
4485
4486 #ifdef OBJ_ELF
4487 inline static char *
4488 sh_end_of_match (char *cont, char *what)
4489 {
4490 int len = strlen (what);
4491
4492 if (strncasecmp (cont, what, strlen (what)) == 0
4493 && ! is_part_of_name (cont[len]))
4494 return cont + len;
4495
4496 return NULL;
4497 }
4498
4499 int
4500 sh_parse_name (char const *name,
4501 expressionS *exprP,
4502 enum expr_mode mode,
4503 char *nextcharP)
4504 {
4505 char *next = input_line_pointer;
4506 char *next_end;
4507 int reloc_type;
4508 segT segment;
4509
4510 exprP->X_op_symbol = NULL;
4511
4512 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
4513 {
4514 if (! GOT_symbol)
4515 GOT_symbol = symbol_find_or_make (name);
4516
4517 exprP->X_add_symbol = GOT_symbol;
4518 no_suffix:
4519 /* If we have an absolute symbol or a reg, then we know its
4520 value now. */
4521 segment = S_GET_SEGMENT (exprP->X_add_symbol);
4522 if (mode != expr_defer && segment == absolute_section)
4523 {
4524 exprP->X_op = O_constant;
4525 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4526 exprP->X_add_symbol = NULL;
4527 }
4528 else if (mode != expr_defer && segment == reg_section)
4529 {
4530 exprP->X_op = O_register;
4531 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4532 exprP->X_add_symbol = NULL;
4533 }
4534 else
4535 {
4536 exprP->X_op = O_symbol;
4537 exprP->X_add_number = 0;
4538 }
4539
4540 return 1;
4541 }
4542
4543 exprP->X_add_symbol = symbol_find_or_make (name);
4544
4545 if (*nextcharP != '@')
4546 goto no_suffix;
4547 else if ((next_end = sh_end_of_match (next + 1, "GOTOFF")))
4548 reloc_type = BFD_RELOC_32_GOTOFF;
4549 else if ((next_end = sh_end_of_match (next + 1, "GOTPLT")))
4550 reloc_type = BFD_RELOC_SH_GOTPLT32;
4551 else if ((next_end = sh_end_of_match (next + 1, "GOT")))
4552 reloc_type = BFD_RELOC_32_GOT_PCREL;
4553 else if ((next_end = sh_end_of_match (next + 1, "PLT")))
4554 reloc_type = BFD_RELOC_32_PLT_PCREL;
4555 else if ((next_end = sh_end_of_match (next + 1, "TLSGD")))
4556 reloc_type = BFD_RELOC_SH_TLS_GD_32;
4557 else if ((next_end = sh_end_of_match (next + 1, "TLSLDM")))
4558 reloc_type = BFD_RELOC_SH_TLS_LD_32;
4559 else if ((next_end = sh_end_of_match (next + 1, "GOTTPOFF")))
4560 reloc_type = BFD_RELOC_SH_TLS_IE_32;
4561 else if ((next_end = sh_end_of_match (next + 1, "TPOFF")))
4562 reloc_type = BFD_RELOC_SH_TLS_LE_32;
4563 else if ((next_end = sh_end_of_match (next + 1, "DTPOFF")))
4564 reloc_type = BFD_RELOC_SH_TLS_LDO_32;
4565 else if ((next_end = sh_end_of_match (next + 1, "PCREL")))
4566 reloc_type = BFD_RELOC_32_PCREL;
4567 else if ((next_end = sh_end_of_match (next + 1, "GOTFUNCDESC")))
4568 reloc_type = BFD_RELOC_SH_GOTFUNCDESC;
4569 else if ((next_end = sh_end_of_match (next + 1, "GOTOFFFUNCDESC")))
4570 reloc_type = BFD_RELOC_SH_GOTOFFFUNCDESC;
4571 else if ((next_end = sh_end_of_match (next + 1, "FUNCDESC")))
4572 reloc_type = BFD_RELOC_SH_FUNCDESC;
4573 else
4574 goto no_suffix;
4575
4576 *input_line_pointer = *nextcharP;
4577 input_line_pointer = next_end;
4578 *nextcharP = *input_line_pointer;
4579 *input_line_pointer = '\0';
4580
4581 exprP->X_op = O_PIC_reloc;
4582 exprP->X_add_number = 0;
4583 exprP->X_md = reloc_type;
4584
4585 return 1;
4586 }
4587
4588 void
4589 sh_cfi_frame_initial_instructions (void)
4590 {
4591 cfi_add_CFA_def_cfa (15, 0);
4592 }
4593
4594 int
4595 sh_regname_to_dw2regnum (char *regname)
4596 {
4597 unsigned int regnum = -1;
4598 unsigned int i;
4599 const char *p;
4600 char *q;
4601 static struct { char *name; int dw2regnum; } regnames[] =
4602 {
4603 { "pr", 17 }, { "t", 18 }, { "gbr", 19 }, { "mach", 20 },
4604 { "macl", 21 }, { "fpul", 23 }
4605 };
4606
4607 for (i = 0; i < ARRAY_SIZE (regnames); ++i)
4608 if (strcmp (regnames[i].name, regname) == 0)
4609 return regnames[i].dw2regnum;
4610
4611 if (regname[0] == 'r')
4612 {
4613 p = regname + 1;
4614 regnum = strtoul (p, &q, 10);
4615 if (p == q || *q || regnum >= 16)
4616 return -1;
4617 }
4618 else if (regname[0] == 'f' && regname[1] == 'r')
4619 {
4620 p = regname + 2;
4621 regnum = strtoul (p, &q, 10);
4622 if (p == q || *q || regnum >= 16)
4623 return -1;
4624 regnum += 25;
4625 }
4626 else if (regname[0] == 'x' && regname[1] == 'd')
4627 {
4628 p = regname + 2;
4629 regnum = strtoul (p, &q, 10);
4630 if (p == q || *q || regnum >= 8)
4631 return -1;
4632 regnum += 87;
4633 }
4634 return regnum;
4635 }
4636 #endif /* OBJ_ELF */
4637