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