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