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