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