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