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