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