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