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"
31 #include "struc-symbol.h"
37 #include "dwarf2dbg.h"
39 const char comment_chars
[] = "!";
40 const char line_separator_chars
[] = ";";
41 const char line_comment_chars
[] = "!#";
43 static void s_uses
PARAMS ((int));
45 static void sh_count_relocs
PARAMS ((bfd
*, segT
, PTR
));
46 static void sh_frob_section
PARAMS ((bfd
*, segT
, PTR
));
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
*));
55 static void sh_elf_cons
PARAMS ((int));
57 symbolS
*GOT_symbol
; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
64 int ignore ATTRIBUTE_UNUSED
;
67 target_big_endian
= 0;
70 /* This table describes all the machine specific pseudo-ops the assembler
71 has to support. The fields are:
72 pseudo-op name without dot
73 function to call to execute this pseudo-op
74 Integer arg to pass to the function. */
76 const pseudo_typeS md_pseudo_table
[] =
79 {"long", sh_elf_cons
, 4},
80 {"int", sh_elf_cons
, 4},
81 {"word", sh_elf_cons
, 2},
82 {"short", sh_elf_cons
, 2},
87 {"form", listing_psize
, 0},
88 {"little", little
, 0},
89 {"heading", listing_title
, 0},
90 {"import", s_ignore
, 0},
91 {"page", listing_eject
, 0},
92 {"program", s_ignore
, 0},
94 {"uaword", s_uacons
, 2},
95 {"ualong", s_uacons
, 4},
96 {"uaquad", s_uacons
, 8},
97 {"2byte", s_uacons
, 2},
98 {"4byte", s_uacons
, 4},
99 {"8byte", s_uacons
, 8},
100 {"file", dwarf2_directive_file
, 0 },
101 {"loc", dwarf2_directive_loc
, 0 },
105 /*int md_reloc_size; */
107 int sh_relax
; /* set if -relax seen */
109 /* Whether -small was seen. */
113 /* Whether -dsp was seen. */
117 /* The bit mask of architectures that could
118 accomodate the insns seen so far. */
119 static int valid_arch
;
121 const char EXP_CHARS
[] = "eE";
123 /* Chars that mean this number is a floating point constant. */
126 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
128 #define C(a,b) ENCODE_RELAX(a,b)
130 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
131 #define GET_WHAT(x) ((x>>4))
133 /* These are the three types of relaxable instrction. */
135 #define COND_JUMP_DELAY 2
136 #define UNCOND_JUMP 3
143 #define UNDEF_WORD_DISP 4
148 /* Branch displacements are from the address of the branch plus
149 four, thus all minimum and maximum values have 4 added to them. */
152 #define COND8_LENGTH 2
154 /* There is one extra instruction before the branch, so we must add
155 two more bytes to account for it. */
156 #define COND12_F 4100
157 #define COND12_M -4090
158 #define COND12_LENGTH 6
160 #define COND12_DELAY_LENGTH 4
162 /* ??? The minimum and maximum values are wrong, but this does not matter
163 since this relocation type is not supported yet. */
164 #define COND32_F (1<<30)
165 #define COND32_M -(1<<30)
166 #define COND32_LENGTH 14
168 #define UNCOND12_F 4098
169 #define UNCOND12_M -4092
170 #define UNCOND12_LENGTH 2
172 /* ??? The minimum and maximum values are wrong, but this does not matter
173 since this relocation type is not supported yet. */
174 #define UNCOND32_F (1<<30)
175 #define UNCOND32_M -(1<<30)
176 #define UNCOND32_LENGTH 14
178 #define EMPTY { 0, 0, 0, 0 }
180 const relax_typeS md_relax_table
[C (END
, 0)] = {
181 EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
,
182 EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
,
185 /* C (COND_JUMP, COND8) */
186 { COND8_F
, COND8_M
, COND8_LENGTH
, C (COND_JUMP
, COND12
) },
187 /* C (COND_JUMP, COND12) */
188 { COND12_F
, COND12_M
, COND12_LENGTH
, C (COND_JUMP
, COND32
), },
189 /* C (COND_JUMP, COND32) */
190 { COND32_F
, COND32_M
, COND32_LENGTH
, 0, },
191 /* C (COND_JUMP, UNDEF_WORD_DISP) */
192 { 0, 0, COND32_LENGTH
, 0, },
194 EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
,
197 /* C (COND_JUMP_DELAY, COND8) */
198 { COND8_F
, COND8_M
, COND8_LENGTH
, C (COND_JUMP_DELAY
, COND12
) },
199 /* C (COND_JUMP_DELAY, COND12) */
200 { COND12_F
, COND12_M
, COND12_DELAY_LENGTH
, C (COND_JUMP_DELAY
, COND32
), },
201 /* C (COND_JUMP_DELAY, COND32) */
202 { COND32_F
, COND32_M
, COND32_LENGTH
, 0, },
203 /* C (COND_JUMP_DELAY, UNDEF_WORD_DISP) */
204 { 0, 0, COND32_LENGTH
, 0, },
206 EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
,
209 /* C (UNCOND_JUMP, UNCOND12) */
210 { UNCOND12_F
, UNCOND12_M
, UNCOND12_LENGTH
, C (UNCOND_JUMP
, UNCOND32
), },
211 /* C (UNCOND_JUMP, UNCOND32) */
212 { UNCOND32_F
, UNCOND32_M
, UNCOND32_LENGTH
, 0, },
214 /* C (UNCOND_JUMP, UNDEF_WORD_DISP) */
215 { 0, 0, UNCOND32_LENGTH
, 0, },
217 EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
, EMPTY
,
222 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
226 /* Parse @got, etc. and return the desired relocation.
227 If we have additional arithmetic expression, then we fill in new_exp_p. */
228 static bfd_reloc_code_real_type
229 sh_elf_suffix (str_p
, exp_p
, new_exp_p
)
231 expressionS
*exp_p
, *new_exp_p
;
236 bfd_reloc_code_real_type reloc
;
246 #define MAP(str,reloc) { str, sizeof (str)-1, reloc }
248 static struct map_bfd mapping
[] = {
249 MAP ("got", BFD_RELOC_32_GOT_PCREL
),
250 MAP ("plt", BFD_RELOC_32_PLT_PCREL
),
251 MAP ("gotoff", BFD_RELOC_32_GOTOFF
),
252 { (char *)0, 0, BFD_RELOC_UNUSED
}
256 return BFD_RELOC_UNUSED
;
258 for (ch
= *str
, str2
= ident
;
259 (str2
< ident
+ sizeof (ident
) - 1
260 && (isalnum (ch
) || ch
== '@'));
263 *str2
++ = (islower (ch
)) ? ch
: tolower (ch
);
270 for (ptr
= &mapping
[0]; ptr
->length
> 0; ptr
++)
271 if (ch
== ptr
->string
[0]
272 && len
== ptr
->length
273 && memcmp (ident
, ptr
->string
, ptr
->length
) == 0)
275 /* Now check for identifier@suffix+constant */
276 if (*str
== '-' || *str
== '+')
278 char *orig_line
= input_line_pointer
;
280 input_line_pointer
= str
;
281 expression (new_exp_p
);
282 if (new_exp_p
->X_op
== O_constant
)
284 exp_p
->X_add_number
+= new_exp_p
->X_add_number
;
285 str
= input_line_pointer
;
287 if (new_exp_p
->X_op
== O_subtract
)
288 str
= input_line_pointer
;
290 if (&input_line_pointer
!= str_p
)
291 input_line_pointer
= orig_line
;
298 return BFD_RELOC_UNUSED
;
301 /* The regular cons() function, that reads constants, doesn't support
302 suffixes such as @GOT, @GOTOFF and @PLT, that generate
303 machine-specific relocation types. So we must define it here. */
304 /* Clobbers input_line_pointer, checks end-of-line. */
307 register int nbytes
; /* 1=.byte, 2=.word, 4=.long */
309 expressionS exp
, new_exp
;
310 bfd_reloc_code_real_type reloc
;
313 if (is_it_end_of_statement ())
315 demand_empty_rest_of_line ();
322 new_exp
.X_op
= O_absent
;
323 new_exp
.X_add_symbol
= new_exp
.X_op_symbol
= NULL
;
324 /* If the _GLOBAL_OFFSET_TABLE_ symbol hasn't been found yet,
325 use the name of the symbol to tell whether it's the
326 _GLOBAL_OFFSET_TABLE_. If it has, comparing the symbols is
328 if (! GOT_symbol
&& exp
.X_add_symbol
)
329 name
= S_GET_NAME (exp
.X_add_symbol
);
332 /* Check whether this expression involves the
333 _GLOBAL_OFFSET_TABLE_ symbol, by itself or added to a
334 difference of two other symbols. */
335 if (((GOT_symbol
&& GOT_symbol
== exp
.X_add_symbol
)
336 || (! GOT_symbol
&& name
337 && strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0))
338 && (exp
.X_op
== O_symbol
339 || (exp
.X_op
== O_add
340 && ((symbol_get_value_expression (exp
.X_op_symbol
)->X_op
)
343 reloc_howto_type
*reloc_howto
= bfd_reloc_type_lookup (stdoutput
,
345 int size
= bfd_get_reloc_size (reloc_howto
);
347 if (GOT_symbol
== NULL
)
348 GOT_symbol
= symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME
);
351 as_bad (_("%s relocations do not fit in %d bytes\n"),
352 reloc_howto
->name
, nbytes
);
355 register char *p
= frag_more ((int) nbytes
);
356 int offset
= nbytes
- size
;
358 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
+ offset
,
359 size
, &exp
, 0, TC_RELOC_GLOBAL_OFFSET_TABLE
);
362 /* Check if this symbol involves one of the magic suffixes, such
363 as @GOT, @GOTOFF or @PLT, and determine which relocation type
365 else if ((exp
.X_op
== O_symbol
|| (exp
.X_op
== O_add
&& exp
.X_op_symbol
))
366 && *input_line_pointer
== '@'
367 && ((reloc
= sh_elf_suffix (&input_line_pointer
, &exp
, &new_exp
))
368 != BFD_RELOC_UNUSED
))
370 reloc_howto_type
*reloc_howto
= bfd_reloc_type_lookup (stdoutput
,
372 int size
= bfd_get_reloc_size (reloc_howto
);
374 /* Force a GOT to be generated. */
375 if (GOT_symbol
== NULL
)
376 GOT_symbol
= symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME
);
379 as_bad (_("%s relocations do not fit in %d bytes\n"),
380 reloc_howto
->name
, nbytes
);
383 register char *p
= frag_more ((int) nbytes
);
384 int offset
= nbytes
- size
;
386 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
+ offset
, size
,
388 if (new_exp
.X_op
!= O_absent
)
389 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
+ offset
, size
,
390 &new_exp
, 0, BFD_RELOC_32
);
394 emit_expr (&exp
, (unsigned int) nbytes
);
396 while (*input_line_pointer
++ == ',');
398 input_line_pointer
--; /* Put terminator back into stream. */
399 if (*input_line_pointer
== '#' || *input_line_pointer
== '!')
401 while (! is_end_of_line
[(unsigned char) *input_line_pointer
++]);
404 demand_empty_rest_of_line ();
409 /* This function is called once, at assembler startup time. This should
410 set up all the tables, etc that the MD part of the assembler needs. */
415 sh_opcode_info
*opcode
;
416 char *prev_name
= "";
420 /* The WinCE OS only supports little endian executables. */
421 target_big_endian
= 0;
424 target_big_endian
= 1;
427 target_arch
= arch_sh1_up
& ~(sh_dsp
? arch_sh3e_up
: arch_sh_dsp_up
);
428 valid_arch
= target_arch
;
430 opcode_hash_control
= hash_new ();
432 /* Insert unique names into hash table. */
433 for (opcode
= sh_table
; opcode
->name
; opcode
++)
435 if (strcmp (prev_name
, opcode
->name
))
437 if (! (opcode
->arch
& target_arch
))
439 prev_name
= opcode
->name
;
440 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
444 /* Make all the opcodes with the same name point to the same
446 opcode
->name
= prev_name
;
453 static int reg_x
, reg_y
;
461 expressionS immediate
;
465 #define IDENT_CHAR(c) (isalnum (c) || (c) == '_')
467 /* Try to parse a reg name. Return the number of chars consumed. */
470 parse_reg (src
, mode
, reg
)
475 char l0
= tolower (src
[0]);
476 char l1
= l0
? tolower (src
[1]) : 0;
478 /* We use ! IDENT_CHAR for the next character after the register name, to
479 make sure that we won't accidentally recognize a symbol name such as
480 'sram' or sr_ram as being a reference to the register 'sr'. */
486 if (src
[2] >= '0' && src
[2] <= '5'
487 && ! IDENT_CHAR ((unsigned char) src
[3]))
490 *reg
= 10 + src
[2] - '0';
494 if (l1
>= '0' && l1
<= '9'
495 && ! IDENT_CHAR ((unsigned char) src
[2]))
501 if (l1
>= '0' && l1
<= '7' && strncasecmp (&src
[2], "_bank", 5) == 0
502 && ! IDENT_CHAR ((unsigned char) src
[7]))
509 if (l1
== 'e' && ! IDENT_CHAR ((unsigned char) src
[2]))
514 if (l1
== 's' && ! IDENT_CHAR ((unsigned char) src
[2]))
525 if (! IDENT_CHAR ((unsigned char) src
[2]))
531 if (tolower (src
[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src
[3]))
540 if (! IDENT_CHAR ((unsigned char) src
[2]))
546 if (tolower (src
[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src
[3]))
554 if (l1
== 'x' && src
[2] >= '0' && src
[2] <= '1'
555 && ! IDENT_CHAR ((unsigned char) src
[3]))
558 *reg
= 4 + (l1
- '0');
561 if (l1
== 'y' && src
[2] >= '0' && src
[2] <= '1'
562 && ! IDENT_CHAR ((unsigned char) src
[3]))
565 *reg
= 6 + (l1
- '0');
568 if (l1
== 's' && src
[2] >= '0' && src
[2] <= '3'
569 && ! IDENT_CHAR ((unsigned char) src
[3]))
574 *reg
= n
| ((~n
& 2) << 1);
579 if (l0
== 'i' && l1
&& ! IDENT_CHAR ((unsigned char) src
[3]))
601 if (l0
== 'x' && l1
>= '0' && l1
<= '1'
602 && ! IDENT_CHAR ((unsigned char) src
[2]))
605 *reg
= A_X0_NUM
+ l1
- '0';
609 if (l0
== 'y' && l1
>= '0' && l1
<= '1'
610 && ! IDENT_CHAR ((unsigned char) src
[2]))
613 *reg
= A_Y0_NUM
+ l1
- '0';
617 if (l0
== 'm' && l1
>= '0' && l1
<= '1'
618 && ! IDENT_CHAR ((unsigned char) src
[2]))
621 *reg
= l1
== '0' ? A_M0_NUM
: A_M1_NUM
;
627 && tolower (src
[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src
[3]))
633 if (l0
== 's' && l1
== 'p' && tolower (src
[2]) == 'c'
634 && ! IDENT_CHAR ((unsigned char) src
[3]))
640 if (l0
== 's' && l1
== 'g' && tolower (src
[2]) == 'r'
641 && ! IDENT_CHAR ((unsigned char) src
[3]))
647 if (l0
== 'd' && l1
== 's' && tolower (src
[2]) == 'r'
648 && ! IDENT_CHAR ((unsigned char) src
[3]))
654 if (l0
== 'd' && l1
== 'b' && tolower (src
[2]) == 'r'
655 && ! IDENT_CHAR ((unsigned char) src
[3]))
661 if (l0
== 's' && l1
== 'r' && ! IDENT_CHAR ((unsigned char) src
[2]))
667 if (l0
== 's' && l1
== 'p' && ! IDENT_CHAR ((unsigned char) src
[2]))
674 if (l0
== 'p' && l1
== 'r' && ! IDENT_CHAR ((unsigned char) src
[2]))
679 if (l0
== 'p' && l1
== 'c' && ! IDENT_CHAR ((unsigned char) src
[2]))
681 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
682 and use an uninitialized immediate. */
686 if (l0
== 'g' && l1
== 'b' && tolower (src
[2]) == 'r'
687 && ! IDENT_CHAR ((unsigned char) src
[3]))
692 if (l0
== 'v' && l1
== 'b' && tolower (src
[2]) == 'r'
693 && ! IDENT_CHAR ((unsigned char) src
[3]))
699 if (l0
== 'm' && l1
== 'a' && tolower (src
[2]) == 'c'
700 && ! IDENT_CHAR ((unsigned char) src
[4]))
702 if (tolower (src
[3]) == 'l')
707 if (tolower (src
[3]) == 'h')
713 if (l0
== 'm' && l1
== 'o' && tolower (src
[2]) == 'd'
714 && ! IDENT_CHAR ((unsigned char) src
[4]))
719 if (l0
== 'f' && l1
== 'r')
723 if (src
[3] >= '0' && src
[3] <= '5'
724 && ! IDENT_CHAR ((unsigned char) src
[4]))
727 *reg
= 10 + src
[3] - '0';
731 if (src
[2] >= '0' && src
[2] <= '9'
732 && ! IDENT_CHAR ((unsigned char) src
[3]))
735 *reg
= (src
[2] - '0');
739 if (l0
== 'd' && l1
== 'r')
743 if (src
[3] >= '0' && src
[3] <= '4' && ! ((src
[3] - '0') & 1)
744 && ! IDENT_CHAR ((unsigned char) src
[4]))
747 *reg
= 10 + src
[3] - '0';
751 if (src
[2] >= '0' && src
[2] <= '8' && ! ((src
[2] - '0') & 1)
752 && ! IDENT_CHAR ((unsigned char) src
[3]))
755 *reg
= (src
[2] - '0');
759 if (l0
== 'x' && l1
== 'd')
763 if (src
[3] >= '0' && src
[3] <= '4' && ! ((src
[3] - '0') & 1)
764 && ! IDENT_CHAR ((unsigned char) src
[4]))
767 *reg
= 11 + src
[3] - '0';
771 if (src
[2] >= '0' && src
[2] <= '8' && ! ((src
[2] - '0') & 1)
772 && ! IDENT_CHAR ((unsigned char) src
[3]))
775 *reg
= (src
[2] - '0') + 1;
779 if (l0
== 'f' && l1
== 'v')
781 if (src
[2] == '1'&& src
[3] == '2' && ! IDENT_CHAR ((unsigned char) src
[4]))
787 if ((src
[2] == '0' || src
[2] == '4' || src
[2] == '8')
788 && ! IDENT_CHAR ((unsigned char) src
[3]))
791 *reg
= (src
[2] - '0');
795 if (l0
== 'f' && l1
== 'p' && tolower (src
[2]) == 'u'
796 && tolower (src
[3]) == 'l'
797 && ! IDENT_CHAR ((unsigned char) src
[4]))
803 if (l0
== 'f' && l1
== 'p' && tolower (src
[2]) == 's'
804 && tolower (src
[3]) == 'c'
805 && tolower (src
[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src
[5]))
811 if (l0
== 'x' && l1
== 'm' && tolower (src
[2]) == 't'
812 && tolower (src
[3]) == 'r'
813 && tolower (src
[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src
[5]))
827 /* JF: '.' is pseudo symbol with value of current location
828 in current segment. */
829 fake
= FAKE_LABEL_NAME
;
830 return symbol_new (fake
,
832 (valueT
) frag_now_fix (),
844 save
= input_line_pointer
;
845 input_line_pointer
= s
;
846 expression (&op
->immediate
);
847 if (op
->immediate
.X_op
== O_absent
)
848 as_bad (_("missing operand"));
849 new = input_line_pointer
;
850 input_line_pointer
= save
;
854 /* The many forms of operand:
857 @Rn Register indirect
870 pr, gbr, vbr, macl, mach
883 /* Must be predecrement. */
886 len
= parse_reg (src
, &mode
, &(op
->reg
));
888 as_bad (_("illegal register after @-"));
893 else if (src
[0] == '(')
895 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
898 len
= parse_reg (src
, &mode
, &(op
->reg
));
899 if (len
&& mode
== A_REG_N
)
904 as_bad (_("must be @(r0,...)"));
908 /* Now can be rn or gbr */
909 len
= parse_reg (src
, &mode
, &(op
->reg
));
914 else if (mode
== A_REG_N
)
916 op
->type
= A_IND_R0_REG_N
;
920 as_bad (_("syntax error in @(r0,...)"));
925 /* Must be an @(disp,.. thing) */
926 src
= parse_exp (src
, op
);
929 /* Now can be rn, gbr or pc */
930 len
= parse_reg (src
, &mode
, &op
->reg
);
935 op
->type
= A_DISP_REG_N
;
937 else if (mode
== A_GBR
)
939 op
->type
= A_DISP_GBR
;
941 else if (mode
== A_PC
)
943 /* Turn a plain @(4,pc) into @(.+4,pc). */
944 if (op
->immediate
.X_op
== O_constant
)
946 op
->immediate
.X_add_symbol
= dot();
947 op
->immediate
.X_op
= O_symbol
;
949 op
->type
= A_DISP_PC
;
953 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
958 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
963 as_bad (_("expecting )"));
969 src
+= parse_reg (src
, &mode
, &(op
->reg
));
972 as_bad (_("illegal register after @"));
977 if ((src
[0] == 'r' && src
[1] == '8')
978 || (src
[0] == 'i' && (src
[1] == 'x' || src
[1] == 's')))
983 if ((src
[0] == 'r' && src
[1] == '9')
984 || (src
[0] == 'i' && src
[1] == 'y'))
987 op
->type
= A_PMODY_N
;
1001 get_operand (ptr
, op
)
1003 sh_operand_info
*op
;
1012 *ptr
= parse_exp (src
, op
);
1017 else if (src
[0] == '@')
1019 *ptr
= parse_at (src
, op
);
1022 len
= parse_reg (src
, &mode
, &(op
->reg
));
1031 /* Not a reg, the only thing left is a displacement. */
1032 *ptr
= parse_exp (src
, op
);
1033 op
->type
= A_DISP_PC
;
1039 get_operands (info
, args
, operand
)
1040 sh_opcode_info
*info
;
1042 sh_operand_info
*operand
;
1047 /* The pre-processor will eliminate whitespace in front of '@'
1048 after the first argument; we may be called multiple times
1049 from assemble_ppi, so don't insist on finding whitespace here. */
1053 get_operand (&ptr
, operand
+ 0);
1060 get_operand (&ptr
, operand
+ 1);
1061 /* ??? Hack: psha/pshl have a varying operand number depending on
1062 the type of the first operand. We handle this by having the
1063 three-operand version first and reducing the number of operands
1064 parsed to two if we see that the first operand is an immediate.
1065 This works because no insn with three operands has an immediate
1066 as first operand. */
1067 if (info
->arg
[2] && operand
[0].type
!= A_IMM
)
1073 get_operand (&ptr
, operand
+ 2);
1077 operand
[2].type
= 0;
1082 operand
[1].type
= 0;
1083 operand
[2].type
= 0;
1088 operand
[0].type
= 0;
1089 operand
[1].type
= 0;
1090 operand
[2].type
= 0;
1095 /* Passed a pointer to a list of opcodes which use different
1096 addressing modes, return the opcode which matches the opcodes
1099 static sh_opcode_info
*
1100 get_specific (opcode
, operands
)
1101 sh_opcode_info
*opcode
;
1102 sh_operand_info
*operands
;
1104 sh_opcode_info
*this_try
= opcode
;
1105 char *name
= opcode
->name
;
1108 while (opcode
->name
)
1110 this_try
= opcode
++;
1111 if (this_try
->name
!= name
)
1113 /* We've looked so far down the table that we've run out of
1114 opcodes with the same name. */
1118 /* Look at both operands needed by the opcodes and provided by
1119 the user - since an arg test will often fail on the same arg
1120 again and again, we'll try and test the last failing arg the
1121 first on each opcode try. */
1122 for (n
= 0; this_try
->arg
[n
]; n
++)
1124 sh_operand_info
*user
= operands
+ n
;
1125 sh_arg_type arg
= this_try
->arg
[n
];
1137 if (user
->type
!= arg
)
1141 /* opcode needs r0 */
1142 if (user
->type
!= A_REG_N
|| user
->reg
!= 0)
1146 if (user
->type
!= A_R0_GBR
|| user
->reg
!= 0)
1150 if (user
->type
!= F_REG_N
|| user
->reg
!= 0)
1158 case A_IND_R0_REG_N
:
1169 /* Opcode needs rn */
1170 if (user
->type
!= arg
)
1175 if (user
->type
!= D_REG_N
&& user
->type
!= X_REG_N
)
1190 if (user
->type
!= arg
)
1195 if (user
->type
!= arg
)
1204 case A_IND_R0_REG_M
:
1207 /* Opcode needs rn */
1208 if (user
->type
!= arg
- A_REG_M
+ A_REG_N
)
1214 if (user
->type
!= DSP_REG_N
)
1236 if (user
->type
!= DSP_REG_N
)
1258 if (user
->type
!= DSP_REG_N
)
1280 if (user
->type
!= DSP_REG_N
)
1302 if (user
->type
!= DSP_REG_N
)
1324 if (user
->type
!= DSP_REG_N
|| user
->reg
!= A_A0_NUM
)
1328 if (user
->type
!= DSP_REG_N
|| user
->reg
!= A_X0_NUM
)
1332 if (user
->type
!= DSP_REG_N
|| user
->reg
!= A_X1_NUM
)
1336 if (user
->type
!= DSP_REG_N
|| user
->reg
!= A_Y0_NUM
)
1340 if (user
->type
!= DSP_REG_N
|| user
->reg
!= A_Y1_NUM
)
1350 /* Opcode needs rn */
1351 if (user
->type
!= arg
- F_REG_M
+ F_REG_N
)
1356 if (user
->type
!= D_REG_N
&& user
->type
!= X_REG_N
)
1361 if (user
->type
!= XMTRX_M4
)
1367 printf (_("unhandled %d\n"), arg
);
1371 if ( !(valid_arch
& this_try
->arch
))
1373 valid_arch
&= this_try
->arch
;
1383 check (operand
, low
, high
)
1384 expressionS
*operand
;
1388 if (operand
->X_op
!= O_constant
1389 || operand
->X_add_number
< low
1390 || operand
->X_add_number
> high
)
1392 as_bad (_("operand must be absolute in range %d..%d"), low
, high
);
1394 return operand
->X_add_number
;
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
;
1506 char *output
= frag_more (2);
1507 unsigned int size
= 2;
1508 int low_byte
= target_big_endian
? 1 : 0;
1514 for (index
= 0; index
< 4; index
++)
1516 sh_nibble_type i
= opcode
->nibbles
[index
];
1526 nbuf
[index
] = reg_n
;
1529 nbuf
[index
] = reg_m
;
1532 if (reg_n
< 2 || reg_n
> 5)
1533 as_bad (_("Invalid register: 'r%d'"), reg_n
);
1534 nbuf
[index
] = (reg_n
& 3) | 4;
1537 nbuf
[index
] = reg_n
| (reg_m
>> 2);
1540 nbuf
[index
] = reg_b
| 0x08;
1543 insert (output
+ low_byte
, BFD_RELOC_SH_IMM4BY4
, 0, operand
);
1546 insert (output
+ low_byte
, BFD_RELOC_SH_IMM4BY2
, 0, operand
);
1549 insert (output
+ low_byte
, BFD_RELOC_SH_IMM4
, 0, operand
);
1552 insert (output
+ low_byte
, BFD_RELOC_SH_IMM4BY4
, 0, operand
+ 1);
1555 insert (output
+ low_byte
, BFD_RELOC_SH_IMM4BY2
, 0, operand
+ 1);
1558 insert (output
+ low_byte
, BFD_RELOC_SH_IMM4
, 0, operand
+ 1);
1561 insert (output
+ low_byte
, BFD_RELOC_SH_IMM8BY4
, 0, operand
);
1564 insert (output
+ low_byte
, BFD_RELOC_SH_IMM8BY2
, 0, operand
);
1567 insert (output
+ low_byte
, BFD_RELOC_SH_IMM8
, 0, operand
);
1570 insert (output
+ low_byte
, BFD_RELOC_SH_IMM8BY4
, 0, operand
+ 1);
1573 insert (output
+ low_byte
, BFD_RELOC_SH_IMM8BY2
, 0, operand
+ 1);
1576 insert (output
+ low_byte
, BFD_RELOC_SH_IMM8
, 0, operand
+ 1);
1579 insert (output
, BFD_RELOC_SH_PCRELIMM8BY4
, 1, operand
);
1582 insert (output
, BFD_RELOC_SH_PCRELIMM8BY2
, 1, operand
);
1585 output
= insert_loop_bounds (output
, operand
);
1586 nbuf
[index
] = opcode
->nibbles
[3];
1590 printf (_("failed for %d\n"), i
);
1594 if (!target_big_endian
)
1596 output
[1] = (nbuf
[0] << 4) | (nbuf
[1]);
1597 output
[0] = (nbuf
[2] << 4) | (nbuf
[3]);
1601 output
[0] = (nbuf
[0] << 4) | (nbuf
[1]);
1602 output
[1] = (nbuf
[2] << 4) | (nbuf
[3]);
1607 /* Find an opcode at the start of *STR_P in the hash table, and set
1608 *STR_P to the first character after the last one read. */
1610 static sh_opcode_info
*
1611 find_cooked_opcode (str_p
)
1615 unsigned char *op_start
;
1616 unsigned char *op_end
;
1620 /* Drop leading whitespace. */
1624 /* Find the op code end.
1625 The pre-processor will eliminate whitespace in front of
1626 any '@' after the first argument; we may be called from
1627 assemble_ppi, so the opcode might be terminated by an '@'. */
1628 for (op_start
= op_end
= (unsigned char *) (str
);
1631 && !is_end_of_line
[*op_end
] && *op_end
!= ' ' && *op_end
!= '@';
1634 unsigned char c
= op_start
[nlen
];
1636 /* The machine independent code will convert CMP/EQ into cmp/EQ
1637 because it thinks the '/' is the end of the symbol. Moreover,
1638 all but the first sub-insn is a parallel processing insn won't
1639 be capitailzed. Instead of hacking up the machine independent
1640 code, we just deal with it here. */
1641 c
= isupper (c
) ? tolower (c
) : c
;
1650 as_bad (_("can't find opcode "));
1652 return (sh_opcode_info
*) hash_find (opcode_hash_control
, name
);
1655 /* Assemble a parallel processing insn. */
1656 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
1659 assemble_ppi (op_end
, opcode
)
1661 sh_opcode_info
*opcode
;
1671 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
1672 Make sure we encode a defined insn pattern. */
1678 sh_operand_info operand
[3];
1680 if (opcode
->arg
[0] != A_END
)
1681 op_end
= get_operands (opcode
, op_end
, operand
);
1682 opcode
= get_specific (opcode
, operand
);
1685 /* Couldn't find an opcode which matched the operands. */
1686 char *where
= frag_more (2);
1691 as_bad (_("invalid operands for opcode"));
1695 if (opcode
->nibbles
[0] != PPI
)
1696 as_bad (_("insn can't be combined with parallel processing insn"));
1698 switch (opcode
->nibbles
[1])
1703 as_bad (_("multiple movx specifications"));
1708 as_bad (_("multiple movy specifications"));
1714 as_bad (_("multiple movx specifications"));
1715 if (reg_n
< 4 || reg_n
> 5)
1716 as_bad (_("invalid movx address register"));
1717 if (opcode
->nibbles
[2] & 8)
1719 if (reg_m
== A_A1_NUM
)
1721 else if (reg_m
!= A_A0_NUM
)
1722 as_bad (_("invalid movx dsp register"));
1727 as_bad (_("invalid movx dsp register"));
1730 movx
+= ((reg_n
- 4) << 9) + (opcode
->nibbles
[2] << 2) + DDT_BASE
;
1735 as_bad (_("multiple movy specifications"));
1736 if (opcode
->nibbles
[2] & 8)
1738 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
1741 if (reg_m
== A_A1_NUM
)
1743 else if (reg_m
!= A_A0_NUM
)
1744 as_bad (_("invalid movy dsp register"));
1749 as_bad (_("invalid movy dsp register"));
1752 if (reg_n
< 6 || reg_n
> 7)
1753 as_bad (_("invalid movy address register"));
1754 movy
+= ((reg_n
- 6) << 8) + opcode
->nibbles
[2] + DDT_BASE
;
1758 if (operand
[0].immediate
.X_op
!= O_constant
)
1759 as_bad (_("dsp immediate shift value not constant"));
1760 field_b
= ((opcode
->nibbles
[2] << 12)
1761 | (operand
[0].immediate
.X_add_number
& 127) << 4
1766 as_bad (_("multiple parallel processing specifications"));
1767 field_b
= ((opcode
->nibbles
[2] << 12) + (opcode
->nibbles
[3] << 8)
1768 + (reg_x
<< 6) + (reg_y
<< 4) + reg_n
);
1772 as_bad (_("multiple condition specifications"));
1773 cond
= opcode
->nibbles
[2] << 8;
1775 goto skip_cond_check
;
1779 as_bad (_("multiple parallel processing specifications"));
1780 field_b
= ((opcode
->nibbles
[2] << 12) + (opcode
->nibbles
[3] << 8)
1781 + cond
+ (reg_x
<< 6) + (reg_y
<< 4) + reg_n
);
1787 if ((field_b
& 0xef00) != 0xa100)
1788 as_bad (_("insn cannot be combined with pmuls"));
1790 switch (field_b
& 0xf)
1793 field_b
+= 0 - A_X0_NUM
;
1796 field_b
+= 1 - A_Y0_NUM
;
1799 field_b
+= 2 - A_A0_NUM
;
1802 field_b
+= 3 - A_A1_NUM
;
1805 as_bad (_("bad padd / psub pmuls output operand"));
1808 field_b
+= 0x4000 + reg_efg
;
1815 as_bad (_("condition not followed by conditionalizable insn"));
1821 opcode
= find_cooked_opcode (&op_end
);
1825 (_("unrecognized characters at end of parallel processing insn")));
1830 move_code
= movx
| movy
;
1833 /* Parallel processing insn. */
1834 unsigned long ppi_code
= (movx
| movy
| 0xf800) << 16 | field_b
;
1836 output
= frag_more (4);
1838 if (! target_big_endian
)
1840 output
[3] = ppi_code
>> 8;
1841 output
[2] = ppi_code
;
1845 output
[2] = ppi_code
>> 8;
1846 output
[3] = ppi_code
;
1848 move_code
|= 0xf800;
1852 /* Just a double data transfer. */
1853 output
= frag_more (2);
1856 if (! target_big_endian
)
1858 output
[1] = move_code
>> 8;
1859 output
[0] = move_code
;
1863 output
[0] = move_code
>> 8;
1864 output
[1] = move_code
;
1869 /* This is the guts of the machine-dependent assembler. STR points to a
1870 machine dependent instruction. This function is supposed to emit
1871 the frags/bytes it assembles to. */
1877 unsigned char *op_end
;
1878 sh_operand_info operand
[3];
1879 sh_opcode_info
*opcode
;
1880 unsigned int size
= 0;
1882 opcode
= find_cooked_opcode (&str
);
1887 as_bad (_("unknown opcode"));
1892 && ! seg_info (now_seg
)->tc_segment_info_data
.in_code
)
1894 /* Output a CODE reloc to tell the linker that the following
1895 bytes are instructions, not data. */
1896 fix_new (frag_now
, frag_now_fix (), 2, &abs_symbol
, 0, 0,
1898 seg_info (now_seg
)->tc_segment_info_data
.in_code
= 1;
1901 if (opcode
->nibbles
[0] == PPI
)
1903 size
= assemble_ppi (op_end
, opcode
);
1907 if (opcode
->arg
[0] == A_BDISP12
1908 || opcode
->arg
[0] == A_BDISP8
)
1910 parse_exp (op_end
+ 1, &operand
[0]);
1911 build_relax (opcode
, &operand
[0]);
1915 if (opcode
->arg
[0] == A_END
)
1917 /* Ignore trailing whitespace. If there is any, it has already
1918 been compressed to a single space. */
1924 op_end
= get_operands (opcode
, op_end
, operand
);
1926 opcode
= get_specific (opcode
, operand
);
1930 /* Couldn't find an opcode which matched the operands. */
1931 char *where
= frag_more (2);
1936 as_bad (_("invalid operands for opcode"));
1941 as_bad (_("excess operands: '%s'"), op_end
);
1943 size
= build_Mytes (opcode
, operand
);
1948 dwarf2_emit_insn (size
);
1951 /* This routine is called each time a label definition is seen. It
1952 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
1957 static fragS
*last_label_frag
;
1958 static int last_label_offset
;
1961 && seg_info (now_seg
)->tc_segment_info_data
.in_code
)
1965 offset
= frag_now_fix ();
1966 if (frag_now
!= last_label_frag
1967 || offset
!= last_label_offset
)
1969 fix_new (frag_now
, offset
, 2, &abs_symbol
, 0, 0, BFD_RELOC_SH_LABEL
);
1970 last_label_frag
= frag_now
;
1971 last_label_offset
= offset
;
1976 /* This routine is called when the assembler is about to output some
1977 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
1980 sh_flush_pending_output ()
1983 && seg_info (now_seg
)->tc_segment_info_data
.in_code
)
1985 fix_new (frag_now
, frag_now_fix (), 2, &abs_symbol
, 0, 0,
1987 seg_info (now_seg
)->tc_segment_info_data
.in_code
= 0;
1992 md_undefined_symbol (name
)
1996 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE. Otherwise we
1997 have no need to default values of symbols. */
1998 if (strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0)
2002 if (symbol_find (name
))
2003 as_bad ("GOT already in the symbol table");
2005 GOT_symbol
= symbol_new (name
, undefined_section
,
2006 (valueT
)0, & zero_address_frag
);
2011 #endif /* OBJ_ELF */
2017 #ifndef BFD_ASSEMBLER
2020 tc_crawl_symbol_chain (headers
)
2021 object_headers
*headers
;
2023 printf (_("call to tc_crawl_symbol_chain \n"));
2027 tc_headers_hook (headers
)
2028 object_headers
*headers
;
2030 printf (_("call to tc_headers_hook \n"));
2036 /* Various routines to kill one day. */
2037 /* Equal to MAX_PRECISION in atof-ieee.c. */
2038 #define MAX_LITTLENUMS 6
2040 /* Turn a string in input_line_pointer into a floating point constant
2041 of type TYPE, and store the appropriate bytes in *LITP. The number
2042 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2043 returned, or NULL on OK. */
2046 md_atof (type
, litP
, sizeP
)
2052 LITTLENUM_TYPE words
[4];
2068 return _("bad call to md_atof");
2071 t
= atof_ieee (input_line_pointer
, type
, words
);
2073 input_line_pointer
= t
;
2077 if (! target_big_endian
)
2079 for (i
= prec
- 1; i
>= 0; i
--)
2081 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
2087 for (i
= 0; i
< prec
; i
++)
2089 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
2097 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
2098 call instruction. It refers to a label of the instruction which
2099 loads the register which the call uses. We use it to generate a
2100 special reloc for the linker. */
2104 int ignore ATTRIBUTE_UNUSED
;
2109 as_warn (_(".uses pseudo-op seen when not relaxing"));
2113 if (ex
.X_op
!= O_symbol
|| ex
.X_add_number
!= 0)
2115 as_bad (_("bad .uses format"));
2116 ignore_rest_of_line ();
2120 fix_new_exp (frag_now
, frag_now_fix (), 2, &ex
, 1, BFD_RELOC_SH_USES
);
2122 demand_empty_rest_of_line ();
2125 CONST
char *md_shortopts
= "";
2126 struct option md_longopts
[] =
2128 #define OPTION_RELAX (OPTION_MD_BASE)
2129 #define OPTION_LITTLE (OPTION_MD_BASE + 1)
2130 #define OPTION_SMALL (OPTION_LITTLE + 1)
2131 #define OPTION_DSP (OPTION_SMALL + 1)
2133 {"relax", no_argument
, NULL
, OPTION_RELAX
},
2134 {"little", no_argument
, NULL
, OPTION_LITTLE
},
2135 {"small", no_argument
, NULL
, OPTION_SMALL
},
2136 {"dsp", no_argument
, NULL
, OPTION_DSP
},
2137 {NULL
, no_argument
, NULL
, 0}
2139 size_t md_longopts_size
= sizeof (md_longopts
);
2142 md_parse_option (c
, arg
)
2144 char *arg ATTRIBUTE_UNUSED
;
2154 target_big_endian
= 0;
2173 md_show_usage (stream
)
2176 fprintf (stream
, _("\
2178 -little generate little endian code\n\
2179 -relax alter jump instructions for long displacements\n\
2180 -small align sections to 4 byte boundaries, not 16\n\
2181 -dsp enable sh-dsp insns, and disable sh3e / sh4 insns.\n"));
2185 tc_Nout_fix_to_chars ()
2187 printf (_("call to tc_Nout_fix_to_chars \n"));
2191 /* This struct is used to pass arguments to sh_count_relocs through
2192 bfd_map_over_sections. */
2194 struct sh_count_relocs
2196 /* Symbol we are looking for. */
2198 /* Count of relocs found. */
2202 /* Count the number of fixups in a section which refer to a particular
2203 symbol. When using BFD_ASSEMBLER, this is called via
2204 bfd_map_over_sections. */
2207 sh_count_relocs (abfd
, sec
, data
)
2208 bfd
*abfd ATTRIBUTE_UNUSED
;
2212 struct sh_count_relocs
*info
= (struct sh_count_relocs
*) data
;
2213 segment_info_type
*seginfo
;
2217 seginfo
= seg_info (sec
);
2218 if (seginfo
== NULL
)
2222 for (fix
= seginfo
->fix_root
; fix
!= NULL
; fix
= fix
->fx_next
)
2224 if (fix
->fx_addsy
== sym
)
2232 /* Handle the count relocs for a particular section. When using
2233 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
2236 sh_frob_section (abfd
, sec
, ignore
)
2237 bfd
*abfd ATTRIBUTE_UNUSED
;
2239 PTR ignore ATTRIBUTE_UNUSED
;
2241 segment_info_type
*seginfo
;
2244 seginfo
= seg_info (sec
);
2245 if (seginfo
== NULL
)
2248 for (fix
= seginfo
->fix_root
; fix
!= NULL
; fix
= fix
->fx_next
)
2253 struct sh_count_relocs info
;
2255 if (fix
->fx_r_type
!= BFD_RELOC_SH_USES
)
2258 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2259 symbol in the same section. */
2260 sym
= fix
->fx_addsy
;
2262 || fix
->fx_subsy
!= NULL
2263 || fix
->fx_addnumber
!= 0
2264 || S_GET_SEGMENT (sym
) != sec
2265 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2266 || S_GET_STORAGE_CLASS (sym
) == C_EXT
2268 || S_IS_EXTERNAL (sym
))
2270 as_warn_where (fix
->fx_file
, fix
->fx_line
,
2271 _(".uses does not refer to a local symbol in the same section"));
2275 /* Look through the fixups again, this time looking for one
2276 at the same location as sym. */
2277 val
= S_GET_VALUE (sym
);
2278 for (fscan
= seginfo
->fix_root
;
2280 fscan
= fscan
->fx_next
)
2281 if (val
== fscan
->fx_frag
->fr_address
+ fscan
->fx_where
2282 && fscan
->fx_r_type
!= BFD_RELOC_SH_ALIGN
2283 && fscan
->fx_r_type
!= BFD_RELOC_SH_CODE
2284 && fscan
->fx_r_type
!= BFD_RELOC_SH_DATA
2285 && fscan
->fx_r_type
!= BFD_RELOC_SH_LABEL
)
2289 as_warn_where (fix
->fx_file
, fix
->fx_line
,
2290 _("can't find fixup pointed to by .uses"));
2294 if (fscan
->fx_tcbit
)
2296 /* We've already done this one. */
2300 /* The variable fscan should also be a fixup to a local symbol
2301 in the same section. */
2302 sym
= fscan
->fx_addsy
;
2304 || fscan
->fx_subsy
!= NULL
2305 || fscan
->fx_addnumber
!= 0
2306 || S_GET_SEGMENT (sym
) != sec
2307 #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
2308 || S_GET_STORAGE_CLASS (sym
) == C_EXT
2310 || S_IS_EXTERNAL (sym
))
2312 as_warn_where (fix
->fx_file
, fix
->fx_line
,
2313 _(".uses target does not refer to a local symbol in the same section"));
2317 /* Now we look through all the fixups of all the sections,
2318 counting the number of times we find a reference to sym. */
2321 #ifdef BFD_ASSEMBLER
2322 bfd_map_over_sections (stdoutput
, sh_count_relocs
, (PTR
) &info
);
2327 for (iscan
= SEG_E0
; iscan
< SEG_UNKNOWN
; iscan
++)
2328 sh_count_relocs ((bfd
*) NULL
, iscan
, (PTR
) &info
);
2335 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
2336 We have already adjusted the value of sym to include the
2337 fragment address, so we undo that adjustment here. */
2338 subseg_change (sec
, 0);
2339 fix_new (symbol_get_frag (sym
),
2340 S_GET_VALUE (sym
) - symbol_get_frag (sym
)->fr_address
,
2341 4, &abs_symbol
, info
.count
, 0, BFD_RELOC_SH_COUNT
);
2345 /* This function is called after the symbol table has been completed,
2346 but before the relocs or section contents have been written out.
2347 If we have seen any .uses pseudo-ops, they point to an instruction
2348 which loads a register with the address of a function. We look
2349 through the fixups to find where the function address is being
2350 loaded from. We then generate a COUNT reloc giving the number of
2351 times that function address is referred to. The linker uses this
2352 information when doing relaxing, to decide when it can eliminate
2353 the stored function address entirely. */
2361 #ifdef BFD_ASSEMBLER
2362 bfd_map_over_sections (stdoutput
, sh_frob_section
, (PTR
) NULL
);
2367 for (iseg
= SEG_E0
; iseg
< SEG_UNKNOWN
; iseg
++)
2368 sh_frob_section ((bfd
*) NULL
, iseg
, (PTR
) NULL
);
2373 /* Called after relaxing. Set the correct sizes of the fragments, and
2374 create relocs so that md_apply_fix will fill in the correct values. */
2377 md_convert_frag (headers
, seg
, fragP
)
2378 #ifdef BFD_ASSEMBLER
2379 bfd
*headers ATTRIBUTE_UNUSED
;
2381 object_headers
*headers
;
2388 switch (fragP
->fr_subtype
)
2390 case C (COND_JUMP
, COND8
):
2391 case C (COND_JUMP_DELAY
, COND8
):
2392 subseg_change (seg
, 0);
2393 fix_new (fragP
, fragP
->fr_fix
, 2, fragP
->fr_symbol
, fragP
->fr_offset
,
2394 1, BFD_RELOC_SH_PCDISP8BY2
);
2399 case C (UNCOND_JUMP
, UNCOND12
):
2400 subseg_change (seg
, 0);
2401 fix_new (fragP
, fragP
->fr_fix
, 2, fragP
->fr_symbol
, fragP
->fr_offset
,
2402 1, BFD_RELOC_SH_PCDISP12BY2
);
2407 case C (UNCOND_JUMP
, UNCOND32
):
2408 case C (UNCOND_JUMP
, UNDEF_WORD_DISP
):
2409 if (fragP
->fr_symbol
== NULL
)
2410 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2411 _("displacement overflows 12-bit field"));
2412 else if (S_IS_DEFINED (fragP
->fr_symbol
))
2413 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2414 _("displacement to defined symbol %s overflows 12-bit field"),
2415 S_GET_NAME (fragP
->fr_symbol
));
2417 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2418 _("displacement to undefined symbol %s overflows 12-bit field"),
2419 S_GET_NAME (fragP
->fr_symbol
));
2420 /* Stabilize this frag, so we don't trip an assert. */
2421 fragP
->fr_fix
+= fragP
->fr_var
;
2425 case C (COND_JUMP
, COND12
):
2426 case C (COND_JUMP_DELAY
, COND12
):
2427 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
2428 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
2429 was due to gas incorrectly relaxing an out-of-range conditional
2430 branch with delay slot. It turned:
2431 bf.s L6 (slot mov.l r12,@(44,r0))
2434 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
2436 32: 10 cb mov.l r12,@(44,r0)
2437 Therefore, branches with delay slots have to be handled
2438 differently from ones without delay slots. */
2440 unsigned char *buffer
=
2441 (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
2442 int highbyte
= target_big_endian
? 0 : 1;
2443 int lowbyte
= target_big_endian
? 1 : 0;
2444 int delay
= fragP
->fr_subtype
== C (COND_JUMP_DELAY
, COND12
);
2446 /* Toggle the true/false bit of the bcond. */
2447 buffer
[highbyte
] ^= 0x2;
2449 /* If this is a dalayed branch, we may not put the the bra in the
2450 slot. So we change it to a non-delayed branch, like that:
2451 b! cond slot_label; bra disp; slot_label: slot_insn
2452 ??? We should try if swapping the conditional branch and
2453 its delay-slot insn already makes the branch reach. */
2455 /* Build a relocation to six / four bytes farther on. */
2456 subseg_change (seg
, 0);
2457 fix_new (fragP
, fragP
->fr_fix
, 2,
2458 #ifdef BFD_ASSEMBLER
2459 section_symbol (seg
),
2461 seg_info (seg
)->dot
,
2463 fragP
->fr_address
+ fragP
->fr_fix
+ (delay
? 4 : 6),
2464 1, BFD_RELOC_SH_PCDISP8BY2
);
2466 /* Set up a jump instruction. */
2467 buffer
[highbyte
+ 2] = 0xa0;
2468 buffer
[lowbyte
+ 2] = 0;
2469 fix_new (fragP
, fragP
->fr_fix
+ 2, 2, fragP
->fr_symbol
,
2470 fragP
->fr_offset
, 1, BFD_RELOC_SH_PCDISP12BY2
);
2474 buffer
[highbyte
] &= ~0x4; /* Removes delay slot from branch. */
2479 /* Fill in a NOP instruction. */
2480 buffer
[highbyte
+ 4] = 0x0;
2481 buffer
[lowbyte
+ 4] = 0x9;
2490 case C (COND_JUMP
, COND32
):
2491 case C (COND_JUMP_DELAY
, COND32
):
2492 case C (COND_JUMP
, UNDEF_WORD_DISP
):
2493 case C (COND_JUMP_DELAY
, UNDEF_WORD_DISP
):
2494 if (fragP
->fr_symbol
== NULL
)
2495 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2496 _("displacement overflows 8-bit field"));
2497 else if (S_IS_DEFINED (fragP
->fr_symbol
))
2498 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2499 _("displacement to defined symbol %s overflows 8-bit field"),
2500 S_GET_NAME (fragP
->fr_symbol
));
2502 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2503 _("displacement to undefined symbol %s overflows 8-bit field "),
2504 S_GET_NAME (fragP
->fr_symbol
));
2505 /* Stabilize this frag, so we don't trip an assert. */
2506 fragP
->fr_fix
+= fragP
->fr_var
;
2514 if (donerelax
&& !sh_relax
)
2515 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2516 _("overflow in branch to %s; converted into longer instruction sequence"),
2517 (fragP
->fr_symbol
!= NULL
2518 ? S_GET_NAME (fragP
->fr_symbol
)
2523 md_section_align (seg
, size
)
2524 segT seg ATTRIBUTE_UNUSED
;
2527 #ifdef BFD_ASSEMBLER
2530 #else /* ! OBJ_ELF */
2531 return ((size
+ (1 << bfd_get_section_alignment (stdoutput
, seg
)) - 1)
2532 & (-1 << bfd_get_section_alignment (stdoutput
, seg
)));
2533 #endif /* ! OBJ_ELF */
2534 #else /* ! BFD_ASSEMBLER */
2535 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
2536 & (-1 << section_alignment
[(int) seg
]));
2537 #endif /* ! BFD_ASSEMBLER */
2540 /* This static variable is set by s_uacons to tell sh_cons_align that
2541 the expession does not need to be aligned. */
2543 static int sh_no_align_cons
= 0;
2545 /* This handles the unaligned space allocation pseudo-ops, such as
2546 .uaword. .uaword is just like .word, but the value does not need
2553 /* Tell sh_cons_align not to align this value. */
2554 sh_no_align_cons
= 1;
2558 /* If a .word, et. al., pseud-op is seen, warn if the value is not
2559 aligned correctly. Note that this can cause warnings to be issued
2560 when assembling initialized structured which were declared with the
2561 packed attribute. FIXME: Perhaps we should require an option to
2562 enable this warning? */
2565 sh_cons_align (nbytes
)
2571 if (sh_no_align_cons
)
2573 /* This is an unaligned pseudo-op. */
2574 sh_no_align_cons
= 0;
2579 while ((nbytes
& 1) == 0)
2588 if (now_seg
== absolute_section
)
2590 if ((abs_section_offset
& ((1 << nalign
) - 1)) != 0)
2591 as_warn (_("misaligned data"));
2595 p
= frag_var (rs_align_test
, 1, 1, (relax_substateT
) 0,
2596 (symbolS
*) NULL
, (offsetT
) nalign
, (char *) NULL
);
2598 record_alignment (now_seg
, nalign
);
2601 /* When relaxing, we need to output a reloc for any .align directive
2602 that requests alignment to a four byte boundary or larger. This is
2603 also where we check for misaligned data. */
2606 sh_handle_align (frag
)
2609 int bytes
= frag
->fr_next
->fr_address
- frag
->fr_address
- frag
->fr_fix
;
2611 if (frag
->fr_type
== rs_align_code
)
2613 static const unsigned char big_nop_pattern
[] = { 0x00, 0x09 };
2614 static const unsigned char little_nop_pattern
[] = { 0x09, 0x00 };
2616 char *p
= frag
->fr_literal
+ frag
->fr_fix
;
2625 if (target_big_endian
)
2627 memcpy (p
, big_nop_pattern
, sizeof big_nop_pattern
);
2628 frag
->fr_var
= sizeof big_nop_pattern
;
2632 memcpy (p
, little_nop_pattern
, sizeof little_nop_pattern
);
2633 frag
->fr_var
= sizeof little_nop_pattern
;
2636 else if (frag
->fr_type
== rs_align_test
)
2639 as_warn_where (frag
->fr_file
, frag
->fr_line
, _("misaligned data"));
2643 && (frag
->fr_type
== rs_align
2644 || frag
->fr_type
== rs_align_code
)
2645 && frag
->fr_address
+ frag
->fr_fix
> 0
2646 && frag
->fr_offset
> 1
2647 && now_seg
!= bss_section
)
2648 fix_new (frag
, frag
->fr_fix
, 2, &abs_symbol
, frag
->fr_offset
, 0,
2649 BFD_RELOC_SH_ALIGN
);
2652 /* This macro decides whether a particular reloc is an entry in a
2653 switch table. It is used when relaxing, because the linker needs
2654 to know about all such entries so that it can adjust them if
2657 #ifdef BFD_ASSEMBLER
2658 #define SWITCH_TABLE_CONS(fix) (0)
2660 #define SWITCH_TABLE_CONS(fix) \
2661 ((fix)->fx_r_type == 0 \
2662 && ((fix)->fx_size == 2 \
2663 || (fix)->fx_size == 1 \
2664 || (fix)->fx_size == 4))
2667 #define SWITCH_TABLE(fix) \
2668 ((fix)->fx_addsy != NULL \
2669 && (fix)->fx_subsy != NULL \
2670 && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
2671 && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
2672 && ((fix)->fx_r_type == BFD_RELOC_32 \
2673 || (fix)->fx_r_type == BFD_RELOC_16 \
2674 || (fix)->fx_r_type == BFD_RELOC_8 \
2675 || SWITCH_TABLE_CONS (fix)))
2677 /* See whether we need to force a relocation into the output file.
2678 This is used to force out switch and PC relative relocations when
2682 sh_force_relocation (fix
)
2686 if (fix
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2687 || fix
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
2688 || fix
->fx_r_type
== BFD_RELOC_SH_LOOP_START
2689 || fix
->fx_r_type
== BFD_RELOC_SH_LOOP_END
)
2695 return (fix
->fx_pcrel
2696 || SWITCH_TABLE (fix
)
2697 || fix
->fx_r_type
== BFD_RELOC_SH_COUNT
2698 || fix
->fx_r_type
== BFD_RELOC_SH_ALIGN
2699 || fix
->fx_r_type
== BFD_RELOC_SH_CODE
2700 || fix
->fx_r_type
== BFD_RELOC_SH_DATA
2701 || fix
->fx_r_type
== BFD_RELOC_SH_LABEL
);
2706 sh_fix_adjustable (fixP
)
2710 if (fixP
->fx_addsy
== NULL
)
2713 if (fixP
->fx_r_type
== BFD_RELOC_SH_PCDISP8BY2
2714 || fixP
->fx_r_type
== BFD_RELOC_SH_PCDISP12BY2
2715 || fixP
->fx_r_type
== BFD_RELOC_SH_PCRELIMM8BY2
2716 || fixP
->fx_r_type
== BFD_RELOC_SH_PCRELIMM8BY4
2717 || fixP
->fx_r_type
== BFD_RELOC_8_PCREL
2718 || fixP
->fx_r_type
== BFD_RELOC_SH_SWITCH16
2719 || fixP
->fx_r_type
== BFD_RELOC_SH_SWITCH32
)
2722 if (! TC_RELOC_RTSYM_LOC_FIXUP (fixP
)
2723 || fixP
->fx_r_type
== BFD_RELOC_32_GOTOFF
2724 || fixP
->fx_r_type
== BFD_RELOC_RVA
)
2727 /* We need the symbol name for the VTABLE entries */
2728 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2729 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
2736 sh_elf_final_processing ()
2740 /* Set file-specific flags to indicate if this code needs
2741 a processor with the sh-dsp / sh3e ISA to execute. */
2742 if (valid_arch
& arch_sh1
)
2744 else if (valid_arch
& arch_sh2
)
2746 else if (valid_arch
& arch_sh_dsp
)
2748 else if (valid_arch
& arch_sh3
)
2750 else if (valid_arch
& arch_sh3_dsp
)
2752 else if (valid_arch
& arch_sh3e
)
2754 else if (valid_arch
& arch_sh4
)
2759 elf_elfheader (stdoutput
)->e_flags
&= ~EF_SH_MACH_MASK
;
2760 elf_elfheader (stdoutput
)->e_flags
|= val
;
2764 /* Apply a fixup to the object file. */
2766 #ifdef BFD_ASSEMBLER
2768 md_apply_fix (fixP
, valp
)
2773 md_apply_fix (fixP
, val
)
2778 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
2779 int lowbyte
= target_big_endian
? 1 : 0;
2780 int highbyte
= target_big_endian
? 0 : 1;
2781 #ifdef BFD_ASSEMBLER
2787 #ifdef BFD_ASSEMBLER
2788 /* A difference between two symbols, the second of which is in the
2789 current section, is transformed in a PC-relative relocation to
2790 the other symbol. We have to adjust the relocation type here. */
2793 switch (fixP
->fx_r_type
)
2799 fixP
->fx_r_type
= BFD_RELOC_32_PCREL
;
2802 /* Currently, we only support 32-bit PCREL relocations.
2803 We'd need a new reloc type to handle 16_PCREL, and
2804 8_PCREL is already taken for R_SH_SWITCH8, which
2805 apparently does something completely different than what
2808 bfd_set_error (bfd_error_bad_value
);
2812 bfd_set_error (bfd_error_bad_value
);
2817 /* The function adjust_reloc_syms won't convert a reloc against a weak
2818 symbol into a reloc against a section, but bfd_install_relocation
2819 will screw up if the symbol is defined, so we have to adjust val here
2820 to avoid the screw up later.
2822 For ordinary relocs, this does not happen for ELF, since for ELF,
2823 bfd_install_relocation uses the "special function" field of the
2824 howto, and does not execute the code that needs to be undone, as long
2825 as the special function does not return bfd_reloc_continue.
2826 It can happen for GOT- and PLT-type relocs the way they are
2827 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
2828 doesn't matter here since those relocs don't use VAL; see below. */
2829 if (OUTPUT_FLAVOR
!= bfd_target_elf_flavour
2830 && fixP
->fx_addsy
!= NULL
2831 && S_IS_WEAK (fixP
->fx_addsy
))
2832 val
-= S_GET_VALUE (fixP
->fx_addsy
);
2835 #ifndef BFD_ASSEMBLER
2836 if (fixP
->fx_r_type
== 0)
2838 if (fixP
->fx_size
== 2)
2839 fixP
->fx_r_type
= BFD_RELOC_16
;
2840 else if (fixP
->fx_size
== 4)
2841 fixP
->fx_r_type
= BFD_RELOC_32
;
2842 else if (fixP
->fx_size
== 1)
2843 fixP
->fx_r_type
= BFD_RELOC_8
;
2851 switch (fixP
->fx_r_type
)
2853 case BFD_RELOC_SH_IMM4
:
2855 *buf
= (*buf
& 0xf0) | (val
& 0xf);
2858 case BFD_RELOC_SH_IMM4BY2
:
2861 *buf
= (*buf
& 0xf0) | ((val
>> 1) & 0xf);
2864 case BFD_RELOC_SH_IMM4BY4
:
2867 *buf
= (*buf
& 0xf0) | ((val
>> 2) & 0xf);
2870 case BFD_RELOC_SH_IMM8BY2
:
2876 case BFD_RELOC_SH_IMM8BY4
:
2883 case BFD_RELOC_SH_IMM8
:
2884 /* Sometimes the 8 bit value is sign extended (e.g., add) and
2885 sometimes it is not (e.g., and). We permit any 8 bit value.
2886 Note that adding further restrictions may invalidate
2887 reasonable looking assembly code, such as ``and -0x1,r0''. */
2893 case BFD_RELOC_SH_PCRELIMM8BY4
:
2894 /* The lower two bits of the PC are cleared before the
2895 displacement is added in. We can assume that the destination
2896 is on a 4 byte bounday. If this instruction is also on a 4
2897 byte boundary, then we want
2899 and target - here is a multiple of 4.
2900 Otherwise, we are on a 2 byte boundary, and we want
2901 (target - (here - 2)) / 4
2902 and target - here is not a multiple of 4. Computing
2903 (target - (here - 2)) / 4 == (target - here + 2) / 4
2904 works for both cases, since in the first case the addition of
2905 2 will be removed by the division. target - here is in the
2907 val
= (val
+ 2) / 4;
2909 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("pcrel too far"));
2913 case BFD_RELOC_SH_PCRELIMM8BY2
:
2916 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("pcrel too far"));
2920 case BFD_RELOC_SH_PCDISP8BY2
:
2922 if (val
< -0x80 || val
> 0x7f)
2923 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("pcrel too far"));
2927 case BFD_RELOC_SH_PCDISP12BY2
:
2929 if (val
< -0x800 || val
> 0x7ff)
2930 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("pcrel too far"));
2931 buf
[lowbyte
] = val
& 0xff;
2932 buf
[highbyte
] |= (val
>> 8) & 0xf;
2936 case BFD_RELOC_32_PCREL
:
2937 md_number_to_chars (buf
, val
, 4);
2941 md_number_to_chars (buf
, val
, 2);
2944 case BFD_RELOC_SH_USES
:
2945 /* Pass the value into sh_coff_reloc_mangle. */
2946 fixP
->fx_addnumber
= val
;
2949 case BFD_RELOC_SH_COUNT
:
2950 case BFD_RELOC_SH_ALIGN
:
2951 case BFD_RELOC_SH_CODE
:
2952 case BFD_RELOC_SH_DATA
:
2953 case BFD_RELOC_SH_LABEL
:
2954 /* Nothing to do here. */
2957 case BFD_RELOC_SH_LOOP_START
:
2958 case BFD_RELOC_SH_LOOP_END
:
2960 case BFD_RELOC_VTABLE_INHERIT
:
2961 case BFD_RELOC_VTABLE_ENTRY
:
2963 #ifdef BFD_ASSEMBLER
2970 case BFD_RELOC_32_PLT_PCREL
:
2971 /* Make the jump instruction point to the address of the operand. At
2972 runtime we merely add the offset to the actual PLT entry. */
2976 case BFD_RELOC_SH_GOTPC
:
2977 /* This is tough to explain. We end up with this one if we have
2978 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
2979 The goal here is to obtain the absolute address of the GOT,
2980 and it is strongly preferable from a performance point of
2981 view to avoid using a runtime relocation for this. There are
2982 cases where you have something like:
2984 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
2986 and here no correction would be required. Internally in the
2987 assembler we treat operands of this form as not being pcrel
2988 since the '.' is explicitly mentioned, and I wonder whether
2989 it would simplify matters to do it this way. Who knows. In
2990 earlier versions of the PIC patches, the pcrel_adjust field
2991 was used to store the correction, but since the expression is
2992 not pcrel, I felt it would be confusing to do it this way. */
2994 md_number_to_chars (buf
, val
, 4);
2997 case BFD_RELOC_32_GOT_PCREL
:
2998 *valp
= 0; /* Fully resolved at runtime. No addend. */
2999 md_number_to_chars (buf
, 0, 4);
3002 case BFD_RELOC_32_GOTOFF
:
3012 if ((val
& ((1 << shift
) - 1)) != 0)
3013 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("misaligned offset"));
3017 val
= ((val
>> shift
)
3018 | ((long) -1 & ~ ((long) -1 >> shift
)));
3020 if (max
!= 0 && (val
< min
|| val
> max
))
3021 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("offset out of range"));
3023 #ifdef BFD_ASSEMBLER
3028 /* Called just before address relaxation. Return the length
3029 by which a fragment must grow to reach it's destination. */
3032 md_estimate_size_before_relax (fragP
, segment_type
)
3033 register fragS
*fragP
;
3034 register segT segment_type
;
3038 switch (fragP
->fr_subtype
)
3043 case C (UNCOND_JUMP
, UNDEF_DISP
):
3044 /* Used to be a branch to somewhere which was unknown. */
3045 if (!fragP
->fr_symbol
)
3047 fragP
->fr_subtype
= C (UNCOND_JUMP
, UNCOND12
);
3049 else if (S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
3051 fragP
->fr_subtype
= C (UNCOND_JUMP
, UNCOND12
);
3055 fragP
->fr_subtype
= C (UNCOND_JUMP
, UNDEF_WORD_DISP
);
3059 case C (COND_JUMP
, UNDEF_DISP
):
3060 case C (COND_JUMP_DELAY
, UNDEF_DISP
):
3061 what
= GET_WHAT (fragP
->fr_subtype
);
3062 /* Used to be a branch to somewhere which was unknown. */
3063 if (fragP
->fr_symbol
3064 && S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
3066 /* Got a symbol and it's defined in this segment, become byte
3067 sized - maybe it will fix up. */
3068 fragP
->fr_subtype
= C (what
, COND8
);
3070 else if (fragP
->fr_symbol
)
3072 /* Its got a segment, but its not ours, so it will always be long. */
3073 fragP
->fr_subtype
= C (what
, UNDEF_WORD_DISP
);
3077 /* We know the abs value. */
3078 fragP
->fr_subtype
= C (what
, COND8
);
3082 case C (UNCOND_JUMP
, UNCOND12
):
3083 case C (UNCOND_JUMP
, UNCOND32
):
3084 case C (UNCOND_JUMP
, UNDEF_WORD_DISP
):
3085 case C (COND_JUMP
, COND8
):
3086 case C (COND_JUMP
, COND12
):
3087 case C (COND_JUMP
, COND32
):
3088 case C (COND_JUMP
, UNDEF_WORD_DISP
):
3089 case C (COND_JUMP_DELAY
, COND8
):
3090 case C (COND_JUMP_DELAY
, COND12
):
3091 case C (COND_JUMP_DELAY
, COND32
):
3092 case C (COND_JUMP_DELAY
, UNDEF_WORD_DISP
):
3093 /* When relaxing a section for the second time, we don't need to
3094 do anything besides return the current size. */
3098 fragP
->fr_var
= md_relax_table
[fragP
->fr_subtype
].rlx_length
;
3099 return fragP
->fr_var
;
3102 /* Put number into target byte order. */
3105 md_number_to_chars (ptr
, use
, nbytes
)
3110 if (! target_big_endian
)
3111 number_to_chars_littleendian (ptr
, use
, nbytes
);
3113 number_to_chars_bigendian (ptr
, use
, nbytes
);
3117 md_pcrel_from (fixP
)
3120 return fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
+ 2;
3126 tc_coff_sizemachdep (frag
)
3129 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
3132 #endif /* OBJ_COFF */
3134 #ifndef BFD_ASSEMBLER
3137 /* Map BFD relocs to SH COFF relocs. */
3141 bfd_reloc_code_real_type bfd_reloc
;
3145 static const struct reloc_map coff_reloc_map
[] =
3147 { BFD_RELOC_32
, R_SH_IMM32
},
3148 { BFD_RELOC_16
, R_SH_IMM16
},
3149 { BFD_RELOC_8
, R_SH_IMM8
},
3150 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_PCDISP8BY2
},
3151 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_PCDISP
},
3152 { BFD_RELOC_SH_IMM4
, R_SH_IMM4
},
3153 { BFD_RELOC_SH_IMM4BY2
, R_SH_IMM4BY2
},
3154 { BFD_RELOC_SH_IMM4BY4
, R_SH_IMM4BY4
},
3155 { BFD_RELOC_SH_IMM8
, R_SH_IMM8
},
3156 { BFD_RELOC_SH_IMM8BY2
, R_SH_IMM8BY2
},
3157 { BFD_RELOC_SH_IMM8BY4
, R_SH_IMM8BY4
},
3158 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_PCRELIMM8BY2
},
3159 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_PCRELIMM8BY4
},
3160 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
3161 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
3162 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
3163 { BFD_RELOC_SH_USES
, R_SH_USES
},
3164 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
3165 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
3166 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
3167 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
3168 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
3169 { BFD_RELOC_UNUSED
, 0 }
3172 /* Adjust a reloc for the SH. This is similar to the generic code,
3173 but does some minor tweaking. */
3176 sh_coff_reloc_mangle (seg
, fix
, intr
, paddr
)
3177 segment_info_type
*seg
;
3179 struct internal_reloc
*intr
;
3182 symbolS
*symbol_ptr
= fix
->fx_addsy
;
3185 intr
->r_vaddr
= paddr
+ fix
->fx_frag
->fr_address
+ fix
->fx_where
;
3187 if (! SWITCH_TABLE (fix
))
3189 const struct reloc_map
*rm
;
3191 for (rm
= coff_reloc_map
; rm
->bfd_reloc
!= BFD_RELOC_UNUSED
; rm
++)
3192 if (rm
->bfd_reloc
== (bfd_reloc_code_real_type
) fix
->fx_r_type
)
3194 if (rm
->bfd_reloc
== BFD_RELOC_UNUSED
)
3195 as_bad_where (fix
->fx_file
, fix
->fx_line
,
3196 _("Can not represent %s relocation in this object file format"),
3197 bfd_get_reloc_code_name (fix
->fx_r_type
));
3198 intr
->r_type
= rm
->sh_reloc
;
3205 if (fix
->fx_r_type
== BFD_RELOC_16
)
3206 intr
->r_type
= R_SH_SWITCH16
;
3207 else if (fix
->fx_r_type
== BFD_RELOC_8
)
3208 intr
->r_type
= R_SH_SWITCH8
;
3209 else if (fix
->fx_r_type
== BFD_RELOC_32
)
3210 intr
->r_type
= R_SH_SWITCH32
;
3214 /* For a switch reloc, we set r_offset to the difference between
3215 the reloc address and the subtrahend. When the linker is
3216 doing relaxing, it can use the determine the starting and
3217 ending points of the switch difference expression. */
3218 intr
->r_offset
= intr
->r_vaddr
- S_GET_VALUE (fix
->fx_subsy
);
3221 /* PC relative relocs are always against the current section. */
3222 if (symbol_ptr
== NULL
)
3224 switch (fix
->fx_r_type
)
3226 case BFD_RELOC_SH_PCRELIMM8BY2
:
3227 case BFD_RELOC_SH_PCRELIMM8BY4
:
3228 case BFD_RELOC_SH_PCDISP8BY2
:
3229 case BFD_RELOC_SH_PCDISP12BY2
:
3230 case BFD_RELOC_SH_USES
:
3231 symbol_ptr
= seg
->dot
;
3238 if (fix
->fx_r_type
== BFD_RELOC_SH_USES
)
3240 /* We can't store the offset in the object file, since this
3241 reloc does not take up any space, so we store it in r_offset.
3242 The fx_addnumber field was set in md_apply_fix. */
3243 intr
->r_offset
= fix
->fx_addnumber
;
3245 else if (fix
->fx_r_type
== BFD_RELOC_SH_COUNT
)
3247 /* We can't store the count in the object file, since this reloc
3248 does not take up any space, so we store it in r_offset. The
3249 fx_offset field was set when the fixup was created in
3250 sh_coff_frob_file. */
3251 intr
->r_offset
= fix
->fx_offset
;
3252 /* This reloc is always absolute. */
3255 else if (fix
->fx_r_type
== BFD_RELOC_SH_ALIGN
)
3257 /* Store the alignment in the r_offset field. */
3258 intr
->r_offset
= fix
->fx_offset
;
3259 /* This reloc is always absolute. */
3262 else if (fix
->fx_r_type
== BFD_RELOC_SH_CODE
3263 || fix
->fx_r_type
== BFD_RELOC_SH_DATA
3264 || fix
->fx_r_type
== BFD_RELOC_SH_LABEL
)
3266 /* These relocs are always absolute. */
3270 /* Turn the segment of the symbol into an offset. */
3271 if (symbol_ptr
!= NULL
)
3273 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
3275 intr
->r_symndx
= dot
->sy_number
;
3277 intr
->r_symndx
= symbol_ptr
->sy_number
;
3280 intr
->r_symndx
= -1;
3283 #endif /* OBJ_COFF */
3284 #endif /* ! BFD_ASSEMBLER */
3286 #ifdef BFD_ASSEMBLER
3288 /* Create a reloc. */
3291 tc_gen_reloc (section
, fixp
)
3292 asection
*section ATTRIBUTE_UNUSED
;
3296 bfd_reloc_code_real_type r_type
;
3298 rel
= (arelent
*) xmalloc (sizeof (arelent
));
3299 rel
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
3300 *rel
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
3301 rel
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
3303 r_type
= fixp
->fx_r_type
;
3305 if (SWITCH_TABLE (fixp
))
3307 rel
->addend
= rel
->address
- S_GET_VALUE (fixp
->fx_subsy
);
3308 if (r_type
== BFD_RELOC_16
)
3309 r_type
= BFD_RELOC_SH_SWITCH16
;
3310 else if (r_type
== BFD_RELOC_8
)
3311 r_type
= BFD_RELOC_8_PCREL
;
3312 else if (r_type
== BFD_RELOC_32
)
3313 r_type
= BFD_RELOC_SH_SWITCH32
;
3317 else if (r_type
== BFD_RELOC_SH_USES
)
3318 rel
->addend
= fixp
->fx_addnumber
;
3319 else if (r_type
== BFD_RELOC_SH_COUNT
)
3320 rel
->addend
= fixp
->fx_offset
;
3321 else if (r_type
== BFD_RELOC_SH_ALIGN
)
3322 rel
->addend
= fixp
->fx_offset
;
3323 else if (r_type
== BFD_RELOC_VTABLE_INHERIT
3324 || r_type
== BFD_RELOC_VTABLE_ENTRY
)
3325 rel
->addend
= fixp
->fx_offset
;
3326 else if (r_type
== BFD_RELOC_SH_LOOP_START
3327 || r_type
== BFD_RELOC_SH_LOOP_END
)
3328 rel
->addend
= fixp
->fx_offset
;
3329 else if (r_type
== BFD_RELOC_SH_LABEL
&& fixp
->fx_pcrel
)
3332 rel
->address
= rel
->addend
= fixp
->fx_offset
;
3334 else if (fixp
->fx_pcrel
)
3335 rel
->addend
= fixp
->fx_addnumber
;
3336 else if (r_type
== BFD_RELOC_32
|| r_type
== BFD_RELOC_32_GOTOFF
)
3337 rel
->addend
= fixp
->fx_addnumber
;
3341 rel
->howto
= bfd_reloc_type_lookup (stdoutput
, r_type
);
3342 if (rel
->howto
== NULL
)
3344 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
3345 _("Cannot represent relocation type %s"),
3346 bfd_get_reloc_code_name (r_type
));
3347 /* Set howto to a garbage value so that we can keep going. */
3348 rel
->howto
= bfd_reloc_type_lookup (stdoutput
, BFD_RELOC_32
);
3349 assert (rel
->howto
!= NULL
);
3355 #endif /* BFD_ASSEMBLER */