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