2010-05-21 Daniel Jacobowitz <dan@codesourcery.com>
[binutils.git] / gas / config / tc-sh.c
blob4e49e4e8ccd185e5d5398ba0626831239abc3d7f
1 /* tc-sh.c -- Assemble code for the Renesas / SuperH SH
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* Written By Steve Chamberlain <sac@cygnus.com> */
25 #include "as.h"
26 #include "subsegs.h"
27 #define DEFINE_TABLE
28 #include "opcodes/sh-opc.h"
29 #include "safe-ctype.h"
30 #include "struc-symbol.h"
32 #ifdef OBJ_ELF
33 #include "elf/sh.h"
34 #endif
36 #include "dwarf2dbg.h"
37 #include "dw2gencfi.h"
39 typedef struct
41 sh_arg_type type;
42 int reg;
43 expressionS immediate;
45 sh_operand_info;
47 const char comment_chars[] = "!";
48 const char line_separator_chars[] = ";";
49 const char line_comment_chars[] = "!#";
51 static void s_uses (int);
52 static void s_uacons (int);
54 #ifdef OBJ_ELF
55 static void sh_elf_cons (int);
57 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
58 #endif
60 static void
61 big (int ignore ATTRIBUTE_UNUSED)
63 if (! target_big_endian)
64 as_bad (_("directive .big encountered when option -big required"));
66 /* Stop further messages. */
67 target_big_endian = 1;
70 static void
71 little (int ignore ATTRIBUTE_UNUSED)
73 if (target_big_endian)
74 as_bad (_("directive .little encountered when option -little required"));
76 /* Stop further messages. */
77 target_big_endian = 0;
80 /* This table describes all the machine specific pseudo-ops the assembler
81 has to support. The fields are:
82 pseudo-op name without dot
83 function to call to execute this pseudo-op
84 Integer arg to pass to the function. */
86 const pseudo_typeS md_pseudo_table[] =
88 #ifdef OBJ_ELF
89 {"long", sh_elf_cons, 4},
90 {"int", sh_elf_cons, 4},
91 {"word", sh_elf_cons, 2},
92 {"short", sh_elf_cons, 2},
93 #else
94 {"int", cons, 4},
95 {"word", cons, 2},
96 #endif /* OBJ_ELF */
97 {"big", big, 0},
98 {"form", listing_psize, 0},
99 {"little", little, 0},
100 {"heading", listing_title, 0},
101 {"import", s_ignore, 0},
102 {"page", listing_eject, 0},
103 {"program", s_ignore, 0},
104 {"uses", s_uses, 0},
105 {"uaword", s_uacons, 2},
106 {"ualong", s_uacons, 4},
107 {"uaquad", s_uacons, 8},
108 {"2byte", s_uacons, 2},
109 {"4byte", s_uacons, 4},
110 {"8byte", s_uacons, 8},
111 #ifdef HAVE_SH64
112 {"mode", s_sh64_mode, 0 },
114 /* Have the old name too. */
115 {"isa", s_sh64_mode, 0 },
117 /* Assert that the right ABI is used. */
118 {"abi", s_sh64_abi, 0 },
120 { "vtable_inherit", sh64_vtable_inherit, 0 },
121 { "vtable_entry", sh64_vtable_entry, 0 },
122 #endif /* HAVE_SH64 */
123 {0, 0, 0}
126 int sh_relax; /* set if -relax seen */
128 /* Whether -small was seen. */
130 int sh_small;
132 /* Flag to generate relocations against symbol values for local symbols. */
134 static int dont_adjust_reloc_32;
136 /* Flag to indicate that '$' is allowed as a register prefix. */
138 static int allow_dollar_register_prefix;
140 /* Preset architecture set, if given; zero otherwise. */
142 static unsigned int preset_target_arch;
144 /* The bit mask of architectures that could
145 accommodate the insns seen so far. */
146 static unsigned int valid_arch;
148 /* Whether --fdpic was given. */
149 static int sh_fdpic;
151 const char EXP_CHARS[] = "eE";
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";
158 #define C(a,b) ENCODE_RELAX(a,b)
160 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
161 #define GET_WHAT(x) ((x>>4))
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
170 #ifdef HAVE_SH64
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
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
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
189 #else /* HAVE_SH64 */
191 #define END 4
193 #endif /* HAVE_SH64 */
195 #define UNDEF_DISP 0
196 #define COND8 1
197 #define COND12 2
198 #define COND32 3
199 #define UNDEF_WORD_DISP 4
201 #define UNCOND12 1
202 #define UNCOND32 2
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
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 */
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
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
234 #define COND12_DELAY_LENGTH 4
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
242 #define UNCOND12_F 4098
243 #define UNCOND12_M -4092
244 #define UNCOND12_LENGTH 2
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
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
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
270 PTREL R25,TRd
271 which means two extra insns, 8 extra bytes. This is the limit for the
272 32-bit ABI.
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)
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 */
298 /* And similarly for the 64-bit expansion; a MOVI + SHORI + SHORI + SHORI
299 + PTREL sequence. */
300 #define SH64PCREL64_LENGTH (4 * 4)
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
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 */
328 #define MOVI_64_LENGTH 12
329 #endif /* HAVE_SH64 */
331 #define EMPTY { 0, 0, 0, 0 }
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
481 #endif /* HAVE_SH64 */
485 #undef EMPTY
487 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
490 #ifdef OBJ_ELF
491 /* Determinet whether the symbol needs any kind of PIC relocation. */
493 inline static int
494 sh_PIC_related_p (symbolS *sym)
496 expressionS *exp;
498 if (! sym)
499 return 0;
501 if (sym == GOT_symbol)
502 return 1;
504 #ifdef HAVE_SH64
505 if (sh_PIC_related_p (*symbol_get_tc (sym)))
506 return 1;
507 #endif
509 exp = symbol_get_value_expression (sym);
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));
516 /* Determine the relocation type to be used to represent the
517 expression, that may be rearranged. */
519 static int
520 sh_check_fixup (expressionS *main_exp, bfd_reloc_code_real_type *r_type_p)
522 expressionS *exp = main_exp;
524 /* This is here for backward-compatibility only. GCC used to generated:
526 f@PLT + . - (.LPCS# + 2)
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:
531 O_subtract (O_add (f@PLT, .), .LPCS#+2)
533 so we attempt to transform this into:
535 O_subtract (f@PLT, O_subtract (.LPCS#+2, .))
537 which we can handle simply below. */
538 if (exp->X_op == O_subtract)
540 if (sh_PIC_related_p (exp->X_op_symbol))
541 return 1;
543 exp = symbol_get_value_expression (exp->X_add_symbol);
545 if (exp && sh_PIC_related_p (exp->X_op_symbol))
546 return 1;
548 if (exp && exp->X_op == O_add
549 && sh_PIC_related_p (exp->X_add_symbol))
551 symbolS *sym = exp->X_add_symbol;
553 exp->X_op = O_subtract;
554 exp->X_add_symbol = main_exp->X_op_symbol;
556 main_exp->X_op_symbol = main_exp->X_add_symbol;
557 main_exp->X_add_symbol = sym;
559 main_exp->X_add_number += exp->X_add_number;
560 exp->X_add_number = 0;
563 exp = main_exp;
565 else if (exp->X_op == O_add && sh_PIC_related_p (exp->X_op_symbol))
566 return 1;
568 if (exp->X_op == O_symbol || exp->X_op == O_add || exp->X_op == O_subtract)
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)))
576 switch (*r_type_p)
578 case BFD_RELOC_SH_IMM_LOW16:
579 *r_type_p = BFD_RELOC_SH_GOTPC_LOW16;
580 break;
582 case BFD_RELOC_SH_IMM_MEDLOW16:
583 *r_type_p = BFD_RELOC_SH_GOTPC_MEDLOW16;
584 break;
586 case BFD_RELOC_SH_IMM_MEDHI16:
587 *r_type_p = BFD_RELOC_SH_GOTPC_MEDHI16;
588 break;
590 case BFD_RELOC_SH_IMM_HI16:
591 *r_type_p = BFD_RELOC_SH_GOTPC_HI16;
592 break;
594 case BFD_RELOC_NONE:
595 case BFD_RELOC_UNUSED:
596 *r_type_p = BFD_RELOC_SH_GOTPC;
597 break;
599 default:
600 abort ();
602 return 0;
604 #else
605 if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
607 *r_type_p = BFD_RELOC_SH_GOTPC;
608 return 0;
610 #endif
611 exp = symbol_get_value_expression (exp->X_add_symbol);
612 if (! exp)
613 return 0;
616 if (exp->X_op == O_PIC_reloc)
618 switch (*r_type_p)
620 case BFD_RELOC_NONE:
621 case BFD_RELOC_UNUSED:
622 *r_type_p = exp->X_md;
623 break;
625 case BFD_RELOC_SH_DISP20:
626 switch (exp->X_md)
628 case BFD_RELOC_32_GOT_PCREL:
629 *r_type_p = BFD_RELOC_SH_GOT20;
630 break;
632 case BFD_RELOC_32_GOTOFF:
633 *r_type_p = BFD_RELOC_SH_GOTOFF20;
634 break;
636 case BFD_RELOC_SH_GOTFUNCDESC:
637 *r_type_p = BFD_RELOC_SH_GOTFUNCDESC20;
638 break;
640 case BFD_RELOC_SH_GOTOFFFUNCDESC:
641 *r_type_p = BFD_RELOC_SH_GOTOFFFUNCDESC20;
642 break;
644 default:
645 abort ();
647 break;
649 #ifdef HAVE_SH64
650 case BFD_RELOC_SH_IMM_LOW16:
651 switch (exp->X_md)
653 case BFD_RELOC_32_GOTOFF:
654 *r_type_p = BFD_RELOC_SH_GOTOFF_LOW16;
655 break;
657 case BFD_RELOC_SH_GOTPLT32:
658 *r_type_p = BFD_RELOC_SH_GOTPLT_LOW16;
659 break;
661 case BFD_RELOC_32_GOT_PCREL:
662 *r_type_p = BFD_RELOC_SH_GOT_LOW16;
663 break;
665 case BFD_RELOC_32_PLT_PCREL:
666 *r_type_p = BFD_RELOC_SH_PLT_LOW16;
667 break;
669 default:
670 abort ();
672 break;
674 case BFD_RELOC_SH_IMM_MEDLOW16:
675 switch (exp->X_md)
677 case BFD_RELOC_32_GOTOFF:
678 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDLOW16;
679 break;
681 case BFD_RELOC_SH_GOTPLT32:
682 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDLOW16;
683 break;
685 case BFD_RELOC_32_GOT_PCREL:
686 *r_type_p = BFD_RELOC_SH_GOT_MEDLOW16;
687 break;
689 case BFD_RELOC_32_PLT_PCREL:
690 *r_type_p = BFD_RELOC_SH_PLT_MEDLOW16;
691 break;
693 default:
694 abort ();
696 break;
698 case BFD_RELOC_SH_IMM_MEDHI16:
699 switch (exp->X_md)
701 case BFD_RELOC_32_GOTOFF:
702 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDHI16;
703 break;
705 case BFD_RELOC_SH_GOTPLT32:
706 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDHI16;
707 break;
709 case BFD_RELOC_32_GOT_PCREL:
710 *r_type_p = BFD_RELOC_SH_GOT_MEDHI16;
711 break;
713 case BFD_RELOC_32_PLT_PCREL:
714 *r_type_p = BFD_RELOC_SH_PLT_MEDHI16;
715 break;
717 default:
718 abort ();
720 break;
722 case BFD_RELOC_SH_IMM_HI16:
723 switch (exp->X_md)
725 case BFD_RELOC_32_GOTOFF:
726 *r_type_p = BFD_RELOC_SH_GOTOFF_HI16;
727 break;
729 case BFD_RELOC_SH_GOTPLT32:
730 *r_type_p = BFD_RELOC_SH_GOTPLT_HI16;
731 break;
733 case BFD_RELOC_32_GOT_PCREL:
734 *r_type_p = BFD_RELOC_SH_GOT_HI16;
735 break;
737 case BFD_RELOC_32_PLT_PCREL:
738 *r_type_p = BFD_RELOC_SH_PLT_HI16;
739 break;
741 default:
742 abort ();
744 break;
745 #endif
747 default:
748 abort ();
750 if (exp == main_exp)
751 exp->X_op = O_symbol;
752 else
754 main_exp->X_add_symbol = exp->X_add_symbol;
755 main_exp->X_add_number += exp->X_add_number;
758 else
759 return (sh_PIC_related_p (exp->X_add_symbol)
760 || sh_PIC_related_p (exp->X_op_symbol));
762 return 0;
765 /* Add expression EXP of SIZE bytes to offset OFF of fragment FRAG. */
767 void
768 sh_cons_fix_new (fragS *frag, int off, int size, expressionS *exp)
770 bfd_reloc_code_real_type r_type = BFD_RELOC_UNUSED;
772 if (sh_check_fixup (exp, &r_type))
773 as_bad (_("Invalid PIC expression."));
775 if (r_type == BFD_RELOC_UNUSED)
776 switch (size)
778 case 1:
779 r_type = BFD_RELOC_8;
780 break;
782 case 2:
783 r_type = BFD_RELOC_16;
784 break;
786 case 4:
787 r_type = BFD_RELOC_32;
788 break;
790 #ifdef HAVE_SH64
791 case 8:
792 r_type = BFD_RELOC_64;
793 break;
794 #endif
796 default:
797 goto error;
799 else if (size != 4)
801 error:
802 as_bad (_("unsupported BFD relocation size %u"), size);
803 r_type = BFD_RELOC_UNUSED;
806 fix_new_exp (frag, off, size, exp, 0, r_type);
809 /* The regular cons() function, that reads constants, doesn't support
810 suffixes such as @GOT, @GOTOFF and @PLT, that generate
811 machine-specific relocation types. So we must define it here. */
812 /* Clobbers input_line_pointer, checks end-of-line. */
813 /* NBYTES 1=.byte, 2=.word, 4=.long */
814 static void
815 sh_elf_cons (register int nbytes)
817 expressionS exp;
819 #ifdef HAVE_SH64
821 /* Update existing range to include a previous insn, if there was one. */
822 sh64_update_contents_mark (TRUE);
824 /* We need to make sure the contents type is set to data. */
825 sh64_flag_output ();
827 #endif /* HAVE_SH64 */
829 if (is_it_end_of_statement ())
831 demand_empty_rest_of_line ();
832 return;
835 #ifdef md_cons_align
836 md_cons_align (nbytes);
837 #endif
841 expression (&exp);
842 emit_expr (&exp, (unsigned int) nbytes);
844 while (*input_line_pointer++ == ',');
846 input_line_pointer--; /* Put terminator back into stream. */
847 if (*input_line_pointer == '#' || *input_line_pointer == '!')
849 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
851 else
852 demand_empty_rest_of_line ();
855 /* The regular frag_offset_fixed_p doesn't work for rs_align_test
856 frags. */
858 static bfd_boolean
859 align_test_frag_offset_fixed_p (const fragS *frag1, const fragS *frag2,
860 bfd_vma *offset)
862 const fragS *frag;
863 bfd_vma off;
865 /* Start with offset initialised to difference between the two frags.
866 Prior to assigning frag addresses this will be zero. */
867 off = frag1->fr_address - frag2->fr_address;
868 if (frag1 == frag2)
870 *offset = off;
871 return TRUE;
874 /* Maybe frag2 is after frag1. */
875 frag = frag1;
876 while (frag->fr_type == rs_fill
877 || frag->fr_type == rs_align_test)
879 if (frag->fr_type == rs_fill)
880 off += frag->fr_fix + frag->fr_offset * frag->fr_var;
881 else
882 off += frag->fr_fix;
883 frag = frag->fr_next;
884 if (frag == NULL)
885 break;
886 if (frag == frag2)
888 *offset = off;
889 return TRUE;
893 /* Maybe frag1 is after frag2. */
894 off = frag1->fr_address - frag2->fr_address;
895 frag = frag2;
896 while (frag->fr_type == rs_fill
897 || frag->fr_type == rs_align_test)
899 if (frag->fr_type == rs_fill)
900 off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
901 else
902 off -= frag->fr_fix;
903 frag = frag->fr_next;
904 if (frag == NULL)
905 break;
906 if (frag == frag1)
908 *offset = off;
909 return TRUE;
913 return FALSE;
916 /* Optimize a difference of symbols which have rs_align_test frag if
917 possible. */
920 sh_optimize_expr (expressionS *l, operatorT op, expressionS *r)
922 bfd_vma frag_off;
924 if (op == O_subtract
925 && l->X_op == O_symbol
926 && r->X_op == O_symbol
927 && S_GET_SEGMENT (l->X_add_symbol) == S_GET_SEGMENT (r->X_add_symbol)
928 && (SEG_NORMAL (S_GET_SEGMENT (l->X_add_symbol))
929 || r->X_add_symbol == l->X_add_symbol)
930 && align_test_frag_offset_fixed_p (symbol_get_frag (l->X_add_symbol),
931 symbol_get_frag (r->X_add_symbol),
932 &frag_off))
934 l->X_add_number -= r->X_add_number;
935 l->X_add_number -= frag_off / OCTETS_PER_BYTE;
936 l->X_add_number += (S_GET_VALUE (l->X_add_symbol)
937 - S_GET_VALUE (r->X_add_symbol));
938 l->X_op = O_constant;
939 l->X_add_symbol = 0;
940 return 1;
942 return 0;
944 #endif /* OBJ_ELF */
946 /* This function is called once, at assembler startup time. This should
947 set up all the tables, etc that the MD part of the assembler needs. */
949 void
950 md_begin (void)
952 const sh_opcode_info *opcode;
953 char *prev_name = "";
954 unsigned int target_arch;
956 target_arch
957 = preset_target_arch ? preset_target_arch : arch_sh_up & ~arch_sh_has_dsp;
958 valid_arch = target_arch;
960 #ifdef HAVE_SH64
961 shmedia_md_begin ();
962 #endif
964 opcode_hash_control = hash_new ();
966 /* Insert unique names into hash table. */
967 for (opcode = sh_table; opcode->name; opcode++)
969 if (strcmp (prev_name, opcode->name) != 0)
971 if (!SH_MERGE_ARCH_SET_VALID (opcode->arch, target_arch))
972 continue;
973 prev_name = opcode->name;
974 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
979 static int reg_m;
980 static int reg_n;
981 static int reg_x, reg_y;
982 static int reg_efg;
983 static int reg_b;
985 #define IDENT_CHAR(c) (ISALNUM (c) || (c) == '_')
987 /* Try to parse a reg name. Return the number of chars consumed. */
989 static unsigned int
990 parse_reg_without_prefix (char *src, int *mode, int *reg)
992 char l0 = TOLOWER (src[0]);
993 char l1 = l0 ? TOLOWER (src[1]) : 0;
995 /* We use ! IDENT_CHAR for the next character after the register name, to
996 make sure that we won't accidentally recognize a symbol name such as
997 'sram' or sr_ram as being a reference to the register 'sr'. */
999 if (l0 == 'r')
1001 if (l1 == '1')
1003 if (src[2] >= '0' && src[2] <= '5'
1004 && ! IDENT_CHAR ((unsigned char) src[3]))
1006 *mode = A_REG_N;
1007 *reg = 10 + src[2] - '0';
1008 return 3;
1011 if (l1 >= '0' && l1 <= '9'
1012 && ! IDENT_CHAR ((unsigned char) src[2]))
1014 *mode = A_REG_N;
1015 *reg = (l1 - '0');
1016 return 2;
1018 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
1019 && ! IDENT_CHAR ((unsigned char) src[7]))
1021 *mode = A_REG_B;
1022 *reg = (l1 - '0');
1023 return 7;
1026 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
1028 *mode = A_RE;
1029 return 2;
1031 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
1033 *mode = A_RS;
1034 return 2;
1038 if (l0 == 'a')
1040 if (l1 == '0')
1042 if (! IDENT_CHAR ((unsigned char) src[2]))
1044 *mode = DSP_REG_N;
1045 *reg = A_A0_NUM;
1046 return 2;
1048 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
1050 *mode = DSP_REG_N;
1051 *reg = A_A0G_NUM;
1052 return 3;
1055 if (l1 == '1')
1057 if (! IDENT_CHAR ((unsigned char) src[2]))
1059 *mode = DSP_REG_N;
1060 *reg = A_A1_NUM;
1061 return 2;
1063 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
1065 *mode = DSP_REG_N;
1066 *reg = A_A1G_NUM;
1067 return 3;
1071 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
1072 && ! IDENT_CHAR ((unsigned char) src[3]))
1074 *mode = A_REG_N;
1075 *reg = 4 + (l1 - '0');
1076 return 3;
1078 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
1079 && ! IDENT_CHAR ((unsigned char) src[3]))
1081 *mode = A_REG_N;
1082 *reg = 6 + (l1 - '0');
1083 return 3;
1085 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
1086 && ! IDENT_CHAR ((unsigned char) src[3]))
1088 int n = l1 - '0';
1090 *mode = A_REG_N;
1091 *reg = n | ((~n & 2) << 1);
1092 return 3;
1096 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[2]))
1098 if (l1 == 's')
1100 *mode = A_REG_N;
1101 *reg = 8;
1102 return 2;
1104 if (l1 == 'x')
1106 *mode = A_REG_N;
1107 *reg = 8;
1108 return 2;
1110 if (l1 == 'y')
1112 *mode = A_REG_N;
1113 *reg = 9;
1114 return 2;
1118 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
1119 && ! IDENT_CHAR ((unsigned char) src[2]))
1121 *mode = DSP_REG_N;
1122 *reg = A_X0_NUM + l1 - '0';
1123 return 2;
1126 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
1127 && ! IDENT_CHAR ((unsigned char) src[2]))
1129 *mode = DSP_REG_N;
1130 *reg = A_Y0_NUM + l1 - '0';
1131 return 2;
1134 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
1135 && ! IDENT_CHAR ((unsigned char) src[2]))
1137 *mode = DSP_REG_N;
1138 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
1139 return 2;
1142 if (l0 == 's'
1143 && l1 == 's'
1144 && TOLOWER (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
1146 *mode = A_SSR;
1147 return 3;
1150 if (l0 == 's' && l1 == 'p' && TOLOWER (src[2]) == 'c'
1151 && ! IDENT_CHAR ((unsigned char) src[3]))
1153 *mode = A_SPC;
1154 return 3;
1157 if (l0 == 's' && l1 == 'g' && TOLOWER (src[2]) == 'r'
1158 && ! IDENT_CHAR ((unsigned char) src[3]))
1160 *mode = A_SGR;
1161 return 3;
1164 if (l0 == 'd' && l1 == 's' && TOLOWER (src[2]) == 'r'
1165 && ! IDENT_CHAR ((unsigned char) src[3]))
1167 *mode = A_DSR;
1168 return 3;
1171 if (l0 == 'd' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1172 && ! IDENT_CHAR ((unsigned char) src[3]))
1174 *mode = A_DBR;
1175 return 3;
1178 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
1180 *mode = A_SR;
1181 return 2;
1184 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
1186 *mode = A_REG_N;
1187 *reg = 15;
1188 return 2;
1191 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
1193 *mode = A_PR;
1194 return 2;
1196 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
1198 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
1199 and use an uninitialized immediate. */
1200 *mode = A_PC;
1201 return 2;
1203 if (l0 == 'g' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1204 && ! IDENT_CHAR ((unsigned char) src[3]))
1206 *mode = A_GBR;
1207 return 3;
1209 if (l0 == 'v' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1210 && ! IDENT_CHAR ((unsigned char) src[3]))
1212 *mode = A_VBR;
1213 return 3;
1216 if (l0 == 't' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1217 && ! IDENT_CHAR ((unsigned char) src[3]))
1219 *mode = A_TBR;
1220 return 3;
1222 if (l0 == 'm' && l1 == 'a' && TOLOWER (src[2]) == 'c'
1223 && ! IDENT_CHAR ((unsigned char) src[4]))
1225 if (TOLOWER (src[3]) == 'l')
1227 *mode = A_MACL;
1228 return 4;
1230 if (TOLOWER (src[3]) == 'h')
1232 *mode = A_MACH;
1233 return 4;
1236 if (l0 == 'm' && l1 == 'o' && TOLOWER (src[2]) == 'd'
1237 && ! IDENT_CHAR ((unsigned char) src[3]))
1239 *mode = A_MOD;
1240 return 3;
1242 if (l0 == 'f' && l1 == 'r')
1244 if (src[2] == '1')
1246 if (src[3] >= '0' && src[3] <= '5'
1247 && ! IDENT_CHAR ((unsigned char) src[4]))
1249 *mode = F_REG_N;
1250 *reg = 10 + src[3] - '0';
1251 return 4;
1254 if (src[2] >= '0' && src[2] <= '9'
1255 && ! IDENT_CHAR ((unsigned char) src[3]))
1257 *mode = F_REG_N;
1258 *reg = (src[2] - '0');
1259 return 3;
1262 if (l0 == 'd' && l1 == 'r')
1264 if (src[2] == '1')
1266 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
1267 && ! IDENT_CHAR ((unsigned char) src[4]))
1269 *mode = D_REG_N;
1270 *reg = 10 + src[3] - '0';
1271 return 4;
1274 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
1275 && ! IDENT_CHAR ((unsigned char) src[3]))
1277 *mode = D_REG_N;
1278 *reg = (src[2] - '0');
1279 return 3;
1282 if (l0 == 'x' && l1 == 'd')
1284 if (src[2] == '1')
1286 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
1287 && ! IDENT_CHAR ((unsigned char) src[4]))
1289 *mode = X_REG_N;
1290 *reg = 11 + src[3] - '0';
1291 return 4;
1294 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
1295 && ! IDENT_CHAR ((unsigned char) src[3]))
1297 *mode = X_REG_N;
1298 *reg = (src[2] - '0') + 1;
1299 return 3;
1302 if (l0 == 'f' && l1 == 'v')
1304 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
1306 *mode = V_REG_N;
1307 *reg = 12;
1308 return 4;
1310 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
1311 && ! IDENT_CHAR ((unsigned char) src[3]))
1313 *mode = V_REG_N;
1314 *reg = (src[2] - '0');
1315 return 3;
1318 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 'u'
1319 && TOLOWER (src[3]) == 'l'
1320 && ! IDENT_CHAR ((unsigned char) src[4]))
1322 *mode = FPUL_N;
1323 return 4;
1326 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 's'
1327 && TOLOWER (src[3]) == 'c'
1328 && TOLOWER (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
1330 *mode = FPSCR_N;
1331 return 5;
1334 if (l0 == 'x' && l1 == 'm' && TOLOWER (src[2]) == 't'
1335 && TOLOWER (src[3]) == 'r'
1336 && TOLOWER (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
1338 *mode = XMTRX_M4;
1339 return 5;
1342 return 0;
1345 /* Like parse_reg_without_prefix, but this version supports
1346 $-prefixed register names if enabled by the user. */
1348 static unsigned int
1349 parse_reg (char *src, int *mode, int *reg)
1351 unsigned int prefix;
1352 unsigned int consumed;
1354 if (src[0] == '$')
1356 if (allow_dollar_register_prefix)
1358 src ++;
1359 prefix = 1;
1361 else
1362 return 0;
1364 else
1365 prefix = 0;
1367 consumed = parse_reg_without_prefix (src, mode, reg);
1369 if (consumed == 0)
1370 return 0;
1372 return consumed + prefix;
1375 static char *
1376 parse_exp (char *s, sh_operand_info *op)
1378 char *save;
1379 char *new_pointer;
1381 save = input_line_pointer;
1382 input_line_pointer = s;
1383 expression (&op->immediate);
1384 if (op->immediate.X_op == O_absent)
1385 as_bad (_("missing operand"));
1386 new_pointer = input_line_pointer;
1387 input_line_pointer = save;
1388 return new_pointer;
1391 /* The many forms of operand:
1393 Rn Register direct
1394 @Rn Register indirect
1395 @Rn+ Autoincrement
1396 @-Rn Autodecrement
1397 @(disp:4,Rn)
1398 @(disp:8,GBR)
1399 @(disp:8,PC)
1401 @(R0,Rn)
1402 @(R0,GBR)
1404 disp:8
1405 disp:12
1406 #imm8
1407 pr, gbr, vbr, macl, mach
1410 static char *
1411 parse_at (char *src, sh_operand_info *op)
1413 int len;
1414 int mode;
1415 src++;
1416 if (src[0] == '@')
1418 src = parse_at (src, op);
1419 if (op->type == A_DISP_TBR)
1420 op->type = A_DISP2_TBR;
1421 else
1422 as_bad (_("illegal double indirection"));
1424 else if (src[0] == '-')
1426 /* Must be predecrement. */
1427 src++;
1429 len = parse_reg (src, &mode, &(op->reg));
1430 if (mode != A_REG_N)
1431 as_bad (_("illegal register after @-"));
1433 op->type = A_DEC_N;
1434 src += len;
1436 else if (src[0] == '(')
1438 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
1439 @(r0, rn). */
1440 src++;
1441 len = parse_reg (src, &mode, &(op->reg));
1442 if (len && mode == A_REG_N)
1444 src += len;
1445 if (op->reg != 0)
1447 as_bad (_("must be @(r0,...)"));
1449 if (src[0] == ',')
1451 src++;
1452 /* Now can be rn or gbr. */
1453 len = parse_reg (src, &mode, &(op->reg));
1455 else
1457 len = 0;
1459 if (len)
1461 if (mode == A_GBR)
1463 op->type = A_R0_GBR;
1465 else if (mode == A_REG_N)
1467 op->type = A_IND_R0_REG_N;
1469 else
1471 as_bad (_("syntax error in @(r0,...)"));
1474 else
1476 as_bad (_("syntax error in @(r0...)"));
1479 else
1481 /* Must be an @(disp,.. thing). */
1482 src = parse_exp (src, op);
1483 if (src[0] == ',')
1484 src++;
1485 /* Now can be rn, gbr or pc. */
1486 len = parse_reg (src, &mode, &op->reg);
1487 if (len)
1489 if (mode == A_REG_N)
1491 op->type = A_DISP_REG_N;
1493 else if (mode == A_GBR)
1495 op->type = A_DISP_GBR;
1497 else if (mode == A_TBR)
1499 op->type = A_DISP_TBR;
1501 else if (mode == A_PC)
1503 /* We want @(expr, pc) to uniformly address . + expr,
1504 no matter if expr is a constant, or a more complex
1505 expression, e.g. sym-. or sym1-sym2.
1506 However, we also used to accept @(sym,pc)
1507 as addressing sym, i.e. meaning the same as plain sym.
1508 Some existing code does use the @(sym,pc) syntax, so
1509 we give it the old semantics for now, but warn about
1510 its use, so that users have some time to fix their code.
1512 Note that due to this backward compatibility hack,
1513 we'll get unexpected results when @(offset, pc) is used,
1514 and offset is a symbol that is set later to an an address
1515 difference, or an external symbol that is set to an
1516 address difference in another source file, so we want to
1517 eventually remove it. */
1518 if (op->immediate.X_op == O_symbol)
1520 op->type = A_DISP_PC;
1521 as_warn (_("Deprecated syntax."));
1523 else
1525 op->type = A_DISP_PC_ABS;
1526 /* Such operands don't get corrected for PC==.+4, so
1527 make the correction here. */
1528 op->immediate.X_add_number -= 4;
1531 else
1533 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1536 else
1538 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1541 src += len;
1542 if (src[0] != ')')
1543 as_bad (_("expecting )"));
1544 else
1545 src++;
1547 else
1549 src += parse_reg (src, &mode, &(op->reg));
1550 if (mode != A_REG_N)
1551 as_bad (_("illegal register after @"));
1553 if (src[0] == '+')
1555 char l0, l1;
1557 src++;
1558 l0 = TOLOWER (src[0]);
1559 l1 = TOLOWER (src[1]);
1561 if ((l0 == 'r' && l1 == '8')
1562 || (l0 == 'i' && (l1 == 'x' || l1 == 's')))
1564 src += 2;
1565 op->type = AX_PMOD_N;
1567 else if ( (l0 == 'r' && l1 == '9')
1568 || (l0 == 'i' && l1 == 'y'))
1570 src += 2;
1571 op->type = AY_PMOD_N;
1573 else
1574 op->type = A_INC_N;
1576 else
1577 op->type = A_IND_N;
1579 return src;
1582 static void
1583 get_operand (char **ptr, sh_operand_info *op)
1585 char *src = *ptr;
1586 int mode = -1;
1587 unsigned int len;
1589 if (src[0] == '#')
1591 src++;
1592 *ptr = parse_exp (src, op);
1593 op->type = A_IMM;
1594 return;
1597 else if (src[0] == '@')
1599 *ptr = parse_at (src, op);
1600 return;
1602 len = parse_reg (src, &mode, &(op->reg));
1603 if (len)
1605 *ptr = src + len;
1606 op->type = mode;
1607 return;
1609 else
1611 /* Not a reg, the only thing left is a displacement. */
1612 *ptr = parse_exp (src, op);
1613 op->type = A_DISP_PC;
1614 return;
1618 static char *
1619 get_operands (sh_opcode_info *info, char *args, sh_operand_info *operand)
1621 char *ptr = args;
1622 if (info->arg[0])
1624 /* The pre-processor will eliminate whitespace in front of '@'
1625 after the first argument; we may be called multiple times
1626 from assemble_ppi, so don't insist on finding whitespace here. */
1627 if (*ptr == ' ')
1628 ptr++;
1630 get_operand (&ptr, operand + 0);
1631 if (info->arg[1])
1633 if (*ptr == ',')
1635 ptr++;
1637 get_operand (&ptr, operand + 1);
1638 /* ??? Hack: psha/pshl have a varying operand number depending on
1639 the type of the first operand. We handle this by having the
1640 three-operand version first and reducing the number of operands
1641 parsed to two if we see that the first operand is an immediate.
1642 This works because no insn with three operands has an immediate
1643 as first operand. */
1644 if (info->arg[2] && operand[0].type != A_IMM)
1646 if (*ptr == ',')
1648 ptr++;
1650 get_operand (&ptr, operand + 2);
1652 else
1654 operand[2].type = 0;
1657 else
1659 operand[1].type = 0;
1660 operand[2].type = 0;
1663 else
1665 operand[0].type = 0;
1666 operand[1].type = 0;
1667 operand[2].type = 0;
1669 return ptr;
1672 /* Passed a pointer to a list of opcodes which use different
1673 addressing modes, return the opcode which matches the opcodes
1674 provided. */
1676 static sh_opcode_info *
1677 get_specific (sh_opcode_info *opcode, sh_operand_info *operands)
1679 sh_opcode_info *this_try = opcode;
1680 char *name = opcode->name;
1681 int n = 0;
1683 while (opcode->name)
1685 this_try = opcode++;
1686 if ((this_try->name != name) && (strcmp (this_try->name, name) != 0))
1688 /* We've looked so far down the table that we've run out of
1689 opcodes with the same name. */
1690 return 0;
1693 /* Look at both operands needed by the opcodes and provided by
1694 the user - since an arg test will often fail on the same arg
1695 again and again, we'll try and test the last failing arg the
1696 first on each opcode try. */
1697 for (n = 0; this_try->arg[n]; n++)
1699 sh_operand_info *user = operands + n;
1700 sh_arg_type arg = this_try->arg[n];
1702 switch (arg)
1704 case A_DISP_PC:
1705 if (user->type == A_DISP_PC_ABS)
1706 break;
1707 /* Fall through. */
1708 case A_IMM:
1709 case A_BDISP12:
1710 case A_BDISP8:
1711 case A_DISP_GBR:
1712 case A_DISP2_TBR:
1713 case A_MACH:
1714 case A_PR:
1715 case A_MACL:
1716 if (user->type != arg)
1717 goto fail;
1718 break;
1719 case A_R0:
1720 /* opcode needs r0 */
1721 if (user->type != A_REG_N || user->reg != 0)
1722 goto fail;
1723 break;
1724 case A_R0_GBR:
1725 if (user->type != A_R0_GBR || user->reg != 0)
1726 goto fail;
1727 break;
1728 case F_FR0:
1729 if (user->type != F_REG_N || user->reg != 0)
1730 goto fail;
1731 break;
1733 case A_REG_N:
1734 case A_INC_N:
1735 case A_DEC_N:
1736 case A_IND_N:
1737 case A_IND_R0_REG_N:
1738 case A_DISP_REG_N:
1739 case F_REG_N:
1740 case D_REG_N:
1741 case X_REG_N:
1742 case V_REG_N:
1743 case FPUL_N:
1744 case FPSCR_N:
1745 case DSP_REG_N:
1746 /* Opcode needs rn */
1747 if (user->type != arg)
1748 goto fail;
1749 reg_n = user->reg;
1750 break;
1751 case DX_REG_N:
1752 if (user->type != D_REG_N && user->type != X_REG_N)
1753 goto fail;
1754 reg_n = user->reg;
1755 break;
1756 case A_GBR:
1757 case A_TBR:
1758 case A_SR:
1759 case A_VBR:
1760 case A_DSR:
1761 case A_MOD:
1762 case A_RE:
1763 case A_RS:
1764 case A_SSR:
1765 case A_SPC:
1766 case A_SGR:
1767 case A_DBR:
1768 if (user->type != arg)
1769 goto fail;
1770 break;
1772 case A_REG_B:
1773 if (user->type != arg)
1774 goto fail;
1775 reg_b = user->reg;
1776 break;
1778 case A_INC_R15:
1779 if (user->type != A_INC_N)
1780 goto fail;
1781 if (user->reg != 15)
1782 goto fail;
1783 reg_n = user->reg;
1784 break;
1786 case A_DEC_R15:
1787 if (user->type != A_DEC_N)
1788 goto fail;
1789 if (user->reg != 15)
1790 goto fail;
1791 reg_n = user->reg;
1792 break;
1794 case A_REG_M:
1795 case A_INC_M:
1796 case A_DEC_M:
1797 case A_IND_M:
1798 case A_IND_R0_REG_M:
1799 case A_DISP_REG_M:
1800 case DSP_REG_M:
1801 /* Opcode needs rn */
1802 if (user->type != arg - A_REG_M + A_REG_N)
1803 goto fail;
1804 reg_m = user->reg;
1805 break;
1807 case AS_DEC_N:
1808 if (user->type != A_DEC_N)
1809 goto fail;
1810 if (user->reg < 2 || user->reg > 5)
1811 goto fail;
1812 reg_n = user->reg;
1813 break;
1815 case AS_INC_N:
1816 if (user->type != A_INC_N)
1817 goto fail;
1818 if (user->reg < 2 || user->reg > 5)
1819 goto fail;
1820 reg_n = user->reg;
1821 break;
1823 case AS_IND_N:
1824 if (user->type != A_IND_N)
1825 goto fail;
1826 if (user->reg < 2 || user->reg > 5)
1827 goto fail;
1828 reg_n = user->reg;
1829 break;
1831 case AS_PMOD_N:
1832 if (user->type != AX_PMOD_N)
1833 goto fail;
1834 if (user->reg < 2 || user->reg > 5)
1835 goto fail;
1836 reg_n = user->reg;
1837 break;
1839 case AX_INC_N:
1840 if (user->type != A_INC_N)
1841 goto fail;
1842 if (user->reg < 4 || user->reg > 5)
1843 goto fail;
1844 reg_n = user->reg;
1845 break;
1847 case AX_IND_N:
1848 if (user->type != A_IND_N)
1849 goto fail;
1850 if (user->reg < 4 || user->reg > 5)
1851 goto fail;
1852 reg_n = user->reg;
1853 break;
1855 case AX_PMOD_N:
1856 if (user->type != AX_PMOD_N)
1857 goto fail;
1858 if (user->reg < 4 || user->reg > 5)
1859 goto fail;
1860 reg_n = user->reg;
1861 break;
1863 case AXY_INC_N:
1864 if (user->type != A_INC_N)
1865 goto fail;
1866 if ((user->reg < 4 || user->reg > 5)
1867 && (user->reg < 0 || user->reg > 1))
1868 goto fail;
1869 reg_n = user->reg;
1870 break;
1872 case AXY_IND_N:
1873 if (user->type != A_IND_N)
1874 goto fail;
1875 if ((user->reg < 4 || user->reg > 5)
1876 && (user->reg < 0 || user->reg > 1))
1877 goto fail;
1878 reg_n = user->reg;
1879 break;
1881 case AXY_PMOD_N:
1882 if (user->type != AX_PMOD_N)
1883 goto fail;
1884 if ((user->reg < 4 || user->reg > 5)
1885 && (user->reg < 0 || user->reg > 1))
1886 goto fail;
1887 reg_n = user->reg;
1888 break;
1890 case AY_INC_N:
1891 if (user->type != A_INC_N)
1892 goto fail;
1893 if (user->reg < 6 || user->reg > 7)
1894 goto fail;
1895 reg_n = user->reg;
1896 break;
1898 case AY_IND_N:
1899 if (user->type != A_IND_N)
1900 goto fail;
1901 if (user->reg < 6 || user->reg > 7)
1902 goto fail;
1903 reg_n = user->reg;
1904 break;
1906 case AY_PMOD_N:
1907 if (user->type != AY_PMOD_N)
1908 goto fail;
1909 if (user->reg < 6 || user->reg > 7)
1910 goto fail;
1911 reg_n = user->reg;
1912 break;
1914 case AYX_INC_N:
1915 if (user->type != A_INC_N)
1916 goto fail;
1917 if ((user->reg < 6 || user->reg > 7)
1918 && (user->reg < 2 || user->reg > 3))
1919 goto fail;
1920 reg_n = user->reg;
1921 break;
1923 case AYX_IND_N:
1924 if (user->type != A_IND_N)
1925 goto fail;
1926 if ((user->reg < 6 || user->reg > 7)
1927 && (user->reg < 2 || user->reg > 3))
1928 goto fail;
1929 reg_n = user->reg;
1930 break;
1932 case AYX_PMOD_N:
1933 if (user->type != AY_PMOD_N)
1934 goto fail;
1935 if ((user->reg < 6 || user->reg > 7)
1936 && (user->reg < 2 || user->reg > 3))
1937 goto fail;
1938 reg_n = user->reg;
1939 break;
1941 case DSP_REG_A_M:
1942 if (user->type != DSP_REG_N)
1943 goto fail;
1944 if (user->reg != A_A0_NUM
1945 && user->reg != A_A1_NUM)
1946 goto fail;
1947 reg_m = user->reg;
1948 break;
1950 case DSP_REG_AX:
1951 if (user->type != DSP_REG_N)
1952 goto fail;
1953 switch (user->reg)
1955 case A_A0_NUM:
1956 reg_x = 0;
1957 break;
1958 case A_A1_NUM:
1959 reg_x = 2;
1960 break;
1961 case A_X0_NUM:
1962 reg_x = 1;
1963 break;
1964 case A_X1_NUM:
1965 reg_x = 3;
1966 break;
1967 default:
1968 goto fail;
1970 break;
1972 case DSP_REG_XY:
1973 if (user->type != DSP_REG_N)
1974 goto fail;
1975 switch (user->reg)
1977 case A_X0_NUM:
1978 reg_x = 0;
1979 break;
1980 case A_X1_NUM:
1981 reg_x = 2;
1982 break;
1983 case A_Y0_NUM:
1984 reg_x = 1;
1985 break;
1986 case A_Y1_NUM:
1987 reg_x = 3;
1988 break;
1989 default:
1990 goto fail;
1992 break;
1994 case DSP_REG_AY:
1995 if (user->type != DSP_REG_N)
1996 goto fail;
1997 switch (user->reg)
1999 case A_A0_NUM:
2000 reg_y = 0;
2001 break;
2002 case A_A1_NUM:
2003 reg_y = 1;
2004 break;
2005 case A_Y0_NUM:
2006 reg_y = 2;
2007 break;
2008 case A_Y1_NUM:
2009 reg_y = 3;
2010 break;
2011 default:
2012 goto fail;
2014 break;
2016 case DSP_REG_YX:
2017 if (user->type != DSP_REG_N)
2018 goto fail;
2019 switch (user->reg)
2021 case A_Y0_NUM:
2022 reg_y = 0;
2023 break;
2024 case A_Y1_NUM:
2025 reg_y = 1;
2026 break;
2027 case A_X0_NUM:
2028 reg_y = 2;
2029 break;
2030 case A_X1_NUM:
2031 reg_y = 3;
2032 break;
2033 default:
2034 goto fail;
2036 break;
2038 case DSP_REG_X:
2039 if (user->type != DSP_REG_N)
2040 goto fail;
2041 switch (user->reg)
2043 case A_X0_NUM:
2044 reg_x = 0;
2045 break;
2046 case A_X1_NUM:
2047 reg_x = 1;
2048 break;
2049 case A_A0_NUM:
2050 reg_x = 2;
2051 break;
2052 case A_A1_NUM:
2053 reg_x = 3;
2054 break;
2055 default:
2056 goto fail;
2058 break;
2060 case DSP_REG_Y:
2061 if (user->type != DSP_REG_N)
2062 goto fail;
2063 switch (user->reg)
2065 case A_Y0_NUM:
2066 reg_y = 0;
2067 break;
2068 case A_Y1_NUM:
2069 reg_y = 1;
2070 break;
2071 case A_M0_NUM:
2072 reg_y = 2;
2073 break;
2074 case A_M1_NUM:
2075 reg_y = 3;
2076 break;
2077 default:
2078 goto fail;
2080 break;
2082 case DSP_REG_E:
2083 if (user->type != DSP_REG_N)
2084 goto fail;
2085 switch (user->reg)
2087 case A_X0_NUM:
2088 reg_efg = 0 << 10;
2089 break;
2090 case A_X1_NUM:
2091 reg_efg = 1 << 10;
2092 break;
2093 case A_Y0_NUM:
2094 reg_efg = 2 << 10;
2095 break;
2096 case A_A1_NUM:
2097 reg_efg = 3 << 10;
2098 break;
2099 default:
2100 goto fail;
2102 break;
2104 case DSP_REG_F:
2105 if (user->type != DSP_REG_N)
2106 goto fail;
2107 switch (user->reg)
2109 case A_Y0_NUM:
2110 reg_efg |= 0 << 8;
2111 break;
2112 case A_Y1_NUM:
2113 reg_efg |= 1 << 8;
2114 break;
2115 case A_X0_NUM:
2116 reg_efg |= 2 << 8;
2117 break;
2118 case A_A1_NUM:
2119 reg_efg |= 3 << 8;
2120 break;
2121 default:
2122 goto fail;
2124 break;
2126 case DSP_REG_G:
2127 if (user->type != DSP_REG_N)
2128 goto fail;
2129 switch (user->reg)
2131 case A_M0_NUM:
2132 reg_efg |= 0 << 2;
2133 break;
2134 case A_M1_NUM:
2135 reg_efg |= 1 << 2;
2136 break;
2137 case A_A0_NUM:
2138 reg_efg |= 2 << 2;
2139 break;
2140 case A_A1_NUM:
2141 reg_efg |= 3 << 2;
2142 break;
2143 default:
2144 goto fail;
2146 break;
2148 case A_A0:
2149 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
2150 goto fail;
2151 break;
2152 case A_X0:
2153 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
2154 goto fail;
2155 break;
2156 case A_X1:
2157 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
2158 goto fail;
2159 break;
2160 case A_Y0:
2161 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
2162 goto fail;
2163 break;
2164 case A_Y1:
2165 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
2166 goto fail;
2167 break;
2169 case F_REG_M:
2170 case D_REG_M:
2171 case X_REG_M:
2172 case V_REG_M:
2173 case FPUL_M:
2174 case FPSCR_M:
2175 /* Opcode needs rn */
2176 if (user->type != arg - F_REG_M + F_REG_N)
2177 goto fail;
2178 reg_m = user->reg;
2179 break;
2180 case DX_REG_M:
2181 if (user->type != D_REG_N && user->type != X_REG_N)
2182 goto fail;
2183 reg_m = user->reg;
2184 break;
2185 case XMTRX_M4:
2186 if (user->type != XMTRX_M4)
2187 goto fail;
2188 reg_m = 4;
2189 break;
2191 default:
2192 printf (_("unhandled %d\n"), arg);
2193 goto fail;
2195 if (SH_MERGE_ARCH_SET_VALID (valid_arch, arch_sh2a_nofpu_up)
2196 && ( arg == A_DISP_REG_M
2197 || arg == A_DISP_REG_N))
2199 /* Check a few key IMM* fields for overflow. */
2200 int opf;
2201 long val = user->immediate.X_add_number;
2203 for (opf = 0; opf < 4; opf ++)
2204 switch (this_try->nibbles[opf])
2206 case IMM0_4:
2207 case IMM1_4:
2208 if (val < 0 || val > 15)
2209 goto fail;
2210 break;
2211 case IMM0_4BY2:
2212 case IMM1_4BY2:
2213 if (val < 0 || val > 15 * 2)
2214 goto fail;
2215 break;
2216 case IMM0_4BY4:
2217 case IMM1_4BY4:
2218 if (val < 0 || val > 15 * 4)
2219 goto fail;
2220 break;
2221 default:
2222 break;
2226 if ( !SH_MERGE_ARCH_SET_VALID (valid_arch, this_try->arch))
2227 goto fail;
2228 valid_arch = SH_MERGE_ARCH_SET (valid_arch, this_try->arch);
2229 return this_try;
2230 fail:
2234 return 0;
2237 static void
2238 insert (char *where, int how, int pcrel, sh_operand_info *op)
2240 fix_new_exp (frag_now,
2241 where - frag_now->fr_literal,
2243 &op->immediate,
2244 pcrel,
2245 how);
2248 static void
2249 insert4 (char * where, int how, int pcrel, sh_operand_info * op)
2251 fix_new_exp (frag_now,
2252 where - frag_now->fr_literal,
2254 & op->immediate,
2255 pcrel,
2256 how);
2258 static void
2259 build_relax (sh_opcode_info *opcode, sh_operand_info *op)
2261 int high_byte = target_big_endian ? 0 : 1;
2262 char *p;
2264 if (opcode->arg[0] == A_BDISP8)
2266 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
2267 p = frag_var (rs_machine_dependent,
2268 md_relax_table[C (what, COND32)].rlx_length,
2269 md_relax_table[C (what, COND8)].rlx_length,
2270 C (what, 0),
2271 op->immediate.X_add_symbol,
2272 op->immediate.X_add_number,
2274 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
2276 else if (opcode->arg[0] == A_BDISP12)
2278 p = frag_var (rs_machine_dependent,
2279 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
2280 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
2281 C (UNCOND_JUMP, 0),
2282 op->immediate.X_add_symbol,
2283 op->immediate.X_add_number,
2285 p[high_byte] = (opcode->nibbles[0] << 4);
2290 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
2292 static char *
2293 insert_loop_bounds (char *output, sh_operand_info *operand)
2295 char *name;
2296 symbolS *end_sym;
2298 /* Since the low byte of the opcode will be overwritten by the reloc, we
2299 can just stash the high byte into both bytes and ignore endianness. */
2300 output[0] = 0x8c;
2301 output[1] = 0x8c;
2302 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
2303 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
2305 if (sh_relax)
2307 static int count = 0;
2309 /* If the last loop insn is a two-byte-insn, it is in danger of being
2310 swapped with the insn after it. To prevent this, create a new
2311 symbol - complete with SH_LABEL reloc - after the last loop insn.
2312 If the last loop insn is four bytes long, the symbol will be
2313 right in the middle, but four byte insns are not swapped anyways. */
2314 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
2315 Hence a 9 digit number should be enough to count all REPEATs. */
2316 name = alloca (11);
2317 sprintf (name, "_R%x", count++ & 0x3fffffff);
2318 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
2319 /* Make this a local symbol. */
2320 #ifdef OBJ_COFF
2321 SF_SET_LOCAL (end_sym);
2322 #endif /* OBJ_COFF */
2323 symbol_table_insert (end_sym);
2324 end_sym->sy_value = operand[1].immediate;
2325 end_sym->sy_value.X_add_number += 2;
2326 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
2329 output = frag_more (2);
2330 output[0] = 0x8e;
2331 output[1] = 0x8e;
2332 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
2333 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
2335 return frag_more (2);
2338 /* Now we know what sort of opcodes it is, let's build the bytes. */
2340 static unsigned int
2341 build_Mytes (sh_opcode_info *opcode, sh_operand_info *operand)
2343 int indx;
2344 char nbuf[8];
2345 char *output;
2346 unsigned int size = 2;
2347 int low_byte = target_big_endian ? 1 : 0;
2348 int max_index = 4;
2349 bfd_reloc_code_real_type r_type;
2350 int unhandled_pic = 0;
2352 nbuf[0] = 0;
2353 nbuf[1] = 0;
2354 nbuf[2] = 0;
2355 nbuf[3] = 0;
2356 nbuf[4] = 0;
2357 nbuf[5] = 0;
2358 nbuf[6] = 0;
2359 nbuf[7] = 0;
2361 for (indx = 0; indx < 3; indx++)
2362 if (opcode->arg[indx] == A_IMM
2363 && operand[indx].type == A_IMM
2364 && (operand[indx].immediate.X_op == O_PIC_reloc
2365 || sh_PIC_related_p (operand[indx].immediate.X_add_symbol)
2366 || sh_PIC_related_p (operand[indx].immediate.X_op_symbol)))
2367 unhandled_pic = 1;
2369 if (SH_MERGE_ARCH_SET (opcode->arch, arch_op32))
2371 output = frag_more (4);
2372 size = 4;
2373 max_index = 8;
2375 else
2376 output = frag_more (2);
2378 for (indx = 0; indx < max_index; indx++)
2380 sh_nibble_type i = opcode->nibbles[indx];
2381 if (i < 16)
2383 nbuf[indx] = i;
2385 else
2387 switch (i)
2389 case REG_N:
2390 case REG_N_D:
2391 nbuf[indx] = reg_n;
2392 break;
2393 case REG_M:
2394 nbuf[indx] = reg_m;
2395 break;
2396 case SDT_REG_N:
2397 if (reg_n < 2 || reg_n > 5)
2398 as_bad (_("Invalid register: 'r%d'"), reg_n);
2399 nbuf[indx] = (reg_n & 3) | 4;
2400 break;
2401 case REG_NM:
2402 nbuf[indx] = reg_n | (reg_m >> 2);
2403 break;
2404 case REG_B:
2405 nbuf[indx] = reg_b | 0x08;
2406 break;
2407 case REG_N_B01:
2408 nbuf[indx] = reg_n | 0x01;
2409 break;
2410 case IMM0_3s:
2411 nbuf[indx] |= 0x08;
2412 case IMM0_3c:
2413 insert (output + low_byte, BFD_RELOC_SH_IMM3, 0, operand);
2414 break;
2415 case IMM0_3Us:
2416 nbuf[indx] |= 0x80;
2417 case IMM0_3Uc:
2418 insert (output + low_byte, BFD_RELOC_SH_IMM3U, 0, operand);
2419 break;
2420 case DISP0_12:
2421 insert (output + 2, BFD_RELOC_SH_DISP12, 0, operand);
2422 break;
2423 case DISP0_12BY2:
2424 insert (output + 2, BFD_RELOC_SH_DISP12BY2, 0, operand);
2425 break;
2426 case DISP0_12BY4:
2427 insert (output + 2, BFD_RELOC_SH_DISP12BY4, 0, operand);
2428 break;
2429 case DISP0_12BY8:
2430 insert (output + 2, BFD_RELOC_SH_DISP12BY8, 0, operand);
2431 break;
2432 case DISP1_12:
2433 insert (output + 2, BFD_RELOC_SH_DISP12, 0, operand+1);
2434 break;
2435 case DISP1_12BY2:
2436 insert (output + 2, BFD_RELOC_SH_DISP12BY2, 0, operand+1);
2437 break;
2438 case DISP1_12BY4:
2439 insert (output + 2, BFD_RELOC_SH_DISP12BY4, 0, operand+1);
2440 break;
2441 case DISP1_12BY8:
2442 insert (output + 2, BFD_RELOC_SH_DISP12BY8, 0, operand+1);
2443 break;
2444 case IMM0_20_4:
2445 break;
2446 case IMM0_20:
2447 r_type = BFD_RELOC_SH_DISP20;
2448 if (sh_check_fixup (&operand->immediate, &r_type))
2449 as_bad (_("Invalid PIC expression."));
2450 unhandled_pic = 0;
2451 insert4 (output, r_type, 0, operand);
2452 break;
2453 case IMM0_20BY8:
2454 insert4 (output, BFD_RELOC_SH_DISP20BY8, 0, operand);
2455 break;
2456 case IMM0_4BY4:
2457 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
2458 break;
2459 case IMM0_4BY2:
2460 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
2461 break;
2462 case IMM0_4:
2463 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
2464 break;
2465 case IMM1_4BY4:
2466 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
2467 break;
2468 case IMM1_4BY2:
2469 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
2470 break;
2471 case IMM1_4:
2472 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
2473 break;
2474 case IMM0_8BY4:
2475 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
2476 break;
2477 case IMM0_8BY2:
2478 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
2479 break;
2480 case IMM0_8:
2481 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
2482 break;
2483 case IMM1_8BY4:
2484 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
2485 break;
2486 case IMM1_8BY2:
2487 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
2488 break;
2489 case IMM1_8:
2490 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
2491 break;
2492 case PCRELIMM_8BY4:
2493 insert (output, BFD_RELOC_SH_PCRELIMM8BY4,
2494 operand->type != A_DISP_PC_ABS, operand);
2495 break;
2496 case PCRELIMM_8BY2:
2497 insert (output, BFD_RELOC_SH_PCRELIMM8BY2,
2498 operand->type != A_DISP_PC_ABS, operand);
2499 break;
2500 case REPEAT:
2501 output = insert_loop_bounds (output, operand);
2502 nbuf[indx] = opcode->nibbles[3];
2503 operand += 2;
2504 break;
2505 default:
2506 printf (_("failed for %d\n"), i);
2510 if (unhandled_pic)
2511 as_bad (_("misplaced PIC operand"));
2512 if (!target_big_endian)
2514 output[1] = (nbuf[0] << 4) | (nbuf[1]);
2515 output[0] = (nbuf[2] << 4) | (nbuf[3]);
2517 else
2519 output[0] = (nbuf[0] << 4) | (nbuf[1]);
2520 output[1] = (nbuf[2] << 4) | (nbuf[3]);
2522 if (SH_MERGE_ARCH_SET (opcode->arch, arch_op32))
2524 if (!target_big_endian)
2526 output[3] = (nbuf[4] << 4) | (nbuf[5]);
2527 output[2] = (nbuf[6] << 4) | (nbuf[7]);
2529 else
2531 output[2] = (nbuf[4] << 4) | (nbuf[5]);
2532 output[3] = (nbuf[6] << 4) | (nbuf[7]);
2535 return size;
2538 /* Find an opcode at the start of *STR_P in the hash table, and set
2539 *STR_P to the first character after the last one read. */
2541 static sh_opcode_info *
2542 find_cooked_opcode (char **str_p)
2544 char *str = *str_p;
2545 unsigned char *op_start;
2546 unsigned char *op_end;
2547 char name[20];
2548 int nlen = 0;
2550 /* Drop leading whitespace. */
2551 while (*str == ' ')
2552 str++;
2554 /* Find the op code end.
2555 The pre-processor will eliminate whitespace in front of
2556 any '@' after the first argument; we may be called from
2557 assemble_ppi, so the opcode might be terminated by an '@'. */
2558 for (op_start = op_end = (unsigned char *) str;
2559 *op_end
2560 && nlen < 20
2561 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
2562 op_end++)
2564 unsigned char c = op_start[nlen];
2566 /* The machine independent code will convert CMP/EQ into cmp/EQ
2567 because it thinks the '/' is the end of the symbol. Moreover,
2568 all but the first sub-insn is a parallel processing insn won't
2569 be capitalized. Instead of hacking up the machine independent
2570 code, we just deal with it here. */
2571 c = TOLOWER (c);
2572 name[nlen] = c;
2573 nlen++;
2576 name[nlen] = 0;
2577 *str_p = (char *) op_end;
2579 if (nlen == 0)
2580 as_bad (_("can't find opcode "));
2582 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
2585 /* Assemble a parallel processing insn. */
2586 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
2588 static unsigned int
2589 assemble_ppi (char *op_end, sh_opcode_info *opcode)
2591 int movx = 0;
2592 int movy = 0;
2593 int cond = 0;
2594 int field_b = 0;
2595 char *output;
2596 int move_code;
2597 unsigned int size;
2599 for (;;)
2601 sh_operand_info operand[3];
2603 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
2604 Make sure we encode a defined insn pattern. */
2605 reg_x = 0;
2606 reg_y = 0;
2607 reg_n = 0;
2609 if (opcode->arg[0] != A_END)
2610 op_end = get_operands (opcode, op_end, operand);
2611 try_another_opcode:
2612 opcode = get_specific (opcode, operand);
2613 if (opcode == 0)
2615 /* Couldn't find an opcode which matched the operands. */
2616 char *where = frag_more (2);
2617 size = 2;
2619 where[0] = 0x0;
2620 where[1] = 0x0;
2621 as_bad (_("invalid operands for opcode"));
2622 return size;
2625 if (opcode->nibbles[0] != PPI)
2626 as_bad (_("insn can't be combined with parallel processing insn"));
2628 switch (opcode->nibbles[1])
2631 case NOPX:
2632 if (movx)
2633 as_bad (_("multiple movx specifications"));
2634 movx = DDT_BASE;
2635 break;
2636 case NOPY:
2637 if (movy)
2638 as_bad (_("multiple movy specifications"));
2639 movy = DDT_BASE;
2640 break;
2642 case MOVX_NOPY:
2643 if (movx)
2644 as_bad (_("multiple movx specifications"));
2645 if ((reg_n < 4 || reg_n > 5)
2646 && (reg_n < 0 || reg_n > 1))
2647 as_bad (_("invalid movx address register"));
2648 if (movy && movy != DDT_BASE)
2649 as_bad (_("insn cannot be combined with non-nopy"));
2650 movx = ((((reg_n & 1) != 0) << 9)
2651 + (((reg_n & 4) == 0) << 8)
2652 + (reg_x << 6)
2653 + (opcode->nibbles[2] << 4)
2654 + opcode->nibbles[3]
2655 + DDT_BASE);
2656 break;
2658 case MOVY_NOPX:
2659 if (movy)
2660 as_bad (_("multiple movy specifications"));
2661 if ((reg_n < 6 || reg_n > 7)
2662 && (reg_n < 2 || reg_n > 3))
2663 as_bad (_("invalid movy address register"));
2664 if (movx && movx != DDT_BASE)
2665 as_bad (_("insn cannot be combined with non-nopx"));
2666 movy = ((((reg_n & 1) != 0) << 8)
2667 + (((reg_n & 4) == 0) << 9)
2668 + (reg_y << 6)
2669 + (opcode->nibbles[2] << 4)
2670 + opcode->nibbles[3]
2671 + DDT_BASE);
2672 break;
2674 case MOVX:
2675 if (movx)
2676 as_bad (_("multiple movx specifications"));
2677 if (movy & 0x2ac)
2678 as_bad (_("previous movy requires nopx"));
2679 if (reg_n < 4 || reg_n > 5)
2680 as_bad (_("invalid movx address register"));
2681 if (opcode->nibbles[2] & 8)
2683 if (reg_m == A_A1_NUM)
2684 movx = 1 << 7;
2685 else if (reg_m != A_A0_NUM)
2686 as_bad (_("invalid movx dsp register"));
2688 else
2690 if (reg_x > 1)
2691 as_bad (_("invalid movx dsp register"));
2692 movx = reg_x << 7;
2694 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
2695 break;
2697 case MOVY:
2698 if (movy)
2699 as_bad (_("multiple movy specifications"));
2700 if (movx & 0x153)
2701 as_bad (_("previous movx requires nopy"));
2702 if (opcode->nibbles[2] & 8)
2704 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
2705 so add 8 more. */
2706 movy = 8;
2707 if (reg_m == A_A1_NUM)
2708 movy += 1 << 6;
2709 else if (reg_m != A_A0_NUM)
2710 as_bad (_("invalid movy dsp register"));
2712 else
2714 if (reg_y > 1)
2715 as_bad (_("invalid movy dsp register"));
2716 movy = reg_y << 6;
2718 if (reg_n < 6 || reg_n > 7)
2719 as_bad (_("invalid movy address register"));
2720 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
2721 break;
2723 case PSH:
2724 if (operand[0].immediate.X_op != O_constant)
2725 as_bad (_("dsp immediate shift value not constant"));
2726 field_b = ((opcode->nibbles[2] << 12)
2727 | (operand[0].immediate.X_add_number & 127) << 4
2728 | reg_n);
2729 break;
2730 case PPI3NC:
2731 if (cond)
2733 opcode++;
2734 goto try_another_opcode;
2736 /* Fall through. */
2737 case PPI3:
2738 if (field_b)
2739 as_bad (_("multiple parallel processing specifications"));
2740 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2741 + (reg_x << 6) + (reg_y << 4) + reg_n);
2742 switch (opcode->nibbles[4])
2744 case HEX_0:
2745 case HEX_XX00:
2746 case HEX_00YY:
2747 break;
2748 case HEX_1:
2749 case HEX_4:
2750 field_b += opcode->nibbles[4] << 4;
2751 break;
2752 default:
2753 abort ();
2755 break;
2756 case PDC:
2757 if (cond)
2758 as_bad (_("multiple condition specifications"));
2759 cond = opcode->nibbles[2] << 8;
2760 if (*op_end)
2761 goto skip_cond_check;
2762 break;
2763 case PPIC:
2764 if (field_b)
2765 as_bad (_("multiple parallel processing specifications"));
2766 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2767 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
2768 cond = 0;
2769 switch (opcode->nibbles[4])
2771 case HEX_0:
2772 case HEX_XX00:
2773 case HEX_00YY:
2774 break;
2775 case HEX_1:
2776 case HEX_4:
2777 field_b += opcode->nibbles[4] << 4;
2778 break;
2779 default:
2780 abort ();
2782 break;
2783 case PMUL:
2784 if (field_b)
2786 if ((field_b & 0xef00) == 0xa100)
2787 field_b -= 0x8100;
2788 /* pclr Dz pmuls Se,Sf,Dg */
2789 else if ((field_b & 0xff00) == 0x8d00
2790 && (SH_MERGE_ARCH_SET_VALID (valid_arch, arch_sh4al_dsp_up)))
2792 valid_arch = SH_MERGE_ARCH_SET (valid_arch, arch_sh4al_dsp_up);
2793 field_b -= 0x8cf0;
2795 else
2796 as_bad (_("insn cannot be combined with pmuls"));
2797 switch (field_b & 0xf)
2799 case A_X0_NUM:
2800 field_b += 0 - A_X0_NUM;
2801 break;
2802 case A_Y0_NUM:
2803 field_b += 1 - A_Y0_NUM;
2804 break;
2805 case A_A0_NUM:
2806 field_b += 2 - A_A0_NUM;
2807 break;
2808 case A_A1_NUM:
2809 field_b += 3 - A_A1_NUM;
2810 break;
2811 default:
2812 as_bad (_("bad combined pmuls output operand"));
2814 /* Generate warning if the destination register for padd / psub
2815 and pmuls is the same ( only for A0 or A1 ).
2816 If the last nibble is 1010 then A0 is used in both
2817 padd / psub and pmuls. If it is 1111 then A1 is used
2818 as destination register in both padd / psub and pmuls. */
2820 if ((((field_b | reg_efg) & 0x000F) == 0x000A)
2821 || (((field_b | reg_efg) & 0x000F) == 0x000F))
2822 as_warn (_("destination register is same for parallel insns"));
2824 field_b += 0x4000 + reg_efg;
2825 break;
2826 default:
2827 abort ();
2829 if (cond)
2831 as_bad (_("condition not followed by conditionalizable insn"));
2832 cond = 0;
2834 if (! *op_end)
2835 break;
2836 skip_cond_check:
2837 opcode = find_cooked_opcode (&op_end);
2838 if (opcode == NULL)
2840 (as_bad
2841 (_("unrecognized characters at end of parallel processing insn")));
2842 break;
2846 move_code = movx | movy;
2847 if (field_b)
2849 /* Parallel processing insn. */
2850 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
2852 output = frag_more (4);
2853 size = 4;
2854 if (! target_big_endian)
2856 output[3] = ppi_code >> 8;
2857 output[2] = ppi_code;
2859 else
2861 output[2] = ppi_code >> 8;
2862 output[3] = ppi_code;
2864 move_code |= 0xf800;
2866 else
2868 /* Just a double data transfer. */
2869 output = frag_more (2);
2870 size = 2;
2872 if (! target_big_endian)
2874 output[1] = move_code >> 8;
2875 output[0] = move_code;
2877 else
2879 output[0] = move_code >> 8;
2880 output[1] = move_code;
2882 return size;
2885 /* This is the guts of the machine-dependent assembler. STR points to a
2886 machine dependent instruction. This function is supposed to emit
2887 the frags/bytes it assembles to. */
2889 void
2890 md_assemble (char *str)
2892 char *op_end;
2893 sh_operand_info operand[3];
2894 sh_opcode_info *opcode;
2895 unsigned int size = 0;
2896 char *initial_str = str;
2898 #ifdef HAVE_SH64
2899 if (sh64_isa_mode == sh64_isa_shmedia)
2901 shmedia_md_assemble (str);
2902 return;
2904 else
2906 /* If we've seen pseudo-directives, make sure any emitted data or
2907 frags are marked as data. */
2908 if (!seen_insn)
2910 sh64_update_contents_mark (TRUE);
2911 sh64_set_contents_type (CRT_SH5_ISA16);
2914 seen_insn = TRUE;
2916 #endif /* HAVE_SH64 */
2918 opcode = find_cooked_opcode (&str);
2919 op_end = str;
2921 if (opcode == NULL)
2923 /* The opcode is not in the hash table.
2924 This means we definitely have an assembly failure,
2925 but the instruction may be valid in another CPU variant.
2926 In this case emit something better than 'unknown opcode'.
2927 Search the full table in sh-opc.h to check. */
2929 char *name = initial_str;
2930 int name_length = 0;
2931 const sh_opcode_info *op;
2932 int found = 0;
2934 /* identify opcode in string */
2935 while (ISSPACE (*name))
2937 name++;
2939 while (!ISSPACE (name[name_length]))
2941 name_length++;
2944 /* search for opcode in full list */
2945 for (op = sh_table; op->name; op++)
2947 if (strncasecmp (op->name, name, name_length) == 0
2948 && op->name[name_length] == '\0')
2950 found = 1;
2951 break;
2955 if ( found )
2957 as_bad (_("opcode not valid for this cpu variant"));
2959 else
2961 as_bad (_("unknown opcode"));
2963 return;
2966 if (sh_relax
2967 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
2969 /* Output a CODE reloc to tell the linker that the following
2970 bytes are instructions, not data. */
2971 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2972 BFD_RELOC_SH_CODE);
2973 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
2976 if (opcode->nibbles[0] == PPI)
2978 size = assemble_ppi (op_end, opcode);
2980 else
2982 if (opcode->arg[0] == A_BDISP12
2983 || opcode->arg[0] == A_BDISP8)
2985 /* Since we skip get_specific here, we have to check & update
2986 valid_arch now. */
2987 if (SH_MERGE_ARCH_SET_VALID (valid_arch, opcode->arch))
2988 valid_arch = SH_MERGE_ARCH_SET (valid_arch, opcode->arch);
2989 else
2990 as_bad (_("Delayed branches not available on SH1"));
2991 parse_exp (op_end + 1, &operand[0]);
2992 build_relax (opcode, &operand[0]);
2994 /* All branches are currently 16 bit. */
2995 size = 2;
2997 else
2999 if (opcode->arg[0] == A_END)
3001 /* Ignore trailing whitespace. If there is any, it has already
3002 been compressed to a single space. */
3003 if (*op_end == ' ')
3004 op_end++;
3006 else
3008 op_end = get_operands (opcode, op_end, operand);
3010 opcode = get_specific (opcode, operand);
3012 if (opcode == 0)
3014 /* Couldn't find an opcode which matched the operands. */
3015 char *where = frag_more (2);
3016 size = 2;
3018 where[0] = 0x0;
3019 where[1] = 0x0;
3020 as_bad (_("invalid operands for opcode"));
3022 else
3024 if (*op_end)
3025 as_bad (_("excess operands: '%s'"), op_end);
3027 size = build_Mytes (opcode, operand);
3032 dwarf2_emit_insn (size);
3035 /* This routine is called each time a label definition is seen. It
3036 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
3038 void
3039 sh_frob_label (symbolS *sym)
3041 static fragS *last_label_frag;
3042 static int last_label_offset;
3044 if (sh_relax
3045 && seg_info (now_seg)->tc_segment_info_data.in_code)
3047 int offset;
3049 offset = frag_now_fix ();
3050 if (frag_now != last_label_frag
3051 || offset != last_label_offset)
3053 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
3054 last_label_frag = frag_now;
3055 last_label_offset = offset;
3059 dwarf2_emit_label (sym);
3062 /* This routine is called when the assembler is about to output some
3063 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
3065 void
3066 sh_flush_pending_output (void)
3068 if (sh_relax
3069 && seg_info (now_seg)->tc_segment_info_data.in_code)
3071 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
3072 BFD_RELOC_SH_DATA);
3073 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
3077 symbolS *
3078 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
3080 return 0;
3083 /* Various routines to kill one day. */
3085 char *
3086 md_atof (int type, char *litP, int *sizeP)
3088 return ieee_md_atof (type, litP, sizeP, target_big_endian);
3091 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
3092 call instruction. It refers to a label of the instruction which
3093 loads the register which the call uses. We use it to generate a
3094 special reloc for the linker. */
3096 static void
3097 s_uses (int ignore ATTRIBUTE_UNUSED)
3099 expressionS ex;
3101 if (! sh_relax)
3102 as_warn (_(".uses pseudo-op seen when not relaxing"));
3104 expression (&ex);
3106 if (ex.X_op != O_symbol || ex.X_add_number != 0)
3108 as_bad (_("bad .uses format"));
3109 ignore_rest_of_line ();
3110 return;
3113 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
3115 demand_empty_rest_of_line ();
3118 enum options
3120 OPTION_RELAX = OPTION_MD_BASE,
3121 OPTION_BIG,
3122 OPTION_LITTLE,
3123 OPTION_SMALL,
3124 OPTION_DSP,
3125 OPTION_ISA,
3126 OPTION_RENESAS,
3127 OPTION_ALLOW_REG_PREFIX,
3128 #ifdef HAVE_SH64
3129 OPTION_ABI,
3130 OPTION_NO_MIX,
3131 OPTION_SHCOMPACT_CONST_CRANGE,
3132 OPTION_NO_EXPAND,
3133 OPTION_PT32,
3134 #endif
3135 OPTION_H_TICK_HEX,
3136 #ifdef OBJ_ELF
3137 OPTION_FDPIC,
3138 #endif
3139 OPTION_DUMMY /* Not used. This is just here to make it easy to add and subtract options from this enum. */
3142 const char *md_shortopts = "";
3143 struct option md_longopts[] =
3145 {"relax", no_argument, NULL, OPTION_RELAX},
3146 {"big", no_argument, NULL, OPTION_BIG},
3147 {"little", no_argument, NULL, OPTION_LITTLE},
3148 /* The next two switches are here because the
3149 generic parts of the linker testsuite uses them. */
3150 {"EB", no_argument, NULL, OPTION_BIG},
3151 {"EL", no_argument, NULL, OPTION_LITTLE},
3152 {"small", no_argument, NULL, OPTION_SMALL},
3153 {"dsp", no_argument, NULL, OPTION_DSP},
3154 {"isa", required_argument, NULL, OPTION_ISA},
3155 {"renesas", no_argument, NULL, OPTION_RENESAS},
3156 {"allow-reg-prefix", no_argument, NULL, OPTION_ALLOW_REG_PREFIX},
3158 #ifdef HAVE_SH64
3159 {"abi", required_argument, NULL, OPTION_ABI},
3160 {"no-mix", no_argument, NULL, OPTION_NO_MIX},
3161 {"shcompact-const-crange", no_argument, NULL, OPTION_SHCOMPACT_CONST_CRANGE},
3162 {"no-expand", no_argument, NULL, OPTION_NO_EXPAND},
3163 {"expand-pt32", no_argument, NULL, OPTION_PT32},
3164 #endif /* HAVE_SH64 */
3165 { "h-tick-hex", no_argument, NULL, OPTION_H_TICK_HEX },
3167 #ifdef OBJ_ELF
3168 {"fdpic", no_argument, NULL, OPTION_FDPIC},
3169 #endif
3171 {NULL, no_argument, NULL, 0}
3173 size_t md_longopts_size = sizeof (md_longopts);
3176 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
3178 switch (c)
3180 case OPTION_RELAX:
3181 sh_relax = 1;
3182 break;
3184 case OPTION_BIG:
3185 target_big_endian = 1;
3186 break;
3188 case OPTION_LITTLE:
3189 target_big_endian = 0;
3190 break;
3192 case OPTION_SMALL:
3193 sh_small = 1;
3194 break;
3196 case OPTION_DSP:
3197 preset_target_arch = arch_sh_up & ~(arch_sh_sp_fpu|arch_sh_dp_fpu);
3198 break;
3200 case OPTION_RENESAS:
3201 dont_adjust_reloc_32 = 1;
3202 break;
3204 case OPTION_ALLOW_REG_PREFIX:
3205 allow_dollar_register_prefix = 1;
3206 break;
3208 case OPTION_ISA:
3209 if (strcasecmp (arg, "dsp") == 0)
3210 preset_target_arch = arch_sh_up & ~(arch_sh_sp_fpu|arch_sh_dp_fpu);
3211 else if (strcasecmp (arg, "fp") == 0)
3212 preset_target_arch = arch_sh_up & ~arch_sh_has_dsp;
3213 else if (strcasecmp (arg, "any") == 0)
3214 preset_target_arch = arch_sh_up;
3215 #ifdef HAVE_SH64
3216 else if (strcasecmp (arg, "shmedia") == 0)
3218 if (sh64_isa_mode == sh64_isa_shcompact)
3219 as_bad (_("Invalid combination: --isa=SHcompact with --isa=SHmedia"));
3220 sh64_isa_mode = sh64_isa_shmedia;
3222 else if (strcasecmp (arg, "shcompact") == 0)
3224 if (sh64_isa_mode == sh64_isa_shmedia)
3225 as_bad (_("Invalid combination: --isa=SHmedia with --isa=SHcompact"));
3226 if (sh64_abi == sh64_abi_64)
3227 as_bad (_("Invalid combination: --abi=64 with --isa=SHcompact"));
3228 sh64_isa_mode = sh64_isa_shcompact;
3230 #endif /* HAVE_SH64 */
3231 else
3233 extern const bfd_arch_info_type bfd_sh_arch;
3234 bfd_arch_info_type const *bfd_arch = &bfd_sh_arch;
3236 preset_target_arch = 0;
3237 for (; bfd_arch; bfd_arch=bfd_arch->next)
3239 int len = strlen(bfd_arch->printable_name);
3241 if (bfd_arch->mach == bfd_mach_sh5)
3242 continue;
3244 if (strncasecmp (bfd_arch->printable_name, arg, len) != 0)
3245 continue;
3247 if (arg[len] == '\0')
3248 preset_target_arch =
3249 sh_get_arch_from_bfd_mach (bfd_arch->mach);
3250 else if (strcasecmp(&arg[len], "-up") == 0)
3251 preset_target_arch =
3252 sh_get_arch_up_from_bfd_mach (bfd_arch->mach);
3253 else
3254 continue;
3255 break;
3258 if (!preset_target_arch)
3259 as_bad (_("Invalid argument to --isa option: %s"), arg);
3261 break;
3263 #ifdef HAVE_SH64
3264 case OPTION_ABI:
3265 if (strcmp (arg, "32") == 0)
3267 if (sh64_abi == sh64_abi_64)
3268 as_bad (_("Invalid combination: --abi=32 with --abi=64"));
3269 sh64_abi = sh64_abi_32;
3271 else if (strcmp (arg, "64") == 0)
3273 if (sh64_abi == sh64_abi_32)
3274 as_bad (_("Invalid combination: --abi=64 with --abi=32"));
3275 if (sh64_isa_mode == sh64_isa_shcompact)
3276 as_bad (_("Invalid combination: --isa=SHcompact with --abi=64"));
3277 sh64_abi = sh64_abi_64;
3279 else
3280 as_bad (_("Invalid argument to --abi option: %s"), arg);
3281 break;
3283 case OPTION_NO_MIX:
3284 sh64_mix = FALSE;
3285 break;
3287 case OPTION_SHCOMPACT_CONST_CRANGE:
3288 sh64_shcompact_const_crange = TRUE;
3289 break;
3291 case OPTION_NO_EXPAND:
3292 sh64_expand = FALSE;
3293 break;
3295 case OPTION_PT32:
3296 sh64_pt32 = TRUE;
3297 break;
3298 #endif /* HAVE_SH64 */
3300 case OPTION_H_TICK_HEX:
3301 enable_h_tick_hex = 1;
3302 break;
3304 #ifdef OBJ_ELF
3305 case OPTION_FDPIC:
3306 sh_fdpic = TRUE;
3307 break;
3308 #endif /* OBJ_ELF */
3310 default:
3311 return 0;
3314 return 1;
3317 void
3318 md_show_usage (FILE *stream)
3320 fprintf (stream, _("\
3321 SH options:\n\
3322 --little generate little endian code\n\
3323 --big generate big endian code\n\
3324 --relax alter jump instructions for long displacements\n\
3325 --renesas disable optimization with section symbol for\n\
3326 compatibility with Renesas assembler.\n\
3327 --small align sections to 4 byte boundaries, not 16\n\
3328 --dsp enable sh-dsp insns, and disable floating-point ISAs.\n\
3329 --allow-reg-prefix allow '$' as a register name prefix.\n\
3330 --isa=[any use most appropriate isa\n\
3331 | dsp same as '-dsp'\n\
3332 | fp"));
3334 extern const bfd_arch_info_type bfd_sh_arch;
3335 bfd_arch_info_type const *bfd_arch = &bfd_sh_arch;
3337 for (; bfd_arch; bfd_arch=bfd_arch->next)
3338 if (bfd_arch->mach != bfd_mach_sh5)
3340 fprintf (stream, "\n | %s", bfd_arch->printable_name);
3341 fprintf (stream, "\n | %s-up", bfd_arch->printable_name);
3344 fprintf (stream, "]\n");
3345 #ifdef HAVE_SH64
3346 fprintf (stream, _("\
3347 --isa=[shmedia set as the default instruction set for SH64\n\
3348 | SHmedia\n\
3349 | shcompact\n\
3350 | SHcompact]\n"));
3351 fprintf (stream, _("\
3352 --abi=[32|64] set size of expanded SHmedia operands and object\n\
3353 file type\n\
3354 --shcompact-const-crange emit code-range descriptors for constants in\n\
3355 SHcompact code sections\n\
3356 --no-mix disallow SHmedia code in the same section as\n\
3357 constants and SHcompact code\n\
3358 --no-expand do not expand MOVI, PT, PTA or PTB instructions\n\
3359 --expand-pt32 with -abi=64, expand PT, PTA and PTB instructions\n\
3360 to 32 bits only\n"));
3361 #endif /* HAVE_SH64 */
3362 #ifdef OBJ_ELF
3363 fprintf (stream, _("\
3364 --fdpic generate an FDPIC object file\n"));
3365 #endif /* OBJ_ELF */
3368 /* This struct is used to pass arguments to sh_count_relocs through
3369 bfd_map_over_sections. */
3371 struct sh_count_relocs
3373 /* Symbol we are looking for. */
3374 symbolS *sym;
3375 /* Count of relocs found. */
3376 int count;
3379 /* Count the number of fixups in a section which refer to a particular
3380 symbol. This is called via bfd_map_over_sections. */
3382 static void
3383 sh_count_relocs (bfd *abfd ATTRIBUTE_UNUSED, segT sec, void *data)
3385 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
3386 segment_info_type *seginfo;
3387 symbolS *sym;
3388 fixS *fix;
3390 seginfo = seg_info (sec);
3391 if (seginfo == NULL)
3392 return;
3394 sym = info->sym;
3395 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3397 if (fix->fx_addsy == sym)
3399 ++info->count;
3400 fix->fx_tcbit = 1;
3405 /* Handle the count relocs for a particular section.
3406 This is called via bfd_map_over_sections. */
3408 static void
3409 sh_frob_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec,
3410 void *ignore ATTRIBUTE_UNUSED)
3412 segment_info_type *seginfo;
3413 fixS *fix;
3415 seginfo = seg_info (sec);
3416 if (seginfo == NULL)
3417 return;
3419 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3421 symbolS *sym;
3423 sym = fix->fx_addsy;
3424 /* Check for a local_symbol. */
3425 if (sym && sym->bsym == NULL)
3427 struct local_symbol *ls = (struct local_symbol *)sym;
3428 /* See if it's been converted. If so, canonicalize. */
3429 if (local_symbol_converted_p (ls))
3430 fix->fx_addsy = local_symbol_get_real_symbol (ls);
3434 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3436 symbolS *sym;
3437 bfd_vma val;
3438 fixS *fscan;
3439 struct sh_count_relocs info;
3441 if (fix->fx_r_type != BFD_RELOC_SH_USES)
3442 continue;
3444 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
3445 symbol in the same section. */
3446 sym = fix->fx_addsy;
3447 if (sym == NULL
3448 || fix->fx_subsy != NULL
3449 || fix->fx_addnumber != 0
3450 || S_GET_SEGMENT (sym) != sec
3451 || S_IS_EXTERNAL (sym))
3453 as_warn_where (fix->fx_file, fix->fx_line,
3454 _(".uses does not refer to a local symbol in the same section"));
3455 continue;
3458 /* Look through the fixups again, this time looking for one
3459 at the same location as sym. */
3460 val = S_GET_VALUE (sym);
3461 for (fscan = seginfo->fix_root;
3462 fscan != NULL;
3463 fscan = fscan->fx_next)
3464 if (val == fscan->fx_frag->fr_address + fscan->fx_where
3465 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
3466 && fscan->fx_r_type != BFD_RELOC_SH_CODE
3467 && fscan->fx_r_type != BFD_RELOC_SH_DATA
3468 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
3469 break;
3470 if (fscan == NULL)
3472 as_warn_where (fix->fx_file, fix->fx_line,
3473 _("can't find fixup pointed to by .uses"));
3474 continue;
3477 if (fscan->fx_tcbit)
3479 /* We've already done this one. */
3480 continue;
3483 /* The variable fscan should also be a fixup to a local symbol
3484 in the same section. */
3485 sym = fscan->fx_addsy;
3486 if (sym == NULL
3487 || fscan->fx_subsy != NULL
3488 || fscan->fx_addnumber != 0
3489 || S_GET_SEGMENT (sym) != sec
3490 || S_IS_EXTERNAL (sym))
3492 as_warn_where (fix->fx_file, fix->fx_line,
3493 _(".uses target does not refer to a local symbol in the same section"));
3494 continue;
3497 /* Now we look through all the fixups of all the sections,
3498 counting the number of times we find a reference to sym. */
3499 info.sym = sym;
3500 info.count = 0;
3501 bfd_map_over_sections (stdoutput, sh_count_relocs, &info);
3503 if (info.count < 1)
3504 abort ();
3506 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
3507 We have already adjusted the value of sym to include the
3508 fragment address, so we undo that adjustment here. */
3509 subseg_change (sec, 0);
3510 fix_new (fscan->fx_frag,
3511 S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
3512 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
3516 /* This function is called after the symbol table has been completed,
3517 but before the relocs or section contents have been written out.
3518 If we have seen any .uses pseudo-ops, they point to an instruction
3519 which loads a register with the address of a function. We look
3520 through the fixups to find where the function address is being
3521 loaded from. We then generate a COUNT reloc giving the number of
3522 times that function address is referred to. The linker uses this
3523 information when doing relaxing, to decide when it can eliminate
3524 the stored function address entirely. */
3526 void
3527 sh_frob_file (void)
3529 #ifdef HAVE_SH64
3530 shmedia_frob_file_before_adjust ();
3531 #endif
3533 if (! sh_relax)
3534 return;
3536 bfd_map_over_sections (stdoutput, sh_frob_section, NULL);
3539 /* Called after relaxing. Set the correct sizes of the fragments, and
3540 create relocs so that md_apply_fix will fill in the correct values. */
3542 void
3543 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT seg, fragS *fragP)
3545 int donerelax = 0;
3547 switch (fragP->fr_subtype)
3549 case C (COND_JUMP, COND8):
3550 case C (COND_JUMP_DELAY, COND8):
3551 subseg_change (seg, 0);
3552 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3553 1, BFD_RELOC_SH_PCDISP8BY2);
3554 fragP->fr_fix += 2;
3555 fragP->fr_var = 0;
3556 break;
3558 case C (UNCOND_JUMP, UNCOND12):
3559 subseg_change (seg, 0);
3560 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3561 1, BFD_RELOC_SH_PCDISP12BY2);
3562 fragP->fr_fix += 2;
3563 fragP->fr_var = 0;
3564 break;
3566 case C (UNCOND_JUMP, UNCOND32):
3567 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3568 if (fragP->fr_symbol == NULL)
3569 as_bad_where (fragP->fr_file, fragP->fr_line,
3570 _("displacement overflows 12-bit field"));
3571 else if (S_IS_DEFINED (fragP->fr_symbol))
3572 as_bad_where (fragP->fr_file, fragP->fr_line,
3573 _("displacement to defined symbol %s overflows 12-bit field"),
3574 S_GET_NAME (fragP->fr_symbol));
3575 else
3576 as_bad_where (fragP->fr_file, fragP->fr_line,
3577 _("displacement to undefined symbol %s overflows 12-bit field"),
3578 S_GET_NAME (fragP->fr_symbol));
3579 /* Stabilize this frag, so we don't trip an assert. */
3580 fragP->fr_fix += fragP->fr_var;
3581 fragP->fr_var = 0;
3582 break;
3584 case C (COND_JUMP, COND12):
3585 case C (COND_JUMP_DELAY, COND12):
3586 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
3587 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
3588 was due to gas incorrectly relaxing an out-of-range conditional
3589 branch with delay slot. It turned:
3590 bf.s L6 (slot mov.l r12,@(44,r0))
3591 into:
3593 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
3594 30: 00 09 nop
3595 32: 10 cb mov.l r12,@(44,r0)
3596 Therefore, branches with delay slots have to be handled
3597 differently from ones without delay slots. */
3599 unsigned char *buffer =
3600 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
3601 int highbyte = target_big_endian ? 0 : 1;
3602 int lowbyte = target_big_endian ? 1 : 0;
3603 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
3605 /* Toggle the true/false bit of the bcond. */
3606 buffer[highbyte] ^= 0x2;
3608 /* If this is a delayed branch, we may not put the bra in the
3609 slot. So we change it to a non-delayed branch, like that:
3610 b! cond slot_label; bra disp; slot_label: slot_insn
3611 ??? We should try if swapping the conditional branch and
3612 its delay-slot insn already makes the branch reach. */
3614 /* Build a relocation to six / four bytes farther on. */
3615 subseg_change (seg, 0);
3616 fix_new (fragP, fragP->fr_fix, 2, section_symbol (seg),
3617 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
3618 1, BFD_RELOC_SH_PCDISP8BY2);
3620 /* Set up a jump instruction. */
3621 buffer[highbyte + 2] = 0xa0;
3622 buffer[lowbyte + 2] = 0;
3623 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
3624 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
3626 if (delay)
3628 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
3629 fragP->fr_fix += 4;
3631 else
3633 /* Fill in a NOP instruction. */
3634 buffer[highbyte + 4] = 0x0;
3635 buffer[lowbyte + 4] = 0x9;
3637 fragP->fr_fix += 6;
3639 fragP->fr_var = 0;
3640 donerelax = 1;
3642 break;
3644 case C (COND_JUMP, COND32):
3645 case C (COND_JUMP_DELAY, COND32):
3646 case C (COND_JUMP, UNDEF_WORD_DISP):
3647 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3648 if (fragP->fr_symbol == NULL)
3649 as_bad_where (fragP->fr_file, fragP->fr_line,
3650 _("displacement overflows 8-bit field"));
3651 else if (S_IS_DEFINED (fragP->fr_symbol))
3652 as_bad_where (fragP->fr_file, fragP->fr_line,
3653 _("displacement to defined symbol %s overflows 8-bit field"),
3654 S_GET_NAME (fragP->fr_symbol));
3655 else
3656 as_bad_where (fragP->fr_file, fragP->fr_line,
3657 _("displacement to undefined symbol %s overflows 8-bit field "),
3658 S_GET_NAME (fragP->fr_symbol));
3659 /* Stabilize this frag, so we don't trip an assert. */
3660 fragP->fr_fix += fragP->fr_var;
3661 fragP->fr_var = 0;
3662 break;
3664 default:
3665 #ifdef HAVE_SH64
3666 shmedia_md_convert_frag (headers, seg, fragP, TRUE);
3667 #else
3668 abort ();
3669 #endif
3672 if (donerelax && !sh_relax)
3673 as_warn_where (fragP->fr_file, fragP->fr_line,
3674 _("overflow in branch to %s; converted into longer instruction sequence"),
3675 (fragP->fr_symbol != NULL
3676 ? S_GET_NAME (fragP->fr_symbol)
3677 : ""));
3680 valueT
3681 md_section_align (segT seg ATTRIBUTE_UNUSED, valueT size)
3683 #ifdef OBJ_ELF
3684 return size;
3685 #else /* ! OBJ_ELF */
3686 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
3687 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
3688 #endif /* ! OBJ_ELF */
3691 /* This static variable is set by s_uacons to tell sh_cons_align that
3692 the expression does not need to be aligned. */
3694 static int sh_no_align_cons = 0;
3696 /* This handles the unaligned space allocation pseudo-ops, such as
3697 .uaword. .uaword is just like .word, but the value does not need
3698 to be aligned. */
3700 static void
3701 s_uacons (int bytes)
3703 /* Tell sh_cons_align not to align this value. */
3704 sh_no_align_cons = 1;
3705 cons (bytes);
3708 /* If a .word, et. al., pseud-op is seen, warn if the value is not
3709 aligned correctly. Note that this can cause warnings to be issued
3710 when assembling initialized structured which were declared with the
3711 packed attribute. FIXME: Perhaps we should require an option to
3712 enable this warning? */
3714 void
3715 sh_cons_align (int nbytes)
3717 int nalign;
3718 char *p;
3720 if (sh_no_align_cons)
3722 /* This is an unaligned pseudo-op. */
3723 sh_no_align_cons = 0;
3724 return;
3727 nalign = 0;
3728 while ((nbytes & 1) == 0)
3730 ++nalign;
3731 nbytes >>= 1;
3734 if (nalign == 0)
3735 return;
3737 if (now_seg == absolute_section)
3739 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3740 as_warn (_("misaligned data"));
3741 return;
3744 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
3745 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3747 record_alignment (now_seg, nalign);
3750 /* When relaxing, we need to output a reloc for any .align directive
3751 that requests alignment to a four byte boundary or larger. This is
3752 also where we check for misaligned data. */
3754 void
3755 sh_handle_align (fragS *frag)
3757 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
3759 if (frag->fr_type == rs_align_code)
3761 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
3762 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
3764 char *p = frag->fr_literal + frag->fr_fix;
3766 if (bytes & 1)
3768 *p++ = 0;
3769 bytes--;
3770 frag->fr_fix += 1;
3773 if (target_big_endian)
3775 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
3776 frag->fr_var = sizeof big_nop_pattern;
3778 else
3780 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
3781 frag->fr_var = sizeof little_nop_pattern;
3784 else if (frag->fr_type == rs_align_test)
3786 if (bytes != 0)
3787 as_bad_where (frag->fr_file, frag->fr_line, _("misaligned data"));
3790 if (sh_relax
3791 && (frag->fr_type == rs_align
3792 || frag->fr_type == rs_align_code)
3793 && frag->fr_address + frag->fr_fix > 0
3794 && frag->fr_offset > 1
3795 && now_seg != bss_section)
3796 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
3797 BFD_RELOC_SH_ALIGN);
3800 /* See whether the relocation should be resolved locally. */
3802 static bfd_boolean
3803 sh_local_pcrel (fixS *fix)
3805 return (! sh_relax
3806 && (fix->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
3807 || fix->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
3808 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
3809 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
3810 || fix->fx_r_type == BFD_RELOC_8_PCREL
3811 || fix->fx_r_type == BFD_RELOC_SH_SWITCH16
3812 || fix->fx_r_type == BFD_RELOC_SH_SWITCH32));
3815 /* See whether we need to force a relocation into the output file.
3816 This is used to force out switch and PC relative relocations when
3817 relaxing. */
3820 sh_force_relocation (fixS *fix)
3822 /* These relocations can't make it into a DSO, so no use forcing
3823 them for global symbols. */
3824 if (sh_local_pcrel (fix))
3825 return 0;
3827 /* Make sure some relocations get emitted. */
3828 if (fix->fx_r_type == BFD_RELOC_SH_LOOP_START
3829 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END
3830 || fix->fx_r_type == BFD_RELOC_SH_TLS_GD_32
3831 || fix->fx_r_type == BFD_RELOC_SH_TLS_LD_32
3832 || fix->fx_r_type == BFD_RELOC_SH_TLS_IE_32
3833 || fix->fx_r_type == BFD_RELOC_SH_TLS_LDO_32
3834 || fix->fx_r_type == BFD_RELOC_SH_TLS_LE_32
3835 || generic_force_reloc (fix))
3836 return 1;
3838 if (! sh_relax)
3839 return 0;
3841 return (fix->fx_pcrel
3842 || SWITCH_TABLE (fix)
3843 || fix->fx_r_type == BFD_RELOC_SH_COUNT
3844 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
3845 || fix->fx_r_type == BFD_RELOC_SH_CODE
3846 || fix->fx_r_type == BFD_RELOC_SH_DATA
3847 #ifdef HAVE_SH64
3848 || fix->fx_r_type == BFD_RELOC_SH_SHMEDIA_CODE
3849 #endif
3850 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
3853 #ifdef OBJ_ELF
3854 bfd_boolean
3855 sh_fix_adjustable (fixS *fixP)
3857 if (fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL
3858 || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
3859 || fixP->fx_r_type == BFD_RELOC_SH_GOT20
3860 || fixP->fx_r_type == BFD_RELOC_SH_GOTPC
3861 || fixP->fx_r_type == BFD_RELOC_SH_GOTFUNCDESC
3862 || fixP->fx_r_type == BFD_RELOC_SH_GOTFUNCDESC20
3863 || fixP->fx_r_type == BFD_RELOC_SH_GOTOFFFUNCDESC
3864 || fixP->fx_r_type == BFD_RELOC_SH_GOTOFFFUNCDESC20
3865 || fixP->fx_r_type == BFD_RELOC_SH_FUNCDESC
3866 || ((fixP->fx_r_type == BFD_RELOC_32) && dont_adjust_reloc_32)
3867 || fixP->fx_r_type == BFD_RELOC_RVA)
3868 return 0;
3870 /* We need the symbol name for the VTABLE entries */
3871 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3872 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3873 return 0;
3875 return 1;
3878 void
3879 sh_elf_final_processing (void)
3881 int val;
3883 /* Set file-specific flags to indicate if this code needs
3884 a processor with the sh-dsp / sh2e ISA to execute. */
3885 #ifdef HAVE_SH64
3886 /* SH5 and above don't know about the valid_arch arch_sh* bits defined
3887 in sh-opc.h, so check SH64 mode before checking valid_arch. */
3888 if (sh64_isa_mode != sh64_isa_unspecified)
3889 val = EF_SH5;
3890 else
3891 #elif defined TARGET_SYMBIAN
3892 if (1)
3894 extern int sh_symbian_find_elf_flags (unsigned int);
3896 val = sh_symbian_find_elf_flags (valid_arch);
3898 else
3899 #endif /* HAVE_SH64 */
3900 val = sh_find_elf_flags (valid_arch);
3902 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
3903 elf_elfheader (stdoutput)->e_flags |= val;
3905 if (sh_fdpic)
3906 elf_elfheader (stdoutput)->e_flags |= EF_SH_FDPIC;
3908 #endif
3910 #ifdef TE_UCLINUX
3911 /* Return the target format for uClinux. */
3913 const char *
3914 sh_uclinux_target_format (void)
3916 if (sh_fdpic)
3917 return (!target_big_endian ? "elf32-sh-fdpic" : "elf32-shbig-fdpic");
3918 else
3919 return (!target_big_endian ? "elf32-shl" : "elf32-sh");
3921 #endif
3923 /* Apply fixup FIXP to SIZE-byte field BUF given that VAL is its
3924 assembly-time value. If we're generating a reloc for FIXP,
3925 see whether the addend should be stored in-place or whether
3926 it should be in an ELF r_addend field. */
3928 static void
3929 apply_full_field_fix (fixS *fixP, char *buf, bfd_vma val, int size)
3931 reloc_howto_type *howto;
3933 if (fixP->fx_addsy != NULL || fixP->fx_pcrel)
3935 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
3936 if (howto && !howto->partial_inplace)
3938 fixP->fx_addnumber = val;
3939 return;
3942 md_number_to_chars (buf, val, size);
3945 /* Apply a fixup to the object file. */
3947 void
3948 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3950 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3951 int lowbyte = target_big_endian ? 1 : 0;
3952 int highbyte = target_big_endian ? 0 : 1;
3953 long val = (long) *valP;
3954 long max, min;
3955 int shift;
3957 /* A difference between two symbols, the second of which is in the
3958 current section, is transformed in a PC-relative relocation to
3959 the other symbol. We have to adjust the relocation type here. */
3960 if (fixP->fx_pcrel)
3962 switch (fixP->fx_r_type)
3964 default:
3965 break;
3967 case BFD_RELOC_32:
3968 fixP->fx_r_type = BFD_RELOC_32_PCREL;
3969 break;
3971 /* Currently, we only support 32-bit PCREL relocations.
3972 We'd need a new reloc type to handle 16_PCREL, and
3973 8_PCREL is already taken for R_SH_SWITCH8, which
3974 apparently does something completely different than what
3975 we need. FIXME. */
3976 case BFD_RELOC_16:
3977 bfd_set_error (bfd_error_bad_value);
3978 return;
3980 case BFD_RELOC_8:
3981 bfd_set_error (bfd_error_bad_value);
3982 return;
3986 /* The function adjust_reloc_syms won't convert a reloc against a weak
3987 symbol into a reloc against a section, but bfd_install_relocation
3988 will screw up if the symbol is defined, so we have to adjust val here
3989 to avoid the screw up later.
3991 For ordinary relocs, this does not happen for ELF, since for ELF,
3992 bfd_install_relocation uses the "special function" field of the
3993 howto, and does not execute the code that needs to be undone, as long
3994 as the special function does not return bfd_reloc_continue.
3995 It can happen for GOT- and PLT-type relocs the way they are
3996 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
3997 doesn't matter here since those relocs don't use VAL; see below. */
3998 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3999 && fixP->fx_addsy != NULL
4000 && S_IS_WEAK (fixP->fx_addsy))
4001 val -= S_GET_VALUE (fixP->fx_addsy);
4003 if (SWITCH_TABLE (fixP))
4004 val -= S_GET_VALUE (fixP->fx_subsy);
4006 max = min = 0;
4007 shift = 0;
4008 switch (fixP->fx_r_type)
4010 case BFD_RELOC_SH_IMM3:
4011 max = 0x7;
4012 * buf = (* buf & 0xf8) | (val & 0x7);
4013 break;
4014 case BFD_RELOC_SH_IMM3U:
4015 max = 0x7;
4016 * buf = (* buf & 0x8f) | ((val & 0x7) << 4);
4017 break;
4018 case BFD_RELOC_SH_DISP12:
4019 max = 0xfff;
4020 buf[lowbyte] = val & 0xff;
4021 buf[highbyte] |= (val >> 8) & 0x0f;
4022 break;
4023 case BFD_RELOC_SH_DISP12BY2:
4024 max = 0xfff;
4025 shift = 1;
4026 buf[lowbyte] = (val >> 1) & 0xff;
4027 buf[highbyte] |= (val >> 9) & 0x0f;
4028 break;
4029 case BFD_RELOC_SH_DISP12BY4:
4030 max = 0xfff;
4031 shift = 2;
4032 buf[lowbyte] = (val >> 2) & 0xff;
4033 buf[highbyte] |= (val >> 10) & 0x0f;
4034 break;
4035 case BFD_RELOC_SH_DISP12BY8:
4036 max = 0xfff;
4037 shift = 3;
4038 buf[lowbyte] = (val >> 3) & 0xff;
4039 buf[highbyte] |= (val >> 11) & 0x0f;
4040 break;
4041 case BFD_RELOC_SH_DISP20:
4042 if (! target_big_endian)
4043 abort();
4044 max = 0x7ffff;
4045 min = -0x80000;
4046 buf[1] = (buf[1] & 0x0f) | ((val >> 12) & 0xf0);
4047 buf[2] = (val >> 8) & 0xff;
4048 buf[3] = val & 0xff;
4049 break;
4050 case BFD_RELOC_SH_DISP20BY8:
4051 if (!target_big_endian)
4052 abort();
4053 max = 0x7ffff;
4054 min = -0x80000;
4055 shift = 8;
4056 buf[1] = (buf[1] & 0x0f) | ((val >> 20) & 0xf0);
4057 buf[2] = (val >> 16) & 0xff;
4058 buf[3] = (val >> 8) & 0xff;
4059 break;
4061 case BFD_RELOC_SH_IMM4:
4062 max = 0xf;
4063 *buf = (*buf & 0xf0) | (val & 0xf);
4064 break;
4066 case BFD_RELOC_SH_IMM4BY2:
4067 max = 0xf;
4068 shift = 1;
4069 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
4070 break;
4072 case BFD_RELOC_SH_IMM4BY4:
4073 max = 0xf;
4074 shift = 2;
4075 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
4076 break;
4078 case BFD_RELOC_SH_IMM8BY2:
4079 max = 0xff;
4080 shift = 1;
4081 *buf = val >> 1;
4082 break;
4084 case BFD_RELOC_SH_IMM8BY4:
4085 max = 0xff;
4086 shift = 2;
4087 *buf = val >> 2;
4088 break;
4090 case BFD_RELOC_8:
4091 case BFD_RELOC_SH_IMM8:
4092 /* Sometimes the 8 bit value is sign extended (e.g., add) and
4093 sometimes it is not (e.g., and). We permit any 8 bit value.
4094 Note that adding further restrictions may invalidate
4095 reasonable looking assembly code, such as ``and -0x1,r0''. */
4096 max = 0xff;
4097 min = -0xff;
4098 *buf++ = val;
4099 break;
4101 case BFD_RELOC_SH_PCRELIMM8BY4:
4102 /* If we are dealing with a known destination ... */
4103 if ((fixP->fx_addsy == NULL || S_IS_DEFINED (fixP->fx_addsy))
4104 && (fixP->fx_subsy == NULL || S_IS_DEFINED (fixP->fx_addsy)))
4106 /* Don't silently move the destination due to misalignment.
4107 The absolute address is the fragment base plus the offset into
4108 the fragment plus the pc relative offset to the label. */
4109 if ((fixP->fx_frag->fr_address + fixP->fx_where + val) & 3)
4110 as_bad_where (fixP->fx_file, fixP->fx_line,
4111 _("offset to unaligned destination"));
4113 /* The displacement cannot be zero or backward even if aligned.
4114 Allow -2 because val has already been adjusted somewhere. */
4115 if (val < -2)
4116 as_bad_where (fixP->fx_file, fixP->fx_line, _("negative offset"));
4119 /* The lower two bits of the PC are cleared before the
4120 displacement is added in. We can assume that the destination
4121 is on a 4 byte boundary. If this instruction is also on a 4
4122 byte boundary, then we want
4123 (target - here) / 4
4124 and target - here is a multiple of 4.
4125 Otherwise, we are on a 2 byte boundary, and we want
4126 (target - (here - 2)) / 4
4127 and target - here is not a multiple of 4. Computing
4128 (target - (here - 2)) / 4 == (target - here + 2) / 4
4129 works for both cases, since in the first case the addition of
4130 2 will be removed by the division. target - here is in the
4131 variable val. */
4132 val = (val + 2) / 4;
4133 if (val & ~0xff)
4134 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
4135 buf[lowbyte] = val;
4136 break;
4138 case BFD_RELOC_SH_PCRELIMM8BY2:
4139 val /= 2;
4140 if (val & ~0xff)
4141 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
4142 buf[lowbyte] = val;
4143 break;
4145 case BFD_RELOC_SH_PCDISP8BY2:
4146 val /= 2;
4147 if (val < -0x80 || val > 0x7f)
4148 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
4149 buf[lowbyte] = val;
4150 break;
4152 case BFD_RELOC_SH_PCDISP12BY2:
4153 val /= 2;
4154 if (val < -0x800 || val > 0x7ff)
4155 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
4156 buf[lowbyte] = val & 0xff;
4157 buf[highbyte] |= (val >> 8) & 0xf;
4158 break;
4160 case BFD_RELOC_32:
4161 case BFD_RELOC_32_PCREL:
4162 apply_full_field_fix (fixP, buf, val, 4);
4163 break;
4165 case BFD_RELOC_16:
4166 apply_full_field_fix (fixP, buf, val, 2);
4167 break;
4169 case BFD_RELOC_SH_USES:
4170 /* Pass the value into sh_reloc(). */
4171 fixP->fx_addnumber = val;
4172 break;
4174 case BFD_RELOC_SH_COUNT:
4175 case BFD_RELOC_SH_ALIGN:
4176 case BFD_RELOC_SH_CODE:
4177 case BFD_RELOC_SH_DATA:
4178 case BFD_RELOC_SH_LABEL:
4179 /* Nothing to do here. */
4180 break;
4182 case BFD_RELOC_SH_LOOP_START:
4183 case BFD_RELOC_SH_LOOP_END:
4185 case BFD_RELOC_VTABLE_INHERIT:
4186 case BFD_RELOC_VTABLE_ENTRY:
4187 fixP->fx_done = 0;
4188 return;
4190 #ifdef OBJ_ELF
4191 case BFD_RELOC_32_PLT_PCREL:
4192 /* Make the jump instruction point to the address of the operand. At
4193 runtime we merely add the offset to the actual PLT entry. */
4194 * valP = 0xfffffffc;
4195 val = fixP->fx_offset;
4196 if (fixP->fx_subsy)
4197 val -= S_GET_VALUE (fixP->fx_subsy);
4198 apply_full_field_fix (fixP, buf, val, 4);
4199 break;
4201 case BFD_RELOC_SH_GOTPC:
4202 /* This is tough to explain. We end up with this one if we have
4203 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
4204 The goal here is to obtain the absolute address of the GOT,
4205 and it is strongly preferable from a performance point of
4206 view to avoid using a runtime relocation for this. There are
4207 cases where you have something like:
4209 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
4211 and here no correction would be required. Internally in the
4212 assembler we treat operands of this form as not being pcrel
4213 since the '.' is explicitly mentioned, and I wonder whether
4214 it would simplify matters to do it this way. Who knows. In
4215 earlier versions of the PIC patches, the pcrel_adjust field
4216 was used to store the correction, but since the expression is
4217 not pcrel, I felt it would be confusing to do it this way. */
4218 * valP -= 1;
4219 apply_full_field_fix (fixP, buf, val, 4);
4220 break;
4222 case BFD_RELOC_SH_TLS_GD_32:
4223 case BFD_RELOC_SH_TLS_LD_32:
4224 case BFD_RELOC_SH_TLS_IE_32:
4225 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4226 /* Fallthrough */
4227 case BFD_RELOC_32_GOT_PCREL:
4228 case BFD_RELOC_SH_GOT20:
4229 case BFD_RELOC_SH_GOTPLT32:
4230 case BFD_RELOC_SH_GOTFUNCDESC:
4231 case BFD_RELOC_SH_GOTFUNCDESC20:
4232 case BFD_RELOC_SH_GOTOFFFUNCDESC:
4233 case BFD_RELOC_SH_GOTOFFFUNCDESC20:
4234 case BFD_RELOC_SH_FUNCDESC:
4235 * valP = 0; /* Fully resolved at runtime. No addend. */
4236 apply_full_field_fix (fixP, buf, 0, 4);
4237 break;
4239 case BFD_RELOC_SH_TLS_LDO_32:
4240 case BFD_RELOC_SH_TLS_LE_32:
4241 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4242 /* Fallthrough */
4243 case BFD_RELOC_32_GOTOFF:
4244 case BFD_RELOC_SH_GOTOFF20:
4245 apply_full_field_fix (fixP, buf, val, 4);
4246 break;
4247 #endif
4249 default:
4250 #ifdef HAVE_SH64
4251 shmedia_md_apply_fix (fixP, valP);
4252 return;
4253 #else
4254 abort ();
4255 #endif
4258 if (shift != 0)
4260 if ((val & ((1 << shift) - 1)) != 0)
4261 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
4262 if (val >= 0)
4263 val >>= shift;
4264 else
4265 val = ((val >> shift)
4266 | ((long) -1 & ~ ((long) -1 >> shift)));
4269 /* Extend sign for 64-bit host. */
4270 val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
4271 if (max != 0 && (val < min || val > max))
4272 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
4273 else if (max != 0)
4274 /* Stop the generic code from trying to overlow check the value as well.
4275 It may not have the correct value anyway, as we do not store val back
4276 into *valP. */
4277 fixP->fx_no_overflow = 1;
4279 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
4280 fixP->fx_done = 1;
4283 /* Called just before address relaxation. Return the length
4284 by which a fragment must grow to reach it's destination. */
4287 md_estimate_size_before_relax (fragS *fragP, segT segment_type)
4289 int what;
4291 switch (fragP->fr_subtype)
4293 default:
4294 #ifdef HAVE_SH64
4295 return shmedia_md_estimate_size_before_relax (fragP, segment_type);
4296 #else
4297 abort ();
4298 #endif
4301 case C (UNCOND_JUMP, UNDEF_DISP):
4302 /* Used to be a branch to somewhere which was unknown. */
4303 if (!fragP->fr_symbol)
4305 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
4307 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
4309 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
4311 else
4313 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
4315 break;
4317 case C (COND_JUMP, UNDEF_DISP):
4318 case C (COND_JUMP_DELAY, UNDEF_DISP):
4319 what = GET_WHAT (fragP->fr_subtype);
4320 /* Used to be a branch to somewhere which was unknown. */
4321 if (fragP->fr_symbol
4322 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
4324 /* Got a symbol and it's defined in this segment, become byte
4325 sized - maybe it will fix up. */
4326 fragP->fr_subtype = C (what, COND8);
4328 else if (fragP->fr_symbol)
4330 /* Its got a segment, but its not ours, so it will always be long. */
4331 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
4333 else
4335 /* We know the abs value. */
4336 fragP->fr_subtype = C (what, COND8);
4338 break;
4340 case C (UNCOND_JUMP, UNCOND12):
4341 case C (UNCOND_JUMP, UNCOND32):
4342 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
4343 case C (COND_JUMP, COND8):
4344 case C (COND_JUMP, COND12):
4345 case C (COND_JUMP, COND32):
4346 case C (COND_JUMP, UNDEF_WORD_DISP):
4347 case C (COND_JUMP_DELAY, COND8):
4348 case C (COND_JUMP_DELAY, COND12):
4349 case C (COND_JUMP_DELAY, COND32):
4350 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
4351 /* When relaxing a section for the second time, we don't need to
4352 do anything besides return the current size. */
4353 break;
4356 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
4357 return fragP->fr_var;
4360 /* Put number into target byte order. */
4362 void
4363 md_number_to_chars (char *ptr, valueT use, int nbytes)
4365 #ifdef HAVE_SH64
4366 /* We might need to set the contents type to data. */
4367 sh64_flag_output ();
4368 #endif
4370 if (! target_big_endian)
4371 number_to_chars_littleendian (ptr, use, nbytes);
4372 else
4373 number_to_chars_bigendian (ptr, use, nbytes);
4376 /* This version is used in obj-coff.c eg. for the sh-hms target. */
4378 long
4379 md_pcrel_from (fixS *fixP)
4381 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
4384 long
4385 md_pcrel_from_section (fixS *fixP, segT sec)
4387 if (! sh_local_pcrel (fixP)
4388 && fixP->fx_addsy != (symbolS *) NULL
4389 && (generic_force_reloc (fixP)
4390 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
4392 /* The symbol is undefined (or is defined but not in this section,
4393 or we're not sure about it being the final definition). Let the
4394 linker figure it out. We need to adjust the subtraction of a
4395 symbol to the position of the relocated data, though. */
4396 return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
4399 return md_pcrel_from (fixP);
4402 /* Create a reloc. */
4404 arelent *
4405 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
4407 arelent *rel;
4408 bfd_reloc_code_real_type r_type;
4410 rel = (arelent *) xmalloc (sizeof (arelent));
4411 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4412 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4413 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
4415 r_type = fixp->fx_r_type;
4417 if (SWITCH_TABLE (fixp))
4419 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
4420 rel->addend = 0;
4421 if (r_type == BFD_RELOC_16)
4422 r_type = BFD_RELOC_SH_SWITCH16;
4423 else if (r_type == BFD_RELOC_8)
4424 r_type = BFD_RELOC_8_PCREL;
4425 else if (r_type == BFD_RELOC_32)
4426 r_type = BFD_RELOC_SH_SWITCH32;
4427 else
4428 abort ();
4430 else if (r_type == BFD_RELOC_SH_USES)
4431 rel->addend = fixp->fx_addnumber;
4432 else if (r_type == BFD_RELOC_SH_COUNT)
4433 rel->addend = fixp->fx_offset;
4434 else if (r_type == BFD_RELOC_SH_ALIGN)
4435 rel->addend = fixp->fx_offset;
4436 else if (r_type == BFD_RELOC_VTABLE_INHERIT
4437 || r_type == BFD_RELOC_VTABLE_ENTRY)
4438 rel->addend = fixp->fx_offset;
4439 else if (r_type == BFD_RELOC_SH_LOOP_START
4440 || r_type == BFD_RELOC_SH_LOOP_END)
4441 rel->addend = fixp->fx_offset;
4442 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
4444 rel->addend = 0;
4445 rel->address = rel->addend = fixp->fx_offset;
4447 #ifdef HAVE_SH64
4448 else if (shmedia_init_reloc (rel, fixp))
4450 #endif
4451 else
4452 rel->addend = fixp->fx_addnumber;
4454 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
4456 if (rel->howto == NULL)
4458 as_bad_where (fixp->fx_file, fixp->fx_line,
4459 _("Cannot represent relocation type %s"),
4460 bfd_get_reloc_code_name (r_type));
4461 /* Set howto to a garbage value so that we can keep going. */
4462 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
4463 gas_assert (rel->howto != NULL);
4465 #ifdef OBJ_ELF
4466 else if (rel->howto->type == R_SH_IND12W)
4467 rel->addend += fixp->fx_offset - 4;
4468 #endif
4470 return rel;
4473 #ifdef OBJ_ELF
4474 inline static char *
4475 sh_end_of_match (char *cont, char *what)
4477 int len = strlen (what);
4479 if (strncasecmp (cont, what, strlen (what)) == 0
4480 && ! is_part_of_name (cont[len]))
4481 return cont + len;
4483 return NULL;
4487 sh_parse_name (char const *name,
4488 expressionS *exprP,
4489 enum expr_mode mode,
4490 char *nextcharP)
4492 char *next = input_line_pointer;
4493 char *next_end;
4494 int reloc_type;
4495 segT segment;
4497 exprP->X_op_symbol = NULL;
4499 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
4501 if (! GOT_symbol)
4502 GOT_symbol = symbol_find_or_make (name);
4504 exprP->X_add_symbol = GOT_symbol;
4505 no_suffix:
4506 /* If we have an absolute symbol or a reg, then we know its
4507 value now. */
4508 segment = S_GET_SEGMENT (exprP->X_add_symbol);
4509 if (mode != expr_defer && segment == absolute_section)
4511 exprP->X_op = O_constant;
4512 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4513 exprP->X_add_symbol = NULL;
4515 else if (mode != expr_defer && segment == reg_section)
4517 exprP->X_op = O_register;
4518 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4519 exprP->X_add_symbol = NULL;
4521 else
4523 exprP->X_op = O_symbol;
4524 exprP->X_add_number = 0;
4527 return 1;
4530 exprP->X_add_symbol = symbol_find_or_make (name);
4532 if (*nextcharP != '@')
4533 goto no_suffix;
4534 else if ((next_end = sh_end_of_match (next + 1, "GOTOFF")))
4535 reloc_type = BFD_RELOC_32_GOTOFF;
4536 else if ((next_end = sh_end_of_match (next + 1, "GOTPLT")))
4537 reloc_type = BFD_RELOC_SH_GOTPLT32;
4538 else if ((next_end = sh_end_of_match (next + 1, "GOT")))
4539 reloc_type = BFD_RELOC_32_GOT_PCREL;
4540 else if ((next_end = sh_end_of_match (next + 1, "PLT")))
4541 reloc_type = BFD_RELOC_32_PLT_PCREL;
4542 else if ((next_end = sh_end_of_match (next + 1, "TLSGD")))
4543 reloc_type = BFD_RELOC_SH_TLS_GD_32;
4544 else if ((next_end = sh_end_of_match (next + 1, "TLSLDM")))
4545 reloc_type = BFD_RELOC_SH_TLS_LD_32;
4546 else if ((next_end = sh_end_of_match (next + 1, "GOTTPOFF")))
4547 reloc_type = BFD_RELOC_SH_TLS_IE_32;
4548 else if ((next_end = sh_end_of_match (next + 1, "TPOFF")))
4549 reloc_type = BFD_RELOC_SH_TLS_LE_32;
4550 else if ((next_end = sh_end_of_match (next + 1, "DTPOFF")))
4551 reloc_type = BFD_RELOC_SH_TLS_LDO_32;
4552 else if ((next_end = sh_end_of_match (next + 1, "PCREL")))
4553 reloc_type = BFD_RELOC_32_PCREL;
4554 else if ((next_end = sh_end_of_match (next + 1, "GOTFUNCDESC")))
4555 reloc_type = BFD_RELOC_SH_GOTFUNCDESC;
4556 else if ((next_end = sh_end_of_match (next + 1, "GOTOFFFUNCDESC")))
4557 reloc_type = BFD_RELOC_SH_GOTOFFFUNCDESC;
4558 else if ((next_end = sh_end_of_match (next + 1, "FUNCDESC")))
4559 reloc_type = BFD_RELOC_SH_FUNCDESC;
4560 else
4561 goto no_suffix;
4563 *input_line_pointer = *nextcharP;
4564 input_line_pointer = next_end;
4565 *nextcharP = *input_line_pointer;
4566 *input_line_pointer = '\0';
4568 exprP->X_op = O_PIC_reloc;
4569 exprP->X_add_number = 0;
4570 exprP->X_md = reloc_type;
4572 return 1;
4575 void
4576 sh_cfi_frame_initial_instructions (void)
4578 cfi_add_CFA_def_cfa (15, 0);
4582 sh_regname_to_dw2regnum (char *regname)
4584 unsigned int regnum = -1;
4585 unsigned int i;
4586 const char *p;
4587 char *q;
4588 static struct { char *name; int dw2regnum; } regnames[] =
4590 { "pr", 17 }, { "t", 18 }, { "gbr", 19 }, { "mach", 20 },
4591 { "macl", 21 }, { "fpul", 23 }
4594 for (i = 0; i < ARRAY_SIZE (regnames); ++i)
4595 if (strcmp (regnames[i].name, regname) == 0)
4596 return regnames[i].dw2regnum;
4598 if (regname[0] == 'r')
4600 p = regname + 1;
4601 regnum = strtoul (p, &q, 10);
4602 if (p == q || *q || regnum >= 16)
4603 return -1;
4605 else if (regname[0] == 'f' && regname[1] == 'r')
4607 p = regname + 2;
4608 regnum = strtoul (p, &q, 10);
4609 if (p == q || *q || regnum >= 16)
4610 return -1;
4611 regnum += 25;
4613 else if (regname[0] == 'x' && regname[1] == 'd')
4615 p = regname + 2;
4616 regnum = strtoul (p, &q, 10);
4617 if (p == q || *q || regnum >= 8)
4618 return -1;
4619 regnum += 87;
4621 return regnum;
4623 #endif /* OBJ_ELF */