If the target does not support an ELF style has table, return true, indicating
[deliverable/binutils-gdb.git] / gas / config / tc-sh.c
CommitLineData
252b5132 1/* tc-sh.c -- Assemble code for the Hitachi Super-H
aae6ddf9 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
f7e42eb4 3 Free Software Foundation, Inc.
252b5132
RH
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
6b31947e 22/* Written By Steve Chamberlain <sac@cygnus.com> */
252b5132
RH
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"
3882b010 30#include "safe-ctype.h"
43841e91 31#include "struc-symbol.h"
d4845d57
JR
32
33#ifdef OBJ_ELF
34#include "elf/sh.h"
35#endif
36
0d10e182 37#include "dwarf2dbg.h"
0d10e182 38
e08ae979
HPN
39typedef struct
40 {
41 sh_arg_type type;
42 int reg;
43 expressionS immediate;
44 }
45sh_operand_info;
46
252b5132
RH
47const char comment_chars[] = "!";
48const char line_separator_chars[] = ";";
49const char line_comment_chars[] = "!#";
50
51static void s_uses PARAMS ((int));
52
53static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
54static void sh_frob_section PARAMS ((bfd *, segT, PTR));
55
252b5132 56static void s_uacons PARAMS ((int));
d4845d57 57static sh_opcode_info *find_cooked_opcode PARAMS ((char **));
0d10e182 58static unsigned int assemble_ppi PARAMS ((char *, sh_opcode_info *));
e08ae979 59static void little PARAMS ((int));
05982cac 60static void big PARAMS ((int));
e08ae979 61static int parse_reg PARAMS ((char *, int *, int *));
e08ae979
HPN
62static char *parse_exp PARAMS ((char *, sh_operand_info *));
63static char *parse_at PARAMS ((char *, sh_operand_info *));
64static void get_operand PARAMS ((char **, sh_operand_info *));
65static char *get_operands
66 PARAMS ((sh_opcode_info *, char *, sh_operand_info *));
67static sh_opcode_info *get_specific
68 PARAMS ((sh_opcode_info *, sh_operand_info *));
69static void insert PARAMS ((char *, int, int, sh_operand_info *));
70static void build_relax PARAMS ((sh_opcode_info *, sh_operand_info *));
71static char *insert_loop_bounds PARAMS ((char *, sh_operand_info *));
72static unsigned int build_Mytes
73 PARAMS ((sh_opcode_info *, sh_operand_info *));
252b5132 74
a1cc9221
AO
75#ifdef OBJ_ELF
76static void sh_elf_cons PARAMS ((int));
77
538cd60f
AO
78inline static int sh_PIC_related_p PARAMS ((symbolS *));
79static int sh_check_fixup PARAMS ((expressionS *, bfd_reloc_code_real_type *));
80inline static char *sh_end_of_match PARAMS ((char *, char *));
81
a1cc9221
AO
82symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
83#endif
84
05982cac
HPN
85static void
86big (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}
252b5132
RH
95
96static void
97little (ignore)
43841e91 98 int ignore ATTRIBUTE_UNUSED;
252b5132 99{
05982cac
HPN
100 if (target_big_endian)
101 as_bad (_("directive .little encountered when option -little required"));
102
103 /* Stop further messages. */
252b5132
RH
104 target_big_endian = 0;
105}
106
d4845d57
JR
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
6b31947e 111 Integer arg to pass to the function. */
d4845d57 112
252b5132
RH
113const pseudo_typeS md_pseudo_table[] =
114{
a1cc9221
AO
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
252b5132
RH
121 {"int", cons, 4},
122 {"word", cons, 2},
a1cc9221 123#endif /* OBJ_ELF */
05982cac 124 {"big", big, 0},
252b5132
RH
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},
de68de20
AO
134 {"uaquad", s_uacons, 8},
135 {"2byte", s_uacons, 2},
136 {"4byte", s_uacons, 4},
137 {"8byte", s_uacons, 8},
2bc0a128 138#ifdef BFD_ASSEMBLER
de68de20
AO
139 {"file", dwarf2_directive_file, 0 },
140 {"loc", dwarf2_directive_loc, 0 },
2bc0a128 141#endif
324bfcf3
AO
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 */
252b5132
RH
154 {0, 0, 0}
155};
156
157/*int md_reloc_size; */
158
159int sh_relax; /* set if -relax seen */
160
161/* Whether -small was seen. */
162
163int sh_small;
164
d4845d57
JR
165/* Whether -dsp was seen. */
166
167static int sh_dsp;
168
169/* The bit mask of architectures that could
170 accomodate the insns seen so far. */
171static int valid_arch;
172
252b5132
RH
173const char EXP_CHARS[] = "eE";
174
6b31947e 175/* Chars that mean this number is a floating point constant. */
252b5132
RH
176/* As in 0f12.456 */
177/* or 0d1.2345e12 */
178const char FLT_CHARS[] = "rRsSfFdDxXpP";
179
180#define C(a,b) ENCODE_RELAX(a,b)
181
252b5132
RH
182#define ENCODE_RELAX(what,length) (((what) << 4) + (length))
183#define GET_WHAT(x) ((x>>4))
184
6b31947e 185/* These are the three types of relaxable instrction. */
324bfcf3
AO
186/* These are the types of relaxable instructions; except for END which is
187 a marker. */
252b5132
RH
188#define COND_JUMP 1
189#define COND_JUMP_DELAY 2
190#define UNCOND_JUMP 3
324bfcf3
AO
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
252b5132
RH
213#define END 4
214
324bfcf3
AO
215#endif /* HAVE_SH64 */
216
252b5132
RH
217#define UNDEF_DISP 0
218#define COND8 1
219#define COND12 2
220#define COND32 3
252b5132
RH
221#define UNDEF_WORD_DISP 4
222
223#define UNCOND12 1
224#define UNCOND32 2
225
324bfcf3
AO
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
252b5132
RH
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
324bfcf3
AO
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
43841e91
NC
353#define EMPTY { 0, 0, 0, 0 }
354
252b5132 355const relax_typeS md_relax_table[C (END, 0)] = {
43841e91
NC
356 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
357 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
252b5132 358
43841e91 359 EMPTY,
252b5132
RH
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, },
e66457fb
AM
366 /* C (COND_JUMP, UNDEF_WORD_DISP) */
367 { 0, 0, COND32_LENGTH, 0, },
368 EMPTY, EMPTY, EMPTY,
43841e91 369 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
252b5132 370
43841e91 371 EMPTY,
252b5132
RH
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, },
e66457fb
AM
378 /* C (COND_JUMP_DELAY, UNDEF_WORD_DISP) */
379 { 0, 0, COND32_LENGTH, 0, },
380 EMPTY, EMPTY, EMPTY,
43841e91 381 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
252b5132 382
43841e91 383 EMPTY,
252b5132
RH
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, },
e66457fb
AM
388 EMPTY,
389 /* C (UNCOND_JUMP, UNDEF_WORD_DISP) */
390 { 0, 0, UNCOND32_LENGTH, 0, },
391 EMPTY, EMPTY, EMPTY,
43841e91 392 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
324bfcf3
AO
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,
5d6255fe 436 SH64PCREL32_M,
324bfcf3
AO
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
252b5132
RH
505};
506
43841e91
NC
507#undef EMPTY
508
252b5132
RH
509static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
510
a1cc9221
AO
511\f
512#ifdef OBJ_ELF
538cd60f
AO
513/* Determinet whether the symbol needs any kind of PIC relocation. */
514
515inline static int
516sh_PIC_related_p (sym)
517 symbolS *sym;
a1cc9221 518{
538cd60f 519 expressionS *exp;
a1cc9221 520
538cd60f
AO
521 if (! sym)
522 return 0;
523
524 if (sym == GOT_symbol)
525 return 1;
526
324bfcf3
AO
527#ifdef HAVE_SH64
528 if (sh_PIC_related_p (*symbol_get_tc (sym)))
529 return 1;
530#endif
531
538cd60f
AO
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
542static int
543sh_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)
5d6255fe 557
538cd60f
AO
558 so we attempt to transform this into:
559
560 O_subtract (f@PLT, O_subtract (.LPCS#+2, .))
561
5d6255fe 562 which we can handle simply below. */
538cd60f
AO
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 {
324bfcf3
AO
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;
5d6255fe 623
324bfcf3
AO
624 default:
625 abort ();
626 }
627 return 0;
628 }
629#else
538cd60f
AO
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 }
324bfcf3 635#endif
538cd60f
AO
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 {
324bfcf3
AO
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;
5d6255fe 657
324bfcf3
AO
658 case BFD_RELOC_SH_GOTPLT32:
659 *r_type_p = BFD_RELOC_SH_GOTPLT_LOW16;
660 break;
5d6255fe 661
324bfcf3
AO
662 case BFD_RELOC_32_GOT_PCREL:
663 *r_type_p = BFD_RELOC_SH_GOT_LOW16;
664 break;
5d6255fe 665
324bfcf3
AO
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;
5d6255fe 681
324bfcf3
AO
682 case BFD_RELOC_SH_GOTPLT32:
683 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDLOW16;
684 break;
5d6255fe 685
324bfcf3
AO
686 case BFD_RELOC_32_GOT_PCREL:
687 *r_type_p = BFD_RELOC_SH_GOT_MEDLOW16;
688 break;
5d6255fe 689
324bfcf3
AO
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;
5d6255fe 705
324bfcf3
AO
706 case BFD_RELOC_SH_GOTPLT32:
707 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDHI16;
708 break;
5d6255fe 709
324bfcf3
AO
710 case BFD_RELOC_32_GOT_PCREL:
711 *r_type_p = BFD_RELOC_SH_GOT_MEDHI16;
712 break;
5d6255fe 713
324bfcf3
AO
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;
5d6255fe 729
324bfcf3
AO
730 case BFD_RELOC_SH_GOTPLT32:
731 *r_type_p = BFD_RELOC_SH_GOTPLT_HI16;
732 break;
5d6255fe 733
324bfcf3
AO
734 case BFD_RELOC_32_GOT_PCREL:
735 *r_type_p = BFD_RELOC_SH_GOT_HI16;
736 break;
5d6255fe 737
324bfcf3
AO
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
538cd60f 751 *r_type_p = exp->X_md;
324bfcf3 752#endif
538cd60f
AO
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
770void
771sh_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)
a1cc9221 783 {
538cd60f
AO
784 case 1:
785 r_type = BFD_RELOC_8;
786 break;
a1cc9221 787
538cd60f
AO
788 case 2:
789 r_type = BFD_RELOC_16;
790 break;
791
792 case 4:
793 r_type = BFD_RELOC_32;
794 break;
a1cc9221 795
324bfcf3
AO
796#ifdef HAVE_SH64
797 case 8:
798 r_type = BFD_RELOC_64;
799 break;
800#endif
801
538cd60f
AO
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 }
5d6255fe 811
538cd60f 812 fix_new_exp (frag, off, size, exp, 0, r_type);
a1cc9221
AO
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. */
819static void
820sh_elf_cons (nbytes)
821 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
822{
538cd60f 823 expressionS exp;
a1cc9221 824
324bfcf3
AO
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
a1cc9221
AO
835 if (is_it_end_of_statement ())
836 {
837 demand_empty_rest_of_line ();
838 return;
839 }
840
841 do
842 {
843 expression (&exp);
538cd60f 844 emit_expr (&exp, (unsigned int) nbytes);
a1cc9221
AO
845 }
846 while (*input_line_pointer++ == ',');
847
81d4177b 848 input_line_pointer--; /* Put terminator back into stream. */
a1cc9221
AO
849 if (*input_line_pointer == '#' || *input_line_pointer == '!')
850 {
dda5ecfc 851 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
a1cc9221
AO
852 }
853 else
854 demand_empty_rest_of_line ();
855}
856#endif /* OBJ_ELF */
857
858\f
6b31947e
NC
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. */
252b5132
RH
861
862void
863md_begin ()
864{
865 sh_opcode_info *opcode;
866 char *prev_name = "";
d4845d57 867 int target_arch;
252b5132 868
d4845d57
JR
869 target_arch = arch_sh1_up & ~(sh_dsp ? arch_sh3e_up : arch_sh_dsp_up);
870 valid_arch = target_arch;
871
324bfcf3
AO
872#ifdef HAVE_SH64
873 shmedia_md_begin ();
874#endif
875
252b5132
RH
876 opcode_hash_control = hash_new ();
877
6b31947e 878 /* Insert unique names into hash table. */
252b5132
RH
879 for (opcode = sh_table; opcode->name; opcode++)
880 {
a37c8f88 881 if (strcmp (prev_name, opcode->name))
252b5132 882 {
a37c8f88
JR
883 if (! (opcode->arch & target_arch))
884 continue;
252b5132
RH
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
6b31947e 891 string. */
252b5132
RH
892 opcode->name = prev_name;
893 }
894 }
895}
896
897static int reg_m;
898static int reg_n;
d4845d57
JR
899static int reg_x, reg_y;
900static int reg_efg;
252b5132
RH
901static int reg_b;
902
3882b010 903#define IDENT_CHAR(c) (ISALNUM (c) || (c) == '_')
dead1419 904
6b31947e
NC
905/* Try to parse a reg name. Return the number of chars consumed. */
906
252b5132
RH
907static int
908parse_reg (src, mode, reg)
909 char *src;
910 int *mode;
911 int *reg;
912{
3882b010
L
913 char l0 = TOLOWER (src[0]);
914 char l1 = l0 ? TOLOWER (src[1]) : 0;
e46fee70 915
dead1419 916 /* We use ! IDENT_CHAR for the next character after the register name, to
252b5132 917 make sure that we won't accidentally recognize a symbol name such as
dead1419 918 'sram' or sr_ram as being a reference to the register 'sr'. */
252b5132 919
e46fee70 920 if (l0 == 'r')
252b5132 921 {
e46fee70 922 if (l1 == '1')
d4845d57
JR
923 {
924 if (src[2] >= '0' && src[2] <= '5'
dead1419 925 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
926 {
927 *mode = A_REG_N;
928 *reg = 10 + src[2] - '0';
929 return 3;
930 }
931 }
e46fee70 932 if (l1 >= '0' && l1 <= '9'
dead1419 933 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
934 {
935 *mode = A_REG_N;
e46fee70 936 *reg = (l1 - '0');
d4845d57
JR
937 return 2;
938 }
e46fee70 939 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
dead1419
JR
940 && ! IDENT_CHAR ((unsigned char) src[7]))
941 {
942 *mode = A_REG_B;
e46fee70 943 *reg = (l1 - '0');
dead1419
JR
944 return 7;
945 }
d4845d57 946
e46fee70 947 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
948 {
949 *mode = A_RE;
950 return 2;
951 }
e46fee70 952 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
953 {
954 *mode = A_RS;
955 return 2;
956 }
252b5132
RH
957 }
958
e46fee70 959 if (l0 == 'a')
252b5132 960 {
e46fee70 961 if (l1 == '0')
d4845d57 962 {
dead1419 963 if (! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
964 {
965 *mode = DSP_REG_N;
966 *reg = A_A0_NUM;
967 return 2;
968 }
3882b010 969 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
970 {
971 *mode = DSP_REG_N;
972 *reg = A_A0G_NUM;
973 return 3;
974 }
975 }
e46fee70 976 if (l1 == '1')
252b5132 977 {
dead1419 978 if (! IDENT_CHAR ((unsigned char) src[2]))
252b5132 979 {
d4845d57
JR
980 *mode = DSP_REG_N;
981 *reg = A_A1_NUM;
982 return 2;
983 }
3882b010 984 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
985 {
986 *mode = DSP_REG_N;
987 *reg = A_A1G_NUM;
252b5132
RH
988 return 3;
989 }
990 }
d4845d57 991
e46fee70 992 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
dead1419 993 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
994 {
995 *mode = A_REG_N;
e46fee70 996 *reg = 4 + (l1 - '0');
d4845d57
JR
997 return 3;
998 }
e46fee70 999 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
dead1419 1000 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
1001 {
1002 *mode = A_REG_N;
e46fee70 1003 *reg = 6 + (l1 - '0');
d4845d57
JR
1004 return 3;
1005 }
e46fee70 1006 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
dead1419 1007 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57 1008 {
e46fee70 1009 int n = l1 - '0';
d4845d57
JR
1010
1011 *mode = A_REG_N;
1012 *reg = n | ((~n & 2) << 1);
1013 return 3;
1014 }
1015 }
1016
912a07db 1017 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57 1018 {
e46fee70 1019 if (l1 == 's')
d4845d57
JR
1020 {
1021 *mode = A_REG_N;
1022 *reg = 8;
252b5132
RH
1023 return 2;
1024 }
e46fee70 1025 if (l1 == 'x')
d4845d57
JR
1026 {
1027 *mode = A_REG_N;
1028 *reg = 8;
1029 return 2;
1030 }
e46fee70 1031 if (l1 == 'y')
d4845d57
JR
1032 {
1033 *mode = A_REG_N;
1034 *reg = 9;
1035 return 2;
1036 }
1037 }
1038
e46fee70 1039 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
dead1419 1040 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
1041 {
1042 *mode = DSP_REG_N;
e46fee70 1043 *reg = A_X0_NUM + l1 - '0';
d4845d57
JR
1044 return 2;
1045 }
1046
e46fee70 1047 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
dead1419 1048 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
1049 {
1050 *mode = DSP_REG_N;
e46fee70 1051 *reg = A_Y0_NUM + l1 - '0';
d4845d57
JR
1052 return 2;
1053 }
1054
e46fee70 1055 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
dead1419 1056 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
1057 {
1058 *mode = DSP_REG_N;
e46fee70 1059 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
d4845d57 1060 return 2;
252b5132
RH
1061 }
1062
e46fee70
HPN
1063 if (l0 == 's'
1064 && l1 == 's'
3882b010 1065 && TOLOWER (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1066 {
1067 *mode = A_SSR;
1068 return 3;
1069 }
1070
3882b010 1071 if (l0 == 's' && l1 == 'p' && TOLOWER (src[2]) == 'c'
dead1419 1072 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1073 {
1074 *mode = A_SPC;
1075 return 3;
1076 }
1077
3882b010 1078 if (l0 == 's' && l1 == 'g' && TOLOWER (src[2]) == 'r'
dead1419 1079 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1080 {
1081 *mode = A_SGR;
1082 return 3;
1083 }
1084
3882b010 1085 if (l0 == 'd' && l1 == 's' && TOLOWER (src[2]) == 'r'
dead1419 1086 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
1087 {
1088 *mode = A_DSR;
1089 return 3;
1090 }
1091
3882b010 1092 if (l0 == 'd' && l1 == 'b' && TOLOWER (src[2]) == 'r'
dead1419 1093 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1094 {
1095 *mode = A_DBR;
1096 return 3;
1097 }
1098
e46fee70 1099 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
252b5132
RH
1100 {
1101 *mode = A_SR;
1102 return 2;
1103 }
1104
e46fee70 1105 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
252b5132
RH
1106 {
1107 *mode = A_REG_N;
1108 *reg = 15;
1109 return 2;
1110 }
1111
e46fee70 1112 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
252b5132
RH
1113 {
1114 *mode = A_PR;
1115 return 2;
1116 }
e46fee70 1117 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
252b5132 1118 {
015551fc
JR
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;
252b5132
RH
1122 return 2;
1123 }
3882b010 1124 if (l0 == 'g' && l1 == 'b' && TOLOWER (src[2]) == 'r'
dead1419 1125 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1126 {
1127 *mode = A_GBR;
1128 return 3;
1129 }
3882b010 1130 if (l0 == 'v' && l1 == 'b' && TOLOWER (src[2]) == 'r'
dead1419 1131 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1132 {
1133 *mode = A_VBR;
1134 return 3;
1135 }
1136
3882b010 1137 if (l0 == 'm' && l1 == 'a' && TOLOWER (src[2]) == 'c'
dead1419 1138 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132 1139 {
3882b010 1140 if (TOLOWER (src[3]) == 'l')
252b5132
RH
1141 {
1142 *mode = A_MACL;
1143 return 4;
1144 }
3882b010 1145 if (TOLOWER (src[3]) == 'h')
252b5132
RH
1146 {
1147 *mode = A_MACH;
1148 return 4;
1149 }
1150 }
3882b010 1151 if (l0 == 'm' && l1 == 'o' && TOLOWER (src[2]) == 'd'
912a07db 1152 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
1153 {
1154 *mode = A_MOD;
1155 return 3;
1156 }
e46fee70 1157 if (l0 == 'f' && l1 == 'r')
252b5132
RH
1158 {
1159 if (src[2] == '1')
1160 {
1161 if (src[3] >= '0' && src[3] <= '5'
dead1419 1162 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
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'
dead1419 1170 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1171 {
1172 *mode = F_REG_N;
1173 *reg = (src[2] - '0');
1174 return 3;
1175 }
1176 }
e46fee70 1177 if (l0 == 'd' && l1 == 'r')
252b5132
RH
1178 {
1179 if (src[2] == '1')
1180 {
1181 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
dead1419 1182 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
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)
dead1419 1190 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1191 {
1192 *mode = D_REG_N;
1193 *reg = (src[2] - '0');
1194 return 3;
1195 }
1196 }
e46fee70 1197 if (l0 == 'x' && l1 == 'd')
252b5132
RH
1198 {
1199 if (src[2] == '1')
1200 {
1201 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
dead1419 1202 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
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)
dead1419 1210 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1211 {
1212 *mode = X_REG_N;
1213 *reg = (src[2] - '0') + 1;
1214 return 3;
1215 }
1216 }
e46fee70 1217 if (l0 == 'f' && l1 == 'v')
252b5132 1218 {
dead1419 1219 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
1220 {
1221 *mode = V_REG_N;
1222 *reg = 12;
1223 return 4;
1224 }
1225 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
dead1419 1226 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1227 {
1228 *mode = V_REG_N;
1229 *reg = (src[2] - '0');
1230 return 3;
1231 }
1232 }
3882b010
L
1233 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 'u'
1234 && TOLOWER (src[3]) == 'l'
dead1419 1235 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
1236 {
1237 *mode = FPUL_N;
1238 return 4;
1239 }
1240
3882b010
L
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]))
252b5132
RH
1244 {
1245 *mode = FPSCR_N;
1246 return 5;
1247 }
1248
3882b010
L
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]))
252b5132
RH
1252 {
1253 *mode = XMTRX_M4;
1254 return 5;
1255 }
1256
1257 return 0;
1258}
1259
c4aa876b 1260static char *
015551fc 1261parse_exp (s, op)
252b5132 1262 char *s;
015551fc 1263 sh_operand_info *op;
252b5132
RH
1264{
1265 char *save;
1266 char *new;
1267
1268 save = input_line_pointer;
1269 input_line_pointer = s;
015551fc
JR
1270 expression (&op->immediate);
1271 if (op->immediate.X_op == O_absent)
252b5132 1272 as_bad (_("missing operand"));
538cd60f
AO
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
252b5132
RH
1279 new = input_line_pointer;
1280 input_line_pointer = save;
1281 return new;
1282}
1283
252b5132
RH
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
252b5132
RH
1301 */
1302
c4aa876b 1303static char *
252b5132
RH
1304parse_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 {
6b31947e 1313 /* Must be predecrement. */
252b5132
RH
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
8d4d84c2 1326 @(r0, rn). */
252b5132
RH
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] == ',')
252b5132 1337 {
8d4d84c2
AO
1338 src++;
1339 /* Now can be rn or gbr. */
1340 len = parse_reg (src, &mode, &(op->reg));
1341 }
1342 else
1343 {
1344 len = 0;
252b5132 1345 }
8d4d84c2 1346 if (len)
252b5132 1347 {
8d4d84c2
AO
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 }
252b5132
RH
1360 }
1361 else
1362 {
8d4d84c2 1363 as_bad (_("syntax error in @(r0...)"));
252b5132
RH
1364 }
1365 }
1366 else
1367 {
8d4d84c2 1368 /* Must be an @(disp,.. thing). */
015551fc 1369 src = parse_exp (src, op);
252b5132
RH
1370 if (src[0] == ',')
1371 src++;
8d4d84c2 1372 /* Now can be rn, gbr or pc. */
252b5132
RH
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 }
015551fc 1384 else if (mode == A_PC)
252b5132 1385 {
dbb4348d
JR
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. */
9691d64f
JR
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 }
252b5132
RH
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)
006299d3
NC
1434 as_bad (_("illegal register after @"));
1435
252b5132
RH
1436 if (src[0] == '+')
1437 {
1000a02a
NC
1438 char l0, l1;
1439
252b5132 1440 src++;
1000a02a
NC
1441 l0 = TOLOWER (src[0]);
1442 l1 = TOLOWER (src[1]);
1443
1444 if ((l0 == 'r' && l1 == '8')
1445 || (l0 == 'i' && (l1 == 'x' || l1 == 's')))
d4845d57
JR
1446 {
1447 src += 2;
1448 op->type = A_PMOD_N;
1449 }
006299d3
NC
1450 else if ( (l0 == 'r' && l1 == '9')
1451 || (l0 == 'i' && l1 == 'y'))
d4845d57
JR
1452 {
1453 src += 2;
1454 op->type = A_PMODY_N;
1455 }
1456 else
1457 op->type = A_INC_N;
252b5132
RH
1458 }
1459 else
006299d3 1460 op->type = A_IND_N;
252b5132
RH
1461 }
1462 return src;
1463}
1464
1465static void
1466get_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++;
015551fc 1477 *ptr = parse_exp (src, op);
252b5132
RH
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 {
6b31947e 1496 /* Not a reg, the only thing left is a displacement. */
015551fc 1497 *ptr = parse_exp (src, op);
252b5132
RH
1498 op->type = A_DISP_PC;
1499 return;
1500 }
1501}
1502
c4aa876b 1503static char *
252b5132
RH
1504get_operands (info, args, operand)
1505 sh_opcode_info *info;
1506 char *args;
1507 sh_operand_info *operand;
252b5132
RH
1508{
1509 char *ptr = args;
1510 if (info->arg[0])
1511 {
d4845d57
JR
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++;
252b5132
RH
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);
52ccafd0
JR
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)
252b5132
RH
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
6b31947e 1562 provided. */
252b5132 1563
c4aa876b 1564static sh_opcode_info *
252b5132
RH
1565get_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;
c4aa876b 1572
252b5132
RH
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
6b31947e 1579 opcodes with the same name. */
252b5132
RH
1580 return 0;
1581 }
c4aa876b 1582
6b31947e 1583 /* Look at both operands needed by the opcodes and provided by
252b5132
RH
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
6b31947e 1586 first on each opcode try. */
252b5132
RH
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];
c4aa876b 1591
d8731e7d
NC
1592 /* If this is a parallel insn check to see if both
1593 parts have the same destination register. */
1594 if ((n == 2) && (this_try->nibbles[0] == PPI))
1595 {
1596 static boolean bIsPPI = false;
1597 static int nLastDestReg;
1598
1599 if (!bIsPPI)
1600 {
1601 bIsPPI = true;
1602 nLastDestReg = user->reg;
1603 }
1604 else /* Second insn. */
1605 {
1606 if (nLastDestReg == user->reg)
1607 as_warn (_("destination register is same for parallel insns"));
5d6255fe 1608
d8731e7d
NC
1609 bIsPPI = false;
1610 }
1611 }
1612
252b5132
RH
1613 switch (arg)
1614 {
7679ead9
AO
1615 case A_DISP_PC:
1616 if (user->type == A_DISP_PC_ABS)
1617 break;
1618 /* Fall through. */
252b5132
RH
1619 case A_IMM:
1620 case A_BDISP12:
1621 case A_BDISP8:
1622 case A_DISP_GBR:
252b5132
RH
1623 case A_MACH:
1624 case A_PR:
1625 case A_MACL:
1626 if (user->type != arg)
1627 goto fail;
1628 break;
1629 case A_R0:
1630 /* opcode needs r0 */
1631 if (user->type != A_REG_N || user->reg != 0)
1632 goto fail;
1633 break;
1634 case A_R0_GBR:
1635 if (user->type != A_R0_GBR || user->reg != 0)
1636 goto fail;
1637 break;
1638 case F_FR0:
1639 if (user->type != F_REG_N || user->reg != 0)
1640 goto fail;
1641 break;
1642
1643 case A_REG_N:
1644 case A_INC_N:
1645 case A_DEC_N:
1646 case A_IND_N:
1647 case A_IND_R0_REG_N:
1648 case A_DISP_REG_N:
1649 case F_REG_N:
1650 case D_REG_N:
1651 case X_REG_N:
1652 case V_REG_N:
1653 case FPUL_N:
1654 case FPSCR_N:
d4845d57
JR
1655 case A_PMOD_N:
1656 case A_PMODY_N:
1657 case DSP_REG_N:
252b5132
RH
1658 /* Opcode needs rn */
1659 if (user->type != arg)
1660 goto fail;
1661 reg_n = user->reg;
1662 break;
252b5132
RH
1663 case DX_REG_N:
1664 if (user->type != D_REG_N && user->type != X_REG_N)
1665 goto fail;
1666 reg_n = user->reg;
1667 break;
1668 case A_GBR:
1669 case A_SR:
1670 case A_VBR:
d4845d57
JR
1671 case A_DSR:
1672 case A_MOD:
1673 case A_RE:
1674 case A_RS:
252b5132
RH
1675 case A_SSR:
1676 case A_SPC:
1677 case A_SGR:
1678 case A_DBR:
1679 if (user->type != arg)
1680 goto fail;
1681 break;
1682
c4aa876b 1683 case A_REG_B:
252b5132
RH
1684 if (user->type != arg)
1685 goto fail;
1686 reg_b = user->reg;
1687 break;
1688
1689 case A_REG_M:
1690 case A_INC_M:
1691 case A_DEC_M:
1692 case A_IND_M:
1693 case A_IND_R0_REG_M:
1694 case A_DISP_REG_M:
d4845d57 1695 case DSP_REG_M:
252b5132
RH
1696 /* Opcode needs rn */
1697 if (user->type != arg - A_REG_M + A_REG_N)
1698 goto fail;
1699 reg_m = user->reg;
1700 break;
1701
d4845d57
JR
1702 case DSP_REG_X:
1703 if (user->type != DSP_REG_N)
1704 goto fail;
1705 switch (user->reg)
1706 {
1707 case A_X0_NUM:
1708 reg_x = 0;
1709 break;
1710 case A_X1_NUM:
1711 reg_x = 1;
1712 break;
1713 case A_A0_NUM:
1714 reg_x = 2;
1715 break;
1716 case A_A1_NUM:
1717 reg_x = 3;
1718 break;
1719 default:
1720 goto fail;
1721 }
1722 break;
1723
1724 case DSP_REG_Y:
1725 if (user->type != DSP_REG_N)
1726 goto fail;
1727 switch (user->reg)
1728 {
1729 case A_Y0_NUM:
1730 reg_y = 0;
1731 break;
1732 case A_Y1_NUM:
1733 reg_y = 1;
1734 break;
1735 case A_M0_NUM:
1736 reg_y = 2;
1737 break;
1738 case A_M1_NUM:
1739 reg_y = 3;
1740 break;
1741 default:
1742 goto fail;
1743 }
1744 break;
1745
1746 case DSP_REG_E:
1747 if (user->type != DSP_REG_N)
1748 goto fail;
1749 switch (user->reg)
1750 {
1751 case A_X0_NUM:
1752 reg_efg = 0 << 10;
1753 break;
1754 case A_X1_NUM:
1755 reg_efg = 1 << 10;
1756 break;
1757 case A_Y0_NUM:
1758 reg_efg = 2 << 10;
1759 break;
1760 case A_A1_NUM:
1761 reg_efg = 3 << 10;
1762 break;
1763 default:
1764 goto fail;
1765 }
1766 break;
1767
1768 case DSP_REG_F:
1769 if (user->type != DSP_REG_N)
1770 goto fail;
1771 switch (user->reg)
1772 {
1773 case A_Y0_NUM:
1774 reg_efg |= 0 << 8;
1775 break;
1776 case A_Y1_NUM:
1777 reg_efg |= 1 << 8;
1778 break;
1779 case A_X0_NUM:
1780 reg_efg |= 2 << 8;
1781 break;
1782 case A_A1_NUM:
1783 reg_efg |= 3 << 8;
1784 break;
1785 default:
1786 goto fail;
1787 }
1788 break;
1789
1790 case DSP_REG_G:
1791 if (user->type != DSP_REG_N)
1792 goto fail;
1793 switch (user->reg)
1794 {
1795 case A_M0_NUM:
1796 reg_efg |= 0 << 2;
1797 break;
1798 case A_M1_NUM:
1799 reg_efg |= 1 << 2;
1800 break;
1801 case A_A0_NUM:
1802 reg_efg |= 2 << 2;
1803 break;
1804 case A_A1_NUM:
1805 reg_efg |= 3 << 2;
1806 break;
1807 default:
1808 goto fail;
1809 }
1810 break;
1811
1812 case A_A0:
1813 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1814 goto fail;
1815 break;
1816 case A_X0:
1817 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1818 goto fail;
1819 break;
1820 case A_X1:
1821 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1822 goto fail;
1823 break;
1824 case A_Y0:
1825 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1826 goto fail;
1827 break;
1828 case A_Y1:
1829 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1830 goto fail;
1831 break;
1832
252b5132
RH
1833 case F_REG_M:
1834 case D_REG_M:
1835 case X_REG_M:
1836 case V_REG_M:
1837 case FPUL_M:
1838 case FPSCR_M:
1839 /* Opcode needs rn */
1840 if (user->type != arg - F_REG_M + F_REG_N)
1841 goto fail;
1842 reg_m = user->reg;
1843 break;
1844 case DX_REG_M:
1845 if (user->type != D_REG_N && user->type != X_REG_N)
1846 goto fail;
1847 reg_m = user->reg;
1848 break;
1849 case XMTRX_M4:
1850 if (user->type != XMTRX_M4)
1851 goto fail;
1852 reg_m = 4;
1853 break;
c4aa876b 1854
252b5132
RH
1855 default:
1856 printf (_("unhandled %d\n"), arg);
1857 goto fail;
1858 }
1859 }
a37c8f88
JR
1860 if ( !(valid_arch & this_try->arch))
1861 goto fail;
d4845d57 1862 valid_arch &= this_try->arch;
252b5132 1863 return this_try;
c4aa876b
NC
1864 fail:
1865 ;
252b5132
RH
1866 }
1867
1868 return 0;
1869}
1870
252b5132 1871static void
015551fc 1872insert (where, how, pcrel, op)
252b5132
RH
1873 char *where;
1874 int how;
1875 int pcrel;
015551fc 1876 sh_operand_info *op;
252b5132
RH
1877{
1878 fix_new_exp (frag_now,
1879 where - frag_now->fr_literal,
1880 2,
015551fc 1881 &op->immediate,
252b5132
RH
1882 pcrel,
1883 how);
1884}
1885
1886static void
015551fc 1887build_relax (opcode, op)
252b5132 1888 sh_opcode_info *opcode;
015551fc 1889 sh_operand_info *op;
252b5132
RH
1890{
1891 int high_byte = target_big_endian ? 0 : 1;
1892 char *p;
1893
1894 if (opcode->arg[0] == A_BDISP8)
1895 {
1896 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
1897 p = frag_var (rs_machine_dependent,
1898 md_relax_table[C (what, COND32)].rlx_length,
1899 md_relax_table[C (what, COND8)].rlx_length,
1900 C (what, 0),
015551fc
JR
1901 op->immediate.X_add_symbol,
1902 op->immediate.X_add_number,
252b5132
RH
1903 0);
1904 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
1905 }
1906 else if (opcode->arg[0] == A_BDISP12)
1907 {
1908 p = frag_var (rs_machine_dependent,
1909 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
1910 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
1911 C (UNCOND_JUMP, 0),
015551fc
JR
1912 op->immediate.X_add_symbol,
1913 op->immediate.X_add_number,
252b5132
RH
1914 0);
1915 p[high_byte] = (opcode->nibbles[0] << 4);
1916 }
1917
1918}
1919
6b31947e 1920/* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
d67b5d6d 1921
015551fc
JR
1922static char *
1923insert_loop_bounds (output, operand)
1924 char *output;
1925 sh_operand_info *operand;
1926{
1927 char *name;
1928 symbolS *end_sym;
1929
1930 /* Since the low byte of the opcode will be overwritten by the reloc, we
1931 can just stash the high byte into both bytes and ignore endianness. */
1932 output[0] = 0x8c;
1933 output[1] = 0x8c;
1934 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1935 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1936
1937 if (sh_relax)
1938 {
1939 static int count = 0;
1940
1941 /* If the last loop insn is a two-byte-insn, it is in danger of being
1942 swapped with the insn after it. To prevent this, create a new
1943 symbol - complete with SH_LABEL reloc - after the last loop insn.
1944 If the last loop insn is four bytes long, the symbol will be
1945 right in the middle, but four byte insns are not swapped anyways. */
1946 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
1947 Hence a 9 digit number should be enough to count all REPEATs. */
1948 name = alloca (11);
1949 sprintf (name, "_R%x", count++ & 0x3fffffff);
c4aa876b 1950 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
015551fc
JR
1951 /* Make this a local symbol. */
1952#ifdef OBJ_COFF
1953 SF_SET_LOCAL (end_sym);
1954#endif /* OBJ_COFF */
1955 symbol_table_insert (end_sym);
1956 end_sym->sy_value = operand[1].immediate;
1957 end_sym->sy_value.X_add_number += 2;
1958 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
1959 }
1960
1961 output = frag_more (2);
1962 output[0] = 0x8e;
1963 output[1] = 0x8e;
1964 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1965 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1966
1967 return frag_more (2);
1968}
1969
d67b5d6d 1970/* Now we know what sort of opcodes it is, let's build the bytes. */
6b31947e 1971
0d10e182 1972static unsigned int
252b5132
RH
1973build_Mytes (opcode, operand)
1974 sh_opcode_info *opcode;
1975 sh_operand_info *operand;
252b5132
RH
1976{
1977 int index;
1978 char nbuf[4];
1979 char *output = frag_more (2);
0d10e182 1980 unsigned int size = 2;
252b5132
RH
1981 int low_byte = target_big_endian ? 1 : 0;
1982 nbuf[0] = 0;
1983 nbuf[1] = 0;
1984 nbuf[2] = 0;
1985 nbuf[3] = 0;
1986
1987 for (index = 0; index < 4; index++)
1988 {
1989 sh_nibble_type i = opcode->nibbles[index];
1990 if (i < 16)
1991 {
1992 nbuf[index] = i;
1993 }
1994 else
1995 {
1996 switch (i)
1997 {
1998 case REG_N:
1999 nbuf[index] = reg_n;
2000 break;
2001 case REG_M:
2002 nbuf[index] = reg_m;
2003 break;
d4845d57
JR
2004 case SDT_REG_N:
2005 if (reg_n < 2 || reg_n > 5)
2006 as_bad (_("Invalid register: 'r%d'"), reg_n);
2007 nbuf[index] = (reg_n & 3) | 4;
2008 break;
252b5132
RH
2009 case REG_NM:
2010 nbuf[index] = reg_n | (reg_m >> 2);
2011 break;
c4aa876b 2012 case REG_B:
252b5132
RH
2013 nbuf[index] = reg_b | 0x08;
2014 break;
015551fc
JR
2015 case IMM0_4BY4:
2016 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
2017 break;
2018 case IMM0_4BY2:
2019 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
2020 break;
2021 case IMM0_4:
2022 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
2023 break;
2024 case IMM1_4BY4:
2025 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
2026 break;
2027 case IMM1_4BY2:
2028 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
252b5132 2029 break;
015551fc
JR
2030 case IMM1_4:
2031 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
252b5132 2032 break;
015551fc
JR
2033 case IMM0_8BY4:
2034 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
252b5132 2035 break;
015551fc
JR
2036 case IMM0_8BY2:
2037 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
252b5132 2038 break;
015551fc
JR
2039 case IMM0_8:
2040 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
252b5132 2041 break;
015551fc
JR
2042 case IMM1_8BY4:
2043 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
252b5132 2044 break;
015551fc
JR
2045 case IMM1_8BY2:
2046 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
2047 break;
2048 case IMM1_8:
2049 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
252b5132
RH
2050 break;
2051 case PCRELIMM_8BY4:
7679ead9
AO
2052 insert (output, BFD_RELOC_SH_PCRELIMM8BY4,
2053 operand->type != A_DISP_PC_ABS, operand);
252b5132
RH
2054 break;
2055 case PCRELIMM_8BY2:
7679ead9
AO
2056 insert (output, BFD_RELOC_SH_PCRELIMM8BY2,
2057 operand->type != A_DISP_PC_ABS, operand);
015551fc
JR
2058 break;
2059 case REPEAT:
2060 output = insert_loop_bounds (output, operand);
2061 nbuf[index] = opcode->nibbles[3];
2062 operand += 2;
252b5132
RH
2063 break;
2064 default:
2065 printf (_("failed for %d\n"), i);
2066 }
2067 }
2068 }
c4aa876b
NC
2069 if (!target_big_endian)
2070 {
2071 output[1] = (nbuf[0] << 4) | (nbuf[1]);
2072 output[0] = (nbuf[2] << 4) | (nbuf[3]);
2073 }
2074 else
2075 {
2076 output[0] = (nbuf[0] << 4) | (nbuf[1]);
2077 output[1] = (nbuf[2] << 4) | (nbuf[3]);
2078 }
0d10e182 2079 return size;
252b5132
RH
2080}
2081
d4845d57
JR
2082/* Find an opcode at the start of *STR_P in the hash table, and set
2083 *STR_P to the first character after the last one read. */
252b5132 2084
d4845d57
JR
2085static sh_opcode_info *
2086find_cooked_opcode (str_p)
2087 char **str_p;
252b5132 2088{
d4845d57 2089 char *str = *str_p;
252b5132
RH
2090 unsigned char *op_start;
2091 unsigned char *op_end;
252b5132
RH
2092 char name[20];
2093 int nlen = 0;
c4aa876b 2094
6b31947e 2095 /* Drop leading whitespace. */
252b5132
RH
2096 while (*str == ' ')
2097 str++;
2098
d4845d57
JR
2099 /* Find the op code end.
2100 The pre-processor will eliminate whitespace in front of
2101 any '@' after the first argument; we may be called from
2102 assemble_ppi, so the opcode might be terminated by an '@'. */
252b5132
RH
2103 for (op_start = op_end = (unsigned char *) (str);
2104 *op_end
2105 && nlen < 20
d4845d57 2106 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
252b5132
RH
2107 op_end++)
2108 {
2109 unsigned char c = op_start[nlen];
2110
2111 /* The machine independent code will convert CMP/EQ into cmp/EQ
d4845d57
JR
2112 because it thinks the '/' is the end of the symbol. Moreover,
2113 all but the first sub-insn is a parallel processing insn won't
3882b010 2114 be capitalized. Instead of hacking up the machine independent
d4845d57 2115 code, we just deal with it here. */
3882b010 2116 c = TOLOWER (c);
252b5132
RH
2117 name[nlen] = c;
2118 nlen++;
2119 }
c4aa876b 2120
252b5132 2121 name[nlen] = 0;
d4845d57 2122 *str_p = op_end;
252b5132
RH
2123
2124 if (nlen == 0)
6b31947e 2125 as_bad (_("can't find opcode "));
252b5132 2126
d4845d57
JR
2127 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
2128}
2129
2130/* Assemble a parallel processing insn. */
2131#define DDT_BASE 0xf000 /* Base value for double data transfer insns */
6b31947e 2132
0d10e182 2133static unsigned int
d4845d57
JR
2134assemble_ppi (op_end, opcode)
2135 char *op_end;
2136 sh_opcode_info *opcode;
2137{
2138 int movx = 0;
2139 int movy = 0;
2140 int cond = 0;
2141 int field_b = 0;
2142 char *output;
2143 int move_code;
0d10e182 2144 unsigned int size;
d4845d57
JR
2145
2146 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
2147 Make sure we encode a defined insn pattern. */
2148 reg_x = 0;
2149 reg_y = 0;
96f31fc7 2150 reg_n = 0;
d4845d57
JR
2151
2152 for (;;)
2153 {
2154 sh_operand_info operand[3];
2155
2156 if (opcode->arg[0] != A_END)
2157 op_end = get_operands (opcode, op_end, operand);
2158 opcode = get_specific (opcode, operand);
2159 if (opcode == 0)
2160 {
6b31947e 2161 /* Couldn't find an opcode which matched the operands. */
d4845d57 2162 char *where = frag_more (2);
0d10e182 2163 size = 2;
d4845d57
JR
2164
2165 where[0] = 0x0;
2166 where[1] = 0x0;
2167 as_bad (_("invalid operands for opcode"));
0d10e182 2168 return size;
d4845d57 2169 }
c4aa876b 2170
d4845d57
JR
2171 if (opcode->nibbles[0] != PPI)
2172 as_bad (_("insn can't be combined with parallel processing insn"));
2173
2174 switch (opcode->nibbles[1])
2175 {
2176
2177 case NOPX:
2178 if (movx)
2179 as_bad (_("multiple movx specifications"));
2180 movx = DDT_BASE;
2181 break;
2182 case NOPY:
2183 if (movy)
2184 as_bad (_("multiple movy specifications"));
2185 movy = DDT_BASE;
2186 break;
2187
2188 case MOVX:
2189 if (movx)
2190 as_bad (_("multiple movx specifications"));
2191 if (reg_n < 4 || reg_n > 5)
2192 as_bad (_("invalid movx address register"));
2193 if (opcode->nibbles[2] & 8)
2194 {
2195 if (reg_m == A_A1_NUM)
2196 movx = 1 << 7;
2197 else if (reg_m != A_A0_NUM)
2198 as_bad (_("invalid movx dsp register"));
2199 }
2200 else
2201 {
2202 if (reg_x > 1)
2203 as_bad (_("invalid movx dsp register"));
2204 movx = reg_x << 7;
2205 }
2206 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
2207 break;
2208
2209 case MOVY:
2210 if (movy)
2211 as_bad (_("multiple movy specifications"));
2212 if (opcode->nibbles[2] & 8)
2213 {
2214 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
2215 so add 8 more. */
2216 movy = 8;
2217 if (reg_m == A_A1_NUM)
2218 movy += 1 << 6;
2219 else if (reg_m != A_A0_NUM)
2220 as_bad (_("invalid movy dsp register"));
2221 }
2222 else
2223 {
2224 if (reg_y > 1)
2225 as_bad (_("invalid movy dsp register"));
2226 movy = reg_y << 6;
2227 }
2228 if (reg_n < 6 || reg_n > 7)
2229 as_bad (_("invalid movy address register"));
2230 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
2231 break;
2232
2233 case PSH:
015551fc 2234 if (operand[0].immediate.X_op != O_constant)
d4845d57
JR
2235 as_bad (_("dsp immediate shift value not constant"));
2236 field_b = ((opcode->nibbles[2] << 12)
015551fc 2237 | (operand[0].immediate.X_add_number & 127) << 4
d4845d57
JR
2238 | reg_n);
2239 break;
2240 case PPI3:
2241 if (field_b)
2242 as_bad (_("multiple parallel processing specifications"));
2243 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2244 + (reg_x << 6) + (reg_y << 4) + reg_n);
2245 break;
2246 case PDC:
2247 if (cond)
2248 as_bad (_("multiple condition specifications"));
2249 cond = opcode->nibbles[2] << 8;
2250 if (*op_end)
2251 goto skip_cond_check;
2252 break;
2253 case PPIC:
2254 if (field_b)
2255 as_bad (_("multiple parallel processing specifications"));
2256 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2257 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
2258 cond = 0;
2259 break;
2260 case PMUL:
2261 if (field_b)
2262 {
2263 if ((field_b & 0xef00) != 0xa100)
2264 as_bad (_("insn cannot be combined with pmuls"));
2265 field_b -= 0x8100;
2266 switch (field_b & 0xf)
2267 {
2268 case A_X0_NUM:
2269 field_b += 0 - A_X0_NUM;
2270 break;
2271 case A_Y0_NUM:
2272 field_b += 1 - A_Y0_NUM;
2273 break;
2274 case A_A0_NUM:
2275 field_b += 2 - A_A0_NUM;
2276 break;
2277 case A_A1_NUM:
2278 field_b += 3 - A_A1_NUM;
2279 break;
2280 default:
2281 as_bad (_("bad padd / psub pmuls output operand"));
2282 }
2283 }
2284 field_b += 0x4000 + reg_efg;
2285 break;
2286 default:
2287 abort ();
2288 }
2289 if (cond)
2290 {
2291 as_bad (_("condition not followed by conditionalizable insn"));
2292 cond = 0;
2293 }
2294 if (! *op_end)
2295 break;
2296 skip_cond_check:
2297 opcode = find_cooked_opcode (&op_end);
2298 if (opcode == NULL)
2299 {
2300 (as_bad
2301 (_("unrecognized characters at end of parallel processing insn")));
2302 break;
2303 }
2304 }
2305
2306 move_code = movx | movy;
2307 if (field_b)
2308 {
2309 /* Parallel processing insn. */
2310 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
2311
2312 output = frag_more (4);
0d10e182 2313 size = 4;
d4845d57
JR
2314 if (! target_big_endian)
2315 {
2316 output[3] = ppi_code >> 8;
2317 output[2] = ppi_code;
2318 }
2319 else
2320 {
2321 output[2] = ppi_code >> 8;
2322 output[3] = ppi_code;
2323 }
2324 move_code |= 0xf800;
2325 }
2326 else
0d10e182
JL
2327 {
2328 /* Just a double data transfer. */
2329 output = frag_more (2);
2330 size = 2;
2331 }
d4845d57
JR
2332 if (! target_big_endian)
2333 {
2334 output[1] = move_code >> 8;
2335 output[0] = move_code;
2336 }
2337 else
2338 {
2339 output[0] = move_code >> 8;
2340 output[1] = move_code;
2341 }
0d10e182 2342 return size;
d4845d57
JR
2343}
2344
2345/* This is the guts of the machine-dependent assembler. STR points to a
2346 machine dependent instruction. This function is supposed to emit
6b31947e 2347 the frags/bytes it assembles to. */
d4845d57
JR
2348
2349void
2350md_assemble (str)
2351 char *str;
2352{
2353 unsigned char *op_end;
2354 sh_operand_info operand[3];
2355 sh_opcode_info *opcode;
dda5ecfc 2356 unsigned int size = 0;
d4845d57 2357
324bfcf3
AO
2358#ifdef HAVE_SH64
2359 if (sh64_isa_mode == sh64_isa_shmedia)
2360 {
2361 shmedia_md_assemble (str);
2362 return;
2363 }
2364 else
2365 {
2366 /* If we've seen pseudo-directives, make sure any emitted data or
2367 frags are marked as data. */
2368 if (seen_insn == false)
2369 {
2370 sh64_update_contents_mark (true);
2371 sh64_set_contents_type (CRT_SH5_ISA16);
2372 }
2373
2374 seen_insn = true;
2375 }
2376#endif /* HAVE_SH64 */
2377
d4845d57
JR
2378 opcode = find_cooked_opcode (&str);
2379 op_end = str;
252b5132
RH
2380
2381 if (opcode == NULL)
2382 {
2383 as_bad (_("unknown opcode"));
2384 return;
2385 }
2386
2387 if (sh_relax
2388 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
2389 {
2390 /* Output a CODE reloc to tell the linker that the following
2391 bytes are instructions, not data. */
2392 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2393 BFD_RELOC_SH_CODE);
2394 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
2395 }
2396
d4845d57
JR
2397 if (opcode->nibbles[0] == PPI)
2398 {
0d10e182 2399 size = assemble_ppi (op_end, opcode);
252b5132
RH
2400 }
2401 else
2402 {
0d10e182
JL
2403 if (opcode->arg[0] == A_BDISP12
2404 || opcode->arg[0] == A_BDISP8)
252b5132 2405 {
0d10e182
JL
2406 parse_exp (op_end + 1, &operand[0]);
2407 build_relax (opcode, &operand[0]);
5fc44b2d
JR
2408 }
2409 else
2410 {
0d10e182
JL
2411 if (opcode->arg[0] == A_END)
2412 {
2413 /* Ignore trailing whitespace. If there is any, it has already
2414 been compressed to a single space. */
2415 if (*op_end == ' ')
2416 op_end++;
2417 }
2418 else
2419 {
2420 op_end = get_operands (opcode, op_end, operand);
2421 }
2422 opcode = get_specific (opcode, operand);
252b5132 2423
0d10e182
JL
2424 if (opcode == 0)
2425 {
2426 /* Couldn't find an opcode which matched the operands. */
2427 char *where = frag_more (2);
2428 size = 2;
252b5132 2429
0d10e182
JL
2430 where[0] = 0x0;
2431 where[1] = 0x0;
2432 as_bad (_("invalid operands for opcode"));
2433 }
2434 else
2435 {
2436 if (*op_end)
2437 as_bad (_("excess operands: '%s'"), op_end);
2438
2439 size = build_Mytes (opcode, operand);
2440 }
252b5132 2441 }
0d10e182 2442 }
252b5132 2443
2bc0a128 2444#ifdef BFD_ASSEMBLER
4dc7ead9 2445 dwarf2_emit_insn (size);
2bc0a128 2446#endif
252b5132
RH
2447}
2448
2449/* This routine is called each time a label definition is seen. It
2450 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
2451
2452void
2453sh_frob_label ()
2454{
2455 static fragS *last_label_frag;
2456 static int last_label_offset;
2457
2458 if (sh_relax
2459 && seg_info (now_seg)->tc_segment_info_data.in_code)
2460 {
2461 int offset;
2462
2463 offset = frag_now_fix ();
2464 if (frag_now != last_label_frag
2465 || offset != last_label_offset)
c4aa876b 2466 {
252b5132
RH
2467 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
2468 last_label_frag = frag_now;
2469 last_label_offset = offset;
2470 }
2471 }
2472}
2473
2474/* This routine is called when the assembler is about to output some
2475 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
2476
2477void
2478sh_flush_pending_output ()
2479{
2480 if (sh_relax
2481 && seg_info (now_seg)->tc_segment_info_data.in_code)
2482 {
2483 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2484 BFD_RELOC_SH_DATA);
2485 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
2486 }
2487}
2488
2489symbolS *
c0fecd35 2490md_undefined_symbol (name)
538cd60f 2491 char *name ATTRIBUTE_UNUSED;
252b5132
RH
2492{
2493 return 0;
2494}
2495
2496#ifdef OBJ_COFF
056350c6 2497#ifndef BFD_ASSEMBLER
252b5132
RH
2498
2499void
c0fecd35 2500tc_crawl_symbol_chain (headers)
cce5a618 2501 object_headers *headers ATTRIBUTE_UNUSED;
252b5132
RH
2502{
2503 printf (_("call to tc_crawl_symbol_chain \n"));
2504}
2505
2506void
c0fecd35 2507tc_headers_hook (headers)
cce5a618 2508 object_headers *headers ATTRIBUTE_UNUSED;
252b5132
RH
2509{
2510 printf (_("call to tc_headers_hook \n"));
2511}
2512
056350c6 2513#endif
252b5132
RH
2514#endif
2515
6b31947e
NC
2516/* Various routines to kill one day. */
2517/* Equal to MAX_PRECISION in atof-ieee.c. */
252b5132
RH
2518#define MAX_LITTLENUMS 6
2519
6b31947e
NC
2520/* Turn a string in input_line_pointer into a floating point constant
2521 of type TYPE, and store the appropriate bytes in *LITP. The number
2522 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2523 returned, or NULL on OK. */
2524
252b5132
RH
2525char *
2526md_atof (type, litP, sizeP)
2527 int type;
2528 char *litP;
2529 int *sizeP;
2530{
2531 int prec;
2532 LITTLENUM_TYPE words[4];
2533 char *t;
2534 int i;
2535
2536 switch (type)
2537 {
2538 case 'f':
2539 prec = 2;
2540 break;
2541
2542 case 'd':
2543 prec = 4;
2544 break;
2545
2546 default:
2547 *sizeP = 0;
2548 return _("bad call to md_atof");
2549 }
2550
2551 t = atof_ieee (input_line_pointer, type, words);
2552 if (t)
2553 input_line_pointer = t;
2554
2555 *sizeP = prec * 2;
2556
2557 if (! target_big_endian)
2558 {
2559 for (i = prec - 1; i >= 0; i--)
2560 {
2561 md_number_to_chars (litP, (valueT) words[i], 2);
2562 litP += 2;
2563 }
2564 }
2565 else
2566 {
2567 for (i = 0; i < prec; i++)
2568 {
2569 md_number_to_chars (litP, (valueT) words[i], 2);
2570 litP += 2;
2571 }
2572 }
c4aa876b 2573
252b5132
RH
2574 return NULL;
2575}
2576
2577/* Handle the .uses pseudo-op. This pseudo-op is used just before a
2578 call instruction. It refers to a label of the instruction which
2579 loads the register which the call uses. We use it to generate a
2580 special reloc for the linker. */
2581
2582static void
2583s_uses (ignore)
43841e91 2584 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
2585{
2586 expressionS ex;
2587
2588 if (! sh_relax)
2589 as_warn (_(".uses pseudo-op seen when not relaxing"));
2590
2591 expression (&ex);
2592
2593 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2594 {
2595 as_bad (_("bad .uses format"));
2596 ignore_rest_of_line ();
2597 return;
2598 }
2599
2600 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
2601
2602 demand_empty_rest_of_line ();
2603}
2604\f
5a38dc70 2605const char *md_shortopts = "";
6b31947e
NC
2606struct option md_longopts[] =
2607{
252b5132 2608#define OPTION_RELAX (OPTION_MD_BASE)
05982cac
HPN
2609#define OPTION_BIG (OPTION_MD_BASE + 1)
2610#define OPTION_LITTLE (OPTION_BIG + 1)
252b5132 2611#define OPTION_SMALL (OPTION_LITTLE + 1)
d4845d57 2612#define OPTION_DSP (OPTION_SMALL + 1)
252b5132
RH
2613
2614 {"relax", no_argument, NULL, OPTION_RELAX},
05982cac 2615 {"big", no_argument, NULL, OPTION_BIG},
252b5132
RH
2616 {"little", no_argument, NULL, OPTION_LITTLE},
2617 {"small", no_argument, NULL, OPTION_SMALL},
d4845d57 2618 {"dsp", no_argument, NULL, OPTION_DSP},
324bfcf3
AO
2619#ifdef HAVE_SH64
2620#define OPTION_ISA (OPTION_DSP + 1)
2621#define OPTION_ABI (OPTION_ISA + 1)
2622#define OPTION_NO_MIX (OPTION_ABI + 1)
2623#define OPTION_SHCOMPACT_CONST_CRANGE (OPTION_NO_MIX + 1)
2624#define OPTION_NO_EXPAND (OPTION_SHCOMPACT_CONST_CRANGE + 1)
2625#define OPTION_PT32 (OPTION_NO_EXPAND + 1)
2626 {"isa", required_argument, NULL, OPTION_ISA},
2627 {"abi", required_argument, NULL, OPTION_ABI},
2628 {"no-mix", no_argument, NULL, OPTION_NO_MIX},
2629 {"shcompact-const-crange", no_argument, NULL, OPTION_SHCOMPACT_CONST_CRANGE},
2630 {"no-expand", no_argument, NULL, OPTION_NO_EXPAND},
2631 {"expand-pt32", no_argument, NULL, OPTION_PT32},
2632#endif /* HAVE_SH64 */
2633
252b5132
RH
2634 {NULL, no_argument, NULL, 0}
2635};
c4aa876b 2636size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
2637
2638int
2639md_parse_option (c, arg)
2640 int c;
43841e91 2641 char *arg ATTRIBUTE_UNUSED;
252b5132
RH
2642{
2643 switch (c)
2644 {
2645 case OPTION_RELAX:
2646 sh_relax = 1;
2647 break;
2648
05982cac
HPN
2649 case OPTION_BIG:
2650 target_big_endian = 1;
2651 break;
2652
252b5132 2653 case OPTION_LITTLE:
252b5132
RH
2654 target_big_endian = 0;
2655 break;
2656
2657 case OPTION_SMALL:
2658 sh_small = 1;
2659 break;
2660
d4845d57
JR
2661 case OPTION_DSP:
2662 sh_dsp = 1;
2663 break;
2664
324bfcf3
AO
2665#ifdef HAVE_SH64
2666 case OPTION_ISA:
2667 if (strcasecmp (arg, "shmedia") == 0)
2668 {
2669 if (sh64_isa_mode == sh64_isa_shcompact)
2670 as_bad (_("Invalid combination: --isa=SHcompact with --isa=SHmedia"));
2671 sh64_isa_mode = sh64_isa_shmedia;
2672 }
2673 else if (strcasecmp (arg, "shcompact") == 0)
2674 {
2675 if (sh64_isa_mode == sh64_isa_shmedia)
2676 as_bad (_("Invalid combination: --isa=SHmedia with --isa=SHcompact"));
2677 if (sh64_abi == sh64_abi_64)
2678 as_bad (_("Invalid combination: --abi=64 with --isa=SHcompact"));
2679 sh64_isa_mode = sh64_isa_shcompact;
2680 }
2681 else
2682 as_bad ("Invalid argument to --isa option: %s", arg);
2683 break;
2684
2685 case OPTION_ABI:
2686 if (strcmp (arg, "32") == 0)
2687 {
2688 if (sh64_abi == sh64_abi_64)
2689 as_bad (_("Invalid combination: --abi=32 with --abi=64"));
2690 sh64_abi = sh64_abi_32;
2691 }
2692 else if (strcmp (arg, "64") == 0)
2693 {
2694 if (sh64_abi == sh64_abi_32)
2695 as_bad (_("Invalid combination: --abi=64 with --abi=32"));
2696 if (sh64_isa_mode == sh64_isa_shcompact)
2697 as_bad (_("Invalid combination: --isa=SHcompact with --abi=64"));
2698 sh64_abi = sh64_abi_64;
2699 }
2700 else
2701 as_bad ("Invalid argument to --abi option: %s", arg);
2702 break;
2703
2704 case OPTION_NO_MIX:
2705 sh64_mix = false;
2706 break;
2707
2708 case OPTION_SHCOMPACT_CONST_CRANGE:
2709 sh64_shcompact_const_crange = true;
2710 break;
2711
2712 case OPTION_NO_EXPAND:
2713 sh64_expand = false;
2714 break;
2715
2716 case OPTION_PT32:
2717 sh64_pt32 = true;
2718 break;
2719#endif /* HAVE_SH64 */
2720
252b5132
RH
2721 default:
2722 return 0;
2723 }
2724
2725 return 1;
2726}
2727
2728void
2729md_show_usage (stream)
2730 FILE *stream;
2731{
c4aa876b 2732 fprintf (stream, _("\
252b5132
RH
2733SH options:\n\
2734-little generate little endian code\n\
05982cac 2735-big generate big endian code\n\
252b5132 2736-relax alter jump instructions for long displacements\n\
5b8274e3 2737-small align sections to 4 byte boundaries, not 16\n\
182e89d3 2738-dsp enable sh-dsp insns, and disable sh3e / sh4 insns.\n"));
324bfcf3
AO
2739#ifdef HAVE_SH64
2740 fprintf (stream, _("\
2741-isa=[shmedia set default instruction set for SH64\n\
2742 | SHmedia\n\
2743 | shcompact\n\
2744 | SHcompact]\n\
2745-abi=[32|64] set size of expanded SHmedia operands and object\n\
2746 file type\n\
2747-shcompact-const-crange emit code-range descriptors for constants in\n\
2748 SHcompact code sections\n\
2749-no-mix disallow SHmedia code in the same section as\n\
2750 constants and SHcompact code\n\
2751-no-expand do not expand MOVI, PT, PTA or PTB instructions\n\
2752-expand-pt32 with -abi=64, expand PT, PTA and PTB instructions\n\
2753 to 32 bits only"));
2754#endif /* HAVE_SH64 */
252b5132
RH
2755}
2756\f
252b5132
RH
2757/* This struct is used to pass arguments to sh_count_relocs through
2758 bfd_map_over_sections. */
2759
2760struct sh_count_relocs
2761{
2762 /* Symbol we are looking for. */
2763 symbolS *sym;
2764 /* Count of relocs found. */
2765 int count;
2766};
2767
2768/* Count the number of fixups in a section which refer to a particular
2769 symbol. When using BFD_ASSEMBLER, this is called via
2770 bfd_map_over_sections. */
2771
252b5132
RH
2772static void
2773sh_count_relocs (abfd, sec, data)
43841e91 2774 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
2775 segT sec;
2776 PTR data;
2777{
2778 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
2779 segment_info_type *seginfo;
2780 symbolS *sym;
2781 fixS *fix;
2782
2783 seginfo = seg_info (sec);
2784 if (seginfo == NULL)
2785 return;
2786
2787 sym = info->sym;
2788 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2789 {
2790 if (fix->fx_addsy == sym)
2791 {
2792 ++info->count;
2793 fix->fx_tcbit = 1;
2794 }
2795 }
2796}
2797
2798/* Handle the count relocs for a particular section. When using
2799 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
2800
252b5132
RH
2801static void
2802sh_frob_section (abfd, sec, ignore)
43841e91 2803 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 2804 segT sec;
43841e91 2805 PTR ignore ATTRIBUTE_UNUSED;
252b5132
RH
2806{
2807 segment_info_type *seginfo;
2808 fixS *fix;
2809
2810 seginfo = seg_info (sec);
2811 if (seginfo == NULL)
2812 return;
2813
2814 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2815 {
2816 symbolS *sym;
2817 bfd_vma val;
2818 fixS *fscan;
2819 struct sh_count_relocs info;
2820
2821 if (fix->fx_r_type != BFD_RELOC_SH_USES)
2822 continue;
2823
2824 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2825 symbol in the same section. */
2826 sym = fix->fx_addsy;
2827 if (sym == NULL
2828 || fix->fx_subsy != NULL
2829 || fix->fx_addnumber != 0
2830 || S_GET_SEGMENT (sym) != sec
2831#if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2832 || S_GET_STORAGE_CLASS (sym) == C_EXT
2833#endif
2834 || S_IS_EXTERNAL (sym))
2835 {
2836 as_warn_where (fix->fx_file, fix->fx_line,
2837 _(".uses does not refer to a local symbol in the same section"));
2838 continue;
2839 }
2840
2841 /* Look through the fixups again, this time looking for one
2842 at the same location as sym. */
2843 val = S_GET_VALUE (sym);
2844 for (fscan = seginfo->fix_root;
2845 fscan != NULL;
2846 fscan = fscan->fx_next)
2847 if (val == fscan->fx_frag->fr_address + fscan->fx_where
2848 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
2849 && fscan->fx_r_type != BFD_RELOC_SH_CODE
2850 && fscan->fx_r_type != BFD_RELOC_SH_DATA
2851 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
2852 break;
2853 if (fscan == NULL)
2854 {
2855 as_warn_where (fix->fx_file, fix->fx_line,
2856 _("can't find fixup pointed to by .uses"));
2857 continue;
2858 }
2859
2860 if (fscan->fx_tcbit)
2861 {
2862 /* We've already done this one. */
2863 continue;
2864 }
2865
6b31947e
NC
2866 /* The variable fscan should also be a fixup to a local symbol
2867 in the same section. */
252b5132
RH
2868 sym = fscan->fx_addsy;
2869 if (sym == NULL
2870 || fscan->fx_subsy != NULL
2871 || fscan->fx_addnumber != 0
2872 || S_GET_SEGMENT (sym) != sec
2873#if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2874 || S_GET_STORAGE_CLASS (sym) == C_EXT
2875#endif
2876 || S_IS_EXTERNAL (sym))
2877 {
2878 as_warn_where (fix->fx_file, fix->fx_line,
2879 _(".uses target does not refer to a local symbol in the same section"));
2880 continue;
2881 }
2882
2883 /* Now we look through all the fixups of all the sections,
2884 counting the number of times we find a reference to sym. */
2885 info.sym = sym;
2886 info.count = 0;
2887#ifdef BFD_ASSEMBLER
2888 bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
2889#else
2890 {
2891 int iscan;
2892
2893 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
2894 sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
2895 }
2896#endif
2897
2898 if (info.count < 1)
2899 abort ();
2900
2901 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
2902 We have already adjusted the value of sym to include the
2903 fragment address, so we undo that adjustment here. */
2904 subseg_change (sec, 0);
7bcad3e5
NC
2905 fix_new (fscan->fx_frag,
2906 S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
252b5132
RH
2907 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
2908 }
2909}
2910
2911/* This function is called after the symbol table has been completed,
2912 but before the relocs or section contents have been written out.
2913 If we have seen any .uses pseudo-ops, they point to an instruction
2914 which loads a register with the address of a function. We look
2915 through the fixups to find where the function address is being
2916 loaded from. We then generate a COUNT reloc giving the number of
2917 times that function address is referred to. The linker uses this
2918 information when doing relaxing, to decide when it can eliminate
2919 the stored function address entirely. */
2920
2921void
2922sh_frob_file ()
2923{
324bfcf3
AO
2924#ifdef HAVE_SH64
2925 shmedia_frob_file_before_adjust ();
2926#endif
2927
252b5132
RH
2928 if (! sh_relax)
2929 return;
2930
2931#ifdef BFD_ASSEMBLER
2932 bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
2933#else
2934 {
2935 int iseg;
2936
2937 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
2938 sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
2939 }
2940#endif
2941}
2942
2943/* Called after relaxing. Set the correct sizes of the fragments, and
94f592af 2944 create relocs so that md_apply_fix3 will fill in the correct values. */
252b5132
RH
2945
2946void
2947md_convert_frag (headers, seg, fragP)
2948#ifdef BFD_ASSEMBLER
43841e91 2949 bfd *headers ATTRIBUTE_UNUSED;
252b5132 2950#else
cce5a618 2951 object_headers *headers ATTRIBUTE_UNUSED;
252b5132
RH
2952#endif
2953 segT seg;
2954 fragS *fragP;
2955{
2956 int donerelax = 0;
2957
2958 switch (fragP->fr_subtype)
2959 {
2960 case C (COND_JUMP, COND8):
2961 case C (COND_JUMP_DELAY, COND8):
2962 subseg_change (seg, 0);
2963 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2964 1, BFD_RELOC_SH_PCDISP8BY2);
2965 fragP->fr_fix += 2;
2966 fragP->fr_var = 0;
2967 break;
2968
2969 case C (UNCOND_JUMP, UNCOND12):
2970 subseg_change (seg, 0);
2971 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2972 1, BFD_RELOC_SH_PCDISP12BY2);
2973 fragP->fr_fix += 2;
2974 fragP->fr_var = 0;
2975 break;
2976
2977 case C (UNCOND_JUMP, UNCOND32):
2978 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
2979 if (fragP->fr_symbol == NULL)
99b222b4
HPN
2980 as_bad_where (fragP->fr_file, fragP->fr_line,
2981 _("displacement overflows 12-bit field"));
252b5132 2982 else if (S_IS_DEFINED (fragP->fr_symbol))
99b222b4
HPN
2983 as_bad_where (fragP->fr_file, fragP->fr_line,
2984 _("displacement to defined symbol %s overflows 12-bit field"),
2985 S_GET_NAME (fragP->fr_symbol));
252b5132 2986 else
99b222b4
HPN
2987 as_bad_where (fragP->fr_file, fragP->fr_line,
2988 _("displacement to undefined symbol %s overflows 12-bit field"),
2989 S_GET_NAME (fragP->fr_symbol));
2990 /* Stabilize this frag, so we don't trip an assert. */
2991 fragP->fr_fix += fragP->fr_var;
2992 fragP->fr_var = 0;
252b5132
RH
2993 break;
2994
2995 case C (COND_JUMP, COND12):
2996 case C (COND_JUMP_DELAY, COND12):
6b31947e 2997 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
252b5132
RH
2998 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
2999 was due to gas incorrectly relaxing an out-of-range conditional
3000 branch with delay slot. It turned:
3001 bf.s L6 (slot mov.l r12,@(44,r0))
3002 into:
c4aa876b 3003
252b5132
RH
30042c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
300530: 00 09 nop
300632: 10 cb mov.l r12,@(44,r0)
3007 Therefore, branches with delay slots have to be handled
3008 differently from ones without delay slots. */
3009 {
3010 unsigned char *buffer =
3011 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
3012 int highbyte = target_big_endian ? 0 : 1;
3013 int lowbyte = target_big_endian ? 1 : 0;
3014 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
3015
3016 /* Toggle the true/false bit of the bcond. */
3017 buffer[highbyte] ^= 0x2;
3018
d3ecfc59 3019 /* If this is a delayed branch, we may not put the bra in the
252b5132
RH
3020 slot. So we change it to a non-delayed branch, like that:
3021 b! cond slot_label; bra disp; slot_label: slot_insn
3022 ??? We should try if swapping the conditional branch and
3023 its delay-slot insn already makes the branch reach. */
3024
3025 /* Build a relocation to six / four bytes farther on. */
3026 subseg_change (seg, 0);
3027 fix_new (fragP, fragP->fr_fix, 2,
3028#ifdef BFD_ASSEMBLER
3029 section_symbol (seg),
3030#else
3031 seg_info (seg)->dot,
3032#endif
3033 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
3034 1, BFD_RELOC_SH_PCDISP8BY2);
3035
3036 /* Set up a jump instruction. */
3037 buffer[highbyte + 2] = 0xa0;
3038 buffer[lowbyte + 2] = 0;
3039 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
3040 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
3041
3042 if (delay)
3043 {
3044 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
3045 fragP->fr_fix += 4;
3046 }
3047 else
3048 {
3049 /* Fill in a NOP instruction. */
3050 buffer[highbyte + 4] = 0x0;
3051 buffer[lowbyte + 4] = 0x9;
3052
3053 fragP->fr_fix += 6;
3054 }
3055 fragP->fr_var = 0;
3056 donerelax = 1;
3057 }
3058 break;
3059
3060 case C (COND_JUMP, COND32):
3061 case C (COND_JUMP_DELAY, COND32):
3062 case C (COND_JUMP, UNDEF_WORD_DISP):
3063 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3064 if (fragP->fr_symbol == NULL)
99b222b4
HPN
3065 as_bad_where (fragP->fr_file, fragP->fr_line,
3066 _("displacement overflows 8-bit field"));
252b5132 3067 else if (S_IS_DEFINED (fragP->fr_symbol))
99b222b4
HPN
3068 as_bad_where (fragP->fr_file, fragP->fr_line,
3069 _("displacement to defined symbol %s overflows 8-bit field"),
3070 S_GET_NAME (fragP->fr_symbol));
252b5132 3071 else
99b222b4
HPN
3072 as_bad_where (fragP->fr_file, fragP->fr_line,
3073 _("displacement to undefined symbol %s overflows 8-bit field "),
3074 S_GET_NAME (fragP->fr_symbol));
3075 /* Stabilize this frag, so we don't trip an assert. */
3076 fragP->fr_fix += fragP->fr_var;
3077 fragP->fr_var = 0;
252b5132
RH
3078 break;
3079
3080 default:
324bfcf3
AO
3081#ifdef HAVE_SH64
3082 shmedia_md_convert_frag (headers, seg, fragP, true);
3083#else
252b5132 3084 abort ();
324bfcf3 3085#endif
252b5132
RH
3086 }
3087
3088 if (donerelax && !sh_relax)
3089 as_warn_where (fragP->fr_file, fragP->fr_line,
3090 _("overflow in branch to %s; converted into longer instruction sequence"),
3091 (fragP->fr_symbol != NULL
3092 ? S_GET_NAME (fragP->fr_symbol)
3093 : ""));
3094}
3095
3096valueT
c0fecd35 3097md_section_align (seg, size)
dda5ecfc 3098 segT seg ATTRIBUTE_UNUSED;
c0fecd35 3099 valueT size;
252b5132
RH
3100{
3101#ifdef BFD_ASSEMBLER
3102#ifdef OBJ_ELF
3103 return size;
3104#else /* ! OBJ_ELF */
3105 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
3106 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
3107#endif /* ! OBJ_ELF */
3108#else /* ! BFD_ASSEMBLER */
3109 return ((size + (1 << section_alignment[(int) seg]) - 1)
3110 & (-1 << section_alignment[(int) seg]));
3111#endif /* ! BFD_ASSEMBLER */
3112}
3113
3114/* This static variable is set by s_uacons to tell sh_cons_align that
3115 the expession does not need to be aligned. */
3116
3117static int sh_no_align_cons = 0;
3118
3119/* This handles the unaligned space allocation pseudo-ops, such as
3120 .uaword. .uaword is just like .word, but the value does not need
3121 to be aligned. */
3122
3123static void
3124s_uacons (bytes)
3125 int bytes;
3126{
3127 /* Tell sh_cons_align not to align this value. */
3128 sh_no_align_cons = 1;
3129 cons (bytes);
3130}
3131
3132/* If a .word, et. al., pseud-op is seen, warn if the value is not
3133 aligned correctly. Note that this can cause warnings to be issued
3134 when assembling initialized structured which were declared with the
3135 packed attribute. FIXME: Perhaps we should require an option to
3136 enable this warning? */
3137
3138void
3139sh_cons_align (nbytes)
3140 int nbytes;
3141{
3142 int nalign;
3143 char *p;
3144
3145 if (sh_no_align_cons)
3146 {
3147 /* This is an unaligned pseudo-op. */
3148 sh_no_align_cons = 0;
3149 return;
3150 }
3151
3152 nalign = 0;
3153 while ((nbytes & 1) == 0)
3154 {
3155 ++nalign;
3156 nbytes >>= 1;
3157 }
3158
3159 if (nalign == 0)
3160 return;
3161
3162 if (now_seg == absolute_section)
3163 {
3164 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3165 as_warn (_("misaligned data"));
3166 return;
3167 }
3168
0a9ef439 3169 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
252b5132
RH
3170 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3171
3172 record_alignment (now_seg, nalign);
3173}
3174
3175/* When relaxing, we need to output a reloc for any .align directive
3176 that requests alignment to a four byte boundary or larger. This is
3177 also where we check for misaligned data. */
3178
3179void
3180sh_handle_align (frag)
3181 fragS *frag;
3182{
0a9ef439
RH
3183 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
3184
3185 if (frag->fr_type == rs_align_code)
3186 {
3187 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
3188 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
3189
3190 char *p = frag->fr_literal + frag->fr_fix;
3191
3192 if (bytes & 1)
3193 {
3194 *p++ = 0;
3195 bytes--;
3196 frag->fr_fix += 1;
3197 }
3198
3199 if (target_big_endian)
3200 {
3201 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
3202 frag->fr_var = sizeof big_nop_pattern;
3203 }
3204 else
3205 {
3206 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
3207 frag->fr_var = sizeof little_nop_pattern;
3208 }
3209 }
3210 else if (frag->fr_type == rs_align_test)
3211 {
3212 if (bytes != 0)
3213 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
3214 }
3215
252b5132 3216 if (sh_relax
0a9ef439
RH
3217 && (frag->fr_type == rs_align
3218 || frag->fr_type == rs_align_code)
252b5132
RH
3219 && frag->fr_address + frag->fr_fix > 0
3220 && frag->fr_offset > 1
3221 && now_seg != bss_section)
3222 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
3223 BFD_RELOC_SH_ALIGN);
252b5132
RH
3224}
3225
3226/* This macro decides whether a particular reloc is an entry in a
3227 switch table. It is used when relaxing, because the linker needs
3228 to know about all such entries so that it can adjust them if
3229 necessary. */
3230
3231#ifdef BFD_ASSEMBLER
3232#define SWITCH_TABLE_CONS(fix) (0)
3233#else
3234#define SWITCH_TABLE_CONS(fix) \
3235 ((fix)->fx_r_type == 0 \
3236 && ((fix)->fx_size == 2 \
3237 || (fix)->fx_size == 1 \
3238 || (fix)->fx_size == 4))
3239#endif
3240
3241#define SWITCH_TABLE(fix) \
3242 ((fix)->fx_addsy != NULL \
3243 && (fix)->fx_subsy != NULL \
3244 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
3245 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
3246 && ((fix)->fx_r_type == BFD_RELOC_32 \
3247 || (fix)->fx_r_type == BFD_RELOC_16 \
3248 || (fix)->fx_r_type == BFD_RELOC_8 \
3249 || SWITCH_TABLE_CONS (fix)))
3250
3251/* See whether we need to force a relocation into the output file.
3252 This is used to force out switch and PC relative relocations when
3253 relaxing. */
3254
3255int
3256sh_force_relocation (fix)
3257 fixS *fix;
3258{
3259
3260 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
015551fc
JR
3261 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY
3262 || fix->fx_r_type == BFD_RELOC_SH_LOOP_START
3263 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END)
252b5132
RH
3264 return 1;
3265
3266 if (! sh_relax)
3267 return 0;
3268
3269 return (fix->fx_pcrel
3270 || SWITCH_TABLE (fix)
3271 || fix->fx_r_type == BFD_RELOC_SH_COUNT
3272 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
3273 || fix->fx_r_type == BFD_RELOC_SH_CODE
3274 || fix->fx_r_type == BFD_RELOC_SH_DATA
324bfcf3
AO
3275#ifdef HAVE_SH64
3276 || fix->fx_r_type == BFD_RELOC_SH_SHMEDIA_CODE
3277#endif
252b5132
RH
3278 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
3279}
3280
3281#ifdef OBJ_ELF
3282boolean
3283sh_fix_adjustable (fixP)
3284 fixS *fixP;
3285{
3286
3287 if (fixP->fx_addsy == NULL)
3288 return 1;
c4aa876b 3289
a1cc9221
AO
3290 if (fixP->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
3291 || fixP->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
3292 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
3293 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
3294 || fixP->fx_r_type == BFD_RELOC_8_PCREL
3295 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH16
3296 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH32)
3297 return 1;
3298
3299 if (! TC_RELOC_RTSYM_LOC_FIXUP (fixP)
a1cc9221
AO
3300 || fixP->fx_r_type == BFD_RELOC_RVA)
3301 return 0;
3302
252b5132
RH
3303 /* We need the symbol name for the VTABLE entries */
3304 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3305 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3306 return 0;
3307
3308 return 1;
3309}
d4845d57 3310
6b31947e
NC
3311void
3312sh_elf_final_processing ()
d4845d57
JR
3313{
3314 int val;
3315
3316 /* Set file-specific flags to indicate if this code needs
3317 a processor with the sh-dsp / sh3e ISA to execute. */
324bfcf3
AO
3318#ifdef HAVE_SH64
3319 /* SH5 and above don't know about the valid_arch arch_sh* bits defined
3320 in sh-opc.h, so check SH64 mode before checking valid_arch. */
3321 if (sh64_isa_mode != sh64_isa_unspecified)
3322 val = EF_SH5;
3323 else
3324#endif /* HAVE_SH64 */
d4845d57
JR
3325 if (valid_arch & arch_sh1)
3326 val = EF_SH1;
3327 else if (valid_arch & arch_sh2)
3328 val = EF_SH2;
3329 else if (valid_arch & arch_sh_dsp)
3330 val = EF_SH_DSP;
3331 else if (valid_arch & arch_sh3)
3332 val = EF_SH3;
3333 else if (valid_arch & arch_sh3_dsp)
3334 val = EF_SH_DSP;
3335 else if (valid_arch & arch_sh3e)
3336 val = EF_SH3E;
3337 else if (valid_arch & arch_sh4)
3338 val = EF_SH4;
3339 else
3340 abort ();
3341
3342 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
3343 elf_elfheader (stdoutput)->e_flags |= val;
3344}
252b5132
RH
3345#endif
3346
3347/* Apply a fixup to the object file. */
3348
252b5132 3349void
94f592af
NC
3350md_apply_fix3 (fixP, valP, seg)
3351 fixS * fixP;
3352 valueT * valP;
3353 segT seg ATTRIBUTE_UNUSED;
252b5132
RH
3354{
3355 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3356 int lowbyte = target_big_endian ? 1 : 0;
3357 int highbyte = target_big_endian ? 0 : 1;
2ed5f585 3358 long val = (long) *valP;
252b5132
RH
3359 long max, min;
3360 int shift;
3361
3362#ifdef BFD_ASSEMBLER
a1cc9221
AO
3363 /* A difference between two symbols, the second of which is in the
3364 current section, is transformed in a PC-relative relocation to
3365 the other symbol. We have to adjust the relocation type here. */
3366 if (fixP->fx_pcrel)
3367 {
3368 switch (fixP->fx_r_type)
3369 {
3370 default:
3371 break;
3372
3373 case BFD_RELOC_32:
3374 fixP->fx_r_type = BFD_RELOC_32_PCREL;
3375 break;
3376
3377 /* Currently, we only support 32-bit PCREL relocations.
3378 We'd need a new reloc type to handle 16_PCREL, and
3379 8_PCREL is already taken for R_SH_SWITCH8, which
3380 apparently does something completely different than what
3381 we need. FIXME. */
3382 case BFD_RELOC_16:
3383 bfd_set_error (bfd_error_bad_value);
94f592af 3384 return;
81d4177b 3385
a1cc9221
AO
3386 case BFD_RELOC_8:
3387 bfd_set_error (bfd_error_bad_value);
94f592af 3388 return;
a1cc9221
AO
3389 }
3390 }
3391
6b31947e
NC
3392 /* The function adjust_reloc_syms won't convert a reloc against a weak
3393 symbol into a reloc against a section, but bfd_install_relocation
3394 will screw up if the symbol is defined, so we have to adjust val here
1308f14c
HPN
3395 to avoid the screw up later.
3396
3397 For ordinary relocs, this does not happen for ELF, since for ELF,
3398 bfd_install_relocation uses the "special function" field of the
3399 howto, and does not execute the code that needs to be undone, as long
3400 as the special function does not return bfd_reloc_continue.
3401 It can happen for GOT- and PLT-type relocs the way they are
3402 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
3403 doesn't matter here since those relocs don't use VAL; see below. */
3404 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3405 && fixP->fx_addsy != NULL
252b5132
RH
3406 && S_IS_WEAK (fixP->fx_addsy))
3407 val -= S_GET_VALUE (fixP->fx_addsy);
3408#endif
3409
3410#ifndef BFD_ASSEMBLER
3411 if (fixP->fx_r_type == 0)
3412 {
3413 if (fixP->fx_size == 2)
3414 fixP->fx_r_type = BFD_RELOC_16;
3415 else if (fixP->fx_size == 4)
3416 fixP->fx_r_type = BFD_RELOC_32;
3417 else if (fixP->fx_size == 1)
3418 fixP->fx_r_type = BFD_RELOC_8;
3419 else
3420 abort ();
3421 }
3422#endif
3423
3424 max = min = 0;
3425 shift = 0;
3426 switch (fixP->fx_r_type)
3427 {
3428 case BFD_RELOC_SH_IMM4:
3429 max = 0xf;
3430 *buf = (*buf & 0xf0) | (val & 0xf);
3431 break;
3432
3433 case BFD_RELOC_SH_IMM4BY2:
3434 max = 0xf;
3435 shift = 1;
3436 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
3437 break;
3438
3439 case BFD_RELOC_SH_IMM4BY4:
3440 max = 0xf;
3441 shift = 2;
3442 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
3443 break;
3444
3445 case BFD_RELOC_SH_IMM8BY2:
3446 max = 0xff;
3447 shift = 1;
3448 *buf = val >> 1;
3449 break;
3450
3451 case BFD_RELOC_SH_IMM8BY4:
3452 max = 0xff;
3453 shift = 2;
3454 *buf = val >> 2;
3455 break;
3456
3457 case BFD_RELOC_8:
3458 case BFD_RELOC_SH_IMM8:
3459 /* Sometimes the 8 bit value is sign extended (e.g., add) and
3460 sometimes it is not (e.g., and). We permit any 8 bit value.
3461 Note that adding further restrictions may invalidate
3462 reasonable looking assembly code, such as ``and -0x1,r0''. */
3463 max = 0xff;
c4aa876b 3464 min = -0xff;
252b5132
RH
3465 *buf++ = val;
3466 break;
3467
3468 case BFD_RELOC_SH_PCRELIMM8BY4:
3469 /* The lower two bits of the PC are cleared before the
3470 displacement is added in. We can assume that the destination
3471 is on a 4 byte bounday. If this instruction is also on a 4
3472 byte boundary, then we want
3473 (target - here) / 4
3474 and target - here is a multiple of 4.
3475 Otherwise, we are on a 2 byte boundary, and we want
3476 (target - (here - 2)) / 4
3477 and target - here is not a multiple of 4. Computing
3478 (target - (here - 2)) / 4 == (target - here + 2) / 4
3479 works for both cases, since in the first case the addition of
3480 2 will be removed by the division. target - here is in the
3481 variable val. */
3482 val = (val + 2) / 4;
3483 if (val & ~0xff)
3484 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3485 buf[lowbyte] = val;
3486 break;
3487
3488 case BFD_RELOC_SH_PCRELIMM8BY2:
3489 val /= 2;
3490 if (val & ~0xff)
3491 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3492 buf[lowbyte] = val;
3493 break;
3494
3495 case BFD_RELOC_SH_PCDISP8BY2:
3496 val /= 2;
3497 if (val < -0x80 || val > 0x7f)
3498 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3499 buf[lowbyte] = val;
3500 break;
3501
3502 case BFD_RELOC_SH_PCDISP12BY2:
3503 val /= 2;
8637c045 3504 if (val < -0x800 || val > 0x7ff)
252b5132
RH
3505 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3506 buf[lowbyte] = val & 0xff;
3507 buf[highbyte] |= (val >> 8) & 0xf;
3508 break;
3509
3510 case BFD_RELOC_32:
a1cc9221 3511 case BFD_RELOC_32_PCREL:
1db77c8e 3512 md_number_to_chars (buf, val, 4);
252b5132
RH
3513 break;
3514
3515 case BFD_RELOC_16:
1db77c8e 3516 md_number_to_chars (buf, val, 2);
252b5132
RH
3517 break;
3518
3519 case BFD_RELOC_SH_USES:
3520 /* Pass the value into sh_coff_reloc_mangle. */
3521 fixP->fx_addnumber = val;
3522 break;
3523
3524 case BFD_RELOC_SH_COUNT:
3525 case BFD_RELOC_SH_ALIGN:
3526 case BFD_RELOC_SH_CODE:
3527 case BFD_RELOC_SH_DATA:
3528 case BFD_RELOC_SH_LABEL:
3529 /* Nothing to do here. */
3530 break;
3531
015551fc
JR
3532 case BFD_RELOC_SH_LOOP_START:
3533 case BFD_RELOC_SH_LOOP_END:
3534
252b5132
RH
3535 case BFD_RELOC_VTABLE_INHERIT:
3536 case BFD_RELOC_VTABLE_ENTRY:
3537 fixP->fx_done = 0;
3538 return;
3539
a1cc9221
AO
3540#ifdef OBJ_ELF
3541 case BFD_RELOC_32_PLT_PCREL:
3542 /* Make the jump instruction point to the address of the operand. At
81d4177b 3543 runtime we merely add the offset to the actual PLT entry. */
94f592af 3544 * valP = 0xfffffffc;
ac3f04d7
AO
3545 val = fixP->fx_addnumber;
3546 if (fixP->fx_subsy)
3547 val -= S_GET_VALUE (fixP->fx_subsy);
538cd60f 3548 md_number_to_chars (buf, val, 4);
a1cc9221
AO
3549 break;
3550
3551 case BFD_RELOC_SH_GOTPC:
3552 /* This is tough to explain. We end up with this one if we have
3553 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
3554 The goal here is to obtain the absolute address of the GOT,
3555 and it is strongly preferable from a performance point of
3556 view to avoid using a runtime relocation for this. There are
3557 cases where you have something like:
81d4177b 3558
a1cc9221 3559 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
81d4177b 3560
a1cc9221
AO
3561 and here no correction would be required. Internally in the
3562 assembler we treat operands of this form as not being pcrel
3563 since the '.' is explicitly mentioned, and I wonder whether
3564 it would simplify matters to do it this way. Who knows. In
3565 earlier versions of the PIC patches, the pcrel_adjust field
3566 was used to store the correction, but since the expression is
3567 not pcrel, I felt it would be confusing to do it this way. */
94f592af 3568 * valP -= 1;
a1cc9221
AO
3569 md_number_to_chars (buf, val, 4);
3570 break;
3571
3572 case BFD_RELOC_32_GOT_PCREL:
324bfcf3 3573 case BFD_RELOC_SH_GOTPLT32:
94f592af 3574 * valP = 0; /* Fully resolved at runtime. No addend. */
a1cc9221
AO
3575 md_number_to_chars (buf, 0, 4);
3576 break;
3577
3578 case BFD_RELOC_32_GOTOFF:
538cd60f 3579 md_number_to_chars (buf, val, 4);
a1cc9221
AO
3580 break;
3581#endif
3582
252b5132 3583 default:
324bfcf3
AO
3584#ifdef HAVE_SH64
3585 shmedia_md_apply_fix3 (fixP, valP);
3586 return;
3587#else
252b5132 3588 abort ();
324bfcf3 3589#endif
252b5132
RH
3590 }
3591
3592 if (shift != 0)
3593 {
3594 if ((val & ((1 << shift) - 1)) != 0)
3595 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
3596 if (val >= 0)
3597 val >>= shift;
3598 else
3599 val = ((val >> shift)
3600 | ((long) -1 & ~ ((long) -1 >> shift)));
3601 }
3602 if (max != 0 && (val < min || val > max))
3603 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
3604
94f592af
NC
3605 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
3606 fixP->fx_done = 1;
252b5132
RH
3607}
3608
3609/* Called just before address relaxation. Return the length
3610 by which a fragment must grow to reach it's destination. */
3611
3612int
3613md_estimate_size_before_relax (fragP, segment_type)
3614 register fragS *fragP;
3615 register segT segment_type;
3616{
e66457fb
AM
3617 int what;
3618
252b5132
RH
3619 switch (fragP->fr_subtype)
3620 {
93c2a809 3621 default:
324bfcf3
AO
3622#ifdef HAVE_SH64
3623 return shmedia_md_estimate_size_before_relax (fragP, segment_type);
3624#else
93c2a809 3625 abort ();
324bfcf3
AO
3626#endif
3627
93c2a809 3628
252b5132 3629 case C (UNCOND_JUMP, UNDEF_DISP):
6b31947e 3630 /* Used to be a branch to somewhere which was unknown. */
252b5132
RH
3631 if (!fragP->fr_symbol)
3632 {
3633 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
252b5132
RH
3634 }
3635 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3636 {
3637 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
252b5132
RH
3638 }
3639 else
3640 {
3641 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
252b5132
RH
3642 }
3643 break;
3644
252b5132
RH
3645 case C (COND_JUMP, UNDEF_DISP):
3646 case C (COND_JUMP_DELAY, UNDEF_DISP):
e66457fb 3647 what = GET_WHAT (fragP->fr_subtype);
6b31947e 3648 /* Used to be a branch to somewhere which was unknown. */
252b5132
RH
3649 if (fragP->fr_symbol
3650 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3651 {
252b5132 3652 /* Got a symbol and it's defined in this segment, become byte
6b31947e 3653 sized - maybe it will fix up. */
252b5132 3654 fragP->fr_subtype = C (what, COND8);
252b5132
RH
3655 }
3656 else if (fragP->fr_symbol)
3657 {
6b31947e 3658 /* Its got a segment, but its not ours, so it will always be long. */
252b5132 3659 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
252b5132
RH
3660 }
3661 else
3662 {
6b31947e 3663 /* We know the abs value. */
252b5132 3664 fragP->fr_subtype = C (what, COND8);
252b5132 3665 }
93c2a809 3666 break;
252b5132 3667
93c2a809 3668 case C (UNCOND_JUMP, UNCOND12):
e66457fb 3669 case C (UNCOND_JUMP, UNCOND32):
93c2a809
AM
3670 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3671 case C (COND_JUMP, COND8):
e66457fb
AM
3672 case C (COND_JUMP, COND12):
3673 case C (COND_JUMP, COND32):
93c2a809
AM
3674 case C (COND_JUMP, UNDEF_WORD_DISP):
3675 case C (COND_JUMP_DELAY, COND8):
e66457fb
AM
3676 case C (COND_JUMP_DELAY, COND12):
3677 case C (COND_JUMP_DELAY, COND32):
93c2a809
AM
3678 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3679 /* When relaxing a section for the second time, we don't need to
e66457fb 3680 do anything besides return the current size. */
252b5132
RH
3681 break;
3682 }
e66457fb
AM
3683
3684 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
252b5132
RH
3685 return fragP->fr_var;
3686}
3687
6b31947e 3688/* Put number into target byte order. */
252b5132
RH
3689
3690void
3691md_number_to_chars (ptr, use, nbytes)
3692 char *ptr;
3693 valueT use;
3694 int nbytes;
3695{
324bfcf3
AO
3696#ifdef HAVE_SH64
3697 /* We might need to set the contents type to data. */
3698 sh64_flag_output ();
3699#endif
3700
252b5132
RH
3701 if (! target_big_endian)
3702 number_to_chars_littleendian (ptr, use, nbytes);
3703 else
3704 number_to_chars_bigendian (ptr, use, nbytes);
3705}
3706
cce5a618
NC
3707/* This version is used in obj-coff.c when not using BFD_ASSEMBLER.
3708 eg for the sh-hms target. */
3709
3710long
3711md_pcrel_from (fixP)
3712 fixS *fixP;
3713{
3714 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
3715}
3716
252b5132 3717long
ef17112f 3718md_pcrel_from_section (fixP, sec)
252b5132 3719 fixS *fixP;
ef17112f 3720 segT sec;
252b5132 3721{
ef17112f
HPN
3722 if (fixP->fx_addsy != (symbolS *) NULL
3723 && (! S_IS_DEFINED (fixP->fx_addsy)
3724 || S_IS_EXTERN (fixP->fx_addsy)
3725 || S_IS_WEAK (fixP->fx_addsy)
3726 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3727 {
3728 /* The symbol is undefined (or is defined but not in this section,
3729 or we're not sure about it being the final definition). Let the
3730 linker figure it out. We need to adjust the subtraction of a
3731 symbol to the position of the relocated data, though. */
3732 return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
3733 }
3734
cce5a618 3735 return md_pcrel_from (fixP);
252b5132
RH
3736}
3737
3738#ifdef OBJ_COFF
3739
3740int
3741tc_coff_sizemachdep (frag)
3742 fragS *frag;
3743{
3744 return md_relax_table[frag->fr_subtype].rlx_length;
3745}
3746
3747#endif /* OBJ_COFF */
3748
252b5132
RH
3749#ifndef BFD_ASSEMBLER
3750#ifdef OBJ_COFF
3751
3752/* Map BFD relocs to SH COFF relocs. */
3753
3754struct reloc_map
3755{
3756 bfd_reloc_code_real_type bfd_reloc;
3757 int sh_reloc;
3758};
3759
3760static const struct reloc_map coff_reloc_map[] =
3761{
3762 { BFD_RELOC_32, R_SH_IMM32 },
3763 { BFD_RELOC_16, R_SH_IMM16 },
3764 { BFD_RELOC_8, R_SH_IMM8 },
3765 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
3766 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
3767 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
3768 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
3769 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
3770 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
3771 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
3772 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
3773 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
3774 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
3775 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
3776 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
3777 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
3778 { BFD_RELOC_SH_USES, R_SH_USES },
3779 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
3780 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
3781 { BFD_RELOC_SH_CODE, R_SH_CODE },
3782 { BFD_RELOC_SH_DATA, R_SH_DATA },
3783 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
3784 { BFD_RELOC_UNUSED, 0 }
3785};
3786
3787/* Adjust a reloc for the SH. This is similar to the generic code,
3788 but does some minor tweaking. */
3789
3790void
3791sh_coff_reloc_mangle (seg, fix, intr, paddr)
3792 segment_info_type *seg;
3793 fixS *fix;
3794 struct internal_reloc *intr;
3795 unsigned int paddr;
3796{
3797 symbolS *symbol_ptr = fix->fx_addsy;
3798 symbolS *dot;
3799
3800 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
3801
3802 if (! SWITCH_TABLE (fix))
3803 {
3804 const struct reloc_map *rm;
3805
3806 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
3807 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
3808 break;
3809 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
3810 as_bad_where (fix->fx_file, fix->fx_line,
3811 _("Can not represent %s relocation in this object file format"),
3812 bfd_get_reloc_code_name (fix->fx_r_type));
3813 intr->r_type = rm->sh_reloc;
3814 intr->r_offset = 0;
3815 }
3816 else
3817 {
3818 know (sh_relax);
3819
3820 if (fix->fx_r_type == BFD_RELOC_16)
3821 intr->r_type = R_SH_SWITCH16;
3822 else if (fix->fx_r_type == BFD_RELOC_8)
3823 intr->r_type = R_SH_SWITCH8;
3824 else if (fix->fx_r_type == BFD_RELOC_32)
3825 intr->r_type = R_SH_SWITCH32;
3826 else
3827 abort ();
3828
3829 /* For a switch reloc, we set r_offset to the difference between
3830 the reloc address and the subtrahend. When the linker is
3831 doing relaxing, it can use the determine the starting and
3832 ending points of the switch difference expression. */
3833 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
3834 }
3835
3836 /* PC relative relocs are always against the current section. */
3837 if (symbol_ptr == NULL)
3838 {
3839 switch (fix->fx_r_type)
3840 {
3841 case BFD_RELOC_SH_PCRELIMM8BY2:
3842 case BFD_RELOC_SH_PCRELIMM8BY4:
3843 case BFD_RELOC_SH_PCDISP8BY2:
3844 case BFD_RELOC_SH_PCDISP12BY2:
3845 case BFD_RELOC_SH_USES:
3846 symbol_ptr = seg->dot;
3847 break;
3848 default:
3849 break;
3850 }
3851 }
3852
3853 if (fix->fx_r_type == BFD_RELOC_SH_USES)
3854 {
3855 /* We can't store the offset in the object file, since this
3856 reloc does not take up any space, so we store it in r_offset.
94f592af 3857 The fx_addnumber field was set in md_apply_fix3. */
252b5132
RH
3858 intr->r_offset = fix->fx_addnumber;
3859 }
3860 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
3861 {
3862 /* We can't store the count in the object file, since this reloc
3863 does not take up any space, so we store it in r_offset. The
3864 fx_offset field was set when the fixup was created in
3865 sh_coff_frob_file. */
3866 intr->r_offset = fix->fx_offset;
3867 /* This reloc is always absolute. */
3868 symbol_ptr = NULL;
3869 }
3870 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
3871 {
3872 /* Store the alignment in the r_offset field. */
3873 intr->r_offset = fix->fx_offset;
3874 /* This reloc is always absolute. */
3875 symbol_ptr = NULL;
3876 }
3877 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
3878 || fix->fx_r_type == BFD_RELOC_SH_DATA
3879 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
3880 {
3881 /* These relocs are always absolute. */
3882 symbol_ptr = NULL;
3883 }
3884
3885 /* Turn the segment of the symbol into an offset. */
3886 if (symbol_ptr != NULL)
3887 {
3888 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
3889 if (dot != NULL)
3890 intr->r_symndx = dot->sy_number;
3891 else
3892 intr->r_symndx = symbol_ptr->sy_number;
3893 }
3894 else
3895 intr->r_symndx = -1;
3896}
3897
3898#endif /* OBJ_COFF */
3899#endif /* ! BFD_ASSEMBLER */
3900
3901#ifdef BFD_ASSEMBLER
3902
3903/* Create a reloc. */
3904
3905arelent *
3906tc_gen_reloc (section, fixp)
43841e91 3907 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
3908 fixS *fixp;
3909{
3910 arelent *rel;
3911 bfd_reloc_code_real_type r_type;
3912
3913 rel = (arelent *) xmalloc (sizeof (arelent));
49309057
ILT
3914 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3915 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
3916 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3917
538cd60f
AO
3918 if (fixp->fx_subsy
3919 && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
3920 {
3921 fixp->fx_addnumber -= S_GET_VALUE (fixp->fx_subsy);
3922 fixp->fx_subsy = 0;
3923 }
3924
252b5132
RH
3925 r_type = fixp->fx_r_type;
3926
3927 if (SWITCH_TABLE (fixp))
3928 {
3929 rel->addend = rel->address - S_GET_VALUE (fixp->fx_subsy);
3930 if (r_type == BFD_RELOC_16)
3931 r_type = BFD_RELOC_SH_SWITCH16;
3932 else if (r_type == BFD_RELOC_8)
3933 r_type = BFD_RELOC_8_PCREL;
3934 else if (r_type == BFD_RELOC_32)
3935 r_type = BFD_RELOC_SH_SWITCH32;
3936 else
3937 abort ();
3938 }
3939 else if (r_type == BFD_RELOC_SH_USES)
3940 rel->addend = fixp->fx_addnumber;
3941 else if (r_type == BFD_RELOC_SH_COUNT)
3942 rel->addend = fixp->fx_offset;
3943 else if (r_type == BFD_RELOC_SH_ALIGN)
3944 rel->addend = fixp->fx_offset;
3945 else if (r_type == BFD_RELOC_VTABLE_INHERIT
3946 || r_type == BFD_RELOC_VTABLE_ENTRY)
3947 rel->addend = fixp->fx_offset;
015551fc
JR
3948 else if (r_type == BFD_RELOC_SH_LOOP_START
3949 || r_type == BFD_RELOC_SH_LOOP_END)
3950 rel->addend = fixp->fx_offset;
3951 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
3952 {
3953 rel->addend = 0;
3954 rel->address = rel->addend = fixp->fx_offset;
3955 }
324bfcf3
AO
3956#ifdef HAVE_SH64
3957 else if (shmedia_init_reloc (rel, fixp))
3958 ;
3959#endif
252b5132
RH
3960 else if (fixp->fx_pcrel)
3961 rel->addend = fixp->fx_addnumber;
a1cc9221
AO
3962 else if (r_type == BFD_RELOC_32 || r_type == BFD_RELOC_32_GOTOFF)
3963 rel->addend = fixp->fx_addnumber;
252b5132
RH
3964 else
3965 rel->addend = 0;
3966
3967 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
538cd60f 3968 if (rel->howto == NULL || fixp->fx_subsy)
252b5132
RH
3969 {
3970 as_bad_where (fixp->fx_file, fixp->fx_line,
3971 _("Cannot represent relocation type %s"),
3972 bfd_get_reloc_code_name (r_type));
3973 /* Set howto to a garbage value so that we can keep going. */
3974 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3975 assert (rel->howto != NULL);
3976 }
3977
3978 return rel;
3979}
3980
538cd60f
AO
3981#ifdef OBJ_ELF
3982inline static char *
3983sh_end_of_match (cont, what)
3984 char *cont, *what;
3985{
3986 int len = strlen (what);
3987
3988 if (strncasecmp (cont, what, strlen (what)) == 0
3989 && ! is_part_of_name (cont[len]))
3990 return cont + len;
3991
3992 return NULL;
5d6255fe 3993}
538cd60f
AO
3994
3995int
3996sh_parse_name (name, exprP, nextcharP)
3997 char const *name;
3998 expressionS *exprP;
3999 char *nextcharP;
4000{
4001 char *next = input_line_pointer;
4002 char *next_end;
4003 int reloc_type;
4004 segT segment;
4005
4006 exprP->X_op_symbol = NULL;
4007
4008 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
4009 {
4010 if (! GOT_symbol)
4011 GOT_symbol = symbol_find_or_make (name);
4012
4013 exprP->X_add_symbol = GOT_symbol;
4014 no_suffix:
4015 /* If we have an absolute symbol or a reg, then we know its
4016 value now. */
4017 segment = S_GET_SEGMENT (exprP->X_add_symbol);
4018 if (segment == absolute_section)
4019 {
4020 exprP->X_op = O_constant;
4021 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4022 exprP->X_add_symbol = NULL;
4023 }
4024 else if (segment == reg_section)
4025 {
4026 exprP->X_op = O_register;
4027 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4028 exprP->X_add_symbol = NULL;
4029 }
4030 else
4031 {
4032 exprP->X_op = O_symbol;
4033 exprP->X_add_number = 0;
4034 }
4035
4036 return 1;
4037 }
4038
4039 exprP->X_add_symbol = symbol_find_or_make (name);
5d6255fe 4040
538cd60f
AO
4041 if (*nextcharP != '@')
4042 goto no_suffix;
4043 else if ((next_end = sh_end_of_match (next + 1, "GOTOFF")))
4044 reloc_type = BFD_RELOC_32_GOTOFF;
324bfcf3
AO
4045 else if ((next_end = sh_end_of_match (next + 1, "GOTPLT")))
4046 reloc_type = BFD_RELOC_SH_GOTPLT32;
538cd60f
AO
4047 else if ((next_end = sh_end_of_match (next + 1, "GOT")))
4048 reloc_type = BFD_RELOC_32_GOT_PCREL;
4049 else if ((next_end = sh_end_of_match (next + 1, "PLT")))
4050 reloc_type = BFD_RELOC_32_PLT_PCREL;
4051 else
4052 goto no_suffix;
4053
4054 *input_line_pointer = *nextcharP;
4055 input_line_pointer = next_end;
4056 *nextcharP = *input_line_pointer;
4057 *input_line_pointer = '\0';
4058
4059 exprP->X_op = O_PIC_reloc;
4060 exprP->X_add_number = 0;
4061 exprP->X_md = reloc_type;
4062
4063 return 1;
4064}
4065#endif
252b5132 4066#endif /* BFD_ASSEMBLER */
This page took 0.323575 seconds and 4 git commands to generate.