Check for alignment when emitting constants on the sh-elf target
[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, 2003
3 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 = A_PMOD_N;
1404 }
1405 else if ( (l0 == 'r' && l1 == '9')
1406 || (l0 == 'i' && l1 == 'y'))
1407 {
1408 src += 2;
1409 op->type = A_PMODY_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 src++;
1430 *ptr = parse_exp (src, op);
1431 op->type = A_IMM;
1432 return;
1433 }
1434
1435 else if (src[0] == '@')
1436 {
1437 *ptr = parse_at (src, op);
1438 return;
1439 }
1440 len = parse_reg (src, &mode, &(op->reg));
1441 if (len)
1442 {
1443 *ptr = src + len;
1444 op->type = mode;
1445 return;
1446 }
1447 else
1448 {
1449 /* Not a reg, the only thing left is a displacement. */
1450 *ptr = parse_exp (src, op);
1451 op->type = A_DISP_PC;
1452 return;
1453 }
1454 }
1455
1456 static char *
1457 get_operands (sh_opcode_info *info, char *args, sh_operand_info *operand)
1458 {
1459 char *ptr = args;
1460 if (info->arg[0])
1461 {
1462 /* The pre-processor will eliminate whitespace in front of '@'
1463 after the first argument; we may be called multiple times
1464 from assemble_ppi, so don't insist on finding whitespace here. */
1465 if (*ptr == ' ')
1466 ptr++;
1467
1468 get_operand (&ptr, operand + 0);
1469 if (info->arg[1])
1470 {
1471 if (*ptr == ',')
1472 {
1473 ptr++;
1474 }
1475 get_operand (&ptr, operand + 1);
1476 /* ??? Hack: psha/pshl have a varying operand number depending on
1477 the type of the first operand. We handle this by having the
1478 three-operand version first and reducing the number of operands
1479 parsed to two if we see that the first operand is an immediate.
1480 This works because no insn with three operands has an immediate
1481 as first operand. */
1482 if (info->arg[2] && operand[0].type != A_IMM)
1483 {
1484 if (*ptr == ',')
1485 {
1486 ptr++;
1487 }
1488 get_operand (&ptr, operand + 2);
1489 }
1490 else
1491 {
1492 operand[2].type = 0;
1493 }
1494 }
1495 else
1496 {
1497 operand[1].type = 0;
1498 operand[2].type = 0;
1499 }
1500 }
1501 else
1502 {
1503 operand[0].type = 0;
1504 operand[1].type = 0;
1505 operand[2].type = 0;
1506 }
1507 return ptr;
1508 }
1509
1510 /* Passed a pointer to a list of opcodes which use different
1511 addressing modes, return the opcode which matches the opcodes
1512 provided. */
1513
1514 static sh_opcode_info *
1515 get_specific (sh_opcode_info *opcode, sh_operand_info *operands)
1516 {
1517 sh_opcode_info *this_try = opcode;
1518 char *name = opcode->name;
1519 int n = 0;
1520
1521 while (opcode->name)
1522 {
1523 this_try = opcode++;
1524 if ((this_try->name != name) && (strcmp (this_try->name, name) != 0))
1525 {
1526 /* We've looked so far down the table that we've run out of
1527 opcodes with the same name. */
1528 return 0;
1529 }
1530
1531 /* Look at both operands needed by the opcodes and provided by
1532 the user - since an arg test will often fail on the same arg
1533 again and again, we'll try and test the last failing arg the
1534 first on each opcode try. */
1535 for (n = 0; this_try->arg[n]; n++)
1536 {
1537 sh_operand_info *user = operands + n;
1538 sh_arg_type arg = this_try->arg[n];
1539
1540 switch (arg)
1541 {
1542 case A_DISP_PC:
1543 if (user->type == A_DISP_PC_ABS)
1544 break;
1545 /* Fall through. */
1546 case A_IMM:
1547 case A_BDISP12:
1548 case A_BDISP8:
1549 case A_DISP_GBR:
1550 case A_MACH:
1551 case A_PR:
1552 case A_MACL:
1553 if (user->type != arg)
1554 goto fail;
1555 break;
1556 case A_R0:
1557 /* opcode needs r0 */
1558 if (user->type != A_REG_N || user->reg != 0)
1559 goto fail;
1560 break;
1561 case A_R0_GBR:
1562 if (user->type != A_R0_GBR || user->reg != 0)
1563 goto fail;
1564 break;
1565 case F_FR0:
1566 if (user->type != F_REG_N || user->reg != 0)
1567 goto fail;
1568 break;
1569
1570 case A_REG_N:
1571 case A_INC_N:
1572 case A_DEC_N:
1573 case A_IND_N:
1574 case A_IND_R0_REG_N:
1575 case A_DISP_REG_N:
1576 case F_REG_N:
1577 case D_REG_N:
1578 case X_REG_N:
1579 case V_REG_N:
1580 case FPUL_N:
1581 case FPSCR_N:
1582 case A_PMOD_N:
1583 case A_PMODY_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 DSP_REG_X:
1630 if (user->type != DSP_REG_N)
1631 goto fail;
1632 switch (user->reg)
1633 {
1634 case A_X0_NUM:
1635 reg_x = 0;
1636 break;
1637 case A_X1_NUM:
1638 reg_x = 1;
1639 break;
1640 case A_A0_NUM:
1641 reg_x = 2;
1642 break;
1643 case A_A1_NUM:
1644 reg_x = 3;
1645 break;
1646 default:
1647 goto fail;
1648 }
1649 break;
1650
1651 case DSP_REG_Y:
1652 if (user->type != DSP_REG_N)
1653 goto fail;
1654 switch (user->reg)
1655 {
1656 case A_Y0_NUM:
1657 reg_y = 0;
1658 break;
1659 case A_Y1_NUM:
1660 reg_y = 1;
1661 break;
1662 case A_M0_NUM:
1663 reg_y = 2;
1664 break;
1665 case A_M1_NUM:
1666 reg_y = 3;
1667 break;
1668 default:
1669 goto fail;
1670 }
1671 break;
1672
1673 case DSP_REG_E:
1674 if (user->type != DSP_REG_N)
1675 goto fail;
1676 switch (user->reg)
1677 {
1678 case A_X0_NUM:
1679 reg_efg = 0 << 10;
1680 break;
1681 case A_X1_NUM:
1682 reg_efg = 1 << 10;
1683 break;
1684 case A_Y0_NUM:
1685 reg_efg = 2 << 10;
1686 break;
1687 case A_A1_NUM:
1688 reg_efg = 3 << 10;
1689 break;
1690 default:
1691 goto fail;
1692 }
1693 break;
1694
1695 case DSP_REG_F:
1696 if (user->type != DSP_REG_N)
1697 goto fail;
1698 switch (user->reg)
1699 {
1700 case A_Y0_NUM:
1701 reg_efg |= 0 << 8;
1702 break;
1703 case A_Y1_NUM:
1704 reg_efg |= 1 << 8;
1705 break;
1706 case A_X0_NUM:
1707 reg_efg |= 2 << 8;
1708 break;
1709 case A_A1_NUM:
1710 reg_efg |= 3 << 8;
1711 break;
1712 default:
1713 goto fail;
1714 }
1715 break;
1716
1717 case DSP_REG_G:
1718 if (user->type != DSP_REG_N)
1719 goto fail;
1720 switch (user->reg)
1721 {
1722 case A_M0_NUM:
1723 reg_efg |= 0 << 2;
1724 break;
1725 case A_M1_NUM:
1726 reg_efg |= 1 << 2;
1727 break;
1728 case A_A0_NUM:
1729 reg_efg |= 2 << 2;
1730 break;
1731 case A_A1_NUM:
1732 reg_efg |= 3 << 2;
1733 break;
1734 default:
1735 goto fail;
1736 }
1737 break;
1738
1739 case A_A0:
1740 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1741 goto fail;
1742 break;
1743 case A_X0:
1744 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1745 goto fail;
1746 break;
1747 case A_X1:
1748 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1749 goto fail;
1750 break;
1751 case A_Y0:
1752 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1753 goto fail;
1754 break;
1755 case A_Y1:
1756 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1757 goto fail;
1758 break;
1759
1760 case F_REG_M:
1761 case D_REG_M:
1762 case X_REG_M:
1763 case V_REG_M:
1764 case FPUL_M:
1765 case FPSCR_M:
1766 /* Opcode needs rn */
1767 if (user->type != arg - F_REG_M + F_REG_N)
1768 goto fail;
1769 reg_m = user->reg;
1770 break;
1771 case DX_REG_M:
1772 if (user->type != D_REG_N && user->type != X_REG_N)
1773 goto fail;
1774 reg_m = user->reg;
1775 break;
1776 case XMTRX_M4:
1777 if (user->type != XMTRX_M4)
1778 goto fail;
1779 reg_m = 4;
1780 break;
1781
1782 default:
1783 printf (_("unhandled %d\n"), arg);
1784 goto fail;
1785 }
1786 }
1787 if ( !(valid_arch & this_try->arch))
1788 goto fail;
1789 valid_arch &= this_try->arch;
1790 return this_try;
1791 fail:
1792 ;
1793 }
1794
1795 return 0;
1796 }
1797
1798 static void
1799 insert (char *where, int how, int pcrel, sh_operand_info *op)
1800 {
1801 fix_new_exp (frag_now,
1802 where - frag_now->fr_literal,
1803 2,
1804 &op->immediate,
1805 pcrel,
1806 how);
1807 }
1808
1809 static void
1810 build_relax (sh_opcode_info *opcode, sh_operand_info *op)
1811 {
1812 int high_byte = target_big_endian ? 0 : 1;
1813 char *p;
1814
1815 if (opcode->arg[0] == A_BDISP8)
1816 {
1817 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
1818 p = frag_var (rs_machine_dependent,
1819 md_relax_table[C (what, COND32)].rlx_length,
1820 md_relax_table[C (what, COND8)].rlx_length,
1821 C (what, 0),
1822 op->immediate.X_add_symbol,
1823 op->immediate.X_add_number,
1824 0);
1825 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
1826 }
1827 else if (opcode->arg[0] == A_BDISP12)
1828 {
1829 p = frag_var (rs_machine_dependent,
1830 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
1831 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
1832 C (UNCOND_JUMP, 0),
1833 op->immediate.X_add_symbol,
1834 op->immediate.X_add_number,
1835 0);
1836 p[high_byte] = (opcode->nibbles[0] << 4);
1837 }
1838
1839 }
1840
1841 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
1842
1843 static char *
1844 insert_loop_bounds (char *output, sh_operand_info *operand)
1845 {
1846 char *name;
1847 symbolS *end_sym;
1848
1849 /* Since the low byte of the opcode will be overwritten by the reloc, we
1850 can just stash the high byte into both bytes and ignore endianness. */
1851 output[0] = 0x8c;
1852 output[1] = 0x8c;
1853 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1854 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1855
1856 if (sh_relax)
1857 {
1858 static int count = 0;
1859
1860 /* If the last loop insn is a two-byte-insn, it is in danger of being
1861 swapped with the insn after it. To prevent this, create a new
1862 symbol - complete with SH_LABEL reloc - after the last loop insn.
1863 If the last loop insn is four bytes long, the symbol will be
1864 right in the middle, but four byte insns are not swapped anyways. */
1865 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
1866 Hence a 9 digit number should be enough to count all REPEATs. */
1867 name = alloca (11);
1868 sprintf (name, "_R%x", count++ & 0x3fffffff);
1869 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
1870 /* Make this a local symbol. */
1871 #ifdef OBJ_COFF
1872 SF_SET_LOCAL (end_sym);
1873 #endif /* OBJ_COFF */
1874 symbol_table_insert (end_sym);
1875 end_sym->sy_value = operand[1].immediate;
1876 end_sym->sy_value.X_add_number += 2;
1877 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
1878 }
1879
1880 output = frag_more (2);
1881 output[0] = 0x8e;
1882 output[1] = 0x8e;
1883 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1884 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1885
1886 return frag_more (2);
1887 }
1888
1889 /* Now we know what sort of opcodes it is, let's build the bytes. */
1890
1891 static unsigned int
1892 build_Mytes (sh_opcode_info *opcode, sh_operand_info *operand)
1893 {
1894 int index;
1895 char nbuf[4];
1896 char *output = frag_more (2);
1897 unsigned int size = 2;
1898 int low_byte = target_big_endian ? 1 : 0;
1899 nbuf[0] = 0;
1900 nbuf[1] = 0;
1901 nbuf[2] = 0;
1902 nbuf[3] = 0;
1903
1904 for (index = 0; index < 4; index++)
1905 {
1906 sh_nibble_type i = opcode->nibbles[index];
1907 if (i < 16)
1908 {
1909 nbuf[index] = i;
1910 }
1911 else
1912 {
1913 switch (i)
1914 {
1915 case REG_N:
1916 nbuf[index] = reg_n;
1917 break;
1918 case REG_M:
1919 nbuf[index] = reg_m;
1920 break;
1921 case SDT_REG_N:
1922 if (reg_n < 2 || reg_n > 5)
1923 as_bad (_("Invalid register: 'r%d'"), reg_n);
1924 nbuf[index] = (reg_n & 3) | 4;
1925 break;
1926 case REG_NM:
1927 nbuf[index] = reg_n | (reg_m >> 2);
1928 break;
1929 case REG_B:
1930 nbuf[index] = reg_b | 0x08;
1931 break;
1932 case IMM0_4BY4:
1933 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
1934 break;
1935 case IMM0_4BY2:
1936 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
1937 break;
1938 case IMM0_4:
1939 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
1940 break;
1941 case IMM1_4BY4:
1942 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
1943 break;
1944 case IMM1_4BY2:
1945 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
1946 break;
1947 case IMM1_4:
1948 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
1949 break;
1950 case IMM0_8BY4:
1951 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
1952 break;
1953 case IMM0_8BY2:
1954 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
1955 break;
1956 case IMM0_8:
1957 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
1958 break;
1959 case IMM1_8BY4:
1960 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
1961 break;
1962 case IMM1_8BY2:
1963 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
1964 break;
1965 case IMM1_8:
1966 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
1967 break;
1968 case PCRELIMM_8BY4:
1969 insert (output, BFD_RELOC_SH_PCRELIMM8BY4,
1970 operand->type != A_DISP_PC_ABS, operand);
1971 break;
1972 case PCRELIMM_8BY2:
1973 insert (output, BFD_RELOC_SH_PCRELIMM8BY2,
1974 operand->type != A_DISP_PC_ABS, operand);
1975 break;
1976 case REPEAT:
1977 output = insert_loop_bounds (output, operand);
1978 nbuf[index] = opcode->nibbles[3];
1979 operand += 2;
1980 break;
1981 default:
1982 printf (_("failed for %d\n"), i);
1983 }
1984 }
1985 }
1986 if (!target_big_endian)
1987 {
1988 output[1] = (nbuf[0] << 4) | (nbuf[1]);
1989 output[0] = (nbuf[2] << 4) | (nbuf[3]);
1990 }
1991 else
1992 {
1993 output[0] = (nbuf[0] << 4) | (nbuf[1]);
1994 output[1] = (nbuf[2] << 4) | (nbuf[3]);
1995 }
1996 return size;
1997 }
1998
1999 /* Find an opcode at the start of *STR_P in the hash table, and set
2000 *STR_P to the first character after the last one read. */
2001
2002 static sh_opcode_info *
2003 find_cooked_opcode (char **str_p)
2004 {
2005 char *str = *str_p;
2006 unsigned char *op_start;
2007 unsigned char *op_end;
2008 char name[20];
2009 int nlen = 0;
2010
2011 /* Drop leading whitespace. */
2012 while (*str == ' ')
2013 str++;
2014
2015 /* Find the op code end.
2016 The pre-processor will eliminate whitespace in front of
2017 any '@' after the first argument; we may be called from
2018 assemble_ppi, so the opcode might be terminated by an '@'. */
2019 for (op_start = op_end = (unsigned char *) (str);
2020 *op_end
2021 && nlen < 20
2022 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
2023 op_end++)
2024 {
2025 unsigned char c = op_start[nlen];
2026
2027 /* The machine independent code will convert CMP/EQ into cmp/EQ
2028 because it thinks the '/' is the end of the symbol. Moreover,
2029 all but the first sub-insn is a parallel processing insn won't
2030 be capitalized. Instead of hacking up the machine independent
2031 code, we just deal with it here. */
2032 c = TOLOWER (c);
2033 name[nlen] = c;
2034 nlen++;
2035 }
2036
2037 name[nlen] = 0;
2038 *str_p = op_end;
2039
2040 if (nlen == 0)
2041 as_bad (_("can't find opcode "));
2042
2043 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
2044 }
2045
2046 /* Assemble a parallel processing insn. */
2047 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
2048
2049 static unsigned int
2050 assemble_ppi (char *op_end, sh_opcode_info *opcode)
2051 {
2052 int movx = 0;
2053 int movy = 0;
2054 int cond = 0;
2055 int field_b = 0;
2056 char *output;
2057 int move_code;
2058 unsigned int size;
2059
2060 for (;;)
2061 {
2062 sh_operand_info operand[3];
2063
2064 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
2065 Make sure we encode a defined insn pattern. */
2066 reg_x = 0;
2067 reg_y = 0;
2068 reg_n = 0;
2069
2070 if (opcode->arg[0] != A_END)
2071 op_end = get_operands (opcode, op_end, operand);
2072 opcode = get_specific (opcode, operand);
2073 if (opcode == 0)
2074 {
2075 /* Couldn't find an opcode which matched the operands. */
2076 char *where = frag_more (2);
2077 size = 2;
2078
2079 where[0] = 0x0;
2080 where[1] = 0x0;
2081 as_bad (_("invalid operands for opcode"));
2082 return size;
2083 }
2084
2085 if (opcode->nibbles[0] != PPI)
2086 as_bad (_("insn can't be combined with parallel processing insn"));
2087
2088 switch (opcode->nibbles[1])
2089 {
2090
2091 case NOPX:
2092 if (movx)
2093 as_bad (_("multiple movx specifications"));
2094 movx = DDT_BASE;
2095 break;
2096 case NOPY:
2097 if (movy)
2098 as_bad (_("multiple movy specifications"));
2099 movy = DDT_BASE;
2100 break;
2101
2102 case MOVX:
2103 if (movx)
2104 as_bad (_("multiple movx specifications"));
2105 if (reg_n < 4 || reg_n > 5)
2106 as_bad (_("invalid movx address register"));
2107 if (opcode->nibbles[2] & 8)
2108 {
2109 if (reg_m == A_A1_NUM)
2110 movx = 1 << 7;
2111 else if (reg_m != A_A0_NUM)
2112 as_bad (_("invalid movx dsp register"));
2113 }
2114 else
2115 {
2116 if (reg_x > 1)
2117 as_bad (_("invalid movx dsp register"));
2118 movx = reg_x << 7;
2119 }
2120 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
2121 break;
2122
2123 case MOVY:
2124 if (movy)
2125 as_bad (_("multiple movy specifications"));
2126 if (opcode->nibbles[2] & 8)
2127 {
2128 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
2129 so add 8 more. */
2130 movy = 8;
2131 if (reg_m == A_A1_NUM)
2132 movy += 1 << 6;
2133 else if (reg_m != A_A0_NUM)
2134 as_bad (_("invalid movy dsp register"));
2135 }
2136 else
2137 {
2138 if (reg_y > 1)
2139 as_bad (_("invalid movy dsp register"));
2140 movy = reg_y << 6;
2141 }
2142 if (reg_n < 6 || reg_n > 7)
2143 as_bad (_("invalid movy address register"));
2144 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
2145 break;
2146
2147 case PSH:
2148 if (operand[0].immediate.X_op != O_constant)
2149 as_bad (_("dsp immediate shift value not constant"));
2150 field_b = ((opcode->nibbles[2] << 12)
2151 | (operand[0].immediate.X_add_number & 127) << 4
2152 | reg_n);
2153 break;
2154 case PPI3:
2155 if (field_b)
2156 as_bad (_("multiple parallel processing specifications"));
2157 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2158 + (reg_x << 6) + (reg_y << 4) + reg_n);
2159 break;
2160 case PDC:
2161 if (cond)
2162 as_bad (_("multiple condition specifications"));
2163 cond = opcode->nibbles[2] << 8;
2164 if (*op_end)
2165 goto skip_cond_check;
2166 break;
2167 case PPIC:
2168 if (field_b)
2169 as_bad (_("multiple parallel processing specifications"));
2170 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2171 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
2172 cond = 0;
2173 break;
2174 case PMUL:
2175 if (field_b)
2176 {
2177 if ((field_b & 0xef00) != 0xa100)
2178 as_bad (_("insn cannot be combined with pmuls"));
2179 field_b -= 0x8100;
2180 switch (field_b & 0xf)
2181 {
2182 case A_X0_NUM:
2183 field_b += 0 - A_X0_NUM;
2184 break;
2185 case A_Y0_NUM:
2186 field_b += 1 - A_Y0_NUM;
2187 break;
2188 case A_A0_NUM:
2189 field_b += 2 - A_A0_NUM;
2190 break;
2191 case A_A1_NUM:
2192 field_b += 3 - A_A1_NUM;
2193 break;
2194 default:
2195 as_bad (_("bad padd / psub pmuls output operand"));
2196 }
2197 /* Generate warning if the destination register for padd / psub
2198 and pmuls is the same ( only for A0 or A1 ).
2199 If the last nibble is 1010 then A0 is used in both
2200 padd / psub and pmuls. If it is 1111 then A1 is used
2201 as destination register in both padd / psub and pmuls. */
2202
2203 if ((((field_b | reg_efg) & 0x000F) == 0x000A)
2204 || (((field_b | reg_efg) & 0x000F) == 0x000F))
2205 as_warn (_("destination register is same for parallel insns"));
2206 }
2207 field_b += 0x4000 + reg_efg;
2208 break;
2209 default:
2210 abort ();
2211 }
2212 if (cond)
2213 {
2214 as_bad (_("condition not followed by conditionalizable insn"));
2215 cond = 0;
2216 }
2217 if (! *op_end)
2218 break;
2219 skip_cond_check:
2220 opcode = find_cooked_opcode (&op_end);
2221 if (opcode == NULL)
2222 {
2223 (as_bad
2224 (_("unrecognized characters at end of parallel processing insn")));
2225 break;
2226 }
2227 }
2228
2229 move_code = movx | movy;
2230 if (field_b)
2231 {
2232 /* Parallel processing insn. */
2233 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
2234
2235 output = frag_more (4);
2236 size = 4;
2237 if (! target_big_endian)
2238 {
2239 output[3] = ppi_code >> 8;
2240 output[2] = ppi_code;
2241 }
2242 else
2243 {
2244 output[2] = ppi_code >> 8;
2245 output[3] = ppi_code;
2246 }
2247 move_code |= 0xf800;
2248 }
2249 else
2250 {
2251 /* Just a double data transfer. */
2252 output = frag_more (2);
2253 size = 2;
2254 }
2255 if (! target_big_endian)
2256 {
2257 output[1] = move_code >> 8;
2258 output[0] = move_code;
2259 }
2260 else
2261 {
2262 output[0] = move_code >> 8;
2263 output[1] = move_code;
2264 }
2265 return size;
2266 }
2267
2268 /* This is the guts of the machine-dependent assembler. STR points to a
2269 machine dependent instruction. This function is supposed to emit
2270 the frags/bytes it assembles to. */
2271
2272 void
2273 md_assemble (char *str)
2274 {
2275 unsigned char *op_end;
2276 sh_operand_info operand[3];
2277 sh_opcode_info *opcode;
2278 unsigned int size = 0;
2279
2280 #ifdef HAVE_SH64
2281 if (sh64_isa_mode == sh64_isa_shmedia)
2282 {
2283 shmedia_md_assemble (str);
2284 return;
2285 }
2286 else
2287 {
2288 /* If we've seen pseudo-directives, make sure any emitted data or
2289 frags are marked as data. */
2290 if (!seen_insn)
2291 {
2292 sh64_update_contents_mark (TRUE);
2293 sh64_set_contents_type (CRT_SH5_ISA16);
2294 }
2295
2296 seen_insn = TRUE;
2297 }
2298 #endif /* HAVE_SH64 */
2299
2300 opcode = find_cooked_opcode (&str);
2301 op_end = str;
2302
2303 if (opcode == NULL)
2304 {
2305 as_bad (_("unknown opcode"));
2306 return;
2307 }
2308
2309 if (sh_relax
2310 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
2311 {
2312 /* Output a CODE reloc to tell the linker that the following
2313 bytes are instructions, not data. */
2314 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2315 BFD_RELOC_SH_CODE);
2316 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
2317 }
2318
2319 if (opcode->nibbles[0] == PPI)
2320 {
2321 size = assemble_ppi (op_end, opcode);
2322 }
2323 else
2324 {
2325 if (opcode->arg[0] == A_BDISP12
2326 || opcode->arg[0] == A_BDISP8)
2327 {
2328 /* Since we skip get_specific here, we have to check & update
2329 valid_arch now. */
2330 if (valid_arch & opcode->arch)
2331 valid_arch &= opcode->arch;
2332 else
2333 as_bad (_("Delayed branches not available on SH1"));
2334 parse_exp (op_end + 1, &operand[0]);
2335 build_relax (opcode, &operand[0]);
2336 }
2337 else
2338 {
2339 if (opcode->arg[0] == A_END)
2340 {
2341 /* Ignore trailing whitespace. If there is any, it has already
2342 been compressed to a single space. */
2343 if (*op_end == ' ')
2344 op_end++;
2345 }
2346 else
2347 {
2348 op_end = get_operands (opcode, op_end, operand);
2349 }
2350 opcode = get_specific (opcode, operand);
2351
2352 if (opcode == 0)
2353 {
2354 /* Couldn't find an opcode which matched the operands. */
2355 char *where = frag_more (2);
2356 size = 2;
2357
2358 where[0] = 0x0;
2359 where[1] = 0x0;
2360 as_bad (_("invalid operands for opcode"));
2361 }
2362 else
2363 {
2364 if (*op_end)
2365 as_bad (_("excess operands: '%s'"), op_end);
2366
2367 size = build_Mytes (opcode, operand);
2368 }
2369 }
2370 }
2371
2372 #ifdef BFD_ASSEMBLER
2373 dwarf2_emit_insn (size);
2374 #endif
2375 }
2376
2377 /* This routine is called each time a label definition is seen. It
2378 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
2379
2380 void
2381 sh_frob_label (void)
2382 {
2383 static fragS *last_label_frag;
2384 static int last_label_offset;
2385
2386 if (sh_relax
2387 && seg_info (now_seg)->tc_segment_info_data.in_code)
2388 {
2389 int offset;
2390
2391 offset = frag_now_fix ();
2392 if (frag_now != last_label_frag
2393 || offset != last_label_offset)
2394 {
2395 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
2396 last_label_frag = frag_now;
2397 last_label_offset = offset;
2398 }
2399 }
2400 }
2401
2402 /* This routine is called when the assembler is about to output some
2403 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
2404
2405 void
2406 sh_flush_pending_output (void)
2407 {
2408 if (sh_relax
2409 && seg_info (now_seg)->tc_segment_info_data.in_code)
2410 {
2411 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2412 BFD_RELOC_SH_DATA);
2413 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
2414 }
2415 }
2416
2417 symbolS *
2418 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
2419 {
2420 return 0;
2421 }
2422
2423 #ifdef OBJ_COFF
2424 #ifndef BFD_ASSEMBLER
2425
2426 void
2427 tc_crawl_symbol_chain (object_headers *headers ATTRIBUTE_UNUSED)
2428 {
2429 printf (_("call to tc_crawl_symbol_chain \n"));
2430 }
2431
2432 void
2433 tc_headers_hook (object_headers *headers ATTRIBUTE_UNUSED)
2434 {
2435 printf (_("call to tc_headers_hook \n"));
2436 }
2437
2438 #endif
2439 #endif
2440
2441 /* Various routines to kill one day. */
2442 /* Equal to MAX_PRECISION in atof-ieee.c. */
2443 #define MAX_LITTLENUMS 6
2444
2445 /* Turn a string in input_line_pointer into a floating point constant
2446 of type TYPE, and store the appropriate bytes in *LITP. The number
2447 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2448 returned, or NULL on OK. */
2449
2450 char *
2451 md_atof (int type, char *litP, int *sizeP)
2452 {
2453 int prec;
2454 LITTLENUM_TYPE words[4];
2455 char *t;
2456 int i;
2457
2458 switch (type)
2459 {
2460 case 'f':
2461 prec = 2;
2462 break;
2463
2464 case 'd':
2465 prec = 4;
2466 break;
2467
2468 default:
2469 *sizeP = 0;
2470 return _("bad call to md_atof");
2471 }
2472
2473 t = atof_ieee (input_line_pointer, type, words);
2474 if (t)
2475 input_line_pointer = t;
2476
2477 *sizeP = prec * 2;
2478
2479 if (! target_big_endian)
2480 {
2481 for (i = prec - 1; i >= 0; i--)
2482 {
2483 md_number_to_chars (litP, (valueT) words[i], 2);
2484 litP += 2;
2485 }
2486 }
2487 else
2488 {
2489 for (i = 0; i < prec; i++)
2490 {
2491 md_number_to_chars (litP, (valueT) words[i], 2);
2492 litP += 2;
2493 }
2494 }
2495
2496 return NULL;
2497 }
2498
2499 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
2500 call instruction. It refers to a label of the instruction which
2501 loads the register which the call uses. We use it to generate a
2502 special reloc for the linker. */
2503
2504 static void
2505 s_uses (int ignore ATTRIBUTE_UNUSED)
2506 {
2507 expressionS ex;
2508
2509 if (! sh_relax)
2510 as_warn (_(".uses pseudo-op seen when not relaxing"));
2511
2512 expression (&ex);
2513
2514 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2515 {
2516 as_bad (_("bad .uses format"));
2517 ignore_rest_of_line ();
2518 return;
2519 }
2520
2521 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
2522
2523 demand_empty_rest_of_line ();
2524 }
2525 \f
2526 const char *md_shortopts = "";
2527 struct option md_longopts[] =
2528 {
2529 #define OPTION_RELAX (OPTION_MD_BASE)
2530 #define OPTION_BIG (OPTION_MD_BASE + 1)
2531 #define OPTION_LITTLE (OPTION_BIG + 1)
2532 #define OPTION_SMALL (OPTION_LITTLE + 1)
2533 #define OPTION_DSP (OPTION_SMALL + 1)
2534 #define OPTION_ISA (OPTION_DSP + 1)
2535
2536 {"relax", no_argument, NULL, OPTION_RELAX},
2537 {"big", no_argument, NULL, OPTION_BIG},
2538 {"little", no_argument, NULL, OPTION_LITTLE},
2539 {"small", no_argument, NULL, OPTION_SMALL},
2540 {"dsp", no_argument, NULL, OPTION_DSP},
2541 {"isa", required_argument, NULL, OPTION_ISA},
2542 #ifdef HAVE_SH64
2543 #define OPTION_ABI (OPTION_ISA + 1)
2544 #define OPTION_NO_MIX (OPTION_ABI + 1)
2545 #define OPTION_SHCOMPACT_CONST_CRANGE (OPTION_NO_MIX + 1)
2546 #define OPTION_NO_EXPAND (OPTION_SHCOMPACT_CONST_CRANGE + 1)
2547 #define OPTION_PT32 (OPTION_NO_EXPAND + 1)
2548 {"abi", required_argument, NULL, OPTION_ABI},
2549 {"no-mix", no_argument, NULL, OPTION_NO_MIX},
2550 {"shcompact-const-crange", no_argument, NULL, OPTION_SHCOMPACT_CONST_CRANGE},
2551 {"no-expand", no_argument, NULL, OPTION_NO_EXPAND},
2552 {"expand-pt32", no_argument, NULL, OPTION_PT32},
2553 #endif /* HAVE_SH64 */
2554
2555 {NULL, no_argument, NULL, 0}
2556 };
2557 size_t md_longopts_size = sizeof (md_longopts);
2558
2559 int
2560 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
2561 {
2562 switch (c)
2563 {
2564 case OPTION_RELAX:
2565 sh_relax = 1;
2566 break;
2567
2568 case OPTION_BIG:
2569 target_big_endian = 1;
2570 break;
2571
2572 case OPTION_LITTLE:
2573 target_big_endian = 0;
2574 break;
2575
2576 case OPTION_SMALL:
2577 sh_small = 1;
2578 break;
2579
2580 case OPTION_DSP:
2581 preset_target_arch = arch_sh1_up & ~arch_sh3e_up;
2582 break;
2583
2584 case OPTION_ISA:
2585 if (strcasecmp (arg, "sh4") == 0)
2586 preset_target_arch = arch_sh4;
2587 else if (strcasecmp (arg, "any") == 0)
2588 preset_target_arch = arch_sh1_up;
2589 #ifdef HAVE_SH64
2590 else if (strcasecmp (arg, "shmedia") == 0)
2591 {
2592 if (sh64_isa_mode == sh64_isa_shcompact)
2593 as_bad (_("Invalid combination: --isa=SHcompact with --isa=SHmedia"));
2594 sh64_isa_mode = sh64_isa_shmedia;
2595 }
2596 else if (strcasecmp (arg, "shcompact") == 0)
2597 {
2598 if (sh64_isa_mode == sh64_isa_shmedia)
2599 as_bad (_("Invalid combination: --isa=SHmedia with --isa=SHcompact"));
2600 if (sh64_abi == sh64_abi_64)
2601 as_bad (_("Invalid combination: --abi=64 with --isa=SHcompact"));
2602 sh64_isa_mode = sh64_isa_shcompact;
2603 }
2604 #endif /* HAVE_SH64 */
2605 else
2606 as_bad ("Invalid argument to --isa option: %s", arg);
2607 break;
2608
2609 #ifdef HAVE_SH64
2610 case OPTION_ABI:
2611 if (strcmp (arg, "32") == 0)
2612 {
2613 if (sh64_abi == sh64_abi_64)
2614 as_bad (_("Invalid combination: --abi=32 with --abi=64"));
2615 sh64_abi = sh64_abi_32;
2616 }
2617 else if (strcmp (arg, "64") == 0)
2618 {
2619 if (sh64_abi == sh64_abi_32)
2620 as_bad (_("Invalid combination: --abi=64 with --abi=32"));
2621 if (sh64_isa_mode == sh64_isa_shcompact)
2622 as_bad (_("Invalid combination: --isa=SHcompact with --abi=64"));
2623 sh64_abi = sh64_abi_64;
2624 }
2625 else
2626 as_bad ("Invalid argument to --abi option: %s", arg);
2627 break;
2628
2629 case OPTION_NO_MIX:
2630 sh64_mix = FALSE;
2631 break;
2632
2633 case OPTION_SHCOMPACT_CONST_CRANGE:
2634 sh64_shcompact_const_crange = TRUE;
2635 break;
2636
2637 case OPTION_NO_EXPAND:
2638 sh64_expand = FALSE;
2639 break;
2640
2641 case OPTION_PT32:
2642 sh64_pt32 = TRUE;
2643 break;
2644 #endif /* HAVE_SH64 */
2645
2646 default:
2647 return 0;
2648 }
2649
2650 return 1;
2651 }
2652
2653 void
2654 md_show_usage (FILE *stream)
2655 {
2656 fprintf (stream, _("\
2657 SH options:\n\
2658 -little generate little endian code\n\
2659 -big generate big endian code\n\
2660 -relax alter jump instructions for long displacements\n\
2661 -small align sections to 4 byte boundaries, not 16\n\
2662 -dsp enable sh-dsp insns, and disable sh2e/sh3e/sh4 insns.\n"));
2663 #ifdef HAVE_SH64
2664 fprintf (stream, _("\
2665 -isa=[shmedia set default instruction set for SH64\n\
2666 | SHmedia\n\
2667 | shcompact\n\
2668 | SHcompact]\n\
2669 -abi=[32|64] set size of expanded SHmedia operands and object\n\
2670 file type\n\
2671 -shcompact-const-crange emit code-range descriptors for constants in\n\
2672 SHcompact code sections\n\
2673 -no-mix disallow SHmedia code in the same section as\n\
2674 constants and SHcompact code\n\
2675 -no-expand do not expand MOVI, PT, PTA or PTB instructions\n\
2676 -expand-pt32 with -abi=64, expand PT, PTA and PTB instructions\n\
2677 to 32 bits only"));
2678 #endif /* HAVE_SH64 */
2679 }
2680 \f
2681 /* This struct is used to pass arguments to sh_count_relocs through
2682 bfd_map_over_sections. */
2683
2684 struct sh_count_relocs
2685 {
2686 /* Symbol we are looking for. */
2687 symbolS *sym;
2688 /* Count of relocs found. */
2689 int count;
2690 };
2691
2692 /* Count the number of fixups in a section which refer to a particular
2693 symbol. When using BFD_ASSEMBLER, this is called via
2694 bfd_map_over_sections. */
2695
2696 static void
2697 sh_count_relocs (bfd *abfd ATTRIBUTE_UNUSED, segT sec, void *data)
2698 {
2699 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
2700 segment_info_type *seginfo;
2701 symbolS *sym;
2702 fixS *fix;
2703
2704 seginfo = seg_info (sec);
2705 if (seginfo == NULL)
2706 return;
2707
2708 sym = info->sym;
2709 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2710 {
2711 if (fix->fx_addsy == sym)
2712 {
2713 ++info->count;
2714 fix->fx_tcbit = 1;
2715 }
2716 }
2717 }
2718
2719 /* Handle the count relocs for a particular section. When using
2720 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
2721
2722 static void
2723 sh_frob_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec,
2724 void *ignore ATTRIBUTE_UNUSED)
2725 {
2726 segment_info_type *seginfo;
2727 fixS *fix;
2728
2729 seginfo = seg_info (sec);
2730 if (seginfo == NULL)
2731 return;
2732
2733 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2734 {
2735 symbolS *sym;
2736 bfd_vma val;
2737 fixS *fscan;
2738 struct sh_count_relocs info;
2739
2740 if (fix->fx_r_type != BFD_RELOC_SH_USES)
2741 continue;
2742
2743 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2744 symbol in the same section. */
2745 sym = fix->fx_addsy;
2746 if (sym == NULL
2747 || fix->fx_subsy != NULL
2748 || fix->fx_addnumber != 0
2749 || S_GET_SEGMENT (sym) != sec
2750 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2751 || S_GET_STORAGE_CLASS (sym) == C_EXT
2752 #endif
2753 || S_IS_EXTERNAL (sym))
2754 {
2755 as_warn_where (fix->fx_file, fix->fx_line,
2756 _(".uses does not refer to a local symbol in the same section"));
2757 continue;
2758 }
2759
2760 /* Look through the fixups again, this time looking for one
2761 at the same location as sym. */
2762 val = S_GET_VALUE (sym);
2763 for (fscan = seginfo->fix_root;
2764 fscan != NULL;
2765 fscan = fscan->fx_next)
2766 if (val == fscan->fx_frag->fr_address + fscan->fx_where
2767 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
2768 && fscan->fx_r_type != BFD_RELOC_SH_CODE
2769 && fscan->fx_r_type != BFD_RELOC_SH_DATA
2770 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
2771 break;
2772 if (fscan == NULL)
2773 {
2774 as_warn_where (fix->fx_file, fix->fx_line,
2775 _("can't find fixup pointed to by .uses"));
2776 continue;
2777 }
2778
2779 if (fscan->fx_tcbit)
2780 {
2781 /* We've already done this one. */
2782 continue;
2783 }
2784
2785 /* The variable fscan should also be a fixup to a local symbol
2786 in the same section. */
2787 sym = fscan->fx_addsy;
2788 if (sym == NULL
2789 || fscan->fx_subsy != NULL
2790 || fscan->fx_addnumber != 0
2791 || S_GET_SEGMENT (sym) != sec
2792 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2793 || S_GET_STORAGE_CLASS (sym) == C_EXT
2794 #endif
2795 || S_IS_EXTERNAL (sym))
2796 {
2797 as_warn_where (fix->fx_file, fix->fx_line,
2798 _(".uses target does not refer to a local symbol in the same section"));
2799 continue;
2800 }
2801
2802 /* Now we look through all the fixups of all the sections,
2803 counting the number of times we find a reference to sym. */
2804 info.sym = sym;
2805 info.count = 0;
2806 #ifdef BFD_ASSEMBLER
2807 bfd_map_over_sections (stdoutput, sh_count_relocs, &info);
2808 #else
2809 {
2810 int iscan;
2811
2812 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
2813 sh_count_relocs ((bfd *) NULL, iscan, &info);
2814 }
2815 #endif
2816
2817 if (info.count < 1)
2818 abort ();
2819
2820 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
2821 We have already adjusted the value of sym to include the
2822 fragment address, so we undo that adjustment here. */
2823 subseg_change (sec, 0);
2824 fix_new (fscan->fx_frag,
2825 S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
2826 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
2827 }
2828 }
2829
2830 /* This function is called after the symbol table has been completed,
2831 but before the relocs or section contents have been written out.
2832 If we have seen any .uses pseudo-ops, they point to an instruction
2833 which loads a register with the address of a function. We look
2834 through the fixups to find where the function address is being
2835 loaded from. We then generate a COUNT reloc giving the number of
2836 times that function address is referred to. The linker uses this
2837 information when doing relaxing, to decide when it can eliminate
2838 the stored function address entirely. */
2839
2840 void
2841 sh_frob_file (void)
2842 {
2843 #ifdef HAVE_SH64
2844 shmedia_frob_file_before_adjust ();
2845 #endif
2846
2847 if (! sh_relax)
2848 return;
2849
2850 #ifdef BFD_ASSEMBLER
2851 bfd_map_over_sections (stdoutput, sh_frob_section, NULL);
2852 #else
2853 {
2854 int iseg;
2855
2856 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
2857 sh_frob_section ((bfd *) NULL, iseg, NULL);
2858 }
2859 #endif
2860 }
2861
2862 /* Called after relaxing. Set the correct sizes of the fragments, and
2863 create relocs so that md_apply_fix3 will fill in the correct values. */
2864
2865 void
2866 #ifdef BFD_ASSEMBLER
2867 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT seg, fragS *fragP)
2868 #else
2869 md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED, segT seg,
2870 fragS *fragP)
2871 #endif
2872 {
2873 int donerelax = 0;
2874
2875 switch (fragP->fr_subtype)
2876 {
2877 case C (COND_JUMP, COND8):
2878 case C (COND_JUMP_DELAY, COND8):
2879 subseg_change (seg, 0);
2880 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2881 1, BFD_RELOC_SH_PCDISP8BY2);
2882 fragP->fr_fix += 2;
2883 fragP->fr_var = 0;
2884 break;
2885
2886 case C (UNCOND_JUMP, UNCOND12):
2887 subseg_change (seg, 0);
2888 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2889 1, BFD_RELOC_SH_PCDISP12BY2);
2890 fragP->fr_fix += 2;
2891 fragP->fr_var = 0;
2892 break;
2893
2894 case C (UNCOND_JUMP, UNCOND32):
2895 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
2896 if (fragP->fr_symbol == NULL)
2897 as_bad_where (fragP->fr_file, fragP->fr_line,
2898 _("displacement overflows 12-bit field"));
2899 else if (S_IS_DEFINED (fragP->fr_symbol))
2900 as_bad_where (fragP->fr_file, fragP->fr_line,
2901 _("displacement to defined symbol %s overflows 12-bit field"),
2902 S_GET_NAME (fragP->fr_symbol));
2903 else
2904 as_bad_where (fragP->fr_file, fragP->fr_line,
2905 _("displacement to undefined symbol %s overflows 12-bit field"),
2906 S_GET_NAME (fragP->fr_symbol));
2907 /* Stabilize this frag, so we don't trip an assert. */
2908 fragP->fr_fix += fragP->fr_var;
2909 fragP->fr_var = 0;
2910 break;
2911
2912 case C (COND_JUMP, COND12):
2913 case C (COND_JUMP_DELAY, COND12):
2914 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
2915 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
2916 was due to gas incorrectly relaxing an out-of-range conditional
2917 branch with delay slot. It turned:
2918 bf.s L6 (slot mov.l r12,@(44,r0))
2919 into:
2920
2921 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
2922 30: 00 09 nop
2923 32: 10 cb mov.l r12,@(44,r0)
2924 Therefore, branches with delay slots have to be handled
2925 differently from ones without delay slots. */
2926 {
2927 unsigned char *buffer =
2928 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
2929 int highbyte = target_big_endian ? 0 : 1;
2930 int lowbyte = target_big_endian ? 1 : 0;
2931 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
2932
2933 /* Toggle the true/false bit of the bcond. */
2934 buffer[highbyte] ^= 0x2;
2935
2936 /* If this is a delayed branch, we may not put the bra in the
2937 slot. So we change it to a non-delayed branch, like that:
2938 b! cond slot_label; bra disp; slot_label: slot_insn
2939 ??? We should try if swapping the conditional branch and
2940 its delay-slot insn already makes the branch reach. */
2941
2942 /* Build a relocation to six / four bytes farther on. */
2943 subseg_change (seg, 0);
2944 fix_new (fragP, fragP->fr_fix, 2,
2945 #ifdef BFD_ASSEMBLER
2946 section_symbol (seg),
2947 #else
2948 seg_info (seg)->dot,
2949 #endif
2950 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
2951 1, BFD_RELOC_SH_PCDISP8BY2);
2952
2953 /* Set up a jump instruction. */
2954 buffer[highbyte + 2] = 0xa0;
2955 buffer[lowbyte + 2] = 0;
2956 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
2957 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
2958
2959 if (delay)
2960 {
2961 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
2962 fragP->fr_fix += 4;
2963 }
2964 else
2965 {
2966 /* Fill in a NOP instruction. */
2967 buffer[highbyte + 4] = 0x0;
2968 buffer[lowbyte + 4] = 0x9;
2969
2970 fragP->fr_fix += 6;
2971 }
2972 fragP->fr_var = 0;
2973 donerelax = 1;
2974 }
2975 break;
2976
2977 case C (COND_JUMP, COND32):
2978 case C (COND_JUMP_DELAY, COND32):
2979 case C (COND_JUMP, UNDEF_WORD_DISP):
2980 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
2981 if (fragP->fr_symbol == NULL)
2982 as_bad_where (fragP->fr_file, fragP->fr_line,
2983 _("displacement overflows 8-bit field"));
2984 else if (S_IS_DEFINED (fragP->fr_symbol))
2985 as_bad_where (fragP->fr_file, fragP->fr_line,
2986 _("displacement to defined symbol %s overflows 8-bit field"),
2987 S_GET_NAME (fragP->fr_symbol));
2988 else
2989 as_bad_where (fragP->fr_file, fragP->fr_line,
2990 _("displacement to undefined symbol %s overflows 8-bit field "),
2991 S_GET_NAME (fragP->fr_symbol));
2992 /* Stabilize this frag, so we don't trip an assert. */
2993 fragP->fr_fix += fragP->fr_var;
2994 fragP->fr_var = 0;
2995 break;
2996
2997 default:
2998 #ifdef HAVE_SH64
2999 shmedia_md_convert_frag (headers, seg, fragP, TRUE);
3000 #else
3001 abort ();
3002 #endif
3003 }
3004
3005 if (donerelax && !sh_relax)
3006 as_warn_where (fragP->fr_file, fragP->fr_line,
3007 _("overflow in branch to %s; converted into longer instruction sequence"),
3008 (fragP->fr_symbol != NULL
3009 ? S_GET_NAME (fragP->fr_symbol)
3010 : ""));
3011 }
3012
3013 valueT
3014 md_section_align (segT seg ATTRIBUTE_UNUSED, valueT size)
3015 {
3016 #ifdef BFD_ASSEMBLER
3017 #ifdef OBJ_ELF
3018 return size;
3019 #else /* ! OBJ_ELF */
3020 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
3021 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
3022 #endif /* ! OBJ_ELF */
3023 #else /* ! BFD_ASSEMBLER */
3024 return ((size + (1 << section_alignment[(int) seg]) - 1)
3025 & (-1 << section_alignment[(int) seg]));
3026 #endif /* ! BFD_ASSEMBLER */
3027 }
3028
3029 /* This static variable is set by s_uacons to tell sh_cons_align that
3030 the expression does not need to be aligned. */
3031
3032 static int sh_no_align_cons = 0;
3033
3034 /* This handles the unaligned space allocation pseudo-ops, such as
3035 .uaword. .uaword is just like .word, but the value does not need
3036 to be aligned. */
3037
3038 static void
3039 s_uacons (int bytes)
3040 {
3041 /* Tell sh_cons_align not to align this value. */
3042 sh_no_align_cons = 1;
3043 cons (bytes);
3044 }
3045
3046 /* If a .word, et. al., pseud-op is seen, warn if the value is not
3047 aligned correctly. Note that this can cause warnings to be issued
3048 when assembling initialized structured which were declared with the
3049 packed attribute. FIXME: Perhaps we should require an option to
3050 enable this warning? */
3051
3052 void
3053 sh_cons_align (int nbytes)
3054 {
3055 int nalign;
3056 char *p;
3057
3058 if (sh_no_align_cons)
3059 {
3060 /* This is an unaligned pseudo-op. */
3061 sh_no_align_cons = 0;
3062 return;
3063 }
3064
3065 nalign = 0;
3066 while ((nbytes & 1) == 0)
3067 {
3068 ++nalign;
3069 nbytes >>= 1;
3070 }
3071
3072 if (nalign == 0)
3073 return;
3074
3075 if (now_seg == absolute_section)
3076 {
3077 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3078 as_warn (_("misaligned data"));
3079 return;
3080 }
3081
3082 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
3083 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3084
3085 record_alignment (now_seg, nalign);
3086 }
3087
3088 /* When relaxing, we need to output a reloc for any .align directive
3089 that requests alignment to a four byte boundary or larger. This is
3090 also where we check for misaligned data. */
3091
3092 void
3093 sh_handle_align (fragS *frag)
3094 {
3095 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
3096
3097 if (frag->fr_type == rs_align_code)
3098 {
3099 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
3100 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
3101
3102 char *p = frag->fr_literal + frag->fr_fix;
3103
3104 if (bytes & 1)
3105 {
3106 *p++ = 0;
3107 bytes--;
3108 frag->fr_fix += 1;
3109 }
3110
3111 if (target_big_endian)
3112 {
3113 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
3114 frag->fr_var = sizeof big_nop_pattern;
3115 }
3116 else
3117 {
3118 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
3119 frag->fr_var = sizeof little_nop_pattern;
3120 }
3121 }
3122 else if (frag->fr_type == rs_align_test)
3123 {
3124 if (bytes != 0)
3125 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
3126 }
3127
3128 if (sh_relax
3129 && (frag->fr_type == rs_align
3130 || frag->fr_type == rs_align_code)
3131 && frag->fr_address + frag->fr_fix > 0
3132 && frag->fr_offset > 1
3133 && now_seg != bss_section)
3134 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
3135 BFD_RELOC_SH_ALIGN);
3136 }
3137
3138 /* See whether the relocation should be resolved locally. */
3139
3140 static bfd_boolean
3141 sh_local_pcrel (fixS *fix)
3142 {
3143 return (! sh_relax
3144 && (fix->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
3145 || fix->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
3146 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
3147 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
3148 || fix->fx_r_type == BFD_RELOC_8_PCREL
3149 || fix->fx_r_type == BFD_RELOC_SH_SWITCH16
3150 || fix->fx_r_type == BFD_RELOC_SH_SWITCH32));
3151 }
3152
3153 /* See whether we need to force a relocation into the output file.
3154 This is used to force out switch and PC relative relocations when
3155 relaxing. */
3156
3157 int
3158 sh_force_relocation (fixS *fix)
3159 {
3160 /* These relocations can't make it into a DSO, so no use forcing
3161 them for global symbols. */
3162 if (sh_local_pcrel (fix))
3163 return 0;
3164
3165 /* Make sure some relocations get emitted. */
3166 if (fix->fx_r_type == BFD_RELOC_SH_LOOP_START
3167 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END
3168 || fix->fx_r_type == BFD_RELOC_SH_TLS_GD_32
3169 || fix->fx_r_type == BFD_RELOC_SH_TLS_LD_32
3170 || fix->fx_r_type == BFD_RELOC_SH_TLS_IE_32
3171 || fix->fx_r_type == BFD_RELOC_SH_TLS_LDO_32
3172 || fix->fx_r_type == BFD_RELOC_SH_TLS_LE_32
3173 || generic_force_reloc (fix))
3174 return 1;
3175
3176 if (! sh_relax)
3177 return 0;
3178
3179 return (fix->fx_pcrel
3180 || SWITCH_TABLE (fix)
3181 || fix->fx_r_type == BFD_RELOC_SH_COUNT
3182 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
3183 || fix->fx_r_type == BFD_RELOC_SH_CODE
3184 || fix->fx_r_type == BFD_RELOC_SH_DATA
3185 #ifdef HAVE_SH64
3186 || fix->fx_r_type == BFD_RELOC_SH_SHMEDIA_CODE
3187 #endif
3188 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
3189 }
3190
3191 #ifdef OBJ_ELF
3192 bfd_boolean
3193 sh_fix_adjustable (fixS *fixP)
3194 {
3195 if (fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL
3196 || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
3197 || fixP->fx_r_type == BFD_RELOC_SH_GOTPC
3198 || fixP->fx_r_type == BFD_RELOC_RVA)
3199 return 0;
3200
3201 /* We need the symbol name for the VTABLE entries */
3202 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3203 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3204 return 0;
3205
3206 return 1;
3207 }
3208
3209 void
3210 sh_elf_final_processing (void)
3211 {
3212 int val;
3213
3214 /* Set file-specific flags to indicate if this code needs
3215 a processor with the sh-dsp / sh3e ISA to execute. */
3216 #ifdef HAVE_SH64
3217 /* SH5 and above don't know about the valid_arch arch_sh* bits defined
3218 in sh-opc.h, so check SH64 mode before checking valid_arch. */
3219 if (sh64_isa_mode != sh64_isa_unspecified)
3220 val = EF_SH5;
3221 else
3222 #endif /* HAVE_SH64 */
3223 if (valid_arch & arch_sh1)
3224 val = EF_SH1;
3225 else if (valid_arch & arch_sh2)
3226 val = EF_SH2;
3227 else if (valid_arch & arch_sh2e)
3228 val = EF_SH2E;
3229 else if (valid_arch & arch_sh_dsp)
3230 val = EF_SH_DSP;
3231 else if (valid_arch & arch_sh3)
3232 val = EF_SH3;
3233 else if (valid_arch & arch_sh3_dsp)
3234 val = EF_SH_DSP;
3235 else if (valid_arch & arch_sh3e)
3236 val = EF_SH3E;
3237 else if (valid_arch & arch_sh4)
3238 val = EF_SH4;
3239 else
3240 abort ();
3241
3242 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
3243 elf_elfheader (stdoutput)->e_flags |= val;
3244 }
3245 #endif
3246
3247 /* Apply a fixup to the object file. */
3248
3249 void
3250 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3251 {
3252 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3253 int lowbyte = target_big_endian ? 1 : 0;
3254 int highbyte = target_big_endian ? 0 : 1;
3255 long val = (long) *valP;
3256 long max, min;
3257 int shift;
3258
3259 #ifdef BFD_ASSEMBLER
3260 /* A difference between two symbols, the second of which is in the
3261 current section, is transformed in a PC-relative relocation to
3262 the other symbol. We have to adjust the relocation type here. */
3263 if (fixP->fx_pcrel)
3264 {
3265 switch (fixP->fx_r_type)
3266 {
3267 default:
3268 break;
3269
3270 case BFD_RELOC_32:
3271 fixP->fx_r_type = BFD_RELOC_32_PCREL;
3272 break;
3273
3274 /* Currently, we only support 32-bit PCREL relocations.
3275 We'd need a new reloc type to handle 16_PCREL, and
3276 8_PCREL is already taken for R_SH_SWITCH8, which
3277 apparently does something completely different than what
3278 we need. FIXME. */
3279 case BFD_RELOC_16:
3280 bfd_set_error (bfd_error_bad_value);
3281 return;
3282
3283 case BFD_RELOC_8:
3284 bfd_set_error (bfd_error_bad_value);
3285 return;
3286 }
3287 }
3288
3289 /* The function adjust_reloc_syms won't convert a reloc against a weak
3290 symbol into a reloc against a section, but bfd_install_relocation
3291 will screw up if the symbol is defined, so we have to adjust val here
3292 to avoid the screw up later.
3293
3294 For ordinary relocs, this does not happen for ELF, since for ELF,
3295 bfd_install_relocation uses the "special function" field of the
3296 howto, and does not execute the code that needs to be undone, as long
3297 as the special function does not return bfd_reloc_continue.
3298 It can happen for GOT- and PLT-type relocs the way they are
3299 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
3300 doesn't matter here since those relocs don't use VAL; see below. */
3301 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3302 && fixP->fx_addsy != NULL
3303 && S_IS_WEAK (fixP->fx_addsy))
3304 val -= S_GET_VALUE (fixP->fx_addsy);
3305 #endif
3306
3307 #ifdef BFD_ASSEMBLER
3308 if (SWITCH_TABLE (fixP))
3309 val -= S_GET_VALUE (fixP->fx_subsy);
3310 #else
3311 if (fixP->fx_r_type == 0)
3312 {
3313 if (fixP->fx_size == 2)
3314 fixP->fx_r_type = BFD_RELOC_16;
3315 else if (fixP->fx_size == 4)
3316 fixP->fx_r_type = BFD_RELOC_32;
3317 else if (fixP->fx_size == 1)
3318 fixP->fx_r_type = BFD_RELOC_8;
3319 else
3320 abort ();
3321 }
3322 #endif
3323
3324 max = min = 0;
3325 shift = 0;
3326 switch (fixP->fx_r_type)
3327 {
3328 case BFD_RELOC_SH_IMM4:
3329 max = 0xf;
3330 *buf = (*buf & 0xf0) | (val & 0xf);
3331 break;
3332
3333 case BFD_RELOC_SH_IMM4BY2:
3334 max = 0xf;
3335 shift = 1;
3336 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
3337 break;
3338
3339 case BFD_RELOC_SH_IMM4BY4:
3340 max = 0xf;
3341 shift = 2;
3342 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
3343 break;
3344
3345 case BFD_RELOC_SH_IMM8BY2:
3346 max = 0xff;
3347 shift = 1;
3348 *buf = val >> 1;
3349 break;
3350
3351 case BFD_RELOC_SH_IMM8BY4:
3352 max = 0xff;
3353 shift = 2;
3354 *buf = val >> 2;
3355 break;
3356
3357 case BFD_RELOC_8:
3358 case BFD_RELOC_SH_IMM8:
3359 /* Sometimes the 8 bit value is sign extended (e.g., add) and
3360 sometimes it is not (e.g., and). We permit any 8 bit value.
3361 Note that adding further restrictions may invalidate
3362 reasonable looking assembly code, such as ``and -0x1,r0''. */
3363 max = 0xff;
3364 min = -0xff;
3365 *buf++ = val;
3366 break;
3367
3368 case BFD_RELOC_SH_PCRELIMM8BY4:
3369 /* The lower two bits of the PC are cleared before the
3370 displacement is added in. We can assume that the destination
3371 is on a 4 byte boundary. If this instruction is also on a 4
3372 byte boundary, then we want
3373 (target - here) / 4
3374 and target - here is a multiple of 4.
3375 Otherwise, we are on a 2 byte boundary, and we want
3376 (target - (here - 2)) / 4
3377 and target - here is not a multiple of 4. Computing
3378 (target - (here - 2)) / 4 == (target - here + 2) / 4
3379 works for both cases, since in the first case the addition of
3380 2 will be removed by the division. target - here is in the
3381 variable val. */
3382 val = (val + 2) / 4;
3383 if (val & ~0xff)
3384 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3385 buf[lowbyte] = val;
3386 break;
3387
3388 case BFD_RELOC_SH_PCRELIMM8BY2:
3389 val /= 2;
3390 if (val & ~0xff)
3391 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3392 buf[lowbyte] = val;
3393 break;
3394
3395 case BFD_RELOC_SH_PCDISP8BY2:
3396 val /= 2;
3397 if (val < -0x80 || val > 0x7f)
3398 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3399 buf[lowbyte] = val;
3400 break;
3401
3402 case BFD_RELOC_SH_PCDISP12BY2:
3403 val /= 2;
3404 if (val < -0x800 || val > 0x7ff)
3405 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3406 buf[lowbyte] = val & 0xff;
3407 buf[highbyte] |= (val >> 8) & 0xf;
3408 break;
3409
3410 case BFD_RELOC_32:
3411 case BFD_RELOC_32_PCREL:
3412 md_number_to_chars (buf, val, 4);
3413 break;
3414
3415 case BFD_RELOC_16:
3416 md_number_to_chars (buf, val, 2);
3417 break;
3418
3419 case BFD_RELOC_SH_USES:
3420 /* Pass the value into sh_coff_reloc_mangle. */
3421 fixP->fx_addnumber = val;
3422 break;
3423
3424 case BFD_RELOC_SH_COUNT:
3425 case BFD_RELOC_SH_ALIGN:
3426 case BFD_RELOC_SH_CODE:
3427 case BFD_RELOC_SH_DATA:
3428 case BFD_RELOC_SH_LABEL:
3429 /* Nothing to do here. */
3430 break;
3431
3432 case BFD_RELOC_SH_LOOP_START:
3433 case BFD_RELOC_SH_LOOP_END:
3434
3435 case BFD_RELOC_VTABLE_INHERIT:
3436 case BFD_RELOC_VTABLE_ENTRY:
3437 fixP->fx_done = 0;
3438 return;
3439
3440 #ifdef OBJ_ELF
3441 case BFD_RELOC_32_PLT_PCREL:
3442 /* Make the jump instruction point to the address of the operand. At
3443 runtime we merely add the offset to the actual PLT entry. */
3444 * valP = 0xfffffffc;
3445 val = fixP->fx_offset;
3446 if (fixP->fx_subsy)
3447 val -= S_GET_VALUE (fixP->fx_subsy);
3448 fixP->fx_addnumber = val;
3449 md_number_to_chars (buf, val, 4);
3450 break;
3451
3452 case BFD_RELOC_SH_GOTPC:
3453 /* This is tough to explain. We end up with this one if we have
3454 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
3455 The goal here is to obtain the absolute address of the GOT,
3456 and it is strongly preferable from a performance point of
3457 view to avoid using a runtime relocation for this. There are
3458 cases where you have something like:
3459
3460 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
3461
3462 and here no correction would be required. Internally in the
3463 assembler we treat operands of this form as not being pcrel
3464 since the '.' is explicitly mentioned, and I wonder whether
3465 it would simplify matters to do it this way. Who knows. In
3466 earlier versions of the PIC patches, the pcrel_adjust field
3467 was used to store the correction, but since the expression is
3468 not pcrel, I felt it would be confusing to do it this way. */
3469 * valP -= 1;
3470 md_number_to_chars (buf, val, 4);
3471 break;
3472
3473 case BFD_RELOC_SH_TLS_GD_32:
3474 case BFD_RELOC_SH_TLS_LD_32:
3475 case BFD_RELOC_SH_TLS_IE_32:
3476 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3477 /* Fallthrough */
3478 case BFD_RELOC_32_GOT_PCREL:
3479 case BFD_RELOC_SH_GOTPLT32:
3480 * valP = 0; /* Fully resolved at runtime. No addend. */
3481 md_number_to_chars (buf, 0, 4);
3482 break;
3483
3484 case BFD_RELOC_SH_TLS_LDO_32:
3485 case BFD_RELOC_SH_TLS_LE_32:
3486 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3487 /* Fallthrough */
3488 case BFD_RELOC_32_GOTOFF:
3489 md_number_to_chars (buf, val, 4);
3490 break;
3491 #endif
3492
3493 default:
3494 #ifdef HAVE_SH64
3495 shmedia_md_apply_fix3 (fixP, valP);
3496 return;
3497 #else
3498 abort ();
3499 #endif
3500 }
3501
3502 if (shift != 0)
3503 {
3504 if ((val & ((1 << shift) - 1)) != 0)
3505 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
3506 if (val >= 0)
3507 val >>= shift;
3508 else
3509 val = ((val >> shift)
3510 | ((long) -1 & ~ ((long) -1 >> shift)));
3511 }
3512 if (max != 0 && (val < min || val > max))
3513 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
3514
3515 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
3516 fixP->fx_done = 1;
3517 }
3518
3519 /* Called just before address relaxation. Return the length
3520 by which a fragment must grow to reach it's destination. */
3521
3522 int
3523 md_estimate_size_before_relax (fragS *fragP, segT segment_type)
3524 {
3525 int what;
3526
3527 switch (fragP->fr_subtype)
3528 {
3529 default:
3530 #ifdef HAVE_SH64
3531 return shmedia_md_estimate_size_before_relax (fragP, segment_type);
3532 #else
3533 abort ();
3534 #endif
3535
3536
3537 case C (UNCOND_JUMP, UNDEF_DISP):
3538 /* Used to be a branch to somewhere which was unknown. */
3539 if (!fragP->fr_symbol)
3540 {
3541 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3542 }
3543 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3544 {
3545 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3546 }
3547 else
3548 {
3549 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
3550 }
3551 break;
3552
3553 case C (COND_JUMP, UNDEF_DISP):
3554 case C (COND_JUMP_DELAY, UNDEF_DISP):
3555 what = GET_WHAT (fragP->fr_subtype);
3556 /* Used to be a branch to somewhere which was unknown. */
3557 if (fragP->fr_symbol
3558 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3559 {
3560 /* Got a symbol and it's defined in this segment, become byte
3561 sized - maybe it will fix up. */
3562 fragP->fr_subtype = C (what, COND8);
3563 }
3564 else if (fragP->fr_symbol)
3565 {
3566 /* Its got a segment, but its not ours, so it will always be long. */
3567 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
3568 }
3569 else
3570 {
3571 /* We know the abs value. */
3572 fragP->fr_subtype = C (what, COND8);
3573 }
3574 break;
3575
3576 case C (UNCOND_JUMP, UNCOND12):
3577 case C (UNCOND_JUMP, UNCOND32):
3578 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3579 case C (COND_JUMP, COND8):
3580 case C (COND_JUMP, COND12):
3581 case C (COND_JUMP, COND32):
3582 case C (COND_JUMP, UNDEF_WORD_DISP):
3583 case C (COND_JUMP_DELAY, COND8):
3584 case C (COND_JUMP_DELAY, COND12):
3585 case C (COND_JUMP_DELAY, COND32):
3586 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3587 /* When relaxing a section for the second time, we don't need to
3588 do anything besides return the current size. */
3589 break;
3590 }
3591
3592 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
3593 return fragP->fr_var;
3594 }
3595
3596 /* Put number into target byte order. */
3597
3598 void
3599 md_number_to_chars (char *ptr, valueT use, int nbytes)
3600 {
3601 #ifdef HAVE_SH64
3602 /* We might need to set the contents type to data. */
3603 sh64_flag_output ();
3604 #endif
3605
3606 if (! target_big_endian)
3607 number_to_chars_littleendian (ptr, use, nbytes);
3608 else
3609 number_to_chars_bigendian (ptr, use, nbytes);
3610 }
3611
3612 /* This version is used in obj-coff.c when not using BFD_ASSEMBLER.
3613 eg for the sh-hms target. */
3614
3615 long
3616 md_pcrel_from (fixS *fixP)
3617 {
3618 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
3619 }
3620
3621 long
3622 md_pcrel_from_section (fixS *fixP, segT sec)
3623 {
3624 if (! sh_local_pcrel (fixP)
3625 && fixP->fx_addsy != (symbolS *) NULL
3626 && (generic_force_reloc (fixP)
3627 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3628 {
3629 /* The symbol is undefined (or is defined but not in this section,
3630 or we're not sure about it being the final definition). Let the
3631 linker figure it out. We need to adjust the subtraction of a
3632 symbol to the position of the relocated data, though. */
3633 return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
3634 }
3635
3636 return md_pcrel_from (fixP);
3637 }
3638
3639 #ifdef OBJ_COFF
3640
3641 int
3642 tc_coff_sizemachdep (fragS *frag)
3643 {
3644 return md_relax_table[frag->fr_subtype].rlx_length;
3645 }
3646
3647 #endif /* OBJ_COFF */
3648
3649 #ifndef BFD_ASSEMBLER
3650 #ifdef OBJ_COFF
3651
3652 /* Map BFD relocs to SH COFF relocs. */
3653
3654 struct reloc_map
3655 {
3656 bfd_reloc_code_real_type bfd_reloc;
3657 int sh_reloc;
3658 };
3659
3660 static const struct reloc_map coff_reloc_map[] =
3661 {
3662 { BFD_RELOC_32, R_SH_IMM32 },
3663 { BFD_RELOC_16, R_SH_IMM16 },
3664 { BFD_RELOC_8, R_SH_IMM8 },
3665 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
3666 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
3667 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
3668 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
3669 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
3670 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
3671 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
3672 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
3673 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
3674 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
3675 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
3676 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
3677 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
3678 { BFD_RELOC_SH_USES, R_SH_USES },
3679 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
3680 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
3681 { BFD_RELOC_SH_CODE, R_SH_CODE },
3682 { BFD_RELOC_SH_DATA, R_SH_DATA },
3683 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
3684 { BFD_RELOC_UNUSED, 0 }
3685 };
3686
3687 /* Adjust a reloc for the SH. This is similar to the generic code,
3688 but does some minor tweaking. */
3689
3690 void
3691 sh_coff_reloc_mangle (segment_info_type *seg, fixS *fix,
3692 struct internal_reloc *intr, unsigned int paddr)
3693 {
3694 symbolS *symbol_ptr = fix->fx_addsy;
3695 symbolS *dot;
3696
3697 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
3698
3699 if (! SWITCH_TABLE (fix))
3700 {
3701 const struct reloc_map *rm;
3702
3703 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
3704 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
3705 break;
3706 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
3707 as_bad_where (fix->fx_file, fix->fx_line,
3708 _("Can not represent %s relocation in this object file format"),
3709 bfd_get_reloc_code_name (fix->fx_r_type));
3710 intr->r_type = rm->sh_reloc;
3711 intr->r_offset = 0;
3712 }
3713 else
3714 {
3715 know (sh_relax);
3716
3717 if (fix->fx_r_type == BFD_RELOC_16)
3718 intr->r_type = R_SH_SWITCH16;
3719 else if (fix->fx_r_type == BFD_RELOC_8)
3720 intr->r_type = R_SH_SWITCH8;
3721 else if (fix->fx_r_type == BFD_RELOC_32)
3722 intr->r_type = R_SH_SWITCH32;
3723 else
3724 abort ();
3725
3726 /* For a switch reloc, we set r_offset to the difference between
3727 the reloc address and the subtrahend. When the linker is
3728 doing relaxing, it can use the determine the starting and
3729 ending points of the switch difference expression. */
3730 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
3731 }
3732
3733 /* PC relative relocs are always against the current section. */
3734 if (symbol_ptr == NULL)
3735 {
3736 switch (fix->fx_r_type)
3737 {
3738 case BFD_RELOC_SH_PCRELIMM8BY2:
3739 case BFD_RELOC_SH_PCRELIMM8BY4:
3740 case BFD_RELOC_SH_PCDISP8BY2:
3741 case BFD_RELOC_SH_PCDISP12BY2:
3742 case BFD_RELOC_SH_USES:
3743 symbol_ptr = seg->dot;
3744 break;
3745 default:
3746 break;
3747 }
3748 }
3749
3750 if (fix->fx_r_type == BFD_RELOC_SH_USES)
3751 {
3752 /* We can't store the offset in the object file, since this
3753 reloc does not take up any space, so we store it in r_offset.
3754 The fx_addnumber field was set in md_apply_fix3. */
3755 intr->r_offset = fix->fx_addnumber;
3756 }
3757 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
3758 {
3759 /* We can't store the count in the object file, since this reloc
3760 does not take up any space, so we store it in r_offset. The
3761 fx_offset field was set when the fixup was created in
3762 sh_coff_frob_file. */
3763 intr->r_offset = fix->fx_offset;
3764 /* This reloc is always absolute. */
3765 symbol_ptr = NULL;
3766 }
3767 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
3768 {
3769 /* Store the alignment in the r_offset field. */
3770 intr->r_offset = fix->fx_offset;
3771 /* This reloc is always absolute. */
3772 symbol_ptr = NULL;
3773 }
3774 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
3775 || fix->fx_r_type == BFD_RELOC_SH_DATA
3776 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
3777 {
3778 /* These relocs are always absolute. */
3779 symbol_ptr = NULL;
3780 }
3781
3782 /* Turn the segment of the symbol into an offset. */
3783 if (symbol_ptr != NULL)
3784 {
3785 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
3786 if (dot != NULL)
3787 intr->r_symndx = dot->sy_number;
3788 else
3789 intr->r_symndx = symbol_ptr->sy_number;
3790 }
3791 else
3792 intr->r_symndx = -1;
3793 }
3794
3795 #endif /* OBJ_COFF */
3796 #endif /* ! BFD_ASSEMBLER */
3797
3798 #ifdef BFD_ASSEMBLER
3799
3800 /* Create a reloc. */
3801
3802 arelent *
3803 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
3804 {
3805 arelent *rel;
3806 bfd_reloc_code_real_type r_type;
3807
3808 rel = (arelent *) xmalloc (sizeof (arelent));
3809 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3810 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3811 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3812
3813 r_type = fixp->fx_r_type;
3814
3815 if (SWITCH_TABLE (fixp))
3816 {
3817 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
3818 rel->addend = 0;
3819 if (r_type == BFD_RELOC_16)
3820 r_type = BFD_RELOC_SH_SWITCH16;
3821 else if (r_type == BFD_RELOC_8)
3822 r_type = BFD_RELOC_8_PCREL;
3823 else if (r_type == BFD_RELOC_32)
3824 r_type = BFD_RELOC_SH_SWITCH32;
3825 else
3826 abort ();
3827 }
3828 else if (r_type == BFD_RELOC_SH_USES)
3829 rel->addend = fixp->fx_addnumber;
3830 else if (r_type == BFD_RELOC_SH_COUNT)
3831 rel->addend = fixp->fx_offset;
3832 else if (r_type == BFD_RELOC_SH_ALIGN)
3833 rel->addend = fixp->fx_offset;
3834 else if (r_type == BFD_RELOC_VTABLE_INHERIT
3835 || r_type == BFD_RELOC_VTABLE_ENTRY)
3836 rel->addend = fixp->fx_offset;
3837 else if (r_type == BFD_RELOC_SH_LOOP_START
3838 || r_type == BFD_RELOC_SH_LOOP_END)
3839 rel->addend = fixp->fx_offset;
3840 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
3841 {
3842 rel->addend = 0;
3843 rel->address = rel->addend = fixp->fx_offset;
3844 }
3845 #ifdef HAVE_SH64
3846 else if (shmedia_init_reloc (rel, fixp))
3847 ;
3848 #endif
3849 else if (fixp->fx_pcrel)
3850 rel->addend = fixp->fx_addnumber;
3851 else if (r_type == BFD_RELOC_32 || r_type == BFD_RELOC_32_GOTOFF)
3852 rel->addend = fixp->fx_addnumber;
3853 else
3854 rel->addend = 0;
3855
3856 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
3857 #ifdef OBJ_ELF
3858 if (rel->howto->type == R_SH_IND12W)
3859 rel->addend += fixp->fx_offset - 4;
3860 #endif
3861 if (rel->howto == NULL)
3862 {
3863 as_bad_where (fixp->fx_file, fixp->fx_line,
3864 _("Cannot represent relocation type %s"),
3865 bfd_get_reloc_code_name (r_type));
3866 /* Set howto to a garbage value so that we can keep going. */
3867 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3868 assert (rel->howto != NULL);
3869 }
3870
3871 return rel;
3872 }
3873
3874 #ifdef OBJ_ELF
3875 inline static char *
3876 sh_end_of_match (char *cont, char *what)
3877 {
3878 int len = strlen (what);
3879
3880 if (strncasecmp (cont, what, strlen (what)) == 0
3881 && ! is_part_of_name (cont[len]))
3882 return cont + len;
3883
3884 return NULL;
3885 }
3886
3887 int
3888 sh_parse_name (char const *name, expressionS *exprP, char *nextcharP)
3889 {
3890 char *next = input_line_pointer;
3891 char *next_end;
3892 int reloc_type;
3893 segT segment;
3894
3895 exprP->X_op_symbol = NULL;
3896
3897 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
3898 {
3899 if (! GOT_symbol)
3900 GOT_symbol = symbol_find_or_make (name);
3901
3902 exprP->X_add_symbol = GOT_symbol;
3903 no_suffix:
3904 /* If we have an absolute symbol or a reg, then we know its
3905 value now. */
3906 segment = S_GET_SEGMENT (exprP->X_add_symbol);
3907 if (segment == absolute_section)
3908 {
3909 exprP->X_op = O_constant;
3910 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
3911 exprP->X_add_symbol = NULL;
3912 }
3913 else if (segment == reg_section)
3914 {
3915 exprP->X_op = O_register;
3916 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
3917 exprP->X_add_symbol = NULL;
3918 }
3919 else
3920 {
3921 exprP->X_op = O_symbol;
3922 exprP->X_add_number = 0;
3923 }
3924
3925 return 1;
3926 }
3927
3928 exprP->X_add_symbol = symbol_find_or_make (name);
3929
3930 if (*nextcharP != '@')
3931 goto no_suffix;
3932 else if ((next_end = sh_end_of_match (next + 1, "GOTOFF")))
3933 reloc_type = BFD_RELOC_32_GOTOFF;
3934 else if ((next_end = sh_end_of_match (next + 1, "GOTPLT")))
3935 reloc_type = BFD_RELOC_SH_GOTPLT32;
3936 else if ((next_end = sh_end_of_match (next + 1, "GOT")))
3937 reloc_type = BFD_RELOC_32_GOT_PCREL;
3938 else if ((next_end = sh_end_of_match (next + 1, "PLT")))
3939 reloc_type = BFD_RELOC_32_PLT_PCREL;
3940 else if ((next_end = sh_end_of_match (next + 1, "TLSGD")))
3941 reloc_type = BFD_RELOC_SH_TLS_GD_32;
3942 else if ((next_end = sh_end_of_match (next + 1, "TLSLDM")))
3943 reloc_type = BFD_RELOC_SH_TLS_LD_32;
3944 else if ((next_end = sh_end_of_match (next + 1, "GOTTPOFF")))
3945 reloc_type = BFD_RELOC_SH_TLS_IE_32;
3946 else if ((next_end = sh_end_of_match (next + 1, "TPOFF")))
3947 reloc_type = BFD_RELOC_SH_TLS_LE_32;
3948 else if ((next_end = sh_end_of_match (next + 1, "DTPOFF")))
3949 reloc_type = BFD_RELOC_SH_TLS_LDO_32;
3950 else
3951 goto no_suffix;
3952
3953 *input_line_pointer = *nextcharP;
3954 input_line_pointer = next_end;
3955 *nextcharP = *input_line_pointer;
3956 *input_line_pointer = '\0';
3957
3958 exprP->X_op = O_PIC_reloc;
3959 exprP->X_add_number = 0;
3960 exprP->X_md = reloc_type;
3961
3962 return 1;
3963 }
3964 #endif
3965 #endif /* BFD_ASSEMBLER */
This page took 0.111214 seconds and 5 git commands to generate.