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