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