* config/tc-sh.h (md_cons_align): Define.
[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, 1997 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 /*
22 Written By Steve Chamberlain
23 sac@cygnus.com
24 */
25
26 #include <stdio.h>
27 #include "as.h"
28 #include "bfd.h"
29 #include "subsegs.h"
30 #define DEFINE_TABLE
31 #include "opcodes/sh-opc.h"
32 #include <ctype.h>
33 const char comment_chars[] = "!";
34 const char line_separator_chars[] = ";";
35 const char line_comment_chars[] = "!#";
36
37 static void s_uses PARAMS ((int));
38
39 static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
40 static void sh_frob_section PARAMS ((bfd *, segT, PTR));
41
42 /* This table describes all the machine specific pseudo-ops the assembler
43 has to support. The fields are:
44 pseudo-op name without dot
45 function to call to execute this pseudo-op
46 Integer arg to pass to the function
47 */
48
49 void cons ();
50 void s_align_bytes ();
51 static void s_uacons PARAMS ((int));
52
53 int shl = 0;
54
55 static void
56 little (ignore)
57 int ignore;
58 {
59 shl = 1;
60 target_big_endian = 0;
61 }
62
63 const pseudo_typeS md_pseudo_table[] =
64 {
65 {"int", cons, 4},
66 {"word", cons, 2},
67 {"form", listing_psize, 0},
68 {"little", little, 0},
69 {"heading", listing_title, 0},
70 {"import", s_ignore, 0},
71 {"page", listing_eject, 0},
72 {"program", s_ignore, 0},
73 {"uses", s_uses, 0},
74 {"uaword", s_uacons, 2},
75 {"ualong", s_uacons, 4},
76 {0, 0, 0}
77 };
78
79 /*int md_reloc_size; */
80
81 int sh_relax; /* set if -relax seen */
82
83 const char EXP_CHARS[] = "eE";
84
85 /* Chars that mean this number is a floating point constant */
86 /* As in 0f12.456 */
87 /* or 0d1.2345e12 */
88 const char FLT_CHARS[] = "rRsSfFdDxXpP";
89
90 #define C(a,b) ENCODE_RELAX(a,b)
91
92 #define JREG 14 /* Register used as a temp when relaxing */
93 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
94 #define GET_WHAT(x) ((x>>4))
95
96 /* These are the two types of relaxable instrction */
97 #define COND_JUMP 1
98 #define UNCOND_JUMP 2
99
100 #define UNDEF_DISP 0
101 #define COND8 1
102 #define COND12 2
103 #define COND32 3
104 #define UNCOND12 1
105 #define UNCOND32 2
106 #define UNDEF_WORD_DISP 4
107 #define END 5
108
109 #define UNCOND12 1
110 #define UNCOND32 2
111
112 /* Branch displacements are from the address of the branch plus
113 four, thus all minimum and maximum values have 4 added to them. */
114 #define COND8_F 258
115 #define COND8_M -252
116 #define COND8_LENGTH 2
117
118 /* There is one extra instruction before the branch, so we must add
119 two more bytes to account for it. */
120 #define COND12_F 4100
121 #define COND12_M -4090
122 #define COND12_LENGTH 6
123
124 /* ??? The minimum and maximum values are wrong, but this does not matter
125 since this relocation type is not supported yet. */
126 #define COND32_F (1<<30)
127 #define COND32_M -(1<<30)
128 #define COND32_LENGTH 14
129
130 #define UNCOND12_F 4098
131 #define UNCOND12_M -4092
132 #define UNCOND12_LENGTH 2
133
134 /* ??? The minimum and maximum values are wrong, but this does not matter
135 since this relocation type is not supported yet. */
136 #define UNCOND32_F (1<<30)
137 #define UNCOND32_M -(1<<30)
138 #define UNCOND32_LENGTH 14
139
140 const relax_typeS md_relax_table[C (END, 0)] = {
141 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
142 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
143
144 { 0 },
145 /* C (COND_JUMP, COND8) */
146 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
147 /* C (COND_JUMP, COND12) */
148 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
149 /* C (COND_JUMP, COND32) */
150 { COND32_F, COND32_M, COND32_LENGTH, 0, },
151 { 0 }, { 0 }, { 0 }, { 0 },
152 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
153
154 { 0 },
155 /* C (UNCOND_JUMP, UNCOND12) */
156 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
157 /* C (UNCOND_JUMP, UNCOND32) */
158 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
159 { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
160 { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
161 };
162
163 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
164
165 /*
166 This function is called once, at assembler startup time. This should
167 set up all the tables, etc that the MD part of the assembler needs
168 */
169
170 void
171 md_begin ()
172 {
173 sh_opcode_info *opcode;
174 char *prev_name = "";
175
176 if (! shl)
177 target_big_endian = 1;
178
179 opcode_hash_control = hash_new ();
180
181 /* Insert unique names into hash table */
182 for (opcode = sh_table; opcode->name; opcode++)
183 {
184 if (strcmp (prev_name, opcode->name))
185 {
186 prev_name = opcode->name;
187 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
188 }
189 else
190 {
191 /* Make all the opcodes with the same name point to the same
192 string */
193 opcode->name = prev_name;
194 }
195 }
196 }
197
198 static int reg_m;
199 static int reg_n;
200 static int reg_b;
201
202 static expressionS immediate; /* absolute expression */
203
204 typedef struct
205 {
206 sh_arg_type type;
207 int reg;
208 }
209
210 sh_operand_info;
211
212 /* try and parse a reg name, returns number of chars consumed */
213 static int
214 parse_reg (src, mode, reg)
215 char *src;
216 int *mode;
217 int *reg;
218 {
219 /* We use !isalnum for the next character after the register name, to
220 make sure that we won't accidentally recognize a symbol name such as
221 'sram' as being a reference to the register 'sr'. */
222
223 if (src[0] == 'r')
224 {
225 if (src[1] >= '0' && src[1] <= '7' && strncmp(&src[2], "_bank", 5) == 0
226 && ! isalnum (src[7]))
227 {
228 *mode = A_REG_B;
229 *reg = (src[1] - '0');
230 return 7;
231 }
232 }
233
234 if (src[0] == 'r')
235 {
236 if (src[1] == '1')
237 {
238 if (src[2] >= '0' && src[2] <= '5' && ! isalnum (src[3]))
239 {
240 *mode = A_REG_N;
241 *reg = 10 + src[2] - '0';
242 return 3;
243 }
244 }
245 if (src[1] >= '0' && src[1] <= '9' && ! isalnum (src[2]))
246 {
247 *mode = A_REG_N;
248 *reg = (src[1] - '0');
249 return 2;
250 }
251 }
252
253 if (src[0] == 's' && src[1] == 's' && src[2] == 'r' && ! isalnum (src[3]))
254 {
255 *mode = A_SSR;
256 return 3;
257 }
258
259 if (src[0] == 's' && src[1] == 'p' && src[2] == 'c' && ! isalnum (src[3]))
260 {
261 *mode = A_SPC;
262 return 3;
263 }
264
265 if (src[0] == 's' && src[1] == 'r' && ! isalnum (src[2]))
266 {
267 *mode = A_SR;
268 return 2;
269 }
270
271 if (src[0] == 's' && src[1] == 'p' && ! isalnum (src[2]))
272 {
273 *mode = A_REG_N;
274 *reg = 15;
275 return 2;
276 }
277
278 if (src[0] == 'p' && src[1] == 'r' && ! isalnum (src[2]))
279 {
280 *mode = A_PR;
281 return 2;
282 }
283 if (src[0] == 'p' && src[1] == 'c' && ! isalnum (src[2]))
284 {
285 *mode = A_DISP_PC;
286 return 2;
287 }
288 if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r' && ! isalnum (src[3]))
289 {
290 *mode = A_GBR;
291 return 3;
292 }
293 if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r' && ! isalnum (src[3]))
294 {
295 *mode = A_VBR;
296 return 3;
297 }
298
299 if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c' && ! isalnum (src[4]))
300 {
301 if (src[3] == 'l')
302 {
303 *mode = A_MACL;
304 return 4;
305 }
306 if (src[3] == 'h')
307 {
308 *mode = A_MACH;
309 return 4;
310 }
311 }
312 if (src[0] == 'f' && src[1] == 'r')
313 {
314 if (src[2] == '1')
315 {
316 if (src[3] >= '0' && src[3] <= '5' && ! isalnum (src[4]))
317 {
318 *mode = F_REG_N;
319 *reg = 10 + src[3] - '0';
320 return 4;
321 }
322 }
323 if (src[2] >= '0' && src[2] <= '9' && ! isalnum (src[3]))
324 {
325 *mode = F_REG_N;
326 *reg = (src[2] - '0');
327 return 3;
328 }
329 }
330 if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l'
331 && ! isalnum (src[4]))
332 {
333 *mode = FPUL_N;
334 return 4;
335 }
336
337 if (src[0] == 'f' && src[1] == 'p' && src[2] == 's' && src[3] == 'c'
338 && src[4] == 'r' && ! isalnum (src[5]))
339 {
340 *mode = FPSCR_N;
341 return 5;
342 }
343
344 return 0;
345 }
346
347 static symbolS *dot()
348 {
349 const char *fake;
350
351 /* JF: '.' is pseudo symbol with value of current location
352 in current segment. */
353 fake = FAKE_LABEL_NAME;
354 return symbol_new (fake,
355 now_seg,
356 (valueT) frag_now_fix (),
357 frag_now);
358
359 }
360
361
362 static
363 char *
364 parse_exp (s)
365 char *s;
366 {
367 char *save;
368 char *new;
369
370 save = input_line_pointer;
371 input_line_pointer = s;
372 expression (&immediate);
373 if (immediate.X_op == O_absent)
374 as_bad ("missing operand");
375 new = input_line_pointer;
376 input_line_pointer = save;
377 return new;
378 }
379
380
381 /* The many forms of operand:
382
383 Rn Register direct
384 @Rn Register indirect
385 @Rn+ Autoincrement
386 @-Rn Autodecrement
387 @(disp:4,Rn)
388 @(disp:8,GBR)
389 @(disp:8,PC)
390
391 @(R0,Rn)
392 @(R0,GBR)
393
394 disp:8
395 disp:12
396 #imm8
397 pr, gbr, vbr, macl, mach
398
399 */
400
401 static
402 char *
403 parse_at (src, op)
404 char *src;
405 sh_operand_info *op;
406 {
407 int len;
408 int mode;
409 src++;
410 if (src[0] == '-')
411 {
412 /* Must be predecrement */
413 src++;
414
415 len = parse_reg (src, &mode, &(op->reg));
416 if (mode != A_REG_N)
417 as_bad ("illegal register after @-");
418
419 op->type = A_DEC_N;
420 src += len;
421 }
422 else if (src[0] == '(')
423 {
424 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
425 @(r0, rn) */
426 src++;
427 len = parse_reg (src, &mode, &(op->reg));
428 if (len && mode == A_REG_N)
429 {
430 src += len;
431 if (op->reg != 0)
432 {
433 as_bad ("must be @(r0,...)");
434 }
435 if (src[0] == ',')
436 src++;
437 /* Now can be rn or gbr */
438 len = parse_reg (src, &mode, &(op->reg));
439 if (mode == A_GBR)
440 {
441 op->type = A_R0_GBR;
442 }
443 else if (mode == A_REG_N)
444 {
445 op->type = A_IND_R0_REG_N;
446 }
447 else
448 {
449 as_bad ("syntax error in @(r0,...)");
450 }
451 }
452 else
453 {
454 /* Must be an @(disp,.. thing) */
455 src = parse_exp (src);
456 if (src[0] == ',')
457 src++;
458 /* Now can be rn, gbr or pc */
459 len = parse_reg (src, &mode, &op->reg);
460 if (len)
461 {
462 if (mode == A_REG_N)
463 {
464 op->type = A_DISP_REG_N;
465 }
466 else if (mode == A_GBR)
467 {
468 op->type = A_DISP_GBR;
469 }
470 else if (mode == A_DISP_PC)
471 {
472 /* Turn a plain @(4,pc) into @(.+4,pc) */
473 if (immediate.X_op == O_constant) {
474 immediate.X_add_symbol = dot();
475 immediate.X_op = O_symbol;
476 }
477 op->type = A_DISP_PC;
478 }
479 else
480 {
481 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
482 }
483 }
484 else
485 {
486 as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
487 }
488 }
489 src += len;
490 if (src[0] != ')')
491 as_bad ("expecting )");
492 else
493 src++;
494 }
495 else
496 {
497 src += parse_reg (src, &mode, &(op->reg));
498 if (mode != A_REG_N)
499 {
500 as_bad ("illegal register after @");
501 }
502 if (src[0] == '+')
503 {
504 op->type = A_INC_N;
505 src++;
506 }
507 else
508 {
509 op->type = A_IND_N;
510 }
511 }
512 return src;
513 }
514
515 static void
516 get_operand (ptr, op)
517 char **ptr;
518 sh_operand_info *op;
519 {
520 char *src = *ptr;
521 int mode = -1;
522 unsigned int len;
523
524 if (src[0] == '#')
525 {
526 src++;
527 *ptr = parse_exp (src);
528 op->type = A_IMM;
529 return;
530 }
531
532 else if (src[0] == '@')
533 {
534 *ptr = parse_at (src, op);
535 return;
536 }
537 len = parse_reg (src, &mode, &(op->reg));
538 if (len)
539 {
540 *ptr = src + len;
541 op->type = mode;
542 return;
543 }
544 else
545 {
546 /* Not a reg, the only thing left is a displacement */
547 *ptr = parse_exp (src);
548 op->type = A_DISP_PC;
549 return;
550 }
551 }
552
553 static
554 char *
555 get_operands (info, args, operand)
556 sh_opcode_info *info;
557 char *args;
558 sh_operand_info *operand;
559
560 {
561 char *ptr = args;
562 if (info->arg[0])
563 {
564 ptr++;
565
566 get_operand (&ptr, operand + 0);
567 if (info->arg[1])
568 {
569 if (*ptr == ',')
570 {
571 ptr++;
572 }
573 get_operand (&ptr, operand + 1);
574 if (info->arg[2])
575 {
576 if (*ptr == ',')
577 {
578 ptr++;
579 }
580 get_operand (&ptr, operand + 2);
581 }
582 else
583 {
584 operand[2].type = 0;
585 }
586 }
587 else
588 {
589 operand[1].type = 0;
590 operand[2].type = 0;
591 }
592 }
593 else
594 {
595 operand[0].type = 0;
596 operand[1].type = 0;
597 operand[2].type = 0;
598 }
599 return ptr;
600 }
601
602 /* Passed a pointer to a list of opcodes which use different
603 addressing modes, return the opcode which matches the opcodes
604 provided
605 */
606
607 static
608 sh_opcode_info *
609 get_specific (opcode, operands)
610 sh_opcode_info *opcode;
611 sh_operand_info *operands;
612 {
613 sh_opcode_info *this_try = opcode;
614 char *name = opcode->name;
615 int n = 0;
616 while (opcode->name)
617 {
618 this_try = opcode++;
619 if (this_try->name != name)
620 {
621 /* We've looked so far down the table that we've run out of
622 opcodes with the same name */
623 return 0;
624 }
625 /* look at both operands needed by the opcodes and provided by
626 the user - since an arg test will often fail on the same arg
627 again and again, we'll try and test the last failing arg the
628 first on each opcode try */
629
630 for (n = 0; this_try->arg[n]; n++)
631 {
632 sh_operand_info *user = operands + n;
633 sh_arg_type arg = this_try->arg[n];
634 switch (arg)
635 {
636 case A_IMM:
637 case A_BDISP12:
638 case A_BDISP8:
639 case A_DISP_GBR:
640 case A_DISP_PC:
641 case A_MACH:
642 case A_PR:
643 case A_MACL:
644 if (user->type != arg)
645 goto fail;
646 break;
647 case A_R0:
648 /* opcode needs r0 */
649 if (user->type != A_REG_N || user->reg != 0)
650 goto fail;
651 break;
652 case A_R0_GBR:
653 if (user->type != A_R0_GBR || user->reg != 0)
654 goto fail;
655 break;
656 case F_FR0:
657 if (user->type != F_REG_N || user->reg != 0)
658 goto fail;
659 break;
660
661 case A_REG_N:
662 case A_INC_N:
663 case A_DEC_N:
664 case A_IND_N:
665 case A_IND_R0_REG_N:
666 case A_DISP_REG_N:
667 case F_REG_N:
668 case FPUL_N:
669 case FPSCR_N:
670 /* Opcode needs rn */
671 if (user->type != arg)
672 goto fail;
673 reg_n = user->reg;
674 break;
675 case A_GBR:
676 case A_SR:
677 case A_VBR:
678 case A_SSR:
679 case A_SPC:
680 if (user->type != arg)
681 goto fail;
682 break;
683
684 case A_REG_B:
685 if (user->type != arg)
686 goto fail;
687 reg_b = user->reg;
688 break;
689
690 case A_REG_M:
691 case A_INC_M:
692 case A_DEC_M:
693 case A_IND_M:
694 case A_IND_R0_REG_M:
695 case A_DISP_REG_M:
696 /* Opcode needs rn */
697 if (user->type != arg - A_REG_M + A_REG_N)
698 goto fail;
699 reg_m = user->reg;
700 break;
701
702 case F_REG_M:
703 case FPUL_M:
704 case FPSCR_M:
705 /* Opcode needs rn */
706 if (user->type != arg - F_REG_M + F_REG_N)
707 goto fail;
708 reg_m = user->reg;
709 break;
710
711 default:
712 printf ("unhandled %d\n", arg);
713 goto fail;
714 }
715 }
716 return this_try;
717 fail:;
718 }
719
720 return 0;
721 }
722
723 int
724 check (operand, low, high)
725 expressionS *operand;
726 int low;
727 int high;
728 {
729 if (operand->X_op != O_constant
730 || operand->X_add_number < low
731 || operand->X_add_number > high)
732 {
733 as_bad ("operand must be absolute in range %d..%d", low, high);
734 }
735 return operand->X_add_number;
736 }
737
738
739 static void
740 insert (where, how, pcrel)
741 char *where;
742 int how;
743 int pcrel;
744 {
745 fix_new_exp (frag_now,
746 where - frag_now->fr_literal,
747 2,
748 &immediate,
749 pcrel,
750 how);
751 }
752
753 static void
754 build_relax (opcode)
755 sh_opcode_info *opcode;
756 {
757 int high_byte = target_big_endian ? 0 : 1;
758 char *p;
759
760 if (opcode->arg[0] == A_BDISP8)
761 {
762 p = frag_var (rs_machine_dependent,
763 md_relax_table[C (COND_JUMP, COND32)].rlx_length,
764 md_relax_table[C (COND_JUMP, COND8)].rlx_length,
765 C (COND_JUMP, 0),
766 immediate.X_add_symbol,
767 immediate.X_add_number,
768 0);
769 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
770 }
771 else if (opcode->arg[0] == A_BDISP12)
772 {
773 p = frag_var (rs_machine_dependent,
774 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
775 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
776 C (UNCOND_JUMP, 0),
777 immediate.X_add_symbol,
778 immediate.X_add_number,
779 0);
780 p[high_byte] = (opcode->nibbles[0] << 4);
781 }
782
783 }
784
785 /* Now we know what sort of opcodes it is, lets build the bytes -
786 */
787 static void
788 build_Mytes (opcode, operand)
789 sh_opcode_info *opcode;
790 sh_operand_info *operand;
791
792 {
793 int index;
794 char nbuf[4];
795 char *output = frag_more (2);
796 int low_byte = target_big_endian ? 1 : 0;
797 nbuf[0] = 0;
798 nbuf[1] = 0;
799 nbuf[2] = 0;
800 nbuf[3] = 0;
801
802 for (index = 0; index < 4; index++)
803 {
804 sh_nibble_type i = opcode->nibbles[index];
805 if (i < 16)
806 {
807 nbuf[index] = i;
808 }
809 else
810 {
811 switch (i)
812 {
813 case REG_N:
814 nbuf[index] = reg_n;
815 break;
816 case REG_M:
817 nbuf[index] = reg_m;
818 break;
819 case REG_B:
820 nbuf[index] = reg_b | 0x08;
821 break;
822 case DISP_4:
823 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0);
824 break;
825 case IMM_4BY4:
826 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0);
827 break;
828 case IMM_4BY2:
829 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0);
830 break;
831 case IMM_4:
832 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0);
833 break;
834 case IMM_8BY4:
835 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0);
836 break;
837 case IMM_8BY2:
838 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0);
839 break;
840 case IMM_8:
841 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0);
842 break;
843 case PCRELIMM_8BY4:
844 insert (output, BFD_RELOC_SH_PCRELIMM8BY4, 1);
845 break;
846 case PCRELIMM_8BY2:
847 insert (output, BFD_RELOC_SH_PCRELIMM8BY2, 1);
848 break;
849 default:
850 printf ("failed for %d\n", i);
851 }
852 }
853 }
854 if (! target_big_endian) {
855 output[1] = (nbuf[0] << 4) | (nbuf[1]);
856 output[0] = (nbuf[2] << 4) | (nbuf[3]);
857 }
858 else {
859 output[0] = (nbuf[0] << 4) | (nbuf[1]);
860 output[1] = (nbuf[2] << 4) | (nbuf[3]);
861 }
862 }
863
864 /* This is the guts of the machine-dependent assembler. STR points to a
865 machine dependent instruction. This function is supposed to emit
866 the frags/bytes it assembles to.
867 */
868
869 void
870 md_assemble (str)
871 char *str;
872 {
873 unsigned char *op_start;
874 unsigned char *op_end;
875 sh_operand_info operand[3];
876 sh_opcode_info *opcode;
877 char name[20];
878 int nlen = 0;
879 /* Drop leading whitespace */
880 while (*str == ' ')
881 str++;
882
883 /* find the op code end */
884 for (op_start = op_end = (unsigned char *) (str);
885 *op_end
886 && nlen < 20
887 && !is_end_of_line[*op_end] && *op_end != ' ';
888 op_end++)
889 {
890 name[nlen] = op_start[nlen];
891 nlen++;
892 }
893 name[nlen] = 0;
894
895 if (nlen == 0)
896 {
897 as_bad ("can't find opcode ");
898 }
899
900 opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
901
902 if (opcode == NULL)
903 {
904 as_bad ("unknown opcode");
905 return;
906 }
907
908 if (sh_relax
909 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
910 {
911 /* Output a CODE reloc to tell the linker that the following
912 bytes are instructions, not data. */
913 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
914 BFD_RELOC_SH_CODE);
915 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
916 }
917
918 if (opcode->arg[0] == A_BDISP12
919 || opcode->arg[0] == A_BDISP8)
920 {
921 parse_exp (op_end + 1);
922 build_relax (opcode);
923 }
924 else
925 {
926 if (opcode->arg[0] != A_END)
927 {
928 get_operands (opcode, op_end, operand);
929 }
930 opcode = get_specific (opcode, operand);
931
932 if (opcode == 0)
933 {
934 /* Couldn't find an opcode which matched the operands */
935 char *where = frag_more (2);
936
937 where[0] = 0x0;
938 where[1] = 0x0;
939 as_bad ("invalid operands for opcode");
940 return;
941 }
942
943 build_Mytes (opcode, operand);
944 }
945
946 }
947
948 /* This routine is called each time a label definition is seen. It
949 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
950
951 void
952 sh_frob_label ()
953 {
954 static fragS *last_label_frag;
955 static int last_label_offset;
956
957 if (sh_relax
958 && seg_info (now_seg)->tc_segment_info_data.in_code)
959 {
960 int offset;
961
962 offset = frag_now_fix ();
963 if (frag_now != last_label_frag
964 || offset != last_label_offset)
965 {
966 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
967 last_label_frag = frag_now;
968 last_label_offset = offset;
969 }
970 }
971 }
972
973 /* This routine is called when the assembler is about to output some
974 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
975
976 void
977 sh_flush_pending_output ()
978 {
979 if (sh_relax
980 && seg_info (now_seg)->tc_segment_info_data.in_code)
981 {
982 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
983 BFD_RELOC_SH_DATA);
984 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
985 }
986 }
987
988 symbolS *
989 DEFUN (md_undefined_symbol, (name),
990 char *name)
991 {
992 return 0;
993 }
994
995 #ifdef OBJ_COFF
996
997 void
998 DEFUN (tc_crawl_symbol_chain, (headers),
999 object_headers * headers)
1000 {
1001 printf ("call to tc_crawl_symbol_chain \n");
1002 }
1003
1004 void
1005 DEFUN (tc_headers_hook, (headers),
1006 object_headers * headers)
1007 {
1008 printf ("call to tc_headers_hook \n");
1009 }
1010
1011 #endif
1012
1013 /* Various routines to kill one day */
1014 /* Equal to MAX_PRECISION in atof-ieee.c */
1015 #define MAX_LITTLENUMS 6
1016
1017 /* Turn a string in input_line_pointer into a floating point constant of type
1018 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1019 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1020 */
1021 char *
1022 md_atof (type, litP, sizeP)
1023 int type;
1024 char *litP;
1025 int *sizeP;
1026 {
1027 int prec;
1028 LITTLENUM_TYPE words[4];
1029 char *t;
1030 int i;
1031
1032 switch (type)
1033 {
1034 case 'f':
1035 prec = 2;
1036 break;
1037
1038 case 'd':
1039 prec = 4;
1040 break;
1041
1042 default:
1043 *sizeP = 0;
1044 return "bad call to md_atof";
1045 }
1046
1047 t = atof_ieee (input_line_pointer, type, words);
1048 if (t)
1049 input_line_pointer = t;
1050
1051 *sizeP = prec * 2;
1052
1053 if (! target_big_endian)
1054 {
1055 for (i = prec - 1; i >= 0; i--)
1056 {
1057 md_number_to_chars (litP, (valueT) words[i], 2);
1058 litP += 2;
1059 }
1060 }
1061 else
1062 {
1063 for (i = 0; i < prec; i++)
1064 {
1065 md_number_to_chars (litP, (valueT) words[i], 2);
1066 litP += 2;
1067 }
1068 }
1069
1070 return NULL;
1071 }
1072
1073 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
1074 call instruction. It refers to a label of the instruction which
1075 loads the register which the call uses. We use it to generate a
1076 special reloc for the linker. */
1077
1078 static void
1079 s_uses (ignore)
1080 int ignore;
1081 {
1082 expressionS ex;
1083
1084 if (! sh_relax)
1085 as_warn (".uses pseudo-op seen when not relaxing");
1086
1087 expression (&ex);
1088
1089 if (ex.X_op != O_symbol || ex.X_add_number != 0)
1090 {
1091 as_bad ("bad .uses format");
1092 ignore_rest_of_line ();
1093 return;
1094 }
1095
1096 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
1097
1098 demand_empty_rest_of_line ();
1099 }
1100 \f
1101 CONST char *md_shortopts = "";
1102 struct option md_longopts[] = {
1103
1104 #define OPTION_RELAX (OPTION_MD_BASE)
1105 #define OPTION_LITTLE (OPTION_MD_BASE+1)
1106
1107 {"relax", no_argument, NULL, OPTION_RELAX},
1108 {"little", no_argument, NULL, OPTION_LITTLE},
1109 {NULL, no_argument, NULL, 0}
1110 };
1111 size_t md_longopts_size = sizeof(md_longopts);
1112
1113 int
1114 md_parse_option (c, arg)
1115 int c;
1116 char *arg;
1117 {
1118 switch (c)
1119 {
1120 case OPTION_RELAX:
1121 sh_relax = 1;
1122 break;
1123 case OPTION_LITTLE:
1124 shl = 1;
1125 target_big_endian = 0;
1126 break;
1127
1128 default:
1129 return 0;
1130 }
1131
1132 return 1;
1133 }
1134
1135 void
1136 md_show_usage (stream)
1137 FILE *stream;
1138 {
1139 fprintf(stream, "\
1140 SH options:\n\
1141 -little generate little endian code\n\
1142 -relax alter jump instructions for long displacements\n");
1143 }
1144 \f
1145 int md_short_jump_size;
1146
1147 void
1148 tc_Nout_fix_to_chars ()
1149 {
1150 printf ("call to tc_Nout_fix_to_chars \n");
1151 abort ();
1152 }
1153
1154 void
1155 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1156 char *ptr;
1157 addressT from_Nddr;
1158 addressT to_Nddr;
1159 fragS *frag;
1160 symbolS *to_symbol;
1161 {
1162 as_fatal ("failed sanity check.");
1163 }
1164
1165 void
1166 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1167 char *ptr;
1168 addressT from_Nddr, to_Nddr;
1169 fragS *frag;
1170 symbolS *to_symbol;
1171 {
1172 as_fatal ("failed sanity check.");
1173 }
1174
1175 /* This struct is used to pass arguments to sh_count_relocs through
1176 bfd_map_over_sections. */
1177
1178 struct sh_count_relocs
1179 {
1180 /* Symbol we are looking for. */
1181 symbolS *sym;
1182 /* Count of relocs found. */
1183 int count;
1184 };
1185
1186 /* Count the number of fixups in a section which refer to a particular
1187 symbol. When using BFD_ASSEMBLER, this is called via
1188 bfd_map_over_sections. */
1189
1190 /*ARGSUSED*/
1191 static void
1192 sh_count_relocs (abfd, sec, data)
1193 bfd *abfd;
1194 segT sec;
1195 PTR data;
1196 {
1197 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
1198 segment_info_type *seginfo;
1199 symbolS *sym;
1200 fixS *fix;
1201
1202 seginfo = seg_info (sec);
1203 if (seginfo == NULL)
1204 return;
1205
1206 sym = info->sym;
1207 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
1208 {
1209 if (fix->fx_addsy == sym)
1210 {
1211 ++info->count;
1212 fix->fx_tcbit = 1;
1213 }
1214 }
1215 }
1216
1217 /* Handle the count relocs for a particular section. When using
1218 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
1219
1220 /*ARGSUSED*/
1221 static void
1222 sh_frob_section (abfd, sec, ignore)
1223 bfd *abfd;
1224 segT sec;
1225 PTR ignore;
1226 {
1227 segment_info_type *seginfo;
1228 fixS *fix;
1229
1230 seginfo = seg_info (sec);
1231 if (seginfo == NULL)
1232 return;
1233
1234 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
1235 {
1236 symbolS *sym;
1237 bfd_vma val;
1238 fixS *fscan;
1239 struct sh_count_relocs info;
1240
1241 if (fix->fx_r_type != BFD_RELOC_SH_USES)
1242 continue;
1243
1244 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
1245 symbol in the same section. */
1246 sym = fix->fx_addsy;
1247 if (sym == NULL
1248 || fix->fx_subsy != NULL
1249 || fix->fx_addnumber != 0
1250 || S_GET_SEGMENT (sym) != sec
1251 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
1252 || S_GET_STORAGE_CLASS (sym) == C_EXT
1253 #endif
1254 || S_IS_EXTERNAL (sym))
1255 {
1256 as_warn_where (fix->fx_file, fix->fx_line,
1257 ".uses does not refer to a local symbol in the same section");
1258 continue;
1259 }
1260
1261 /* Look through the fixups again, this time looking for one
1262 at the same location as sym. */
1263 val = S_GET_VALUE (sym);
1264 for (fscan = seginfo->fix_root;
1265 fscan != NULL;
1266 fscan = fscan->fx_next)
1267 if (val == fscan->fx_frag->fr_address + fscan->fx_where
1268 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
1269 && fscan->fx_r_type != BFD_RELOC_SH_CODE
1270 && fscan->fx_r_type != BFD_RELOC_SH_DATA
1271 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
1272 break;
1273 if (fscan == NULL)
1274 {
1275 as_warn_where (fix->fx_file, fix->fx_line,
1276 "can't find fixup pointed to by .uses");
1277 continue;
1278 }
1279
1280 if (fscan->fx_tcbit)
1281 {
1282 /* We've already done this one. */
1283 continue;
1284 }
1285
1286 /* fscan should also be a fixup to a local symbol in the same
1287 section. */
1288 sym = fscan->fx_addsy;
1289 if (sym == NULL
1290 || fscan->fx_subsy != NULL
1291 || fscan->fx_addnumber != 0
1292 || S_GET_SEGMENT (sym) != sec
1293 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
1294 || S_GET_STORAGE_CLASS (sym) == C_EXT
1295 #endif
1296 || S_IS_EXTERNAL (sym))
1297 {
1298 as_warn_where (fix->fx_file, fix->fx_line,
1299 ".uses target does not refer to a local symbol in the same section");
1300 continue;
1301 }
1302
1303 /* Now we look through all the fixups of all the sections,
1304 counting the number of times we find a reference to sym. */
1305 info.sym = sym;
1306 info.count = 0;
1307 #ifdef BFD_ASSEMBLER
1308 bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
1309 #else
1310 {
1311 int iscan;
1312
1313 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
1314 sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
1315 }
1316 #endif
1317
1318 if (info.count < 1)
1319 abort ();
1320
1321 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
1322 We have already adjusted the value of sym to include the
1323 fragment address, so we undo that adjustment here. */
1324 subseg_change (sec, 0);
1325 fix_new (sym->sy_frag, S_GET_VALUE (sym) - sym->sy_frag->fr_address,
1326 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
1327 }
1328 }
1329
1330 /* This function is called after the symbol table has been completed,
1331 but before the relocs or section contents have been written out.
1332 If we have seen any .uses pseudo-ops, they point to an instruction
1333 which loads a register with the address of a function. We look
1334 through the fixups to find where the function address is being
1335 loaded from. We then generate a COUNT reloc giving the number of
1336 times that function address is referred to. The linker uses this
1337 information when doing relaxing, to decide when it can eliminate
1338 the stored function address entirely. */
1339
1340 void
1341 sh_frob_file ()
1342 {
1343 if (! sh_relax)
1344 return;
1345
1346 #ifdef BFD_ASSEMBLER
1347 bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
1348 #else
1349 {
1350 int iseg;
1351
1352 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
1353 sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
1354 }
1355 #endif
1356 }
1357
1358 /* Called after relaxing. Set the correct sizes of the fragments, and
1359 create relocs so that md_apply_fix will fill in the correct values. */
1360
1361 void
1362 md_convert_frag (headers, seg, fragP)
1363 #ifdef BFD_ASSEMBLER
1364 bfd *headers;
1365 #else
1366 object_headers *headers;
1367 #endif
1368 segT seg;
1369 fragS *fragP;
1370 {
1371 int donerelax = 0;
1372
1373 switch (fragP->fr_subtype)
1374 {
1375 case C (COND_JUMP, COND8):
1376 subseg_change (seg, 0);
1377 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1378 1, BFD_RELOC_SH_PCDISP8BY2);
1379 fragP->fr_fix += 2;
1380 fragP->fr_var = 0;
1381 break;
1382
1383 case C (UNCOND_JUMP, UNCOND12):
1384 subseg_change (seg, 0);
1385 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1386 1, BFD_RELOC_SH_PCDISP12BY2);
1387 fragP->fr_fix += 2;
1388 fragP->fr_var = 0;
1389 break;
1390
1391 case C (UNCOND_JUMP, UNCOND32):
1392 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
1393 if (fragP->fr_symbol == NULL)
1394 as_bad ("at 0x%lx, displacement overflows 12-bit field",
1395 (unsigned long) fragP->fr_address);
1396 else
1397 as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 12-bit field",
1398 (unsigned long) fragP->fr_address,
1399 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1400 S_GET_NAME (fragP->fr_symbol));
1401
1402 #if 0 /* This code works, but generates poor code and the compiler
1403 should never produce a sequence that requires it to be used. */
1404
1405 /* A jump wont fit in 12 bits, make code which looks like
1406 bra foo
1407 mov.w @(0, PC), r14
1408 .long disp
1409 foo: bra @r14
1410 */
1411 int t = buffer[0] & 0x10;
1412
1413 buffer[highbyte] = 0xa0; /* branch over move and disp */
1414 buffer[lowbyte] = 3;
1415 buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
1416 buffer[lowbyte+2] = 0x00;
1417
1418 buffer[highbyte+4] = 0; /* space for 32 bit jump disp */
1419 buffer[lowbyte+4] = 0;
1420 buffer[highbyte+6] = 0;
1421 buffer[lowbyte+6] = 0;
1422
1423 buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
1424 buffer[lowbyte+8] = t ? 0xb : 0x2b;
1425
1426 buffer[highbyte+10] = 0x20; /* build nop */
1427 buffer[lowbyte+10] = 0x0b;
1428
1429 /* Make reloc for the long disp */
1430 fix_new (fragP,
1431 fragP->fr_fix + 4,
1432 4,
1433 fragP->fr_symbol,
1434 fragP->fr_offset,
1435 0,
1436 BFD_RELOC_32);
1437 fragP->fr_fix += UNCOND32_LENGTH;
1438 fragP->fr_var = 0;
1439 donerelax = 1;
1440 #endif
1441
1442 break;
1443
1444 case C (COND_JUMP, COND12):
1445 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1446 {
1447 unsigned char *buffer =
1448 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1449 int highbyte = target_big_endian ? 0 : 1;
1450 int lowbyte = target_big_endian ? 1 : 0;
1451
1452 /* Toggle the true/false bit of the bcond. */
1453 buffer[highbyte] ^= 0x2;
1454
1455 /* Build a relocation to six bytes farther on. */
1456 subseg_change (seg, 0);
1457 fix_new (fragP, fragP->fr_fix, 2,
1458 #ifdef BFD_ASSEMBLER
1459 section_symbol (seg),
1460 #else
1461 seg_info (seg)->dot,
1462 #endif
1463 fragP->fr_address + fragP->fr_fix + 6,
1464 1, BFD_RELOC_SH_PCDISP8BY2);
1465
1466 /* Set up a jump instruction. */
1467 buffer[highbyte + 2] = 0xa0;
1468 buffer[lowbyte + 2] = 0;
1469 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
1470 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
1471
1472 /* Fill in a NOP instruction. */
1473 buffer[highbyte + 4] = 0x0;
1474 buffer[lowbyte + 4] = 0x9;
1475
1476 fragP->fr_fix += 6;
1477 fragP->fr_var = 0;
1478 donerelax = 1;
1479 }
1480 break;
1481
1482 case C (COND_JUMP, COND32):
1483 case C (COND_JUMP, UNDEF_WORD_DISP):
1484 if (fragP->fr_symbol == NULL)
1485 as_bad ("at 0x%lx, displacement overflows 8-bit field",
1486 (unsigned long) fragP->fr_address);
1487 else
1488 as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 8-bit field ",
1489 (unsigned long) fragP->fr_address,
1490 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1491 S_GET_NAME (fragP->fr_symbol));
1492
1493 #if 0 /* This code works, but generates poor code, and the compiler
1494 should never produce a sequence that requires it to be used. */
1495
1496 /* A bcond won't fit and it won't go into a 12 bit
1497 displacement either, the code sequence looks like:
1498 b!cond foop
1499 mov.w @(n, PC), r14
1500 jmp @r14
1501 nop
1502 .long where
1503 foop:
1504 */
1505
1506 buffer[0] ^= 0x2; /* Toggle T/F bit */
1507 #define JREG 14
1508 buffer[1] = 5; /* branch over mov, jump, nop and ptr */
1509 buffer[2] = 0xd0 | JREG; /* Build mov insn */
1510 buffer[3] = 0x2;
1511 buffer[4] = 0x40 | JREG; /* Build jmp @JREG */
1512 buffer[5] = 0x0b;
1513 buffer[6] = 0x20; /* build nop */
1514 buffer[7] = 0x0b;
1515 buffer[8] = 0; /* space for 32 bit jump disp */
1516 buffer[9] = 0;
1517 buffer[10] = 0;
1518 buffer[11] = 0;
1519 buffer[12] = 0;
1520 buffer[13] = 0;
1521 /* Make reloc for the long disp */
1522 fix_new (fragP,
1523 fragP->fr_fix + 8,
1524 4,
1525 fragP->fr_symbol,
1526 fragP->fr_offset,
1527 0,
1528 BFD_RELOC_32);
1529 fragP->fr_fix += COND32_LENGTH;
1530 fragP->fr_var = 0;
1531 donerelax = 1;
1532 #endif
1533
1534 break;
1535
1536 default:
1537 abort ();
1538 }
1539
1540 if (donerelax && !sh_relax)
1541 as_warn_where (fragP->fr_file, fragP->fr_line,
1542 "overflow in branch to %s; converted into longer instruction sequence",
1543 (fragP->fr_symbol != NULL
1544 ? S_GET_NAME (fragP->fr_symbol)
1545 : ""));
1546 }
1547
1548 valueT
1549 DEFUN (md_section_align, (seg, size),
1550 segT seg AND
1551 valueT size)
1552 {
1553 #ifdef BFD_ASSEMBLER
1554 #ifdef OBJ_ELF
1555 return size;
1556 #else /* ! OBJ_ELF */
1557 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
1558 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
1559 #endif /* ! OBJ_ELF */
1560 #else /* ! BFD_ASSEMBLER */
1561 return ((size + (1 << section_alignment[(int) seg]) - 1)
1562 & (-1 << section_alignment[(int) seg]));
1563 #endif /* ! BFD_ASSEMBLER */
1564 }
1565
1566 /* This static variable is set by s_uacons to tell sh_cons_align that
1567 the expession does not need to be aligned. */
1568
1569 static int sh_no_align_cons = 0;
1570
1571 /* This handles the unaligned space allocation pseudo-ops, such as
1572 .uaword. .uaword is just like .word, but the value does not need
1573 to be aligned. */
1574
1575 static void
1576 s_uacons (bytes)
1577 int bytes;
1578 {
1579 /* Tell sh_cons_align not to align this value. */
1580 sh_no_align_cons = 1;
1581 cons (bytes);
1582 }
1583
1584 /* If a .word, et. al., pseud-op is seen, warn if the value is not
1585 aligned correctly. Note that this can cause warnings to be issued
1586 when assembling initialized structured which were declared with the
1587 packed attribute. FIXME: Perhaps we should require an option to
1588 enable this warning? */
1589
1590 void
1591 sh_cons_align (nbytes)
1592 int nbytes;
1593 {
1594 int nalign;
1595 char *p;
1596
1597 if (sh_no_align_cons)
1598 {
1599 /* This is an unaligned pseudo-op. */
1600 sh_no_align_cons = 0;
1601 return;
1602 }
1603
1604 nalign = 0;
1605 while ((nbytes & 1) == 0)
1606 {
1607 ++nalign;
1608 nbytes >>= 1;
1609 }
1610
1611 if (nalign == 0)
1612 return;
1613
1614 if (now_seg == absolute_section)
1615 {
1616 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
1617 as_warn ("misaligned data");
1618 return;
1619 }
1620
1621 p = frag_var (rs_align_code, 1, 1, (relax_substateT) 0,
1622 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
1623
1624 record_alignment (now_seg, nalign);
1625 }
1626
1627 /* When relaxing, we need to output a reloc for any .align directive
1628 that requests alignment to a four byte boundary or larger. This is
1629 also where we check for misaligned data. */
1630
1631 void
1632 sh_handle_align (frag)
1633 fragS *frag;
1634 {
1635 if (sh_relax
1636 && frag->fr_type == rs_align
1637 && frag->fr_address + frag->fr_fix > 0
1638 && frag->fr_offset > 1
1639 && now_seg != bss_section)
1640 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
1641 BFD_RELOC_SH_ALIGN);
1642
1643 if (frag->fr_type == rs_align_code
1644 && frag->fr_next->fr_address - frag->fr_address - frag->fr_fix != 0)
1645 as_warn_where (frag->fr_file, frag->fr_line, "misaligned data");
1646 }
1647
1648 /* This macro decides whether a particular reloc is an entry in a
1649 switch table. It is used when relaxing, because the linker needs
1650 to know about all such entries so that it can adjust them if
1651 necessary. */
1652
1653 #ifdef BFD_ASSEMBLER
1654 #define SWITCH_TABLE_CONS(fix) (0)
1655 #else
1656 #define SWITCH_TABLE_CONS(fix) \
1657 ((fix)->fx_r_type == 0 \
1658 && ((fix)->fx_size == 2 \
1659 || (fix)->fx_size == 4))
1660 #endif
1661
1662 #define SWITCH_TABLE(fix) \
1663 ((fix)->fx_addsy != NULL \
1664 && (fix)->fx_subsy != NULL \
1665 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
1666 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
1667 && ((fix)->fx_r_type == BFD_RELOC_32 \
1668 || (fix)->fx_r_type == BFD_RELOC_16 \
1669 || SWITCH_TABLE_CONS (fix)))
1670
1671 /* See whether we need to force a relocation into the output file.
1672 This is used to force out switch and PC relative relocations when
1673 relaxing. */
1674
1675 int
1676 sh_force_relocation (fix)
1677 fixS *fix;
1678 {
1679 if (! sh_relax)
1680 return 0;
1681
1682 return (fix->fx_pcrel
1683 || SWITCH_TABLE (fix)
1684 || fix->fx_r_type == BFD_RELOC_SH_COUNT
1685 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
1686 || fix->fx_r_type == BFD_RELOC_SH_CODE
1687 || fix->fx_r_type == BFD_RELOC_SH_DATA
1688 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
1689 }
1690
1691 /* Apply a fixup to the object file. */
1692
1693 #ifdef BFD_ASSEMBLER
1694 int
1695 md_apply_fix (fixP, valp)
1696 fixS *fixP;
1697 valueT *valp;
1698 #else
1699 void
1700 md_apply_fix (fixP, val)
1701 fixS *fixP;
1702 long val;
1703 #endif
1704 {
1705 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1706 int lowbyte = target_big_endian ? 1 : 0;
1707 int highbyte = target_big_endian ? 0 : 1;
1708 #ifdef BFD_ASSEMBLER
1709 long val = *valp;
1710 #endif
1711
1712 #ifndef BFD_ASSEMBLER
1713 if (fixP->fx_r_type == 0)
1714 {
1715 if (fixP->fx_size == 2)
1716 fixP->fx_r_type = BFD_RELOC_16;
1717 else if (fixP->fx_size == 4)
1718 fixP->fx_r_type = BFD_RELOC_32;
1719 else if (fixP->fx_size == 1)
1720 fixP->fx_r_type = BFD_RELOC_SH_IMM8;
1721 else
1722 abort ();
1723 }
1724 #endif
1725
1726 switch (fixP->fx_r_type)
1727 {
1728 case BFD_RELOC_SH_IMM4:
1729 *buf = (*buf & 0xf0) | (val & 0xf);
1730 break;
1731
1732 case BFD_RELOC_SH_IMM4BY2:
1733 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1734 break;
1735
1736 case BFD_RELOC_SH_IMM4BY4:
1737 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1738 break;
1739
1740 case BFD_RELOC_SH_IMM8BY2:
1741 *buf = val >> 1;
1742 break;
1743
1744 case BFD_RELOC_SH_IMM8BY4:
1745 *buf = val >> 2;
1746 break;
1747
1748 case BFD_RELOC_8:
1749 case BFD_RELOC_SH_IMM8:
1750 *buf++ = val;
1751 break;
1752
1753 case BFD_RELOC_SH_PCRELIMM8BY4:
1754 /* The lower two bits of the PC are cleared before the
1755 displacement is added in. We can assume that the destination
1756 is on a 4 byte bounday. If this instruction is also on a 4
1757 byte boundary, then we want
1758 (target - here) / 4
1759 and target - here is a multiple of 4.
1760 Otherwise, we are on a 2 byte boundary, and we want
1761 (target - (here - 2)) / 4
1762 and target - here is not a multiple of 4. Computing
1763 (target - (here - 2)) / 4 == (target - here + 2) / 4
1764 works for both cases, since in the first case the addition of
1765 2 will be removed by the division. target - here is in the
1766 variable val. */
1767 val = (val + 2) / 4;
1768 if (val & ~0xff)
1769 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1770 buf[lowbyte] = val;
1771 break;
1772
1773 case BFD_RELOC_SH_PCRELIMM8BY2:
1774 val /= 2;
1775 if (val & ~0xff)
1776 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1777 buf[lowbyte] = val;
1778 break;
1779
1780 case BFD_RELOC_SH_PCDISP8BY2:
1781 val /= 2;
1782 if (val < -0x80 || val > 0x7f)
1783 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1784 buf[lowbyte] = val;
1785 break;
1786
1787 case BFD_RELOC_SH_PCDISP12BY2:
1788 val /= 2;
1789 if (val < -0x800 || val >= 0x7ff)
1790 as_bad_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1791 buf[lowbyte] = val & 0xff;
1792 buf[highbyte] |= (val >> 8) & 0xf;
1793 break;
1794
1795 case BFD_RELOC_32:
1796 if (! target_big_endian)
1797 {
1798 *buf++ = val >> 0;
1799 *buf++ = val >> 8;
1800 *buf++ = val >> 16;
1801 *buf++ = val >> 24;
1802 }
1803 else
1804 {
1805 *buf++ = val >> 24;
1806 *buf++ = val >> 16;
1807 *buf++ = val >> 8;
1808 *buf++ = val >> 0;
1809 }
1810 break;
1811
1812 case BFD_RELOC_16:
1813 if (! target_big_endian)
1814 {
1815 *buf++ = val >> 0;
1816 *buf++ = val >> 8;
1817 }
1818 else
1819 {
1820 *buf++ = val >> 8;
1821 *buf++ = val >> 0;
1822 }
1823 break;
1824
1825 case BFD_RELOC_SH_USES:
1826 /* Pass the value into sh_coff_reloc_mangle. */
1827 fixP->fx_addnumber = val;
1828 break;
1829
1830 case BFD_RELOC_SH_COUNT:
1831 case BFD_RELOC_SH_ALIGN:
1832 case BFD_RELOC_SH_CODE:
1833 case BFD_RELOC_SH_DATA:
1834 case BFD_RELOC_SH_LABEL:
1835 /* Nothing to do here. */
1836 break;
1837
1838 default:
1839 abort ();
1840 }
1841
1842 #ifdef BFD_ASSEMBLER
1843 return 0;
1844 #endif
1845 }
1846
1847 int md_long_jump_size;
1848
1849 /* Called just before address relaxation. Return the length
1850 by which a fragment must grow to reach it's destination. */
1851
1852 int
1853 md_estimate_size_before_relax (fragP, segment_type)
1854 register fragS *fragP;
1855 register segT segment_type;
1856 {
1857 switch (fragP->fr_subtype)
1858 {
1859 case C (UNCOND_JUMP, UNDEF_DISP):
1860 /* used to be a branch to somewhere which was unknown */
1861 if (!fragP->fr_symbol)
1862 {
1863 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1864 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1865 }
1866 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1867 {
1868 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1869 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1870 }
1871 else
1872 {
1873 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
1874 fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1875 return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1876 }
1877 break;
1878
1879 default:
1880 abort ();
1881 case C (COND_JUMP, UNDEF_DISP):
1882 /* used to be a branch to somewhere which was unknown */
1883 if (fragP->fr_symbol
1884 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1885 {
1886 /* Got a symbol and it's defined in this segment, become byte
1887 sized - maybe it will fix up */
1888 fragP->fr_subtype = C (COND_JUMP, COND8);
1889 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1890 }
1891 else if (fragP->fr_symbol)
1892 {
1893 /* Its got a segment, but its not ours, so it will always be long */
1894 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
1895 fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1896 return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1897 }
1898 else
1899 {
1900 /* We know the abs value */
1901 fragP->fr_subtype = C (COND_JUMP, COND8);
1902 fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1903 }
1904
1905 break;
1906 }
1907 return fragP->fr_var;
1908 }
1909
1910 /* Put number into target byte order */
1911
1912 void
1913 md_number_to_chars (ptr, use, nbytes)
1914 char *ptr;
1915 valueT use;
1916 int nbytes;
1917 {
1918 if (! target_big_endian)
1919 number_to_chars_littleendian (ptr, use, nbytes);
1920 else
1921 number_to_chars_bigendian (ptr, use, nbytes);
1922 }
1923
1924 long
1925 md_pcrel_from (fixP)
1926 fixS *fixP;
1927 {
1928 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
1929 }
1930
1931 #ifdef OBJ_COFF
1932
1933 int
1934 tc_coff_sizemachdep (frag)
1935 fragS *frag;
1936 {
1937 return md_relax_table[frag->fr_subtype].rlx_length;
1938 }
1939
1940 #endif /* OBJ_COFF */
1941
1942 /* When we align the .text section, insert the correct NOP pattern. */
1943
1944 int
1945 sh_do_align (n, fill, len, max)
1946 int n;
1947 const char *fill;
1948 int len;
1949 int max;
1950 {
1951 if (fill == NULL
1952 #ifdef BFD_ASSEMBLER
1953 && (now_seg->flags & SEC_CODE) != 0
1954 #else
1955 && now_seg != data_section
1956 && now_seg != bss_section
1957 #endif
1958 && n > 1)
1959 {
1960 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
1961 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
1962
1963 /* First align to a 2 byte boundary, in case there is an odd
1964 .byte. */
1965 frag_align (1, 0, 0);
1966 if (target_big_endian)
1967 frag_align_pattern (n, big_nop_pattern, sizeof big_nop_pattern, max);
1968 else
1969 frag_align_pattern (n, little_nop_pattern, sizeof little_nop_pattern,
1970 max);
1971 return 1;
1972 }
1973
1974 return 0;
1975 }
1976
1977 #ifndef BFD_ASSEMBLER
1978 #ifdef OBJ_COFF
1979
1980 /* Map BFD relocs to SH COFF relocs. */
1981
1982 struct reloc_map
1983 {
1984 bfd_reloc_code_real_type bfd_reloc;
1985 int sh_reloc;
1986 };
1987
1988 static const struct reloc_map coff_reloc_map[] =
1989 {
1990 { BFD_RELOC_32, R_SH_IMM32 },
1991 { BFD_RELOC_16, R_SH_IMM16 },
1992 { BFD_RELOC_8, R_SH_IMM8 },
1993 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
1994 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
1995 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
1996 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
1997 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
1998 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
1999 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
2000 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
2001 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
2002 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
2003 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
2004 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
2005 { BFD_RELOC_SH_USES, R_SH_USES },
2006 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
2007 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
2008 { BFD_RELOC_SH_CODE, R_SH_CODE },
2009 { BFD_RELOC_SH_DATA, R_SH_DATA },
2010 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
2011 { BFD_RELOC_UNUSED, 0 }
2012 };
2013
2014 /* Adjust a reloc for the SH. This is similar to the generic code,
2015 but does some minor tweaking. */
2016
2017 void
2018 sh_coff_reloc_mangle (seg, fix, intr, paddr)
2019 segment_info_type *seg;
2020 fixS *fix;
2021 struct internal_reloc *intr;
2022 unsigned int paddr;
2023 {
2024 symbolS *symbol_ptr = fix->fx_addsy;
2025 symbolS *dot;
2026
2027 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
2028
2029 if (! SWITCH_TABLE (fix))
2030 {
2031 const struct reloc_map *rm;
2032
2033 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
2034 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
2035 break;
2036 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
2037 as_bad_where (fix->fx_file, fix->fx_line,
2038 "Can not represent %s relocation in this object file format",
2039 bfd_get_reloc_code_name (fix->fx_r_type));
2040 intr->r_type = rm->sh_reloc;
2041 intr->r_offset = 0;
2042 }
2043 else
2044 {
2045 know (sh_relax);
2046
2047 if (fix->fx_r_type == BFD_RELOC_16)
2048 intr->r_type = R_SH_SWITCH16;
2049 else if (fix->fx_r_type == BFD_RELOC_32)
2050 intr->r_type = R_SH_SWITCH32;
2051 else
2052 abort ();
2053
2054 /* For a switch reloc, we set r_offset to the difference between
2055 the reloc address and the subtrahend. When the linker is
2056 doing relaxing, it can use the determine the starting and
2057 ending points of the switch difference expression. */
2058 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
2059 }
2060
2061 /* PC relative relocs are always against the current section. */
2062 if (symbol_ptr == NULL)
2063 {
2064 switch (fix->fx_r_type)
2065 {
2066 case BFD_RELOC_SH_PCRELIMM8BY2:
2067 case BFD_RELOC_SH_PCRELIMM8BY4:
2068 case BFD_RELOC_SH_PCDISP8BY2:
2069 case BFD_RELOC_SH_PCDISP12BY2:
2070 case BFD_RELOC_SH_USES:
2071 symbol_ptr = seg->dot;
2072 break;
2073 default:
2074 break;
2075 }
2076 }
2077
2078 if (fix->fx_r_type == BFD_RELOC_SH_USES)
2079 {
2080 /* We can't store the offset in the object file, since this
2081 reloc does not take up any space, so we store it in r_offset.
2082 The fx_addnumber field was set in md_apply_fix. */
2083 intr->r_offset = fix->fx_addnumber;
2084 }
2085 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
2086 {
2087 /* We can't store the count in the object file, since this reloc
2088 does not take up any space, so we store it in r_offset. The
2089 fx_offset field was set when the fixup was created in
2090 sh_coff_frob_file. */
2091 intr->r_offset = fix->fx_offset;
2092 /* This reloc is always absolute. */
2093 symbol_ptr = NULL;
2094 }
2095 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
2096 {
2097 /* Store the alignment in the r_offset field. */
2098 intr->r_offset = fix->fx_offset;
2099 /* This reloc is always absolute. */
2100 symbol_ptr = NULL;
2101 }
2102 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
2103 || fix->fx_r_type == BFD_RELOC_SH_DATA
2104 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
2105 {
2106 /* These relocs are always absolute. */
2107 symbol_ptr = NULL;
2108 }
2109
2110 /* Turn the segment of the symbol into an offset. */
2111 if (symbol_ptr != NULL)
2112 {
2113 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
2114 if (dot != NULL)
2115 intr->r_symndx = dot->sy_number;
2116 else
2117 intr->r_symndx = symbol_ptr->sy_number;
2118 }
2119 else
2120 intr->r_symndx = -1;
2121 }
2122
2123 #endif /* OBJ_COFF */
2124 #endif /* ! BFD_ASSEMBLER */
2125
2126 #ifdef BFD_ASSEMBLER
2127
2128 /* Create a reloc. */
2129
2130 arelent *
2131 tc_gen_reloc (section, fixp)
2132 asection *section;
2133 fixS *fixp;
2134 {
2135 arelent *rel;
2136 bfd_reloc_code_real_type r_type;
2137
2138 rel = (arelent *) xmalloc (sizeof (arelent));
2139 rel->sym_ptr_ptr = &fixp->fx_addsy->bsym;
2140 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2141
2142 r_type = fixp->fx_r_type;
2143
2144 if (SWITCH_TABLE (fixp))
2145 {
2146 rel->addend = rel->address - S_GET_VALUE (fixp->fx_subsy);
2147 if (r_type == BFD_RELOC_16)
2148 r_type = BFD_RELOC_SH_SWITCH16;
2149 else if (r_type == BFD_RELOC_32)
2150 r_type = BFD_RELOC_SH_SWITCH32;
2151 else
2152 abort ();
2153 }
2154 else if (r_type == BFD_RELOC_SH_USES)
2155 rel->addend = fixp->fx_addnumber;
2156 else if (r_type == BFD_RELOC_SH_COUNT)
2157 rel->addend = fixp->fx_offset;
2158 else if (r_type == BFD_RELOC_SH_ALIGN)
2159 rel->addend = fixp->fx_offset;
2160 else if (fixp->fx_pcrel)
2161 rel->addend = fixp->fx_addnumber;
2162 else
2163 rel->addend = 0;
2164
2165 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
2166 if (rel->howto == NULL)
2167 {
2168 as_bad_where (fixp->fx_file, fixp->fx_line,
2169 "Cannot represent relocation type %s",
2170 bfd_get_reloc_code_name (r_type));
2171 /* Set howto to a garbage value so that we can keep going. */
2172 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2173 assert (rel->howto != NULL);
2174 }
2175
2176 return rel;
2177 }
2178
2179 #endif /* BFD_ASSEMBLER */
This page took 0.07574 seconds and 4 git commands to generate.