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