1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Written By Steve Chamberlain <sac@cygnus.com> */
29 #include "opcodes/sh-opc.h"
30 #include "safe-ctype.h"
31 #include "struc-symbol.h"
37 #include "dwarf2dbg.h"
43 expressionS immediate;
47 const char comment_chars[] = "!";
48 const char line_separator_chars[] = ";";
49 const char line_comment_chars[] = "!#";
51 static void s_uses PARAMS ((int));
53 static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
54 static void sh_frob_section PARAMS ((bfd *, segT, PTR));
56 static void s_uacons PARAMS ((int));
57 static sh_opcode_info *find_cooked_opcode PARAMS ((char **));
58 static unsigned int assemble_ppi PARAMS ((char *, sh_opcode_info *));
59 static void little PARAMS ((int));
60 static bfd_reloc_code_real_type sh_elf_suffix
61 PARAMS ((char **str_p, expressionS *, expressionS *new_exp_p));
62 static int parse_reg PARAMS ((char *, int *, int *));
63 static symbolS *dot PARAMS ((void));
64 static char *parse_exp PARAMS ((char *, sh_operand_info *));
65 static char *parse_at PARAMS ((char *, sh_operand_info *));
66 static void get_operand PARAMS ((char **, sh_operand_info *));
67 static char *get_operands
68 PARAMS ((sh_opcode_info *, char *, sh_operand_info *));
69 static sh_opcode_info *get_specific
70 PARAMS ((sh_opcode_info *, sh_operand_info *));
71 static void insert PARAMS ((char *, int, int, sh_operand_info *));
72 static void build_relax PARAMS ((sh_opcode_info *, sh_operand_info *));
73 static char *insert_loop_bounds PARAMS ((char *, sh_operand_info *));
74 static unsigned int build_Mytes
75 PARAMS ((sh_opcode_info *, sh_operand_info *));
78 static void sh_elf_cons PARAMS ((int));
80 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
87 int ignore ATTRIBUTE_UNUSED;
90 target_big_endian = 0;
93 /* This table describes all the machine specific pseudo-ops the assembler
94 has to support. The fields are:
95 pseudo-op name without dot
96 function to call to execute this pseudo-op
97 Integer arg to pass to the function. */
99 const pseudo_typeS md_pseudo_table[] =
102 {"long", sh_elf_cons, 4},
103 {"int", sh_elf_cons, 4},
104 {"word", sh_elf_cons, 2},
105 {"short", sh_elf_cons, 2},
110 {"form", listing_psize, 0},
111 {"little", little, 0},
112 {"heading", listing_title, 0},
113 {"import", s_ignore, 0},
114 {"page", listing_eject, 0},
115 {"program", s_ignore, 0},
117 {"uaword", s_uacons, 2},
118 {"ualong", s_uacons, 4},
119 {"uaquad", s_uacons, 8},
120 {"2byte", s_uacons, 2},
121 {"4byte", s_uacons, 4},
122 {"8byte", s_uacons, 8},
124 {"file", dwarf2_directive_file, 0 },
125 {"loc", dwarf2_directive_loc, 0 },
130 /*int md_reloc_size; */
132 int sh_relax; /* set if -relax seen */
134 /* Whether -small was seen. */
138 /* Whether -dsp was seen. */
142 /* The bit mask of architectures that could
143 accomodate the insns seen so far. */
144 static int valid_arch;
146 const char EXP_CHARS[] = "eE";
148 /* Chars that mean this number is a floating point constant. */
151 const char FLT_CHARS[] = "rRsSfFdDxXpP";
153 #define C(a,b) ENCODE_RELAX(a,b)
155 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
156 #define GET_WHAT(x) ((x>>4))
158 /* These are the three types of relaxable instrction. */
160 #define COND_JUMP_DELAY 2
161 #define UNCOND_JUMP 3
168 #define UNDEF_WORD_DISP 4
173 /* Branch displacements are from the address of the branch plus
174 four, thus all minimum and maximum values have 4 added to them. */
177 #define COND8_LENGTH 2
179 /* There is one extra instruction before the branch, so we must add
180 two more bytes to account for it. */
181 #define COND12_F 4100
182 #define COND12_M -4090
183 #define COND12_LENGTH 6
185 #define COND12_DELAY_LENGTH 4
187 /* ??? The minimum and maximum values are wrong, but this does not matter
188 since this relocation type is not supported yet. */
189 #define COND32_F (1<<30)
190 #define COND32_M -(1<<30)
191 #define COND32_LENGTH 14
193 #define UNCOND12_F 4098
194 #define UNCOND12_M -4092
195 #define UNCOND12_LENGTH 2
197 /* ??? The minimum and maximum values are wrong, but this does not matter
198 since this relocation type is not supported yet. */
199 #define UNCOND32_F (1<<30)
200 #define UNCOND32_M -(1<<30)
201 #define UNCOND32_LENGTH 14
203 #define EMPTY { 0, 0, 0, 0 }
205 const relax_typeS md_relax_table[C (END, 0)] = {
206 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
207 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
210 /* C (COND_JUMP, COND8) */
211 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
212 /* C (COND_JUMP, COND12) */
213 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
214 /* C (COND_JUMP, COND32) */
215 { COND32_F, COND32_M, COND32_LENGTH, 0, },
216 /* C (COND_JUMP, UNDEF_WORD_DISP) */
217 { 0, 0, COND32_LENGTH, 0, },
219 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
222 /* C (COND_JUMP_DELAY, COND8) */
223 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
224 /* C (COND_JUMP_DELAY, COND12) */
225 { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
226 /* C (COND_JUMP_DELAY, COND32) */
227 { COND32_F, COND32_M, COND32_LENGTH, 0, },
228 /* C (COND_JUMP_DELAY, UNDEF_WORD_DISP) */
229 { 0, 0, COND32_LENGTH, 0, },
231 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
234 /* C (UNCOND_JUMP, UNCOND12) */
235 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
236 /* C (UNCOND_JUMP, UNCOND32) */
237 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
239 /* C (UNCOND_JUMP, UNDEF_WORD_DISP) */
240 { 0, 0, UNCOND32_LENGTH, 0, },
242 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
247 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
251 /* Parse @got, etc. and return the desired relocation.
252 If we have additional arithmetic expression, then we fill in new_exp_p. */
253 static bfd_reloc_code_real_type
254 sh_elf_suffix (str_p, exp_p, new_exp_p)
256 expressionS *exp_p, *new_exp_p;
261 bfd_reloc_code_real_type reloc;
271 #define MAP(str,reloc) { str, sizeof (str)-1, reloc }
273 static struct map_bfd mapping[] = {
274 MAP ("got", BFD_RELOC_32_GOT_PCREL),
275 MAP ("plt", BFD_RELOC_32_PLT_PCREL),
276 MAP ("gotoff", BFD_RELOC_32_GOTOFF),
277 { (char *)0, 0, BFD_RELOC_UNUSED }
281 return BFD_RELOC_UNUSED;
283 for (ch = *str, str2 = ident;
284 (str2 < ident + sizeof (ident) - 1
285 && (ISALNUM (ch) || ch == '@'));
287 *str2++ = TOLOWER (ch);
293 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
294 if (ch == ptr->string[0]
295 && len == ptr->length
296 && memcmp (ident, ptr->string, ptr->length) == 0)
298 /* Now check for identifier@suffix+constant */
299 if (*str == '-' || *str == '+')
301 char *orig_line = input_line_pointer;
303 input_line_pointer = str;
304 expression (new_exp_p);
305 if (new_exp_p->X_op == O_constant)
307 exp_p->X_add_number += new_exp_p->X_add_number;
308 str = input_line_pointer;
310 if (new_exp_p->X_op == O_subtract)
311 str = input_line_pointer;
313 if (&input_line_pointer != str_p)
314 input_line_pointer = orig_line;
321 return BFD_RELOC_UNUSED;
324 /* The regular cons() function, that reads constants, doesn't support
325 suffixes such as @GOT, @GOTOFF and @PLT, that generate
326 machine-specific relocation types. So we must define it here. */
327 /* Clobbers input_line_pointer, checks end-of-line. */
330 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
332 expressionS exp, new_exp;
333 bfd_reloc_code_real_type reloc;
336 if (is_it_end_of_statement ())
338 demand_empty_rest_of_line ();
345 new_exp.X_op = O_absent;
346 new_exp.X_add_symbol = new_exp.X_op_symbol = NULL;
347 /* If the _GLOBAL_OFFSET_TABLE_ symbol hasn't been found yet,
348 use the name of the symbol to tell whether it's the
349 _GLOBAL_OFFSET_TABLE_. If it has, comparing the symbols is
351 if (! GOT_symbol && exp.X_add_symbol)
352 name = S_GET_NAME (exp.X_add_symbol);
355 /* Check whether this expression involves the
356 _GLOBAL_OFFSET_TABLE_ symbol, by itself or added to a
357 difference of two other symbols. */
358 if (((GOT_symbol && GOT_symbol == exp.X_add_symbol)
359 || (! GOT_symbol && name
360 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0))
361 && (exp.X_op == O_symbol
362 || (exp.X_op == O_add
363 && ((symbol_get_value_expression (exp.X_op_symbol)->X_op)
366 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput,
368 int size = bfd_get_reloc_size (reloc_howto);
370 if (GOT_symbol == NULL)
371 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
374 as_bad (_("%s relocations do not fit in %d bytes\n"),
375 reloc_howto->name, nbytes);
378 register char *p = frag_more ((int) nbytes);
379 int offset = nbytes - size;
381 fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
382 size, &exp, 0, TC_RELOC_GLOBAL_OFFSET_TABLE);
385 /* Check if this symbol involves one of the magic suffixes, such
386 as @GOT, @GOTOFF or @PLT, and determine which relocation type
388 else if ((exp.X_op == O_symbol || (exp.X_op == O_add && exp.X_op_symbol))
389 && *input_line_pointer == '@'
390 && ((reloc = sh_elf_suffix (&input_line_pointer, &exp, &new_exp))
391 != BFD_RELOC_UNUSED))
393 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput,
395 int size = bfd_get_reloc_size (reloc_howto);
397 /* Force a GOT to be generated. */
398 if (GOT_symbol == NULL)
399 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
402 as_bad (_("%s relocations do not fit in %d bytes\n"),
403 reloc_howto->name, nbytes);
406 register char *p = frag_more ((int) nbytes);
407 int offset = nbytes - size;
409 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size,
411 if (new_exp.X_op != O_absent)
412 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size,
413 &new_exp, 0, BFD_RELOC_32);
417 emit_expr (&exp, (unsigned int) nbytes);
419 while (*input_line_pointer++ == ',');
421 input_line_pointer--; /* Put terminator back into stream. */
422 if (*input_line_pointer == '#' || *input_line_pointer == '!')
424 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
427 demand_empty_rest_of_line ();
432 /* This function is called once, at assembler startup time. This should
433 set up all the tables, etc that the MD part of the assembler needs. */
438 sh_opcode_info *opcode;
439 char *prev_name = "";
443 /* The WinCE OS only supports little endian executables. */
444 target_big_endian = 0;
447 target_big_endian = 1;
450 target_arch = arch_sh1_up & ~(sh_dsp ? arch_sh3e_up : arch_sh_dsp_up);
451 valid_arch = target_arch;
453 opcode_hash_control = hash_new ();
455 /* Insert unique names into hash table. */
456 for (opcode = sh_table; opcode->name; opcode++)
458 if (strcmp (prev_name, opcode->name))
460 if (! (opcode->arch & target_arch))
462 prev_name = opcode->name;
463 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
467 /* Make all the opcodes with the same name point to the same
469 opcode->name = prev_name;
476 static int reg_x, reg_y;
480 #define IDENT_CHAR(c) (ISALNUM (c) || (c) == '_')
482 /* Try to parse a reg name. Return the number of chars consumed. */
485 parse_reg (src, mode, reg)
490 char l0 = TOLOWER (src[0]);
491 char l1 = l0 ? TOLOWER (src[1]) : 0;
493 /* We use ! IDENT_CHAR for the next character after the register name, to
494 make sure that we won't accidentally recognize a symbol name such as
495 'sram' or sr_ram as being a reference to the register 'sr'. */
501 if (src[2] >= '0' && src[2] <= '5'
502 && ! IDENT_CHAR ((unsigned char) src[3]))
505 *reg = 10 + src[2] - '0';
509 if (l1 >= '0' && l1 <= '9'
510 && ! IDENT_CHAR ((unsigned char) src[2]))
516 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
517 && ! IDENT_CHAR ((unsigned char) src[7]))
524 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
529 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
540 if (! IDENT_CHAR ((unsigned char) src[2]))
546 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
555 if (! IDENT_CHAR ((unsigned char) src[2]))
561 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
569 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
570 && ! IDENT_CHAR ((unsigned char) src[3]))
573 *reg = 4 + (l1 - '0');
576 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
577 && ! IDENT_CHAR ((unsigned char) src[3]))
580 *reg = 6 + (l1 - '0');
583 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
584 && ! IDENT_CHAR ((unsigned char) src[3]))
589 *reg = n | ((~n & 2) << 1);
594 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[3]))
616 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
617 && ! IDENT_CHAR ((unsigned char) src[2]))
620 *reg = A_X0_NUM + l1 - '0';
624 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
625 && ! IDENT_CHAR ((unsigned char) src[2]))
628 *reg = A_Y0_NUM + l1 - '0';
632 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
633 && ! IDENT_CHAR ((unsigned char) src[2]))
636 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
642 && TOLOWER (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
648 if (l0 == 's' && l1 == 'p' && TOLOWER (src[2]) == 'c'
649 && ! IDENT_CHAR ((unsigned char) src[3]))
655 if (l0 == 's' && l1 == 'g' && TOLOWER (src[2]) == 'r'
656 && ! IDENT_CHAR ((unsigned char) src[3]))
662 if (l0 == 'd' && l1 == 's' && TOLOWER (src[2]) == 'r'
663 && ! IDENT_CHAR ((unsigned char) src[3]))
669 if (l0 == 'd' && l1 == 'b' && TOLOWER (src[2]) == 'r'
670 && ! IDENT_CHAR ((unsigned char) src[3]))
676 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
682 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
689 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
694 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
696 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
697 and use an uninitialized immediate. */
701 if (l0 == 'g' && l1 == 'b' && TOLOWER (src[2]) == 'r'
702 && ! IDENT_CHAR ((unsigned char) src[3]))
707 if (l0 == 'v' && l1 == 'b' && TOLOWER (src[2]) == 'r'
708 && ! IDENT_CHAR ((unsigned char) src[3]))
714 if (l0 == 'm' && l1 == 'a' && TOLOWER (src[2]) == 'c'
715 && ! IDENT_CHAR ((unsigned char) src[4]))
717 if (TOLOWER (src[3]) == 'l')
722 if (TOLOWER (src[3]) == 'h')
728 if (l0 == 'm' && l1 == 'o' && TOLOWER (src[2]) == 'd'
729 && ! IDENT_CHAR ((unsigned char) src[4]))
734 if (l0 == 'f' && l1 == 'r')
738 if (src[3] >= '0' && src[3] <= '5'
739 && ! IDENT_CHAR ((unsigned char) src[4]))
742 *reg = 10 + src[3] - '0';
746 if (src[2] >= '0' && src[2] <= '9'
747 && ! IDENT_CHAR ((unsigned char) src[3]))
750 *reg = (src[2] - '0');
754 if (l0 == 'd' && l1 == 'r')
758 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
759 && ! IDENT_CHAR ((unsigned char) src[4]))
762 *reg = 10 + src[3] - '0';
766 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
767 && ! IDENT_CHAR ((unsigned char) src[3]))
770 *reg = (src[2] - '0');
774 if (l0 == 'x' && l1 == 'd')
778 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
779 && ! IDENT_CHAR ((unsigned char) src[4]))
782 *reg = 11 + src[3] - '0';
786 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
787 && ! IDENT_CHAR ((unsigned char) src[3]))
790 *reg = (src[2] - '0') + 1;
794 if (l0 == 'f' && l1 == 'v')
796 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
802 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
803 && ! IDENT_CHAR ((unsigned char) src[3]))
806 *reg = (src[2] - '0');
810 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 'u'
811 && TOLOWER (src[3]) == 'l'
812 && ! IDENT_CHAR ((unsigned char) src[4]))
818 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 's'
819 && TOLOWER (src[3]) == 'c'
820 && TOLOWER (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
826 if (l0 == 'x' && l1 == 'm' && TOLOWER (src[2]) == 't'
827 && TOLOWER (src[3]) == 'r'
828 && TOLOWER (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
842 /* JF: '.' is pseudo symbol with value of current location
843 in current segment. */
844 fake = FAKE_LABEL_NAME;
845 return symbol_new (fake,
847 (valueT) frag_now_fix (),
859 save = input_line_pointer;
860 input_line_pointer = s;
861 expression (&op->immediate);
862 if (op->immediate.X_op == O_absent)
863 as_bad (_("missing operand"));
864 new = input_line_pointer;
865 input_line_pointer = save;
869 /* The many forms of operand:
872 @Rn Register indirect
885 pr, gbr, vbr, macl, mach
898 /* Must be predecrement. */
901 len = parse_reg (src, &mode, &(op->reg));
903 as_bad (_("illegal register after @-"));
908 else if (src[0] == '(')
910 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
913 len = parse_reg (src, &mode, &(op->reg));
914 if (len && mode == A_REG_N)
919 as_bad (_("must be @(r0,...)"));
923 /* Now can be rn or gbr */
924 len = parse_reg (src, &mode, &(op->reg));
929 else if (mode == A_REG_N)
931 op->type = A_IND_R0_REG_N;
935 as_bad (_("syntax error in @(r0,...)"));
940 /* Must be an @(disp,.. thing) */
941 src = parse_exp (src, op);
944 /* Now can be rn, gbr or pc */
945 len = parse_reg (src, &mode, &op->reg);
950 op->type = A_DISP_REG_N;
952 else if (mode == A_GBR)
954 op->type = A_DISP_GBR;
956 else if (mode == A_PC)
958 /* Turn a plain @(4,pc) into @(.+4,pc). */
959 if (op->immediate.X_op == O_constant)
961 op->immediate.X_add_symbol = dot();
962 op->immediate.X_op = O_symbol;
964 op->type = A_DISP_PC;
968 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
973 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
978 as_bad (_("expecting )"));
984 src += parse_reg (src, &mode, &(op->reg));
987 as_bad (_("illegal register after @"));
992 if ((src[0] == 'r' && src[1] == '8')
993 || (src[0] == 'i' && (src[1] == 'x' || src[1] == 's')))
998 if ((src[0] == 'r' && src[1] == '9')
999 || (src[0] == 'i' && src[1] == 'y'))
1002 op->type = A_PMODY_N;
1016 get_operand (ptr, op)
1018 sh_operand_info *op;
1027 *ptr = parse_exp (src, op);
1032 else if (src[0] == '@')
1034 *ptr = parse_at (src, op);
1037 len = parse_reg (src, &mode, &(op->reg));
1046 /* Not a reg, the only thing left is a displacement. */
1047 *ptr = parse_exp (src, op);
1048 op->type = A_DISP_PC;
1054 get_operands (info, args, operand)
1055 sh_opcode_info *info;
1057 sh_operand_info *operand;
1062 /* The pre-processor will eliminate whitespace in front of '@'
1063 after the first argument; we may be called multiple times
1064 from assemble_ppi, so don't insist on finding whitespace here. */
1068 get_operand (&ptr, operand + 0);
1075 get_operand (&ptr, operand + 1);
1076 /* ??? Hack: psha/pshl have a varying operand number depending on
1077 the type of the first operand. We handle this by having the
1078 three-operand version first and reducing the number of operands
1079 parsed to two if we see that the first operand is an immediate.
1080 This works because no insn with three operands has an immediate
1081 as first operand. */
1082 if (info->arg[2] && operand[0].type != A_IMM)
1088 get_operand (&ptr, operand + 2);
1092 operand[2].type = 0;
1097 operand[1].type = 0;
1098 operand[2].type = 0;
1103 operand[0].type = 0;
1104 operand[1].type = 0;
1105 operand[2].type = 0;
1110 /* Passed a pointer to a list of opcodes which use different
1111 addressing modes, return the opcode which matches the opcodes
1114 static sh_opcode_info *
1115 get_specific (opcode, operands)
1116 sh_opcode_info *opcode;
1117 sh_operand_info *operands;
1119 sh_opcode_info *this_try = opcode;
1120 char *name = opcode->name;
1123 while (opcode->name)
1125 this_try = opcode++;
1126 if (this_try->name != name)
1128 /* We've looked so far down the table that we've run out of
1129 opcodes with the same name. */
1133 /* Look at both operands needed by the opcodes and provided by
1134 the user - since an arg test will often fail on the same arg
1135 again and again, we'll try and test the last failing arg the
1136 first on each opcode try. */
1137 for (n = 0; this_try->arg[n]; n++)
1139 sh_operand_info *user = operands + n;
1140 sh_arg_type arg = this_try->arg[n];
1152 if (user->type != arg)
1156 /* opcode needs r0 */
1157 if (user->type != A_REG_N || user->reg != 0)
1161 if (user->type != A_R0_GBR || user->reg != 0)
1165 if (user->type != F_REG_N || user->reg != 0)
1173 case A_IND_R0_REG_N:
1184 /* Opcode needs rn */
1185 if (user->type != arg)
1190 if (user->type != D_REG_N && user->type != X_REG_N)
1205 if (user->type != arg)
1210 if (user->type != arg)
1219 case A_IND_R0_REG_M:
1222 /* Opcode needs rn */
1223 if (user->type != arg - A_REG_M + A_REG_N)
1229 if (user->type != DSP_REG_N)
1251 if (user->type != DSP_REG_N)
1273 if (user->type != DSP_REG_N)
1295 if (user->type != DSP_REG_N)
1317 if (user->type != DSP_REG_N)
1339 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1343 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1347 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1351 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1355 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1365 /* Opcode needs rn */
1366 if (user->type != arg - F_REG_M + F_REG_N)
1371 if (user->type != D_REG_N && user->type != X_REG_N)
1376 if (user->type != XMTRX_M4)
1382 printf (_("unhandled %d\n"), arg);
1386 if ( !(valid_arch & this_try->arch))
1388 valid_arch &= this_try->arch;
1398 insert (where, how, pcrel, op)
1402 sh_operand_info *op;
1404 fix_new_exp (frag_now,
1405 where - frag_now->fr_literal,
1413 build_relax (opcode, op)
1414 sh_opcode_info *opcode;
1415 sh_operand_info *op;
1417 int high_byte = target_big_endian ? 0 : 1;
1420 if (opcode->arg[0] == A_BDISP8)
1422 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
1423 p = frag_var (rs_machine_dependent,
1424 md_relax_table[C (what, COND32)].rlx_length,
1425 md_relax_table[C (what, COND8)].rlx_length,
1427 op->immediate.X_add_symbol,
1428 op->immediate.X_add_number,
1430 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
1432 else if (opcode->arg[0] == A_BDISP12)
1434 p = frag_var (rs_machine_dependent,
1435 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
1436 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
1438 op->immediate.X_add_symbol,
1439 op->immediate.X_add_number,
1441 p[high_byte] = (opcode->nibbles[0] << 4);
1446 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
1449 insert_loop_bounds (output, operand)
1451 sh_operand_info *operand;
1456 /* Since the low byte of the opcode will be overwritten by the reloc, we
1457 can just stash the high byte into both bytes and ignore endianness. */
1460 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1461 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1465 static int count = 0;
1467 /* If the last loop insn is a two-byte-insn, it is in danger of being
1468 swapped with the insn after it. To prevent this, create a new
1469 symbol - complete with SH_LABEL reloc - after the last loop insn.
1470 If the last loop insn is four bytes long, the symbol will be
1471 right in the middle, but four byte insns are not swapped anyways. */
1472 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
1473 Hence a 9 digit number should be enough to count all REPEATs. */
1475 sprintf (name, "_R%x", count++ & 0x3fffffff);
1476 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
1477 /* Make this a local symbol. */
1479 SF_SET_LOCAL (end_sym);
1480 #endif /* OBJ_COFF */
1481 symbol_table_insert (end_sym);
1482 end_sym->sy_value = operand[1].immediate;
1483 end_sym->sy_value.X_add_number += 2;
1484 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
1487 output = frag_more (2);
1490 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1491 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1493 return frag_more (2);
1496 /* Now we know what sort of opcodes it is, let's build the bytes. */
1499 build_Mytes (opcode, operand)
1500 sh_opcode_info *opcode;
1501 sh_operand_info *operand;
1505 char *output = frag_more (2);
1506 unsigned int size = 2;
1507 int low_byte = target_big_endian ? 1 : 0;
1513 for (index = 0; index < 4; index++)
1515 sh_nibble_type i = opcode->nibbles[index];
1525 nbuf[index] = reg_n;
1528 nbuf[index] = reg_m;
1531 if (reg_n < 2 || reg_n > 5)
1532 as_bad (_("Invalid register: 'r%d'"), reg_n);
1533 nbuf[index] = (reg_n & 3) | 4;
1536 nbuf[index] = reg_n | (reg_m >> 2);
1539 nbuf[index] = reg_b | 0x08;
1542 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
1545 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
1548 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
1551 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
1554 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
1557 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
1560 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
1563 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
1566 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
1569 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
1572 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
1575 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
1578 insert (output, BFD_RELOC_SH_PCRELIMM8BY4, 1, operand);
1581 insert (output, BFD_RELOC_SH_PCRELIMM8BY2, 1, operand);
1584 output = insert_loop_bounds (output, operand);
1585 nbuf[index] = opcode->nibbles[3];
1589 printf (_("failed for %d\n"), i);
1593 if (!target_big_endian)
1595 output[1] = (nbuf[0] << 4) | (nbuf[1]);
1596 output[0] = (nbuf[2] << 4) | (nbuf[3]);
1600 output[0] = (nbuf[0] << 4) | (nbuf[1]);
1601 output[1] = (nbuf[2] << 4) | (nbuf[3]);
1606 /* Find an opcode at the start of *STR_P in the hash table, and set
1607 *STR_P to the first character after the last one read. */
1609 static sh_opcode_info *
1610 find_cooked_opcode (str_p)
1614 unsigned char *op_start;
1615 unsigned char *op_end;
1619 /* Drop leading whitespace. */
1623 /* Find the op code end.
1624 The pre-processor will eliminate whitespace in front of
1625 any '@' after the first argument; we may be called from
1626 assemble_ppi, so the opcode might be terminated by an '@'. */
1627 for (op_start = op_end = (unsigned char *) (str);
1630 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
1633 unsigned char c = op_start[nlen];
1635 /* The machine independent code will convert CMP/EQ into cmp/EQ
1636 because it thinks the '/' is the end of the symbol. Moreover,
1637 all but the first sub-insn is a parallel processing insn won't
1638 be capitalized. Instead of hacking up the machine independent
1639 code, we just deal with it here. */
1649 as_bad (_("can't find opcode "));
1651 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
1654 /* Assemble a parallel processing insn. */
1655 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
1658 assemble_ppi (op_end, opcode)
1660 sh_opcode_info *opcode;
1670 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
1671 Make sure we encode a defined insn pattern. */
1677 sh_operand_info operand[3];
1679 if (opcode->arg[0] != A_END)
1680 op_end = get_operands (opcode, op_end, operand);
1681 opcode = get_specific (opcode, operand);
1684 /* Couldn't find an opcode which matched the operands. */
1685 char *where = frag_more (2);
1690 as_bad (_("invalid operands for opcode"));
1694 if (opcode->nibbles[0] != PPI)
1695 as_bad (_("insn can't be combined with parallel processing insn"));
1697 switch (opcode->nibbles[1])
1702 as_bad (_("multiple movx specifications"));
1707 as_bad (_("multiple movy specifications"));
1713 as_bad (_("multiple movx specifications"));
1714 if (reg_n < 4 || reg_n > 5)
1715 as_bad (_("invalid movx address register"));
1716 if (opcode->nibbles[2] & 8)
1718 if (reg_m == A_A1_NUM)
1720 else if (reg_m != A_A0_NUM)
1721 as_bad (_("invalid movx dsp register"));
1726 as_bad (_("invalid movx dsp register"));
1729 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
1734 as_bad (_("multiple movy specifications"));
1735 if (opcode->nibbles[2] & 8)
1737 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
1740 if (reg_m == A_A1_NUM)
1742 else if (reg_m != A_A0_NUM)
1743 as_bad (_("invalid movy dsp register"));
1748 as_bad (_("invalid movy dsp register"));
1751 if (reg_n < 6 || reg_n > 7)
1752 as_bad (_("invalid movy address register"));
1753 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
1757 if (operand[0].immediate.X_op != O_constant)
1758 as_bad (_("dsp immediate shift value not constant"));
1759 field_b = ((opcode->nibbles[2] << 12)
1760 | (operand[0].immediate.X_add_number & 127) << 4
1765 as_bad (_("multiple parallel processing specifications"));
1766 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
1767 + (reg_x << 6) + (reg_y << 4) + reg_n);
1771 as_bad (_("multiple condition specifications"));
1772 cond = opcode->nibbles[2] << 8;
1774 goto skip_cond_check;
1778 as_bad (_("multiple parallel processing specifications"));
1779 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
1780 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
1786 if ((field_b & 0xef00) != 0xa100)
1787 as_bad (_("insn cannot be combined with pmuls"));
1789 switch (field_b & 0xf)
1792 field_b += 0 - A_X0_NUM;
1795 field_b += 1 - A_Y0_NUM;
1798 field_b += 2 - A_A0_NUM;
1801 field_b += 3 - A_A1_NUM;
1804 as_bad (_("bad padd / psub pmuls output operand"));
1807 field_b += 0x4000 + reg_efg;
1814 as_bad (_("condition not followed by conditionalizable insn"));
1820 opcode = find_cooked_opcode (&op_end);
1824 (_("unrecognized characters at end of parallel processing insn")));
1829 move_code = movx | movy;
1832 /* Parallel processing insn. */
1833 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
1835 output = frag_more (4);
1837 if (! target_big_endian)
1839 output[3] = ppi_code >> 8;
1840 output[2] = ppi_code;
1844 output[2] = ppi_code >> 8;
1845 output[3] = ppi_code;
1847 move_code |= 0xf800;
1851 /* Just a double data transfer. */
1852 output = frag_more (2);
1855 if (! target_big_endian)
1857 output[1] = move_code >> 8;
1858 output[0] = move_code;
1862 output[0] = move_code >> 8;
1863 output[1] = move_code;
1868 /* This is the guts of the machine-dependent assembler. STR points to a
1869 machine dependent instruction. This function is supposed to emit
1870 the frags/bytes it assembles to. */
1876 unsigned char *op_end;
1877 sh_operand_info operand[3];
1878 sh_opcode_info *opcode;
1879 unsigned int size = 0;
1881 opcode = find_cooked_opcode (&str);
1886 as_bad (_("unknown opcode"));
1891 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
1893 /* Output a CODE reloc to tell the linker that the following
1894 bytes are instructions, not data. */
1895 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1897 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
1900 if (opcode->nibbles[0] == PPI)
1902 size = assemble_ppi (op_end, opcode);
1906 if (opcode->arg[0] == A_BDISP12
1907 || opcode->arg[0] == A_BDISP8)
1909 parse_exp (op_end + 1, &operand[0]);
1910 build_relax (opcode, &operand[0]);
1914 if (opcode->arg[0] == A_END)
1916 /* Ignore trailing whitespace. If there is any, it has already
1917 been compressed to a single space. */
1923 op_end = get_operands (opcode, op_end, operand);
1925 opcode = get_specific (opcode, operand);
1929 /* Couldn't find an opcode which matched the operands. */
1930 char *where = frag_more (2);
1935 as_bad (_("invalid operands for opcode"));
1940 as_bad (_("excess operands: '%s'"), op_end);
1942 size = build_Mytes (opcode, operand);
1947 #ifdef BFD_ASSEMBLER
1948 dwarf2_emit_insn (size);
1952 /* This routine is called each time a label definition is seen. It
1953 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
1958 static fragS *last_label_frag;
1959 static int last_label_offset;
1962 && seg_info (now_seg)->tc_segment_info_data.in_code)
1966 offset = frag_now_fix ();
1967 if (frag_now != last_label_frag
1968 || offset != last_label_offset)
1970 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
1971 last_label_frag = frag_now;
1972 last_label_offset = offset;
1977 /* This routine is called when the assembler is about to output some
1978 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
1981 sh_flush_pending_output ()
1984 && seg_info (now_seg)->tc_segment_info_data.in_code)
1986 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
1988 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
1993 md_undefined_symbol (name)
1997 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE. Otherwise we
1998 have no need to default values of symbols. */
1999 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
2003 if (symbol_find (name))
2004 as_bad ("GOT already in the symbol table");
2006 GOT_symbol = symbol_new (name, undefined_section,
2007 (valueT)0, & zero_address_frag);
2012 #endif /* OBJ_ELF */
2018 #ifndef BFD_ASSEMBLER
2021 tc_crawl_symbol_chain (headers)
2022 object_headers *headers;
2024 printf (_("call to tc_crawl_symbol_chain \n"));
2028 tc_headers_hook (headers)
2029 object_headers *headers;
2031 printf (_("call to tc_headers_hook \n"));
2037 /* Various routines to kill one day. */
2038 /* Equal to MAX_PRECISION in atof-ieee.c. */
2039 #define MAX_LITTLENUMS 6
2041 /* Turn a string in input_line_pointer into a floating point constant
2042 of type TYPE, and store the appropriate bytes in *LITP. The number
2043 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2044 returned, or NULL on OK. */
2047 md_atof (type, litP, sizeP)
2053 LITTLENUM_TYPE words[4];
2069 return _("bad call to md_atof");
2072 t = atof_ieee (input_line_pointer, type, words);
2074 input_line_pointer = t;
2078 if (! target_big_endian)
2080 for (i = prec - 1; i >= 0; i--)
2082 md_number_to_chars (litP, (valueT) words[i], 2);
2088 for (i = 0; i < prec; i++)
2090 md_number_to_chars (litP, (valueT) words[i], 2);
2098 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
2099 call instruction. It refers to a label of the instruction which
2100 loads the register which the call uses. We use it to generate a
2101 special reloc for the linker. */
2105 int ignore ATTRIBUTE_UNUSED;
2110 as_warn (_(".uses pseudo-op seen when not relaxing"));
2114 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2116 as_bad (_("bad .uses format"));
2117 ignore_rest_of_line ();
2121 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
2123 demand_empty_rest_of_line ();
2126 CONST char *md_shortopts = "";
2127 struct option md_longopts[] =
2129 #define OPTION_RELAX (OPTION_MD_BASE)
2130 #define OPTION_LITTLE (OPTION_MD_BASE + 1)
2131 #define OPTION_SMALL (OPTION_LITTLE + 1)
2132 #define OPTION_DSP (OPTION_SMALL + 1)
2134 {"relax", no_argument, NULL, OPTION_RELAX},
2135 {"little", no_argument, NULL, OPTION_LITTLE},
2136 {"small", no_argument, NULL, OPTION_SMALL},
2137 {"dsp", no_argument, NULL, OPTION_DSP},
2138 {NULL, no_argument, NULL, 0}
2140 size_t md_longopts_size = sizeof (md_longopts);
2143 md_parse_option (c, arg)
2145 char *arg ATTRIBUTE_UNUSED;
2155 target_big_endian = 0;
2174 md_show_usage (stream)
2177 fprintf (stream, _("\
2179 -little generate little endian code\n\
2180 -relax alter jump instructions for long displacements\n\
2181 -small align sections to 4 byte boundaries, not 16\n\
2182 -dsp enable sh-dsp insns, and disable sh3e / sh4 insns.\n"));
2185 /* This struct is used to pass arguments to sh_count_relocs through
2186 bfd_map_over_sections. */
2188 struct sh_count_relocs
2190 /* Symbol we are looking for. */
2192 /* Count of relocs found. */
2196 /* Count the number of fixups in a section which refer to a particular
2197 symbol. When using BFD_ASSEMBLER, this is called via
2198 bfd_map_over_sections. */
2201 sh_count_relocs (abfd, sec, data)
2202 bfd *abfd ATTRIBUTE_UNUSED;
2206 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
2207 segment_info_type *seginfo;
2211 seginfo = seg_info (sec);
2212 if (seginfo == NULL)
2216 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2218 if (fix->fx_addsy == sym)
2226 /* Handle the count relocs for a particular section. When using
2227 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
2230 sh_frob_section (abfd, sec, ignore)
2231 bfd *abfd ATTRIBUTE_UNUSED;
2233 PTR ignore ATTRIBUTE_UNUSED;
2235 segment_info_type *seginfo;
2238 seginfo = seg_info (sec);
2239 if (seginfo == NULL)
2242 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2247 struct sh_count_relocs info;
2249 if (fix->fx_r_type != BFD_RELOC_SH_USES)
2252 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2253 symbol in the same section. */
2254 sym = fix->fx_addsy;
2256 || fix->fx_subsy != NULL
2257 || fix->fx_addnumber != 0
2258 || S_GET_SEGMENT (sym) != sec
2259 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2260 || S_GET_STORAGE_CLASS (sym) == C_EXT
2262 || S_IS_EXTERNAL (sym))
2264 as_warn_where (fix->fx_file, fix->fx_line,
2265 _(".uses does not refer to a local symbol in the same section"));
2269 /* Look through the fixups again, this time looking for one
2270 at the same location as sym. */
2271 val = S_GET_VALUE (sym);
2272 for (fscan = seginfo->fix_root;
2274 fscan = fscan->fx_next)
2275 if (val == fscan->fx_frag->fr_address + fscan->fx_where
2276 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
2277 && fscan->fx_r_type != BFD_RELOC_SH_CODE
2278 && fscan->fx_r_type != BFD_RELOC_SH_DATA
2279 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
2283 as_warn_where (fix->fx_file, fix->fx_line,
2284 _("can't find fixup pointed to by .uses"));
2288 if (fscan->fx_tcbit)
2290 /* We've already done this one. */
2294 /* The variable fscan should also be a fixup to a local symbol
2295 in the same section. */
2296 sym = fscan->fx_addsy;
2298 || fscan->fx_subsy != NULL
2299 || fscan->fx_addnumber != 0
2300 || S_GET_SEGMENT (sym) != sec
2301 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2302 || S_GET_STORAGE_CLASS (sym) == C_EXT
2304 || S_IS_EXTERNAL (sym))
2306 as_warn_where (fix->fx_file, fix->fx_line,
2307 _(".uses target does not refer to a local symbol in the same section"));
2311 /* Now we look through all the fixups of all the sections,
2312 counting the number of times we find a reference to sym. */
2315 #ifdef BFD_ASSEMBLER
2316 bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
2321 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
2322 sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
2329 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
2330 We have already adjusted the value of sym to include the
2331 fragment address, so we undo that adjustment here. */
2332 subseg_change (sec, 0);
2333 fix_new (fscan->fx_frag,
2334 S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
2335 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
2339 /* This function is called after the symbol table has been completed,
2340 but before the relocs or section contents have been written out.
2341 If we have seen any .uses pseudo-ops, they point to an instruction
2342 which loads a register with the address of a function. We look
2343 through the fixups to find where the function address is being
2344 loaded from. We then generate a COUNT reloc giving the number of
2345 times that function address is referred to. The linker uses this
2346 information when doing relaxing, to decide when it can eliminate
2347 the stored function address entirely. */
2355 #ifdef BFD_ASSEMBLER
2356 bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
2361 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
2362 sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
2367 /* Called after relaxing. Set the correct sizes of the fragments, and
2368 create relocs so that md_apply_fix will fill in the correct values. */
2371 md_convert_frag (headers, seg, fragP)
2372 #ifdef BFD_ASSEMBLER
2373 bfd *headers ATTRIBUTE_UNUSED;
2375 object_headers *headers;
2382 switch (fragP->fr_subtype)
2384 case C (COND_JUMP, COND8):
2385 case C (COND_JUMP_DELAY, COND8):
2386 subseg_change (seg, 0);
2387 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2388 1, BFD_RELOC_SH_PCDISP8BY2);
2393 case C (UNCOND_JUMP, UNCOND12):
2394 subseg_change (seg, 0);
2395 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
2396 1, BFD_RELOC_SH_PCDISP12BY2);
2401 case C (UNCOND_JUMP, UNCOND32):
2402 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
2403 if (fragP->fr_symbol == NULL)
2404 as_bad_where (fragP->fr_file, fragP->fr_line,
2405 _("displacement overflows 12-bit field"));
2406 else if (S_IS_DEFINED (fragP->fr_symbol))
2407 as_bad_where (fragP->fr_file, fragP->fr_line,
2408 _("displacement to defined symbol %s overflows 12-bit field"),
2409 S_GET_NAME (fragP->fr_symbol));
2411 as_bad_where (fragP->fr_file, fragP->fr_line,
2412 _("displacement to undefined symbol %s overflows 12-bit field"),
2413 S_GET_NAME (fragP->fr_symbol));
2414 /* Stabilize this frag, so we don't trip an assert. */
2415 fragP->fr_fix += fragP->fr_var;
2419 case C (COND_JUMP, COND12):
2420 case C (COND_JUMP_DELAY, COND12):
2421 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
2422 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
2423 was due to gas incorrectly relaxing an out-of-range conditional
2424 branch with delay slot. It turned:
2425 bf.s L6 (slot mov.l r12,@(44,r0))
2428 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
2430 32: 10 cb mov.l r12,@(44,r0)
2431 Therefore, branches with delay slots have to be handled
2432 differently from ones without delay slots. */
2434 unsigned char *buffer =
2435 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
2436 int highbyte = target_big_endian ? 0 : 1;
2437 int lowbyte = target_big_endian ? 1 : 0;
2438 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
2440 /* Toggle the true/false bit of the bcond. */
2441 buffer[highbyte] ^= 0x2;
2443 /* If this is a dalayed branch, we may not put the the bra in the
2444 slot. So we change it to a non-delayed branch, like that:
2445 b! cond slot_label; bra disp; slot_label: slot_insn
2446 ??? We should try if swapping the conditional branch and
2447 its delay-slot insn already makes the branch reach. */
2449 /* Build a relocation to six / four bytes farther on. */
2450 subseg_change (seg, 0);
2451 fix_new (fragP, fragP->fr_fix, 2,
2452 #ifdef BFD_ASSEMBLER
2453 section_symbol (seg),
2455 seg_info (seg)->dot,
2457 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
2458 1, BFD_RELOC_SH_PCDISP8BY2);
2460 /* Set up a jump instruction. */
2461 buffer[highbyte + 2] = 0xa0;
2462 buffer[lowbyte + 2] = 0;
2463 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
2464 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
2468 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
2473 /* Fill in a NOP instruction. */
2474 buffer[highbyte + 4] = 0x0;
2475 buffer[lowbyte + 4] = 0x9;
2484 case C (COND_JUMP, COND32):
2485 case C (COND_JUMP_DELAY, COND32):
2486 case C (COND_JUMP, UNDEF_WORD_DISP):
2487 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
2488 if (fragP->fr_symbol == NULL)
2489 as_bad_where (fragP->fr_file, fragP->fr_line,
2490 _("displacement overflows 8-bit field"));
2491 else if (S_IS_DEFINED (fragP->fr_symbol))
2492 as_bad_where (fragP->fr_file, fragP->fr_line,
2493 _("displacement to defined symbol %s overflows 8-bit field"),
2494 S_GET_NAME (fragP->fr_symbol));
2496 as_bad_where (fragP->fr_file, fragP->fr_line,
2497 _("displacement to undefined symbol %s overflows 8-bit field "),
2498 S_GET_NAME (fragP->fr_symbol));
2499 /* Stabilize this frag, so we don't trip an assert. */
2500 fragP->fr_fix += fragP->fr_var;
2508 if (donerelax && !sh_relax)
2509 as_warn_where (fragP->fr_file, fragP->fr_line,
2510 _("overflow in branch to %s; converted into longer instruction sequence"),
2511 (fragP->fr_symbol != NULL
2512 ? S_GET_NAME (fragP->fr_symbol)
2517 md_section_align (seg, size)
2518 segT seg ATTRIBUTE_UNUSED;
2521 #ifdef BFD_ASSEMBLER
2524 #else /* ! OBJ_ELF */
2525 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
2526 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
2527 #endif /* ! OBJ_ELF */
2528 #else /* ! BFD_ASSEMBLER */
2529 return ((size + (1 << section_alignment[(int) seg]) - 1)
2530 & (-1 << section_alignment[(int) seg]));
2531 #endif /* ! BFD_ASSEMBLER */
2534 /* This static variable is set by s_uacons to tell sh_cons_align that
2535 the expession does not need to be aligned. */
2537 static int sh_no_align_cons = 0;
2539 /* This handles the unaligned space allocation pseudo-ops, such as
2540 .uaword. .uaword is just like .word, but the value does not need
2547 /* Tell sh_cons_align not to align this value. */
2548 sh_no_align_cons = 1;
2552 /* If a .word, et. al., pseud-op is seen, warn if the value is not
2553 aligned correctly. Note that this can cause warnings to be issued
2554 when assembling initialized structured which were declared with the
2555 packed attribute. FIXME: Perhaps we should require an option to
2556 enable this warning? */
2559 sh_cons_align (nbytes)
2565 if (sh_no_align_cons)
2567 /* This is an unaligned pseudo-op. */
2568 sh_no_align_cons = 0;
2573 while ((nbytes & 1) == 0)
2582 if (now_seg == absolute_section)
2584 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
2585 as_warn (_("misaligned data"));
2589 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
2590 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
2592 record_alignment (now_seg, nalign);
2595 /* When relaxing, we need to output a reloc for any .align directive
2596 that requests alignment to a four byte boundary or larger. This is
2597 also where we check for misaligned data. */
2600 sh_handle_align (frag)
2603 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
2605 if (frag->fr_type == rs_align_code)
2607 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
2608 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
2610 char *p = frag->fr_literal + frag->fr_fix;
2619 if (target_big_endian)
2621 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
2622 frag->fr_var = sizeof big_nop_pattern;
2626 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
2627 frag->fr_var = sizeof little_nop_pattern;
2630 else if (frag->fr_type == rs_align_test)
2633 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
2637 && (frag->fr_type == rs_align
2638 || frag->fr_type == rs_align_code)
2639 && frag->fr_address + frag->fr_fix > 0
2640 && frag->fr_offset > 1
2641 && now_seg != bss_section)
2642 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
2643 BFD_RELOC_SH_ALIGN);
2646 /* This macro decides whether a particular reloc is an entry in a
2647 switch table. It is used when relaxing, because the linker needs
2648 to know about all such entries so that it can adjust them if
2651 #ifdef BFD_ASSEMBLER
2652 #define SWITCH_TABLE_CONS(fix) (0)
2654 #define SWITCH_TABLE_CONS(fix) \
2655 ((fix)->fx_r_type == 0 \
2656 && ((fix)->fx_size == 2 \
2657 || (fix)->fx_size == 1 \
2658 || (fix)->fx_size == 4))
2661 #define SWITCH_TABLE(fix) \
2662 ((fix)->fx_addsy != NULL \
2663 && (fix)->fx_subsy != NULL \
2664 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
2665 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
2666 && ((fix)->fx_r_type == BFD_RELOC_32 \
2667 || (fix)->fx_r_type == BFD_RELOC_16 \
2668 || (fix)->fx_r_type == BFD_RELOC_8 \
2669 || SWITCH_TABLE_CONS (fix)))
2671 /* See whether we need to force a relocation into the output file.
2672 This is used to force out switch and PC relative relocations when
2676 sh_force_relocation (fix)
2680 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2681 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2682 || fix->fx_r_type == BFD_RELOC_SH_LOOP_START
2683 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END)
2689 return (fix->fx_pcrel
2690 || SWITCH_TABLE (fix)
2691 || fix->fx_r_type == BFD_RELOC_SH_COUNT
2692 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
2693 || fix->fx_r_type == BFD_RELOC_SH_CODE
2694 || fix->fx_r_type == BFD_RELOC_SH_DATA
2695 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
2700 sh_fix_adjustable (fixP)
2704 if (fixP->fx_addsy == NULL)
2707 if (fixP->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
2708 || fixP->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
2709 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
2710 || fixP->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
2711 || fixP->fx_r_type == BFD_RELOC_8_PCREL
2712 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH16
2713 || fixP->fx_r_type == BFD_RELOC_SH_SWITCH32)
2716 if (! TC_RELOC_RTSYM_LOC_FIXUP (fixP)
2717 || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
2718 || fixP->fx_r_type == BFD_RELOC_RVA)
2721 /* We need the symbol name for the VTABLE entries */
2722 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2723 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2730 sh_elf_final_processing ()
2734 /* Set file-specific flags to indicate if this code needs
2735 a processor with the sh-dsp / sh3e ISA to execute. */
2736 if (valid_arch & arch_sh1)
2738 else if (valid_arch & arch_sh2)
2740 else if (valid_arch & arch_sh_dsp)
2742 else if (valid_arch & arch_sh3)
2744 else if (valid_arch & arch_sh3_dsp)
2746 else if (valid_arch & arch_sh3e)
2748 else if (valid_arch & arch_sh4)
2753 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
2754 elf_elfheader (stdoutput)->e_flags |= val;
2758 /* Apply a fixup to the object file. */
2760 #ifdef BFD_ASSEMBLER
2762 md_apply_fix (fixP, valp)
2767 md_apply_fix (fixP, val)
2772 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2773 int lowbyte = target_big_endian ? 1 : 0;
2774 int highbyte = target_big_endian ? 0 : 1;
2775 #ifdef BFD_ASSEMBLER
2781 #ifdef BFD_ASSEMBLER
2782 /* A difference between two symbols, the second of which is in the
2783 current section, is transformed in a PC-relative relocation to
2784 the other symbol. We have to adjust the relocation type here. */
2787 switch (fixP->fx_r_type)
2793 fixP->fx_r_type = BFD_RELOC_32_PCREL;
2796 /* Currently, we only support 32-bit PCREL relocations.
2797 We'd need a new reloc type to handle 16_PCREL, and
2798 8_PCREL is already taken for R_SH_SWITCH8, which
2799 apparently does something completely different than what
2802 bfd_set_error (bfd_error_bad_value);
2806 bfd_set_error (bfd_error_bad_value);
2811 /* The function adjust_reloc_syms won't convert a reloc against a weak
2812 symbol into a reloc against a section, but bfd_install_relocation
2813 will screw up if the symbol is defined, so we have to adjust val here
2814 to avoid the screw up later.
2816 For ordinary relocs, this does not happen for ELF, since for ELF,
2817 bfd_install_relocation uses the "special function" field of the
2818 howto, and does not execute the code that needs to be undone, as long
2819 as the special function does not return bfd_reloc_continue.
2820 It can happen for GOT- and PLT-type relocs the way they are
2821 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
2822 doesn't matter here since those relocs don't use VAL; see below. */
2823 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2824 && fixP->fx_addsy != NULL
2825 && S_IS_WEAK (fixP->fx_addsy))
2826 val -= S_GET_VALUE (fixP->fx_addsy);
2829 #ifndef BFD_ASSEMBLER
2830 if (fixP->fx_r_type == 0)
2832 if (fixP->fx_size == 2)
2833 fixP->fx_r_type = BFD_RELOC_16;
2834 else if (fixP->fx_size == 4)
2835 fixP->fx_r_type = BFD_RELOC_32;
2836 else if (fixP->fx_size == 1)
2837 fixP->fx_r_type = BFD_RELOC_8;
2845 switch (fixP->fx_r_type)
2847 case BFD_RELOC_SH_IMM4:
2849 *buf = (*buf & 0xf0) | (val & 0xf);
2852 case BFD_RELOC_SH_IMM4BY2:
2855 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
2858 case BFD_RELOC_SH_IMM4BY4:
2861 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
2864 case BFD_RELOC_SH_IMM8BY2:
2870 case BFD_RELOC_SH_IMM8BY4:
2877 case BFD_RELOC_SH_IMM8:
2878 /* Sometimes the 8 bit value is sign extended (e.g., add) and
2879 sometimes it is not (e.g., and). We permit any 8 bit value.
2880 Note that adding further restrictions may invalidate
2881 reasonable looking assembly code, such as ``and -0x1,r0''. */
2887 case BFD_RELOC_SH_PCRELIMM8BY4:
2888 /* The lower two bits of the PC are cleared before the
2889 displacement is added in. We can assume that the destination
2890 is on a 4 byte bounday. If this instruction is also on a 4
2891 byte boundary, then we want
2893 and target - here is a multiple of 4.
2894 Otherwise, we are on a 2 byte boundary, and we want
2895 (target - (here - 2)) / 4
2896 and target - here is not a multiple of 4. Computing
2897 (target - (here - 2)) / 4 == (target - here + 2) / 4
2898 works for both cases, since in the first case the addition of
2899 2 will be removed by the division. target - here is in the
2901 val = (val + 2) / 4;
2903 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2907 case BFD_RELOC_SH_PCRELIMM8BY2:
2910 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2914 case BFD_RELOC_SH_PCDISP8BY2:
2916 if (val < -0x80 || val > 0x7f)
2917 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2921 case BFD_RELOC_SH_PCDISP12BY2:
2923 if (val < -0x800 || val > 0x7ff)
2924 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
2925 buf[lowbyte] = val & 0xff;
2926 buf[highbyte] |= (val >> 8) & 0xf;
2930 case BFD_RELOC_32_PCREL:
2931 md_number_to_chars (buf, val, 4);
2935 md_number_to_chars (buf, val, 2);
2938 case BFD_RELOC_SH_USES:
2939 /* Pass the value into sh_coff_reloc_mangle. */
2940 fixP->fx_addnumber = val;
2943 case BFD_RELOC_SH_COUNT:
2944 case BFD_RELOC_SH_ALIGN:
2945 case BFD_RELOC_SH_CODE:
2946 case BFD_RELOC_SH_DATA:
2947 case BFD_RELOC_SH_LABEL:
2948 /* Nothing to do here. */
2951 case BFD_RELOC_SH_LOOP_START:
2952 case BFD_RELOC_SH_LOOP_END:
2954 case BFD_RELOC_VTABLE_INHERIT:
2955 case BFD_RELOC_VTABLE_ENTRY:
2957 #ifdef BFD_ASSEMBLER
2964 case BFD_RELOC_32_PLT_PCREL:
2965 /* Make the jump instruction point to the address of the operand. At
2966 runtime we merely add the offset to the actual PLT entry. */
2970 case BFD_RELOC_SH_GOTPC:
2971 /* This is tough to explain. We end up with this one if we have
2972 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
2973 The goal here is to obtain the absolute address of the GOT,
2974 and it is strongly preferable from a performance point of
2975 view to avoid using a runtime relocation for this. There are
2976 cases where you have something like:
2978 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
2980 and here no correction would be required. Internally in the
2981 assembler we treat operands of this form as not being pcrel
2982 since the '.' is explicitly mentioned, and I wonder whether
2983 it would simplify matters to do it this way. Who knows. In
2984 earlier versions of the PIC patches, the pcrel_adjust field
2985 was used to store the correction, but since the expression is
2986 not pcrel, I felt it would be confusing to do it this way. */
2988 md_number_to_chars (buf, val, 4);
2991 case BFD_RELOC_32_GOT_PCREL:
2992 *valp = 0; /* Fully resolved at runtime. No addend. */
2993 md_number_to_chars (buf, 0, 4);
2996 case BFD_RELOC_32_GOTOFF:
3006 if ((val & ((1 << shift) - 1)) != 0)
3007 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
3011 val = ((val >> shift)
3012 | ((long) -1 & ~ ((long) -1 >> shift)));
3014 if (max != 0 && (val < min || val > max))
3015 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
3017 #ifdef BFD_ASSEMBLER
3022 /* Called just before address relaxation. Return the length
3023 by which a fragment must grow to reach it's destination. */
3026 md_estimate_size_before_relax (fragP, segment_type)
3027 register fragS *fragP;
3028 register segT segment_type;
3032 switch (fragP->fr_subtype)
3037 case C (UNCOND_JUMP, UNDEF_DISP):
3038 /* Used to be a branch to somewhere which was unknown. */
3039 if (!fragP->fr_symbol)
3041 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3043 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3045 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3049 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
3053 case C (COND_JUMP, UNDEF_DISP):
3054 case C (COND_JUMP_DELAY, UNDEF_DISP):
3055 what = GET_WHAT (fragP->fr_subtype);
3056 /* Used to be a branch to somewhere which was unknown. */
3057 if (fragP->fr_symbol
3058 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3060 /* Got a symbol and it's defined in this segment, become byte
3061 sized - maybe it will fix up. */
3062 fragP->fr_subtype = C (what, COND8);
3064 else if (fragP->fr_symbol)
3066 /* Its got a segment, but its not ours, so it will always be long. */
3067 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
3071 /* We know the abs value. */
3072 fragP->fr_subtype = C (what, COND8);
3076 case C (UNCOND_JUMP, UNCOND12):
3077 case C (UNCOND_JUMP, UNCOND32):
3078 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3079 case C (COND_JUMP, COND8):
3080 case C (COND_JUMP, COND12):
3081 case C (COND_JUMP, COND32):
3082 case C (COND_JUMP, UNDEF_WORD_DISP):
3083 case C (COND_JUMP_DELAY, COND8):
3084 case C (COND_JUMP_DELAY, COND12):
3085 case C (COND_JUMP_DELAY, COND32):
3086 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3087 /* When relaxing a section for the second time, we don't need to
3088 do anything besides return the current size. */
3092 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
3093 return fragP->fr_var;
3096 /* Put number into target byte order. */
3099 md_number_to_chars (ptr, use, nbytes)
3104 if (! target_big_endian)
3105 number_to_chars_littleendian (ptr, use, nbytes);
3107 number_to_chars_bigendian (ptr, use, nbytes);
3111 md_pcrel_from_section (fixP, sec)
3115 if (fixP->fx_addsy != (symbolS *) NULL
3116 && (! S_IS_DEFINED (fixP->fx_addsy)
3117 || S_IS_EXTERN (fixP->fx_addsy)
3118 || S_IS_WEAK (fixP->fx_addsy)
3119 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3121 /* The symbol is undefined (or is defined but not in this section,
3122 or we're not sure about it being the final definition). Let the
3123 linker figure it out. We need to adjust the subtraction of a
3124 symbol to the position of the relocated data, though. */
3125 return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
3128 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
3134 tc_coff_sizemachdep (frag)
3137 return md_relax_table[frag->fr_subtype].rlx_length;
3140 #endif /* OBJ_COFF */
3142 #ifndef BFD_ASSEMBLER
3145 /* Map BFD relocs to SH COFF relocs. */
3149 bfd_reloc_code_real_type bfd_reloc;
3153 static const struct reloc_map coff_reloc_map[] =
3155 { BFD_RELOC_32, R_SH_IMM32 },
3156 { BFD_RELOC_16, R_SH_IMM16 },
3157 { BFD_RELOC_8, R_SH_IMM8 },
3158 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
3159 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
3160 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
3161 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
3162 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
3163 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
3164 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
3165 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
3166 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
3167 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
3168 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
3169 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
3170 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
3171 { BFD_RELOC_SH_USES, R_SH_USES },
3172 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
3173 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
3174 { BFD_RELOC_SH_CODE, R_SH_CODE },
3175 { BFD_RELOC_SH_DATA, R_SH_DATA },
3176 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
3177 { BFD_RELOC_UNUSED, 0 }
3180 /* Adjust a reloc for the SH. This is similar to the generic code,
3181 but does some minor tweaking. */
3184 sh_coff_reloc_mangle (seg, fix, intr, paddr)
3185 segment_info_type *seg;
3187 struct internal_reloc *intr;
3190 symbolS *symbol_ptr = fix->fx_addsy;
3193 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
3195 if (! SWITCH_TABLE (fix))
3197 const struct reloc_map *rm;
3199 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
3200 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
3202 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
3203 as_bad_where (fix->fx_file, fix->fx_line,
3204 _("Can not represent %s relocation in this object file format"),
3205 bfd_get_reloc_code_name (fix->fx_r_type));
3206 intr->r_type = rm->sh_reloc;
3213 if (fix->fx_r_type == BFD_RELOC_16)
3214 intr->r_type = R_SH_SWITCH16;
3215 else if (fix->fx_r_type == BFD_RELOC_8)
3216 intr->r_type = R_SH_SWITCH8;
3217 else if (fix->fx_r_type == BFD_RELOC_32)
3218 intr->r_type = R_SH_SWITCH32;
3222 /* For a switch reloc, we set r_offset to the difference between
3223 the reloc address and the subtrahend. When the linker is
3224 doing relaxing, it can use the determine the starting and
3225 ending points of the switch difference expression. */
3226 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
3229 /* PC relative relocs are always against the current section. */
3230 if (symbol_ptr == NULL)
3232 switch (fix->fx_r_type)
3234 case BFD_RELOC_SH_PCRELIMM8BY2:
3235 case BFD_RELOC_SH_PCRELIMM8BY4:
3236 case BFD_RELOC_SH_PCDISP8BY2:
3237 case BFD_RELOC_SH_PCDISP12BY2:
3238 case BFD_RELOC_SH_USES:
3239 symbol_ptr = seg->dot;
3246 if (fix->fx_r_type == BFD_RELOC_SH_USES)
3248 /* We can't store the offset in the object file, since this
3249 reloc does not take up any space, so we store it in r_offset.
3250 The fx_addnumber field was set in md_apply_fix. */
3251 intr->r_offset = fix->fx_addnumber;
3253 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
3255 /* We can't store the count in the object file, since this reloc
3256 does not take up any space, so we store it in r_offset. The
3257 fx_offset field was set when the fixup was created in
3258 sh_coff_frob_file. */
3259 intr->r_offset = fix->fx_offset;
3260 /* This reloc is always absolute. */
3263 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
3265 /* Store the alignment in the r_offset field. */
3266 intr->r_offset = fix->fx_offset;
3267 /* This reloc is always absolute. */
3270 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
3271 || fix->fx_r_type == BFD_RELOC_SH_DATA
3272 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
3274 /* These relocs are always absolute. */
3278 /* Turn the segment of the symbol into an offset. */
3279 if (symbol_ptr != NULL)
3281 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
3283 intr->r_symndx = dot->sy_number;
3285 intr->r_symndx = symbol_ptr->sy_number;
3288 intr->r_symndx = -1;
3291 #endif /* OBJ_COFF */
3292 #endif /* ! BFD_ASSEMBLER */
3294 #ifdef BFD_ASSEMBLER
3296 /* Create a reloc. */
3299 tc_gen_reloc (section, fixp)
3300 asection *section ATTRIBUTE_UNUSED;
3304 bfd_reloc_code_real_type r_type;
3306 rel = (arelent *) xmalloc (sizeof (arelent));
3307 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3308 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3309 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3311 r_type = fixp->fx_r_type;
3313 if (SWITCH_TABLE (fixp))
3315 rel->addend = rel->address - S_GET_VALUE (fixp->fx_subsy);
3316 if (r_type == BFD_RELOC_16)
3317 r_type = BFD_RELOC_SH_SWITCH16;
3318 else if (r_type == BFD_RELOC_8)
3319 r_type = BFD_RELOC_8_PCREL;
3320 else if (r_type == BFD_RELOC_32)
3321 r_type = BFD_RELOC_SH_SWITCH32;
3325 else if (r_type == BFD_RELOC_SH_USES)
3326 rel->addend = fixp->fx_addnumber;
3327 else if (r_type == BFD_RELOC_SH_COUNT)
3328 rel->addend = fixp->fx_offset;
3329 else if (r_type == BFD_RELOC_SH_ALIGN)
3330 rel->addend = fixp->fx_offset;
3331 else if (r_type == BFD_RELOC_VTABLE_INHERIT
3332 || r_type == BFD_RELOC_VTABLE_ENTRY)
3333 rel->addend = fixp->fx_offset;
3334 else if (r_type == BFD_RELOC_SH_LOOP_START
3335 || r_type == BFD_RELOC_SH_LOOP_END)
3336 rel->addend = fixp->fx_offset;
3337 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
3340 rel->address = rel->addend = fixp->fx_offset;
3342 else if (fixp->fx_pcrel)
3343 rel->addend = fixp->fx_addnumber;
3344 else if (r_type == BFD_RELOC_32 || r_type == BFD_RELOC_32_GOTOFF)
3345 rel->addend = fixp->fx_addnumber;
3349 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
3350 if (rel->howto == NULL)
3352 as_bad_where (fixp->fx_file, fixp->fx_line,
3353 _("Cannot represent relocation type %s"),
3354 bfd_get_reloc_code_name (r_type));
3355 /* Set howto to a garbage value so that we can keep going. */
3356 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3357 assert (rel->howto != NULL);
3363 #endif /* BFD_ASSEMBLER */