tc-sh.c revision 1.1.1.7 1 1.1 skrll /* tc-sh.c -- Assemble code for the Renesas / SuperH SH
2 1.1.1.6 christos Copyright (C) 1993-2018 Free Software Foundation, Inc.
3 1.1 skrll
4 1.1 skrll This file is part of GAS, the GNU Assembler.
5 1.1 skrll
6 1.1 skrll GAS is free software; you can redistribute it and/or modify
7 1.1 skrll it under the terms of the GNU General Public License as published by
8 1.1 skrll the Free Software Foundation; either version 3, or (at your option)
9 1.1 skrll any later version.
10 1.1 skrll
11 1.1 skrll GAS is distributed in the hope that it will be useful,
12 1.1 skrll but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 skrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 skrll GNU General Public License for more details.
15 1.1 skrll
16 1.1 skrll You should have received a copy of the GNU General Public License
17 1.1 skrll along with GAS; see the file COPYING. If not, write to
18 1.1 skrll the Free Software Foundation, 51 Franklin Street - Fifth Floor,
19 1.1 skrll Boston, MA 02110-1301, USA. */
20 1.1 skrll
21 1.1 skrll /* Written By Steve Chamberlain <sac (at) cygnus.com> */
22 1.1 skrll
23 1.1 skrll #include "as.h"
24 1.1 skrll #include "subsegs.h"
25 1.1 skrll #define DEFINE_TABLE
26 1.1 skrll #include "opcodes/sh-opc.h"
27 1.1 skrll #include "safe-ctype.h"
28 1.1 skrll #include "struc-symbol.h"
29 1.1 skrll
30 1.1 skrll #ifdef OBJ_ELF
31 1.1 skrll #include "elf/sh.h"
32 1.1 skrll #endif
33 1.1 skrll
34 1.1 skrll #include "dwarf2dbg.h"
35 1.1 skrll #include "dw2gencfi.h"
36 1.1 skrll
37 1.1 skrll typedef struct
38 1.1 skrll {
39 1.1 skrll sh_arg_type type;
40 1.1 skrll int reg;
41 1.1 skrll expressionS immediate;
42 1.1 skrll }
43 1.1 skrll sh_operand_info;
44 1.1 skrll
45 1.1 skrll const char comment_chars[] = "!";
46 1.1 skrll const char line_separator_chars[] = ";";
47 1.1 skrll const char line_comment_chars[] = "!#";
48 1.1 skrll
49 1.1 skrll static void s_uses (int);
50 1.1 skrll static void s_uacons (int);
51 1.1 skrll
52 1.1 skrll #ifdef OBJ_ELF
53 1.1 skrll static void sh_elf_cons (int);
54 1.1 skrll
55 1.1 skrll symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
56 1.1 skrll #endif
57 1.1 skrll
58 1.1 skrll static void
59 1.1 skrll big (int ignore ATTRIBUTE_UNUSED)
60 1.1 skrll {
61 1.1 skrll if (! target_big_endian)
62 1.1 skrll as_bad (_("directive .big encountered when option -big required"));
63 1.1 skrll
64 1.1 skrll /* Stop further messages. */
65 1.1 skrll target_big_endian = 1;
66 1.1 skrll }
67 1.1 skrll
68 1.1 skrll static void
69 1.1 skrll little (int ignore ATTRIBUTE_UNUSED)
70 1.1 skrll {
71 1.1 skrll if (target_big_endian)
72 1.1 skrll as_bad (_("directive .little encountered when option -little required"));
73 1.1 skrll
74 1.1 skrll /* Stop further messages. */
75 1.1 skrll target_big_endian = 0;
76 1.1 skrll }
77 1.1 skrll
78 1.1 skrll /* This table describes all the machine specific pseudo-ops the assembler
79 1.1 skrll has to support. The fields are:
80 1.1 skrll pseudo-op name without dot
81 1.1 skrll function to call to execute this pseudo-op
82 1.1 skrll Integer arg to pass to the function. */
83 1.1 skrll
84 1.1 skrll const pseudo_typeS md_pseudo_table[] =
85 1.1 skrll {
86 1.1 skrll #ifdef OBJ_ELF
87 1.1 skrll {"long", sh_elf_cons, 4},
88 1.1 skrll {"int", sh_elf_cons, 4},
89 1.1 skrll {"word", sh_elf_cons, 2},
90 1.1 skrll {"short", sh_elf_cons, 2},
91 1.1 skrll #else
92 1.1 skrll {"int", cons, 4},
93 1.1 skrll {"word", cons, 2},
94 1.1 skrll #endif /* OBJ_ELF */
95 1.1 skrll {"big", big, 0},
96 1.1 skrll {"form", listing_psize, 0},
97 1.1 skrll {"little", little, 0},
98 1.1 skrll {"heading", listing_title, 0},
99 1.1 skrll {"import", s_ignore, 0},
100 1.1 skrll {"page", listing_eject, 0},
101 1.1 skrll {"program", s_ignore, 0},
102 1.1 skrll {"uses", s_uses, 0},
103 1.1 skrll {"uaword", s_uacons, 2},
104 1.1 skrll {"ualong", s_uacons, 4},
105 1.1 skrll {"uaquad", s_uacons, 8},
106 1.1 skrll {"2byte", s_uacons, 2},
107 1.1 skrll {"4byte", s_uacons, 4},
108 1.1 skrll {"8byte", s_uacons, 8},
109 1.1 skrll {0, 0, 0}
110 1.1 skrll };
111 1.1 skrll
112 1.1 skrll int sh_relax; /* set if -relax seen */
113 1.1 skrll
114 1.1 skrll /* Whether -small was seen. */
115 1.1 skrll
116 1.1 skrll int sh_small;
117 1.1 skrll
118 1.1 skrll /* Flag to generate relocations against symbol values for local symbols. */
119 1.1 skrll
120 1.1 skrll static int dont_adjust_reloc_32;
121 1.1 skrll
122 1.1 skrll /* Flag to indicate that '$' is allowed as a register prefix. */
123 1.1 skrll
124 1.1 skrll static int allow_dollar_register_prefix;
125 1.1 skrll
126 1.1 skrll /* Preset architecture set, if given; zero otherwise. */
127 1.1 skrll
128 1.1 skrll static unsigned int preset_target_arch;
129 1.1 skrll
130 1.1 skrll /* The bit mask of architectures that could
131 1.1 skrll accommodate the insns seen so far. */
132 1.1 skrll static unsigned int valid_arch;
133 1.1 skrll
134 1.1.1.2 christos #ifdef OBJ_ELF
135 1.1.1.2 christos /* Whether --fdpic was given. */
136 1.1.1.2 christos static int sh_fdpic;
137 1.1.1.2 christos #endif
138 1.1.1.2 christos
139 1.1 skrll const char EXP_CHARS[] = "eE";
140 1.1 skrll
141 1.1 skrll /* Chars that mean this number is a floating point constant. */
142 1.1 skrll /* As in 0f12.456 */
143 1.1 skrll /* or 0d1.2345e12 */
144 1.1 skrll const char FLT_CHARS[] = "rRsSfFdDxXpP";
145 1.1 skrll
146 1.1 skrll #define C(a,b) ENCODE_RELAX(a,b)
147 1.1 skrll
148 1.1 skrll #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
149 1.1 skrll #define GET_WHAT(x) ((x>>4))
150 1.1 skrll
151 1.1 skrll /* These are the three types of relaxable instruction. */
152 1.1 skrll /* These are the types of relaxable instructions; except for END which is
153 1.1 skrll a marker. */
154 1.1 skrll #define COND_JUMP 1
155 1.1 skrll #define COND_JUMP_DELAY 2
156 1.1 skrll #define UNCOND_JUMP 3
157 1.1 skrll
158 1.1 skrll #define END 4
159 1.1 skrll
160 1.1 skrll #define UNDEF_DISP 0
161 1.1 skrll #define COND8 1
162 1.1 skrll #define COND12 2
163 1.1 skrll #define COND32 3
164 1.1 skrll #define UNDEF_WORD_DISP 4
165 1.1 skrll
166 1.1 skrll #define UNCOND12 1
167 1.1 skrll #define UNCOND32 2
168 1.1 skrll
169 1.1 skrll /* Branch displacements are from the address of the branch plus
170 1.1 skrll four, thus all minimum and maximum values have 4 added to them. */
171 1.1 skrll #define COND8_F 258
172 1.1 skrll #define COND8_M -252
173 1.1 skrll #define COND8_LENGTH 2
174 1.1 skrll
175 1.1 skrll /* There is one extra instruction before the branch, so we must add
176 1.1 skrll two more bytes to account for it. */
177 1.1 skrll #define COND12_F 4100
178 1.1 skrll #define COND12_M -4090
179 1.1 skrll #define COND12_LENGTH 6
180 1.1 skrll
181 1.1 skrll #define COND12_DELAY_LENGTH 4
182 1.1 skrll
183 1.1 skrll /* ??? The minimum and maximum values are wrong, but this does not matter
184 1.1 skrll since this relocation type is not supported yet. */
185 1.1 skrll #define COND32_F (1<<30)
186 1.1 skrll #define COND32_M -(1<<30)
187 1.1 skrll #define COND32_LENGTH 14
188 1.1 skrll
189 1.1 skrll #define UNCOND12_F 4098
190 1.1 skrll #define UNCOND12_M -4092
191 1.1 skrll #define UNCOND12_LENGTH 2
192 1.1 skrll
193 1.1 skrll /* ??? The minimum and maximum values are wrong, but this does not matter
194 1.1 skrll since this relocation type is not supported yet. */
195 1.1 skrll #define UNCOND32_F (1<<30)
196 1.1 skrll #define UNCOND32_M -(1<<30)
197 1.1 skrll #define UNCOND32_LENGTH 14
198 1.1 skrll
199 1.1 skrll #define EMPTY { 0, 0, 0, 0 }
200 1.1 skrll
201 1.1 skrll const relax_typeS md_relax_table[C (END, 0)] = {
202 1.1 skrll EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
203 1.1 skrll EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
204 1.1 skrll
205 1.1 skrll EMPTY,
206 1.1 skrll /* C (COND_JUMP, COND8) */
207 1.1 skrll { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
208 1.1 skrll /* C (COND_JUMP, COND12) */
209 1.1 skrll { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
210 1.1 skrll /* C (COND_JUMP, COND32) */
211 1.1 skrll { COND32_F, COND32_M, COND32_LENGTH, 0, },
212 1.1 skrll /* C (COND_JUMP, UNDEF_WORD_DISP) */
213 1.1 skrll { 0, 0, COND32_LENGTH, 0, },
214 1.1 skrll EMPTY, EMPTY, EMPTY,
215 1.1 skrll EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
216 1.1 skrll
217 1.1 skrll EMPTY,
218 1.1 skrll /* C (COND_JUMP_DELAY, COND8) */
219 1.1 skrll { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
220 1.1 skrll /* C (COND_JUMP_DELAY, COND12) */
221 1.1 skrll { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
222 1.1 skrll /* C (COND_JUMP_DELAY, COND32) */
223 1.1 skrll { COND32_F, COND32_M, COND32_LENGTH, 0, },
224 1.1 skrll /* C (COND_JUMP_DELAY, UNDEF_WORD_DISP) */
225 1.1 skrll { 0, 0, COND32_LENGTH, 0, },
226 1.1 skrll EMPTY, EMPTY, EMPTY,
227 1.1 skrll EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
228 1.1 skrll
229 1.1 skrll EMPTY,
230 1.1 skrll /* C (UNCOND_JUMP, UNCOND12) */
231 1.1 skrll { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
232 1.1 skrll /* C (UNCOND_JUMP, UNCOND32) */
233 1.1 skrll { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
234 1.1 skrll EMPTY,
235 1.1 skrll /* C (UNCOND_JUMP, UNDEF_WORD_DISP) */
236 1.1 skrll { 0, 0, UNCOND32_LENGTH, 0, },
237 1.1 skrll EMPTY, EMPTY, EMPTY,
238 1.1 skrll EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
239 1.1 skrll
240 1.1 skrll };
241 1.1 skrll
242 1.1 skrll #undef EMPTY
243 1.1 skrll
244 1.1 skrll static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
245 1.1 skrll
246 1.1 skrll
247 1.1 skrll #ifdef OBJ_ELF
249 1.1 skrll /* Determine whether the symbol needs any kind of PIC relocation. */
250 1.1 skrll
251 1.1 skrll inline static int
252 1.1 skrll sh_PIC_related_p (symbolS *sym)
253 1.1 skrll {
254 1.1 skrll expressionS *exp;
255 1.1 skrll
256 1.1 skrll if (! sym)
257 1.1 skrll return 0;
258 1.1 skrll
259 1.1 skrll if (sym == GOT_symbol)
260 1.1 skrll return 1;
261 1.1 skrll
262 1.1 skrll exp = symbol_get_value_expression (sym);
263 1.1 skrll
264 1.1 skrll return (exp->X_op == O_PIC_reloc
265 1.1 skrll || sh_PIC_related_p (exp->X_add_symbol)
266 1.1 skrll || sh_PIC_related_p (exp->X_op_symbol));
267 1.1 skrll }
268 1.1 skrll
269 1.1 skrll /* Determine the relocation type to be used to represent the
270 1.1 skrll expression, that may be rearranged. */
271 1.1 skrll
272 1.1 skrll static int
273 1.1 skrll sh_check_fixup (expressionS *main_exp, bfd_reloc_code_real_type *r_type_p)
274 1.1 skrll {
275 1.1 skrll expressionS *exp = main_exp;
276 1.1 skrll
277 1.1 skrll /* This is here for backward-compatibility only. GCC used to generated:
278 1.1 skrll
279 1.1 skrll f@PLT + . - (.LPCS# + 2)
280 1.1 skrll
281 1.1 skrll but we'd rather be able to handle this as a PIC-related reference
282 1.1 skrll plus/minus a symbol. However, gas' parser gives us:
283 1.1 skrll
284 1.1 skrll O_subtract (O_add (f@PLT, .), .LPCS#+2)
285 1.1 skrll
286 1.1 skrll so we attempt to transform this into:
287 1.1 skrll
288 1.1 skrll O_subtract (f@PLT, O_subtract (.LPCS#+2, .))
289 1.1 skrll
290 1.1 skrll which we can handle simply below. */
291 1.1 skrll if (exp->X_op == O_subtract)
292 1.1 skrll {
293 1.1 skrll if (sh_PIC_related_p (exp->X_op_symbol))
294 1.1 skrll return 1;
295 1.1 skrll
296 1.1 skrll exp = symbol_get_value_expression (exp->X_add_symbol);
297 1.1 skrll
298 1.1 skrll if (exp && sh_PIC_related_p (exp->X_op_symbol))
299 1.1 skrll return 1;
300 1.1 skrll
301 1.1 skrll if (exp && exp->X_op == O_add
302 1.1 skrll && sh_PIC_related_p (exp->X_add_symbol))
303 1.1 skrll {
304 1.1 skrll symbolS *sym = exp->X_add_symbol;
305 1.1 skrll
306 1.1 skrll exp->X_op = O_subtract;
307 1.1 skrll exp->X_add_symbol = main_exp->X_op_symbol;
308 1.1 skrll
309 1.1 skrll main_exp->X_op_symbol = main_exp->X_add_symbol;
310 1.1 skrll main_exp->X_add_symbol = sym;
311 1.1 skrll
312 1.1 skrll main_exp->X_add_number += exp->X_add_number;
313 1.1 skrll exp->X_add_number = 0;
314 1.1 skrll }
315 1.1 skrll
316 1.1 skrll exp = main_exp;
317 1.1 skrll }
318 1.1 skrll else if (exp->X_op == O_add && sh_PIC_related_p (exp->X_op_symbol))
319 1.1 skrll return 1;
320 1.1 skrll
321 1.1 skrll if (exp->X_op == O_symbol || exp->X_op == O_add || exp->X_op == O_subtract)
322 1.1 skrll {
323 1.1 skrll if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
324 1.1 skrll {
325 1.1 skrll *r_type_p = BFD_RELOC_SH_GOTPC;
326 1.1 skrll return 0;
327 1.1 skrll }
328 1.1 skrll exp = symbol_get_value_expression (exp->X_add_symbol);
329 1.1 skrll if (! exp)
330 1.1 skrll return 0;
331 1.1 skrll }
332 1.1 skrll
333 1.1 skrll if (exp->X_op == O_PIC_reloc)
334 1.1 skrll {
335 1.1 skrll switch (*r_type_p)
336 1.1 skrll {
337 1.1 skrll case BFD_RELOC_NONE:
338 1.1 skrll case BFD_RELOC_UNUSED:
339 1.1 skrll *r_type_p = exp->X_md;
340 1.1 skrll break;
341 1.1.1.2 christos
342 1.1.1.2 christos case BFD_RELOC_SH_DISP20:
343 1.1.1.2 christos switch (exp->X_md)
344 1.1.1.2 christos {
345 1.1.1.2 christos case BFD_RELOC_32_GOT_PCREL:
346 1.1.1.2 christos *r_type_p = BFD_RELOC_SH_GOT20;
347 1.1.1.2 christos break;
348 1.1.1.2 christos
349 1.1.1.2 christos case BFD_RELOC_32_GOTOFF:
350 1.1.1.2 christos *r_type_p = BFD_RELOC_SH_GOTOFF20;
351 1.1.1.2 christos break;
352 1.1.1.2 christos
353 1.1.1.2 christos case BFD_RELOC_SH_GOTFUNCDESC:
354 1.1.1.2 christos *r_type_p = BFD_RELOC_SH_GOTFUNCDESC20;
355 1.1.1.2 christos break;
356 1.1.1.2 christos
357 1.1.1.2 christos case BFD_RELOC_SH_GOTOFFFUNCDESC:
358 1.1.1.2 christos *r_type_p = BFD_RELOC_SH_GOTOFFFUNCDESC20;
359 1.1.1.2 christos break;
360 1.1.1.2 christos
361 1.1.1.2 christos default:
362 1.1.1.2 christos abort ();
363 1.1.1.2 christos }
364 1.1.1.2 christos break;
365 1.1 skrll
366 1.1 skrll default:
367 1.1 skrll abort ();
368 1.1 skrll }
369 1.1 skrll if (exp == main_exp)
370 1.1 skrll exp->X_op = O_symbol;
371 1.1 skrll else
372 1.1 skrll {
373 1.1 skrll main_exp->X_add_symbol = exp->X_add_symbol;
374 1.1 skrll main_exp->X_add_number += exp->X_add_number;
375 1.1 skrll }
376 1.1 skrll }
377 1.1 skrll else
378 1.1 skrll return (sh_PIC_related_p (exp->X_add_symbol)
379 1.1 skrll || sh_PIC_related_p (exp->X_op_symbol));
380 1.1 skrll
381 1.1 skrll return 0;
382 1.1 skrll }
383 1.1 skrll
384 1.1 skrll /* Add expression EXP of SIZE bytes to offset OFF of fragment FRAG. */
385 1.1 skrll
386 1.1.1.4 christos void
387 1.1.1.4 christos sh_cons_fix_new (fragS *frag, int off, int size, expressionS *exp,
388 1.1 skrll bfd_reloc_code_real_type r_type)
389 1.1.1.4 christos {
390 1.1 skrll r_type = BFD_RELOC_UNUSED;
391 1.1 skrll
392 1.1 skrll if (sh_check_fixup (exp, &r_type))
393 1.1 skrll as_bad (_("Invalid PIC expression."));
394 1.1 skrll
395 1.1 skrll if (r_type == BFD_RELOC_UNUSED)
396 1.1 skrll switch (size)
397 1.1 skrll {
398 1.1 skrll case 1:
399 1.1 skrll r_type = BFD_RELOC_8;
400 1.1 skrll break;
401 1.1 skrll
402 1.1 skrll case 2:
403 1.1 skrll r_type = BFD_RELOC_16;
404 1.1 skrll break;
405 1.1 skrll
406 1.1 skrll case 4:
407 1.1 skrll r_type = BFD_RELOC_32;
408 1.1 skrll break;
409 1.1 skrll
410 1.1 skrll case 8:
411 1.1 skrll r_type = BFD_RELOC_64;
412 1.1 skrll break;
413 1.1 skrll
414 1.1 skrll default:
415 1.1 skrll goto error;
416 1.1 skrll }
417 1.1 skrll else if (size != 4)
418 1.1 skrll {
419 1.1 skrll error:
420 1.1 skrll as_bad (_("unsupported BFD relocation size %u"), size);
421 1.1 skrll r_type = BFD_RELOC_UNUSED;
422 1.1 skrll }
423 1.1 skrll
424 1.1 skrll fix_new_exp (frag, off, size, exp, 0, r_type);
425 1.1 skrll }
426 1.1 skrll
427 1.1 skrll /* The regular cons() function, that reads constants, doesn't support
428 1.1 skrll suffixes such as @GOT, @GOTOFF and @PLT, that generate
429 1.1 skrll machine-specific relocation types. So we must define it here. */
430 1.1 skrll /* Clobbers input_line_pointer, checks end-of-line. */
431 1.1 skrll /* NBYTES 1=.byte, 2=.word, 4=.long */
432 1.1.1.4 christos static void
433 1.1 skrll sh_elf_cons (int nbytes)
434 1.1 skrll {
435 1.1 skrll expressionS exp;
436 1.1 skrll
437 1.1 skrll if (is_it_end_of_statement ())
438 1.1 skrll {
439 1.1 skrll demand_empty_rest_of_line ();
440 1.1 skrll return;
441 1.1 skrll }
442 1.1 skrll
443 1.1 skrll #ifdef md_cons_align
444 1.1 skrll md_cons_align (nbytes);
445 1.1 skrll #endif
446 1.1 skrll
447 1.1 skrll do
448 1.1 skrll {
449 1.1 skrll expression (&exp);
450 1.1 skrll emit_expr (&exp, (unsigned int) nbytes);
451 1.1 skrll }
452 1.1 skrll while (*input_line_pointer++ == ',');
453 1.1 skrll
454 1.1 skrll input_line_pointer--; /* Put terminator back into stream. */
455 1.1 skrll if (*input_line_pointer == '#' || *input_line_pointer == '!')
456 1.1 skrll {
457 1.1 skrll while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
458 1.1 skrll }
459 1.1 skrll else
460 1.1 skrll demand_empty_rest_of_line ();
461 1.1 skrll }
462 1.1 skrll
463 1.1 skrll /* The regular frag_offset_fixed_p doesn't work for rs_align_test
464 1.1 skrll frags. */
465 1.1 skrll
466 1.1 skrll static bfd_boolean
467 1.1 skrll align_test_frag_offset_fixed_p (const fragS *frag1, const fragS *frag2,
468 1.1 skrll bfd_vma *offset)
469 1.1 skrll {
470 1.1 skrll const fragS *frag;
471 1.1 skrll bfd_vma off;
472 1.1 skrll
473 1.1 skrll /* Start with offset initialised to difference between the two frags.
474 1.1 skrll Prior to assigning frag addresses this will be zero. */
475 1.1 skrll off = frag1->fr_address - frag2->fr_address;
476 1.1 skrll if (frag1 == frag2)
477 1.1 skrll {
478 1.1 skrll *offset = off;
479 1.1 skrll return TRUE;
480 1.1 skrll }
481 1.1 skrll
482 1.1 skrll /* Maybe frag2 is after frag1. */
483 1.1 skrll frag = frag1;
484 1.1 skrll while (frag->fr_type == rs_fill
485 1.1 skrll || frag->fr_type == rs_align_test)
486 1.1 skrll {
487 1.1 skrll if (frag->fr_type == rs_fill)
488 1.1 skrll off += frag->fr_fix + frag->fr_offset * frag->fr_var;
489 1.1 skrll else
490 1.1 skrll off += frag->fr_fix;
491 1.1 skrll frag = frag->fr_next;
492 1.1 skrll if (frag == NULL)
493 1.1 skrll break;
494 1.1 skrll if (frag == frag2)
495 1.1 skrll {
496 1.1 skrll *offset = off;
497 1.1 skrll return TRUE;
498 1.1 skrll }
499 1.1 skrll }
500 1.1 skrll
501 1.1 skrll /* Maybe frag1 is after frag2. */
502 1.1 skrll off = frag1->fr_address - frag2->fr_address;
503 1.1 skrll frag = frag2;
504 1.1 skrll while (frag->fr_type == rs_fill
505 1.1 skrll || frag->fr_type == rs_align_test)
506 1.1 skrll {
507 1.1 skrll if (frag->fr_type == rs_fill)
508 1.1 skrll off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
509 1.1 skrll else
510 1.1 skrll off -= frag->fr_fix;
511 1.1 skrll frag = frag->fr_next;
512 1.1 skrll if (frag == NULL)
513 1.1 skrll break;
514 1.1 skrll if (frag == frag1)
515 1.1 skrll {
516 1.1 skrll *offset = off;
517 1.1 skrll return TRUE;
518 1.1 skrll }
519 1.1 skrll }
520 1.1 skrll
521 1.1 skrll return FALSE;
522 1.1 skrll }
523 1.1 skrll
524 1.1 skrll /* Optimize a difference of symbols which have rs_align_test frag if
525 1.1 skrll possible. */
526 1.1 skrll
527 1.1 skrll int
528 1.1 skrll sh_optimize_expr (expressionS *l, operatorT op, expressionS *r)
529 1.1 skrll {
530 1.1 skrll bfd_vma frag_off;
531 1.1 skrll
532 1.1 skrll if (op == O_subtract
533 1.1 skrll && l->X_op == O_symbol
534 1.1 skrll && r->X_op == O_symbol
535 1.1 skrll && S_GET_SEGMENT (l->X_add_symbol) == S_GET_SEGMENT (r->X_add_symbol)
536 1.1 skrll && (SEG_NORMAL (S_GET_SEGMENT (l->X_add_symbol))
537 1.1 skrll || r->X_add_symbol == l->X_add_symbol)
538 1.1 skrll && align_test_frag_offset_fixed_p (symbol_get_frag (l->X_add_symbol),
539 1.1 skrll symbol_get_frag (r->X_add_symbol),
540 1.1 skrll &frag_off))
541 1.1.1.4 christos {
542 1.1.1.4 christos offsetT symval_diff = S_GET_VALUE (l->X_add_symbol)
543 1.1.1.4 christos - S_GET_VALUE (r->X_add_symbol);
544 1.1.1.4 christos subtract_from_result (l, r->X_add_number, r->X_extrabit);
545 1.1.1.4 christos subtract_from_result (l, frag_off / OCTETS_PER_BYTE, 0);
546 1.1 skrll add_to_result (l, symval_diff, symval_diff < 0);
547 1.1 skrll l->X_op = O_constant;
548 1.1 skrll l->X_add_symbol = 0;
549 1.1 skrll return 1;
550 1.1 skrll }
551 1.1 skrll return 0;
552 1.1 skrll }
553 1.1 skrll #endif /* OBJ_ELF */
554 1.1 skrll
555 1.1 skrll /* This function is called once, at assembler startup time. This should
557 1.1 skrll set up all the tables, etc that the MD part of the assembler needs. */
558 1.1 skrll
559 1.1 skrll void
560 1.1 skrll md_begin (void)
561 1.1.1.5 christos {
562 1.1 skrll const sh_opcode_info *opcode;
563 1.1 skrll const char *prev_name = "";
564 1.1 skrll unsigned int target_arch;
565 1.1 skrll
566 1.1 skrll target_arch
567 1.1 skrll = preset_target_arch ? preset_target_arch : arch_sh_up & ~arch_sh_has_dsp;
568 1.1 skrll valid_arch = target_arch;
569 1.1 skrll
570 1.1 skrll opcode_hash_control = hash_new ();
571 1.1 skrll
572 1.1 skrll /* Insert unique names into hash table. */
573 1.1 skrll for (opcode = sh_table; opcode->name; opcode++)
574 1.1 skrll {
575 1.1 skrll if (strcmp (prev_name, opcode->name) != 0)
576 1.1 skrll {
577 1.1 skrll if (!SH_MERGE_ARCH_SET_VALID (opcode->arch, target_arch))
578 1.1 skrll continue;
579 1.1 skrll prev_name = opcode->name;
580 1.1 skrll hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
581 1.1 skrll }
582 1.1 skrll }
583 1.1 skrll }
584 1.1 skrll
585 1.1 skrll static int reg_m;
586 1.1 skrll static int reg_n;
587 1.1 skrll static int reg_x, reg_y;
588 1.1 skrll static int reg_efg;
589 1.1 skrll static int reg_b;
590 1.1 skrll
591 1.1 skrll #define IDENT_CHAR(c) (ISALNUM (c) || (c) == '_')
592 1.1 skrll
593 1.1 skrll /* Try to parse a reg name. Return the number of chars consumed. */
594 1.1.1.5 christos
595 1.1 skrll static unsigned int
596 1.1 skrll parse_reg_without_prefix (char *src, sh_arg_type *mode, int *reg)
597 1.1 skrll {
598 1.1 skrll char l0 = TOLOWER (src[0]);
599 1.1 skrll char l1 = l0 ? TOLOWER (src[1]) : 0;
600 1.1 skrll
601 1.1 skrll /* We use ! IDENT_CHAR for the next character after the register name, to
602 1.1 skrll make sure that we won't accidentally recognize a symbol name such as
603 1.1 skrll 'sram' or sr_ram as being a reference to the register 'sr'. */
604 1.1 skrll
605 1.1 skrll if (l0 == 'r')
606 1.1 skrll {
607 1.1 skrll if (l1 == '1')
608 1.1 skrll {
609 1.1 skrll if (src[2] >= '0' && src[2] <= '5'
610 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
611 1.1 skrll {
612 1.1 skrll *mode = A_REG_N;
613 1.1 skrll *reg = 10 + src[2] - '0';
614 1.1 skrll return 3;
615 1.1 skrll }
616 1.1 skrll }
617 1.1 skrll if (l1 >= '0' && l1 <= '9'
618 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[2]))
619 1.1 skrll {
620 1.1 skrll *mode = A_REG_N;
621 1.1 skrll *reg = (l1 - '0');
622 1.1 skrll return 2;
623 1.1 skrll }
624 1.1 skrll if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
625 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[7]))
626 1.1 skrll {
627 1.1 skrll *mode = A_REG_B;
628 1.1 skrll *reg = (l1 - '0');
629 1.1 skrll return 7;
630 1.1 skrll }
631 1.1 skrll
632 1.1 skrll if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
633 1.1 skrll {
634 1.1 skrll *mode = A_RE;
635 1.1 skrll return 2;
636 1.1 skrll }
637 1.1 skrll if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
638 1.1 skrll {
639 1.1 skrll *mode = A_RS;
640 1.1 skrll return 2;
641 1.1 skrll }
642 1.1 skrll }
643 1.1 skrll
644 1.1 skrll if (l0 == 'a')
645 1.1 skrll {
646 1.1 skrll if (l1 == '0')
647 1.1 skrll {
648 1.1 skrll if (! IDENT_CHAR ((unsigned char) src[2]))
649 1.1 skrll {
650 1.1 skrll *mode = DSP_REG_N;
651 1.1 skrll *reg = A_A0_NUM;
652 1.1 skrll return 2;
653 1.1 skrll }
654 1.1 skrll if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
655 1.1 skrll {
656 1.1 skrll *mode = DSP_REG_N;
657 1.1 skrll *reg = A_A0G_NUM;
658 1.1 skrll return 3;
659 1.1 skrll }
660 1.1 skrll }
661 1.1 skrll if (l1 == '1')
662 1.1 skrll {
663 1.1 skrll if (! IDENT_CHAR ((unsigned char) src[2]))
664 1.1 skrll {
665 1.1 skrll *mode = DSP_REG_N;
666 1.1 skrll *reg = A_A1_NUM;
667 1.1 skrll return 2;
668 1.1 skrll }
669 1.1 skrll if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
670 1.1 skrll {
671 1.1 skrll *mode = DSP_REG_N;
672 1.1 skrll *reg = A_A1G_NUM;
673 1.1 skrll return 3;
674 1.1 skrll }
675 1.1 skrll }
676 1.1 skrll
677 1.1 skrll if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
678 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
679 1.1 skrll {
680 1.1 skrll *mode = A_REG_N;
681 1.1 skrll *reg = 4 + (l1 - '0');
682 1.1 skrll return 3;
683 1.1 skrll }
684 1.1 skrll if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
685 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
686 1.1 skrll {
687 1.1 skrll *mode = A_REG_N;
688 1.1 skrll *reg = 6 + (l1 - '0');
689 1.1 skrll return 3;
690 1.1 skrll }
691 1.1 skrll if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
692 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
693 1.1 skrll {
694 1.1 skrll int n = l1 - '0';
695 1.1 skrll
696 1.1 skrll *mode = A_REG_N;
697 1.1 skrll *reg = n | ((~n & 2) << 1);
698 1.1 skrll return 3;
699 1.1 skrll }
700 1.1 skrll }
701 1.1 skrll
702 1.1 skrll if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[2]))
703 1.1 skrll {
704 1.1 skrll if (l1 == 's')
705 1.1 skrll {
706 1.1 skrll *mode = A_REG_N;
707 1.1 skrll *reg = 8;
708 1.1 skrll return 2;
709 1.1 skrll }
710 1.1 skrll if (l1 == 'x')
711 1.1 skrll {
712 1.1 skrll *mode = A_REG_N;
713 1.1 skrll *reg = 8;
714 1.1 skrll return 2;
715 1.1 skrll }
716 1.1 skrll if (l1 == 'y')
717 1.1 skrll {
718 1.1 skrll *mode = A_REG_N;
719 1.1 skrll *reg = 9;
720 1.1 skrll return 2;
721 1.1 skrll }
722 1.1 skrll }
723 1.1 skrll
724 1.1 skrll if (l0 == 'x' && l1 >= '0' && l1 <= '1'
725 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[2]))
726 1.1 skrll {
727 1.1 skrll *mode = DSP_REG_N;
728 1.1 skrll *reg = A_X0_NUM + l1 - '0';
729 1.1 skrll return 2;
730 1.1 skrll }
731 1.1 skrll
732 1.1 skrll if (l0 == 'y' && l1 >= '0' && l1 <= '1'
733 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[2]))
734 1.1 skrll {
735 1.1 skrll *mode = DSP_REG_N;
736 1.1 skrll *reg = A_Y0_NUM + l1 - '0';
737 1.1 skrll return 2;
738 1.1 skrll }
739 1.1 skrll
740 1.1 skrll if (l0 == 'm' && l1 >= '0' && l1 <= '1'
741 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[2]))
742 1.1 skrll {
743 1.1 skrll *mode = DSP_REG_N;
744 1.1 skrll *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
745 1.1 skrll return 2;
746 1.1 skrll }
747 1.1 skrll
748 1.1 skrll if (l0 == 's'
749 1.1 skrll && l1 == 's'
750 1.1 skrll && TOLOWER (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
751 1.1 skrll {
752 1.1 skrll *mode = A_SSR;
753 1.1 skrll return 3;
754 1.1 skrll }
755 1.1 skrll
756 1.1 skrll if (l0 == 's' && l1 == 'p' && TOLOWER (src[2]) == 'c'
757 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
758 1.1 skrll {
759 1.1 skrll *mode = A_SPC;
760 1.1 skrll return 3;
761 1.1 skrll }
762 1.1 skrll
763 1.1 skrll if (l0 == 's' && l1 == 'g' && TOLOWER (src[2]) == 'r'
764 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
765 1.1 skrll {
766 1.1 skrll *mode = A_SGR;
767 1.1 skrll return 3;
768 1.1 skrll }
769 1.1 skrll
770 1.1 skrll if (l0 == 'd' && l1 == 's' && TOLOWER (src[2]) == 'r'
771 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
772 1.1 skrll {
773 1.1 skrll *mode = A_DSR;
774 1.1 skrll return 3;
775 1.1 skrll }
776 1.1 skrll
777 1.1 skrll if (l0 == 'd' && l1 == 'b' && TOLOWER (src[2]) == 'r'
778 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
779 1.1 skrll {
780 1.1 skrll *mode = A_DBR;
781 1.1 skrll return 3;
782 1.1 skrll }
783 1.1 skrll
784 1.1 skrll if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
785 1.1 skrll {
786 1.1 skrll *mode = A_SR;
787 1.1 skrll return 2;
788 1.1 skrll }
789 1.1 skrll
790 1.1 skrll if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
791 1.1 skrll {
792 1.1 skrll *mode = A_REG_N;
793 1.1 skrll *reg = 15;
794 1.1 skrll return 2;
795 1.1 skrll }
796 1.1 skrll
797 1.1 skrll if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
798 1.1 skrll {
799 1.1 skrll *mode = A_PR;
800 1.1 skrll return 2;
801 1.1 skrll }
802 1.1 skrll if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
803 1.1 skrll {
804 1.1 skrll /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
805 1.1 skrll and use an uninitialized immediate. */
806 1.1 skrll *mode = A_PC;
807 1.1 skrll return 2;
808 1.1 skrll }
809 1.1 skrll if (l0 == 'g' && l1 == 'b' && TOLOWER (src[2]) == 'r'
810 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
811 1.1 skrll {
812 1.1 skrll *mode = A_GBR;
813 1.1 skrll return 3;
814 1.1 skrll }
815 1.1 skrll if (l0 == 'v' && l1 == 'b' && TOLOWER (src[2]) == 'r'
816 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
817 1.1 skrll {
818 1.1 skrll *mode = A_VBR;
819 1.1 skrll return 3;
820 1.1 skrll }
821 1.1 skrll
822 1.1 skrll if (l0 == 't' && l1 == 'b' && TOLOWER (src[2]) == 'r'
823 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
824 1.1 skrll {
825 1.1 skrll *mode = A_TBR;
826 1.1 skrll return 3;
827 1.1 skrll }
828 1.1 skrll if (l0 == 'm' && l1 == 'a' && TOLOWER (src[2]) == 'c'
829 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[4]))
830 1.1 skrll {
831 1.1 skrll if (TOLOWER (src[3]) == 'l')
832 1.1 skrll {
833 1.1 skrll *mode = A_MACL;
834 1.1 skrll return 4;
835 1.1 skrll }
836 1.1 skrll if (TOLOWER (src[3]) == 'h')
837 1.1 skrll {
838 1.1 skrll *mode = A_MACH;
839 1.1 skrll return 4;
840 1.1 skrll }
841 1.1 skrll }
842 1.1 skrll if (l0 == 'm' && l1 == 'o' && TOLOWER (src[2]) == 'd'
843 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
844 1.1 skrll {
845 1.1 skrll *mode = A_MOD;
846 1.1 skrll return 3;
847 1.1 skrll }
848 1.1 skrll if (l0 == 'f' && l1 == 'r')
849 1.1 skrll {
850 1.1 skrll if (src[2] == '1')
851 1.1 skrll {
852 1.1 skrll if (src[3] >= '0' && src[3] <= '5'
853 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[4]))
854 1.1 skrll {
855 1.1 skrll *mode = F_REG_N;
856 1.1 skrll *reg = 10 + src[3] - '0';
857 1.1 skrll return 4;
858 1.1 skrll }
859 1.1 skrll }
860 1.1 skrll if (src[2] >= '0' && src[2] <= '9'
861 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
862 1.1 skrll {
863 1.1 skrll *mode = F_REG_N;
864 1.1 skrll *reg = (src[2] - '0');
865 1.1 skrll return 3;
866 1.1 skrll }
867 1.1 skrll }
868 1.1 skrll if (l0 == 'd' && l1 == 'r')
869 1.1 skrll {
870 1.1 skrll if (src[2] == '1')
871 1.1 skrll {
872 1.1 skrll if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
873 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[4]))
874 1.1 skrll {
875 1.1 skrll *mode = D_REG_N;
876 1.1 skrll *reg = 10 + src[3] - '0';
877 1.1 skrll return 4;
878 1.1 skrll }
879 1.1 skrll }
880 1.1 skrll if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
881 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
882 1.1 skrll {
883 1.1 skrll *mode = D_REG_N;
884 1.1 skrll *reg = (src[2] - '0');
885 1.1 skrll return 3;
886 1.1 skrll }
887 1.1 skrll }
888 1.1 skrll if (l0 == 'x' && l1 == 'd')
889 1.1 skrll {
890 1.1 skrll if (src[2] == '1')
891 1.1 skrll {
892 1.1 skrll if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
893 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[4]))
894 1.1 skrll {
895 1.1 skrll *mode = X_REG_N;
896 1.1 skrll *reg = 11 + src[3] - '0';
897 1.1 skrll return 4;
898 1.1 skrll }
899 1.1 skrll }
900 1.1 skrll if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
901 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
902 1.1 skrll {
903 1.1 skrll *mode = X_REG_N;
904 1.1 skrll *reg = (src[2] - '0') + 1;
905 1.1 skrll return 3;
906 1.1 skrll }
907 1.1 skrll }
908 1.1 skrll if (l0 == 'f' && l1 == 'v')
909 1.1 skrll {
910 1.1 skrll if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
911 1.1 skrll {
912 1.1 skrll *mode = V_REG_N;
913 1.1 skrll *reg = 12;
914 1.1 skrll return 4;
915 1.1 skrll }
916 1.1 skrll if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
917 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[3]))
918 1.1 skrll {
919 1.1 skrll *mode = V_REG_N;
920 1.1 skrll *reg = (src[2] - '0');
921 1.1 skrll return 3;
922 1.1 skrll }
923 1.1 skrll }
924 1.1 skrll if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 'u'
925 1.1 skrll && TOLOWER (src[3]) == 'l'
926 1.1 skrll && ! IDENT_CHAR ((unsigned char) src[4]))
927 1.1 skrll {
928 1.1 skrll *mode = FPUL_N;
929 1.1 skrll return 4;
930 1.1 skrll }
931 1.1 skrll
932 1.1 skrll if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 's'
933 1.1 skrll && TOLOWER (src[3]) == 'c'
934 1.1 skrll && TOLOWER (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
935 1.1 skrll {
936 1.1 skrll *mode = FPSCR_N;
937 1.1 skrll return 5;
938 1.1 skrll }
939 1.1 skrll
940 1.1 skrll if (l0 == 'x' && l1 == 'm' && TOLOWER (src[2]) == 't'
941 1.1 skrll && TOLOWER (src[3]) == 'r'
942 1.1 skrll && TOLOWER (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
943 1.1 skrll {
944 1.1 skrll *mode = XMTRX_M4;
945 1.1 skrll return 5;
946 1.1 skrll }
947 1.1 skrll
948 1.1 skrll return 0;
949 1.1 skrll }
950 1.1 skrll
951 1.1 skrll /* Like parse_reg_without_prefix, but this version supports
952 1.1 skrll $-prefixed register names if enabled by the user. */
953 1.1.1.5 christos
954 1.1 skrll static unsigned int
955 1.1 skrll parse_reg (char *src, sh_arg_type *mode, int *reg)
956 1.1 skrll {
957 1.1 skrll unsigned int prefix;
958 1.1 skrll unsigned int consumed;
959 1.1 skrll
960 1.1 skrll if (src[0] == '$')
961 1.1 skrll {
962 1.1 skrll if (allow_dollar_register_prefix)
963 1.1 skrll {
964 1.1 skrll src ++;
965 1.1 skrll prefix = 1;
966 1.1 skrll }
967 1.1 skrll else
968 1.1 skrll return 0;
969 1.1 skrll }
970 1.1.1.4 christos else
971 1.1 skrll prefix = 0;
972 1.1 skrll
973 1.1 skrll consumed = parse_reg_without_prefix (src, mode, reg);
974 1.1 skrll
975 1.1 skrll if (consumed == 0)
976 1.1 skrll return 0;
977 1.1 skrll
978 1.1 skrll return consumed + prefix;
979 1.1 skrll }
980 1.1 skrll
981 1.1 skrll static char *
982 1.1 skrll parse_exp (char *s, sh_operand_info *op)
983 1.1.1.2 christos {
984 1.1 skrll char *save;
985 1.1 skrll char *new_pointer;
986 1.1 skrll
987 1.1 skrll save = input_line_pointer;
988 1.1 skrll input_line_pointer = s;
989 1.1 skrll expression (&op->immediate);
990 1.1.1.2 christos if (op->immediate.X_op == O_absent)
991 1.1 skrll as_bad (_("missing operand"));
992 1.1.1.2 christos new_pointer = input_line_pointer;
993 1.1 skrll input_line_pointer = save;
994 1.1 skrll return new_pointer;
995 1.1 skrll }
996 1.1 skrll
997 1.1 skrll /* The many forms of operand:
998 1.1 skrll
999 1.1 skrll Rn Register direct
1000 1.1 skrll @Rn Register indirect
1001 1.1 skrll @Rn+ Autoincrement
1002 1.1 skrll @-Rn Autodecrement
1003 1.1 skrll @(disp:4,Rn)
1004 1.1 skrll @(disp:8,GBR)
1005 1.1 skrll @(disp:8,PC)
1006 1.1 skrll
1007 1.1 skrll @(R0,Rn)
1008 1.1 skrll @(R0,GBR)
1009 1.1 skrll
1010 1.1 skrll disp:8
1011 1.1 skrll disp:12
1012 1.1 skrll #imm8
1013 1.1 skrll pr, gbr, vbr, macl, mach
1014 1.1 skrll */
1015 1.1 skrll
1016 1.1 skrll static char *
1017 1.1 skrll parse_at (char *src, sh_operand_info *op)
1018 1.1.1.5 christos {
1019 1.1 skrll int len;
1020 1.1 skrll sh_arg_type mode;
1021 1.1 skrll src++;
1022 1.1 skrll if (src[0] == '@')
1023 1.1 skrll {
1024 1.1 skrll src = parse_at (src, op);
1025 1.1 skrll if (op->type == A_DISP_TBR)
1026 1.1 skrll op->type = A_DISP2_TBR;
1027 1.1 skrll else
1028 1.1 skrll as_bad (_("illegal double indirection"));
1029 1.1 skrll }
1030 1.1 skrll else if (src[0] == '-')
1031 1.1 skrll {
1032 1.1 skrll /* Must be predecrement. */
1033 1.1 skrll src++;
1034 1.1 skrll
1035 1.1 skrll len = parse_reg (src, &mode, &(op->reg));
1036 1.1 skrll if (mode != A_REG_N)
1037 1.1 skrll as_bad (_("illegal register after @-"));
1038 1.1 skrll
1039 1.1 skrll op->type = A_DEC_N;
1040 1.1 skrll src += len;
1041 1.1 skrll }
1042 1.1 skrll else if (src[0] == '(')
1043 1.1 skrll {
1044 1.1 skrll /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
1045 1.1 skrll @(r0, rn). */
1046 1.1 skrll src++;
1047 1.1 skrll len = parse_reg (src, &mode, &(op->reg));
1048 1.1 skrll if (len && mode == A_REG_N)
1049 1.1 skrll {
1050 1.1 skrll src += len;
1051 1.1 skrll if (op->reg != 0)
1052 1.1 skrll {
1053 1.1 skrll as_bad (_("must be @(r0,...)"));
1054 1.1 skrll }
1055 1.1 skrll if (src[0] == ',')
1056 1.1 skrll {
1057 1.1 skrll src++;
1058 1.1 skrll /* Now can be rn or gbr. */
1059 1.1 skrll len = parse_reg (src, &mode, &(op->reg));
1060 1.1 skrll }
1061 1.1 skrll else
1062 1.1 skrll {
1063 1.1 skrll len = 0;
1064 1.1 skrll }
1065 1.1 skrll if (len)
1066 1.1 skrll {
1067 1.1 skrll if (mode == A_GBR)
1068 1.1 skrll {
1069 1.1 skrll op->type = A_R0_GBR;
1070 1.1 skrll }
1071 1.1 skrll else if (mode == A_REG_N)
1072 1.1 skrll {
1073 1.1 skrll op->type = A_IND_R0_REG_N;
1074 1.1 skrll }
1075 1.1 skrll else
1076 1.1 skrll {
1077 1.1 skrll as_bad (_("syntax error in @(r0,...)"));
1078 1.1 skrll }
1079 1.1 skrll }
1080 1.1 skrll else
1081 1.1 skrll {
1082 1.1 skrll as_bad (_("syntax error in @(r0...)"));
1083 1.1 skrll }
1084 1.1 skrll }
1085 1.1 skrll else
1086 1.1 skrll {
1087 1.1 skrll /* Must be an @(disp,.. thing). */
1088 1.1 skrll src = parse_exp (src, op);
1089 1.1 skrll if (src[0] == ',')
1090 1.1 skrll src++;
1091 1.1 skrll /* Now can be rn, gbr or pc. */
1092 1.1 skrll len = parse_reg (src, &mode, &op->reg);
1093 1.1 skrll if (len)
1094 1.1 skrll {
1095 1.1 skrll if (mode == A_REG_N)
1096 1.1 skrll {
1097 1.1 skrll op->type = A_DISP_REG_N;
1098 1.1 skrll }
1099 1.1 skrll else if (mode == A_GBR)
1100 1.1 skrll {
1101 1.1 skrll op->type = A_DISP_GBR;
1102 1.1 skrll }
1103 1.1 skrll else if (mode == A_TBR)
1104 1.1 skrll {
1105 1.1 skrll op->type = A_DISP_TBR;
1106 1.1 skrll }
1107 1.1 skrll else if (mode == A_PC)
1108 1.1 skrll {
1109 1.1 skrll /* We want @(expr, pc) to uniformly address . + expr,
1110 1.1 skrll no matter if expr is a constant, or a more complex
1111 1.1 skrll expression, e.g. sym-. or sym1-sym2.
1112 1.1 skrll However, we also used to accept @(sym,pc)
1113 1.1 skrll as addressing sym, i.e. meaning the same as plain sym.
1114 1.1 skrll Some existing code does use the @(sym,pc) syntax, so
1115 1.1 skrll we give it the old semantics for now, but warn about
1116 1.1 skrll its use, so that users have some time to fix their code.
1117 1.1 skrll
1118 1.1 skrll Note that due to this backward compatibility hack,
1119 1.1 skrll we'll get unexpected results when @(offset, pc) is used,
1120 1.1 skrll and offset is a symbol that is set later to an an address
1121 1.1 skrll difference, or an external symbol that is set to an
1122 1.1 skrll address difference in another source file, so we want to
1123 1.1 skrll eventually remove it. */
1124 1.1 skrll if (op->immediate.X_op == O_symbol)
1125 1.1 skrll {
1126 1.1 skrll op->type = A_DISP_PC;
1127 1.1 skrll as_warn (_("Deprecated syntax."));
1128 1.1 skrll }
1129 1.1 skrll else
1130 1.1 skrll {
1131 1.1 skrll op->type = A_DISP_PC_ABS;
1132 1.1 skrll /* Such operands don't get corrected for PC==.+4, so
1133 1.1 skrll make the correction here. */
1134 1.1 skrll op->immediate.X_add_number -= 4;
1135 1.1 skrll }
1136 1.1 skrll }
1137 1.1 skrll else
1138 1.1 skrll {
1139 1.1 skrll as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1140 1.1 skrll }
1141 1.1 skrll }
1142 1.1 skrll else
1143 1.1 skrll {
1144 1.1 skrll as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1145 1.1 skrll }
1146 1.1 skrll }
1147 1.1 skrll src += len;
1148 1.1 skrll if (src[0] != ')')
1149 1.1 skrll as_bad (_("expecting )"));
1150 1.1 skrll else
1151 1.1 skrll src++;
1152 1.1 skrll }
1153 1.1 skrll else
1154 1.1 skrll {
1155 1.1 skrll src += parse_reg (src, &mode, &(op->reg));
1156 1.1 skrll if (mode != A_REG_N)
1157 1.1 skrll as_bad (_("illegal register after @"));
1158 1.1 skrll
1159 1.1 skrll if (src[0] == '+')
1160 1.1 skrll {
1161 1.1 skrll char l0, l1;
1162 1.1 skrll
1163 1.1 skrll src++;
1164 1.1 skrll l0 = TOLOWER (src[0]);
1165 1.1 skrll l1 = TOLOWER (src[1]);
1166 1.1 skrll
1167 1.1 skrll if ((l0 == 'r' && l1 == '8')
1168 1.1 skrll || (l0 == 'i' && (l1 == 'x' || l1 == 's')))
1169 1.1 skrll {
1170 1.1 skrll src += 2;
1171 1.1 skrll op->type = AX_PMOD_N;
1172 1.1 skrll }
1173 1.1 skrll else if ( (l0 == 'r' && l1 == '9')
1174 1.1 skrll || (l0 == 'i' && l1 == 'y'))
1175 1.1 skrll {
1176 1.1 skrll src += 2;
1177 1.1 skrll op->type = AY_PMOD_N;
1178 1.1 skrll }
1179 1.1 skrll else
1180 1.1 skrll op->type = A_INC_N;
1181 1.1 skrll }
1182 1.1 skrll else
1183 1.1 skrll op->type = A_IND_N;
1184 1.1 skrll }
1185 1.1 skrll return src;
1186 1.1 skrll }
1187 1.1 skrll
1188 1.1 skrll static void
1189 1.1 skrll get_operand (char **ptr, sh_operand_info *op)
1190 1.1.1.5 christos {
1191 1.1 skrll char *src = *ptr;
1192 1.1 skrll sh_arg_type mode = (sh_arg_type) -1;
1193 1.1 skrll unsigned int len;
1194 1.1 skrll
1195 1.1 skrll if (src[0] == '#')
1196 1.1 skrll {
1197 1.1 skrll src++;
1198 1.1 skrll *ptr = parse_exp (src, op);
1199 1.1 skrll op->type = A_IMM;
1200 1.1 skrll return;
1201 1.1 skrll }
1202 1.1 skrll
1203 1.1 skrll else if (src[0] == '@')
1204 1.1 skrll {
1205 1.1 skrll *ptr = parse_at (src, op);
1206 1.1 skrll return;
1207 1.1 skrll }
1208 1.1 skrll len = parse_reg (src, &mode, &(op->reg));
1209 1.1 skrll if (len)
1210 1.1 skrll {
1211 1.1 skrll *ptr = src + len;
1212 1.1 skrll op->type = mode;
1213 1.1 skrll return;
1214 1.1 skrll }
1215 1.1 skrll else
1216 1.1 skrll {
1217 1.1 skrll /* Not a reg, the only thing left is a displacement. */
1218 1.1 skrll *ptr = parse_exp (src, op);
1219 1.1 skrll op->type = A_DISP_PC;
1220 1.1 skrll return;
1221 1.1 skrll }
1222 1.1 skrll }
1223 1.1 skrll
1224 1.1 skrll static char *
1225 1.1 skrll get_operands (sh_opcode_info *info, char *args, sh_operand_info *operand)
1226 1.1 skrll {
1227 1.1 skrll char *ptr = args;
1228 1.1 skrll if (info->arg[0])
1229 1.1 skrll {
1230 1.1 skrll /* The pre-processor will eliminate whitespace in front of '@'
1231 1.1 skrll after the first argument; we may be called multiple times
1232 1.1 skrll from assemble_ppi, so don't insist on finding whitespace here. */
1233 1.1 skrll if (*ptr == ' ')
1234 1.1 skrll ptr++;
1235 1.1 skrll
1236 1.1 skrll get_operand (&ptr, operand + 0);
1237 1.1 skrll if (info->arg[1])
1238 1.1 skrll {
1239 1.1 skrll if (*ptr == ',')
1240 1.1 skrll {
1241 1.1 skrll ptr++;
1242 1.1 skrll }
1243 1.1 skrll get_operand (&ptr, operand + 1);
1244 1.1 skrll /* ??? Hack: psha/pshl have a varying operand number depending on
1245 1.1 skrll the type of the first operand. We handle this by having the
1246 1.1 skrll three-operand version first and reducing the number of operands
1247 1.1 skrll parsed to two if we see that the first operand is an immediate.
1248 1.1 skrll This works because no insn with three operands has an immediate
1249 1.1 skrll as first operand. */
1250 1.1 skrll if (info->arg[2] && operand[0].type != A_IMM)
1251 1.1 skrll {
1252 1.1 skrll if (*ptr == ',')
1253 1.1 skrll {
1254 1.1 skrll ptr++;
1255 1.1 skrll }
1256 1.1 skrll get_operand (&ptr, operand + 2);
1257 1.1 skrll }
1258 1.1 skrll else
1259 1.1 skrll {
1260 1.1 skrll operand[2].type = 0;
1261 1.1 skrll }
1262 1.1 skrll }
1263 1.1 skrll else
1264 1.1 skrll {
1265 1.1 skrll operand[1].type = 0;
1266 1.1 skrll operand[2].type = 0;
1267 1.1 skrll }
1268 1.1 skrll }
1269 1.1 skrll else
1270 1.1 skrll {
1271 1.1 skrll operand[0].type = 0;
1272 1.1 skrll operand[1].type = 0;
1273 1.1 skrll operand[2].type = 0;
1274 1.1 skrll }
1275 1.1 skrll return ptr;
1276 1.1 skrll }
1277 1.1 skrll
1278 1.1 skrll /* Passed a pointer to a list of opcodes which use different
1279 1.1 skrll addressing modes, return the opcode which matches the opcodes
1280 1.1 skrll provided. */
1281 1.1 skrll
1282 1.1 skrll static sh_opcode_info *
1283 1.1 skrll get_specific (sh_opcode_info *opcode, sh_operand_info *operands)
1284 1.1.1.5 christos {
1285 1.1 skrll sh_opcode_info *this_try = opcode;
1286 1.1 skrll const char *name = opcode->name;
1287 1.1 skrll int n = 0;
1288 1.1 skrll
1289 1.1 skrll while (opcode->name)
1290 1.1 skrll {
1291 1.1 skrll this_try = opcode++;
1292 1.1 skrll if ((this_try->name != name) && (strcmp (this_try->name, name) != 0))
1293 1.1 skrll {
1294 1.1 skrll /* We've looked so far down the table that we've run out of
1295 1.1 skrll opcodes with the same name. */
1296 1.1 skrll return 0;
1297 1.1 skrll }
1298 1.1 skrll
1299 1.1 skrll /* Look at both operands needed by the opcodes and provided by
1300 1.1 skrll the user - since an arg test will often fail on the same arg
1301 1.1 skrll again and again, we'll try and test the last failing arg the
1302 1.1 skrll first on each opcode try. */
1303 1.1 skrll for (n = 0; this_try->arg[n]; n++)
1304 1.1 skrll {
1305 1.1 skrll sh_operand_info *user = operands + n;
1306 1.1 skrll sh_arg_type arg = this_try->arg[n];
1307 1.1 skrll
1308 1.1 skrll switch (arg)
1309 1.1 skrll {
1310 1.1 skrll case A_DISP_PC:
1311 1.1 skrll if (user->type == A_DISP_PC_ABS)
1312 1.1 skrll break;
1313 1.1 skrll /* Fall through. */
1314 1.1 skrll case A_IMM:
1315 1.1 skrll case A_BDISP12:
1316 1.1 skrll case A_BDISP8:
1317 1.1 skrll case A_DISP_GBR:
1318 1.1 skrll case A_DISP2_TBR:
1319 1.1 skrll case A_MACH:
1320 1.1 skrll case A_PR:
1321 1.1 skrll case A_MACL:
1322 1.1 skrll if (user->type != arg)
1323 1.1 skrll goto fail;
1324 1.1 skrll break;
1325 1.1 skrll case A_R0:
1326 1.1 skrll /* opcode needs r0 */
1327 1.1 skrll if (user->type != A_REG_N || user->reg != 0)
1328 1.1 skrll goto fail;
1329 1.1 skrll break;
1330 1.1 skrll case A_R0_GBR:
1331 1.1 skrll if (user->type != A_R0_GBR || user->reg != 0)
1332 1.1 skrll goto fail;
1333 1.1 skrll break;
1334 1.1 skrll case F_FR0:
1335 1.1 skrll if (user->type != F_REG_N || user->reg != 0)
1336 1.1 skrll goto fail;
1337 1.1 skrll break;
1338 1.1 skrll
1339 1.1 skrll case A_REG_N:
1340 1.1 skrll case A_INC_N:
1341 1.1 skrll case A_DEC_N:
1342 1.1 skrll case A_IND_N:
1343 1.1 skrll case A_IND_R0_REG_N:
1344 1.1 skrll case A_DISP_REG_N:
1345 1.1 skrll case F_REG_N:
1346 1.1 skrll case D_REG_N:
1347 1.1 skrll case X_REG_N:
1348 1.1 skrll case V_REG_N:
1349 1.1 skrll case FPUL_N:
1350 1.1 skrll case FPSCR_N:
1351 1.1 skrll case DSP_REG_N:
1352 1.1 skrll /* Opcode needs rn */
1353 1.1 skrll if (user->type != arg)
1354 1.1 skrll goto fail;
1355 1.1 skrll reg_n = user->reg;
1356 1.1 skrll break;
1357 1.1 skrll case DX_REG_N:
1358 1.1 skrll if (user->type != D_REG_N && user->type != X_REG_N)
1359 1.1 skrll goto fail;
1360 1.1 skrll reg_n = user->reg;
1361 1.1 skrll break;
1362 1.1 skrll case A_GBR:
1363 1.1 skrll case A_TBR:
1364 1.1 skrll case A_SR:
1365 1.1 skrll case A_VBR:
1366 1.1 skrll case A_DSR:
1367 1.1 skrll case A_MOD:
1368 1.1 skrll case A_RE:
1369 1.1 skrll case A_RS:
1370 1.1 skrll case A_SSR:
1371 1.1 skrll case A_SPC:
1372 1.1 skrll case A_SGR:
1373 1.1 skrll case A_DBR:
1374 1.1 skrll if (user->type != arg)
1375 1.1 skrll goto fail;
1376 1.1 skrll break;
1377 1.1 skrll
1378 1.1 skrll case A_REG_B:
1379 1.1 skrll if (user->type != arg)
1380 1.1 skrll goto fail;
1381 1.1 skrll reg_b = user->reg;
1382 1.1 skrll break;
1383 1.1 skrll
1384 1.1 skrll case A_INC_R15:
1385 1.1 skrll if (user->type != A_INC_N)
1386 1.1 skrll goto fail;
1387 1.1 skrll if (user->reg != 15)
1388 1.1 skrll goto fail;
1389 1.1 skrll reg_n = user->reg;
1390 1.1 skrll break;
1391 1.1 skrll
1392 1.1 skrll case A_DEC_R15:
1393 1.1 skrll if (user->type != A_DEC_N)
1394 1.1 skrll goto fail;
1395 1.1 skrll if (user->reg != 15)
1396 1.1 skrll goto fail;
1397 1.1 skrll reg_n = user->reg;
1398 1.1 skrll break;
1399 1.1 skrll
1400 1.1 skrll case A_REG_M:
1401 1.1 skrll case A_INC_M:
1402 1.1 skrll case A_DEC_M:
1403 1.1 skrll case A_IND_M:
1404 1.1 skrll case A_IND_R0_REG_M:
1405 1.1 skrll case A_DISP_REG_M:
1406 1.1 skrll case DSP_REG_M:
1407 1.1 skrll /* Opcode needs rn */
1408 1.1 skrll if (user->type != arg - A_REG_M + A_REG_N)
1409 1.1 skrll goto fail;
1410 1.1 skrll reg_m = user->reg;
1411 1.1 skrll break;
1412 1.1 skrll
1413 1.1 skrll case AS_DEC_N:
1414 1.1 skrll if (user->type != A_DEC_N)
1415 1.1 skrll goto fail;
1416 1.1 skrll if (user->reg < 2 || user->reg > 5)
1417 1.1 skrll goto fail;
1418 1.1 skrll reg_n = user->reg;
1419 1.1 skrll break;
1420 1.1 skrll
1421 1.1 skrll case AS_INC_N:
1422 1.1 skrll if (user->type != A_INC_N)
1423 1.1 skrll goto fail;
1424 1.1 skrll if (user->reg < 2 || user->reg > 5)
1425 1.1 skrll goto fail;
1426 1.1 skrll reg_n = user->reg;
1427 1.1 skrll break;
1428 1.1 skrll
1429 1.1 skrll case AS_IND_N:
1430 1.1 skrll if (user->type != A_IND_N)
1431 1.1 skrll goto fail;
1432 1.1 skrll if (user->reg < 2 || user->reg > 5)
1433 1.1 skrll goto fail;
1434 1.1 skrll reg_n = user->reg;
1435 1.1 skrll break;
1436 1.1 skrll
1437 1.1 skrll case AS_PMOD_N:
1438 1.1 skrll if (user->type != AX_PMOD_N)
1439 1.1 skrll goto fail;
1440 1.1 skrll if (user->reg < 2 || user->reg > 5)
1441 1.1 skrll goto fail;
1442 1.1 skrll reg_n = user->reg;
1443 1.1 skrll break;
1444 1.1 skrll
1445 1.1 skrll case AX_INC_N:
1446 1.1 skrll if (user->type != A_INC_N)
1447 1.1 skrll goto fail;
1448 1.1 skrll if (user->reg < 4 || user->reg > 5)
1449 1.1 skrll goto fail;
1450 1.1 skrll reg_n = user->reg;
1451 1.1 skrll break;
1452 1.1 skrll
1453 1.1 skrll case AX_IND_N:
1454 1.1 skrll if (user->type != A_IND_N)
1455 1.1 skrll goto fail;
1456 1.1 skrll if (user->reg < 4 || user->reg > 5)
1457 1.1 skrll goto fail;
1458 1.1 skrll reg_n = user->reg;
1459 1.1 skrll break;
1460 1.1 skrll
1461 1.1 skrll case AX_PMOD_N:
1462 1.1 skrll if (user->type != AX_PMOD_N)
1463 1.1 skrll goto fail;
1464 1.1 skrll if (user->reg < 4 || user->reg > 5)
1465 1.1 skrll goto fail;
1466 1.1 skrll reg_n = user->reg;
1467 1.1 skrll break;
1468 1.1 skrll
1469 1.1 skrll case AXY_INC_N:
1470 1.1 skrll if (user->type != A_INC_N)
1471 1.1 skrll goto fail;
1472 1.1 skrll if ((user->reg < 4 || user->reg > 5)
1473 1.1 skrll && (user->reg < 0 || user->reg > 1))
1474 1.1 skrll goto fail;
1475 1.1 skrll reg_n = user->reg;
1476 1.1 skrll break;
1477 1.1 skrll
1478 1.1 skrll case AXY_IND_N:
1479 1.1 skrll if (user->type != A_IND_N)
1480 1.1 skrll goto fail;
1481 1.1 skrll if ((user->reg < 4 || user->reg > 5)
1482 1.1 skrll && (user->reg < 0 || user->reg > 1))
1483 1.1 skrll goto fail;
1484 1.1 skrll reg_n = user->reg;
1485 1.1 skrll break;
1486 1.1 skrll
1487 1.1 skrll case AXY_PMOD_N:
1488 1.1 skrll if (user->type != AX_PMOD_N)
1489 1.1 skrll goto fail;
1490 1.1 skrll if ((user->reg < 4 || user->reg > 5)
1491 1.1 skrll && (user->reg < 0 || user->reg > 1))
1492 1.1 skrll goto fail;
1493 1.1 skrll reg_n = user->reg;
1494 1.1 skrll break;
1495 1.1 skrll
1496 1.1 skrll case AY_INC_N:
1497 1.1 skrll if (user->type != A_INC_N)
1498 1.1 skrll goto fail;
1499 1.1 skrll if (user->reg < 6 || user->reg > 7)
1500 1.1 skrll goto fail;
1501 1.1 skrll reg_n = user->reg;
1502 1.1 skrll break;
1503 1.1 skrll
1504 1.1 skrll case AY_IND_N:
1505 1.1 skrll if (user->type != A_IND_N)
1506 1.1 skrll goto fail;
1507 1.1 skrll if (user->reg < 6 || user->reg > 7)
1508 1.1 skrll goto fail;
1509 1.1 skrll reg_n = user->reg;
1510 1.1 skrll break;
1511 1.1 skrll
1512 1.1 skrll case AY_PMOD_N:
1513 1.1 skrll if (user->type != AY_PMOD_N)
1514 1.1 skrll goto fail;
1515 1.1 skrll if (user->reg < 6 || user->reg > 7)
1516 1.1 skrll goto fail;
1517 1.1 skrll reg_n = user->reg;
1518 1.1 skrll break;
1519 1.1 skrll
1520 1.1 skrll case AYX_INC_N:
1521 1.1 skrll if (user->type != A_INC_N)
1522 1.1 skrll goto fail;
1523 1.1 skrll if ((user->reg < 6 || user->reg > 7)
1524 1.1 skrll && (user->reg < 2 || user->reg > 3))
1525 1.1 skrll goto fail;
1526 1.1 skrll reg_n = user->reg;
1527 1.1 skrll break;
1528 1.1 skrll
1529 1.1 skrll case AYX_IND_N:
1530 1.1 skrll if (user->type != A_IND_N)
1531 1.1 skrll goto fail;
1532 1.1 skrll if ((user->reg < 6 || user->reg > 7)
1533 1.1 skrll && (user->reg < 2 || user->reg > 3))
1534 1.1 skrll goto fail;
1535 1.1 skrll reg_n = user->reg;
1536 1.1 skrll break;
1537 1.1 skrll
1538 1.1 skrll case AYX_PMOD_N:
1539 1.1 skrll if (user->type != AY_PMOD_N)
1540 1.1 skrll goto fail;
1541 1.1 skrll if ((user->reg < 6 || user->reg > 7)
1542 1.1 skrll && (user->reg < 2 || user->reg > 3))
1543 1.1 skrll goto fail;
1544 1.1 skrll reg_n = user->reg;
1545 1.1 skrll break;
1546 1.1 skrll
1547 1.1 skrll case DSP_REG_A_M:
1548 1.1 skrll if (user->type != DSP_REG_N)
1549 1.1 skrll goto fail;
1550 1.1 skrll if (user->reg != A_A0_NUM
1551 1.1 skrll && user->reg != A_A1_NUM)
1552 1.1 skrll goto fail;
1553 1.1 skrll reg_m = user->reg;
1554 1.1 skrll break;
1555 1.1 skrll
1556 1.1 skrll case DSP_REG_AX:
1557 1.1 skrll if (user->type != DSP_REG_N)
1558 1.1 skrll goto fail;
1559 1.1 skrll switch (user->reg)
1560 1.1 skrll {
1561 1.1 skrll case A_A0_NUM:
1562 1.1 skrll reg_x = 0;
1563 1.1 skrll break;
1564 1.1 skrll case A_A1_NUM:
1565 1.1 skrll reg_x = 2;
1566 1.1 skrll break;
1567 1.1 skrll case A_X0_NUM:
1568 1.1 skrll reg_x = 1;
1569 1.1 skrll break;
1570 1.1 skrll case A_X1_NUM:
1571 1.1 skrll reg_x = 3;
1572 1.1 skrll break;
1573 1.1 skrll default:
1574 1.1 skrll goto fail;
1575 1.1 skrll }
1576 1.1 skrll break;
1577 1.1 skrll
1578 1.1 skrll case DSP_REG_XY:
1579 1.1 skrll if (user->type != DSP_REG_N)
1580 1.1 skrll goto fail;
1581 1.1 skrll switch (user->reg)
1582 1.1 skrll {
1583 1.1 skrll case A_X0_NUM:
1584 1.1 skrll reg_x = 0;
1585 1.1 skrll break;
1586 1.1 skrll case A_X1_NUM:
1587 1.1 skrll reg_x = 2;
1588 1.1 skrll break;
1589 1.1 skrll case A_Y0_NUM:
1590 1.1 skrll reg_x = 1;
1591 1.1 skrll break;
1592 1.1 skrll case A_Y1_NUM:
1593 1.1 skrll reg_x = 3;
1594 1.1 skrll break;
1595 1.1 skrll default:
1596 1.1 skrll goto fail;
1597 1.1 skrll }
1598 1.1 skrll break;
1599 1.1 skrll
1600 1.1 skrll case DSP_REG_AY:
1601 1.1 skrll if (user->type != DSP_REG_N)
1602 1.1 skrll goto fail;
1603 1.1 skrll switch (user->reg)
1604 1.1 skrll {
1605 1.1 skrll case A_A0_NUM:
1606 1.1 skrll reg_y = 0;
1607 1.1 skrll break;
1608 1.1 skrll case A_A1_NUM:
1609 1.1 skrll reg_y = 1;
1610 1.1 skrll break;
1611 1.1 skrll case A_Y0_NUM:
1612 1.1 skrll reg_y = 2;
1613 1.1 skrll break;
1614 1.1 skrll case A_Y1_NUM:
1615 1.1 skrll reg_y = 3;
1616 1.1 skrll break;
1617 1.1 skrll default:
1618 1.1 skrll goto fail;
1619 1.1 skrll }
1620 1.1 skrll break;
1621 1.1 skrll
1622 1.1 skrll case DSP_REG_YX:
1623 1.1 skrll if (user->type != DSP_REG_N)
1624 1.1 skrll goto fail;
1625 1.1 skrll switch (user->reg)
1626 1.1 skrll {
1627 1.1 skrll case A_Y0_NUM:
1628 1.1 skrll reg_y = 0;
1629 1.1 skrll break;
1630 1.1 skrll case A_Y1_NUM:
1631 1.1 skrll reg_y = 1;
1632 1.1 skrll break;
1633 1.1 skrll case A_X0_NUM:
1634 1.1 skrll reg_y = 2;
1635 1.1 skrll break;
1636 1.1 skrll case A_X1_NUM:
1637 1.1 skrll reg_y = 3;
1638 1.1 skrll break;
1639 1.1 skrll default:
1640 1.1 skrll goto fail;
1641 1.1 skrll }
1642 1.1 skrll break;
1643 1.1 skrll
1644 1.1 skrll case DSP_REG_X:
1645 1.1 skrll if (user->type != DSP_REG_N)
1646 1.1 skrll goto fail;
1647 1.1 skrll switch (user->reg)
1648 1.1 skrll {
1649 1.1 skrll case A_X0_NUM:
1650 1.1 skrll reg_x = 0;
1651 1.1 skrll break;
1652 1.1 skrll case A_X1_NUM:
1653 1.1 skrll reg_x = 1;
1654 1.1 skrll break;
1655 1.1 skrll case A_A0_NUM:
1656 1.1 skrll reg_x = 2;
1657 1.1 skrll break;
1658 1.1 skrll case A_A1_NUM:
1659 1.1 skrll reg_x = 3;
1660 1.1 skrll break;
1661 1.1 skrll default:
1662 1.1 skrll goto fail;
1663 1.1 skrll }
1664 1.1 skrll break;
1665 1.1 skrll
1666 1.1 skrll case DSP_REG_Y:
1667 1.1 skrll if (user->type != DSP_REG_N)
1668 1.1 skrll goto fail;
1669 1.1 skrll switch (user->reg)
1670 1.1 skrll {
1671 1.1 skrll case A_Y0_NUM:
1672 1.1 skrll reg_y = 0;
1673 1.1 skrll break;
1674 1.1 skrll case A_Y1_NUM:
1675 1.1 skrll reg_y = 1;
1676 1.1 skrll break;
1677 1.1 skrll case A_M0_NUM:
1678 1.1 skrll reg_y = 2;
1679 1.1 skrll break;
1680 1.1 skrll case A_M1_NUM:
1681 1.1 skrll reg_y = 3;
1682 1.1 skrll break;
1683 1.1 skrll default:
1684 1.1 skrll goto fail;
1685 1.1 skrll }
1686 1.1 skrll break;
1687 1.1 skrll
1688 1.1 skrll case DSP_REG_E:
1689 1.1 skrll if (user->type != DSP_REG_N)
1690 1.1 skrll goto fail;
1691 1.1 skrll switch (user->reg)
1692 1.1 skrll {
1693 1.1 skrll case A_X0_NUM:
1694 1.1 skrll reg_efg = 0 << 10;
1695 1.1 skrll break;
1696 1.1 skrll case A_X1_NUM:
1697 1.1 skrll reg_efg = 1 << 10;
1698 1.1 skrll break;
1699 1.1 skrll case A_Y0_NUM:
1700 1.1 skrll reg_efg = 2 << 10;
1701 1.1 skrll break;
1702 1.1 skrll case A_A1_NUM:
1703 1.1 skrll reg_efg = 3 << 10;
1704 1.1 skrll break;
1705 1.1 skrll default:
1706 1.1 skrll goto fail;
1707 1.1 skrll }
1708 1.1 skrll break;
1709 1.1 skrll
1710 1.1 skrll case DSP_REG_F:
1711 1.1 skrll if (user->type != DSP_REG_N)
1712 1.1 skrll goto fail;
1713 1.1 skrll switch (user->reg)
1714 1.1 skrll {
1715 1.1 skrll case A_Y0_NUM:
1716 1.1 skrll reg_efg |= 0 << 8;
1717 1.1 skrll break;
1718 1.1 skrll case A_Y1_NUM:
1719 1.1 skrll reg_efg |= 1 << 8;
1720 1.1 skrll break;
1721 1.1 skrll case A_X0_NUM:
1722 1.1 skrll reg_efg |= 2 << 8;
1723 1.1 skrll break;
1724 1.1 skrll case A_A1_NUM:
1725 1.1 skrll reg_efg |= 3 << 8;
1726 1.1 skrll break;
1727 1.1 skrll default:
1728 1.1 skrll goto fail;
1729 1.1 skrll }
1730 1.1 skrll break;
1731 1.1 skrll
1732 1.1 skrll case DSP_REG_G:
1733 1.1 skrll if (user->type != DSP_REG_N)
1734 1.1 skrll goto fail;
1735 1.1 skrll switch (user->reg)
1736 1.1 skrll {
1737 1.1 skrll case A_M0_NUM:
1738 1.1 skrll reg_efg |= 0 << 2;
1739 1.1 skrll break;
1740 1.1 skrll case A_M1_NUM:
1741 1.1 skrll reg_efg |= 1 << 2;
1742 1.1 skrll break;
1743 1.1 skrll case A_A0_NUM:
1744 1.1 skrll reg_efg |= 2 << 2;
1745 1.1 skrll break;
1746 1.1 skrll case A_A1_NUM:
1747 1.1 skrll reg_efg |= 3 << 2;
1748 1.1 skrll break;
1749 1.1 skrll default:
1750 1.1 skrll goto fail;
1751 1.1 skrll }
1752 1.1 skrll break;
1753 1.1 skrll
1754 1.1 skrll case A_A0:
1755 1.1 skrll if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1756 1.1 skrll goto fail;
1757 1.1 skrll break;
1758 1.1 skrll case A_X0:
1759 1.1 skrll if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1760 1.1 skrll goto fail;
1761 1.1 skrll break;
1762 1.1 skrll case A_X1:
1763 1.1 skrll if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1764 1.1 skrll goto fail;
1765 1.1 skrll break;
1766 1.1 skrll case A_Y0:
1767 1.1 skrll if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1768 1.1 skrll goto fail;
1769 1.1 skrll break;
1770 1.1 skrll case A_Y1:
1771 1.1 skrll if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1772 1.1 skrll goto fail;
1773 1.1 skrll break;
1774 1.1 skrll
1775 1.1 skrll case F_REG_M:
1776 1.1 skrll case D_REG_M:
1777 1.1 skrll case X_REG_M:
1778 1.1 skrll case V_REG_M:
1779 1.1 skrll case FPUL_M:
1780 1.1 skrll case FPSCR_M:
1781 1.1 skrll /* Opcode needs rn */
1782 1.1 skrll if (user->type != arg - F_REG_M + F_REG_N)
1783 1.1 skrll goto fail;
1784 1.1 skrll reg_m = user->reg;
1785 1.1 skrll break;
1786 1.1 skrll case DX_REG_M:
1787 1.1 skrll if (user->type != D_REG_N && user->type != X_REG_N)
1788 1.1 skrll goto fail;
1789 1.1 skrll reg_m = user->reg;
1790 1.1 skrll break;
1791 1.1 skrll case XMTRX_M4:
1792 1.1 skrll if (user->type != XMTRX_M4)
1793 1.1 skrll goto fail;
1794 1.1 skrll reg_m = 4;
1795 1.1 skrll break;
1796 1.1 skrll
1797 1.1 skrll default:
1798 1.1 skrll printf (_("unhandled %d\n"), arg);
1799 1.1.1.2 christos goto fail;
1800 1.1.1.2 christos }
1801 1.1.1.2 christos if (SH_MERGE_ARCH_SET_VALID (valid_arch, arch_sh2a_nofpu_up)
1802 1.1.1.2 christos && ( arg == A_DISP_REG_M
1803 1.1.1.2 christos || arg == A_DISP_REG_N))
1804 1.1.1.2 christos {
1805 1.1.1.2 christos /* Check a few key IMM* fields for overflow. */
1806 1.1.1.2 christos int opf;
1807 1.1.1.2 christos long val = user->immediate.X_add_number;
1808 1.1.1.2 christos
1809 1.1.1.2 christos for (opf = 0; opf < 4; opf ++)
1810 1.1.1.2 christos switch (this_try->nibbles[opf])
1811 1.1.1.2 christos {
1812 1.1.1.2 christos case IMM0_4:
1813 1.1.1.2 christos case IMM1_4:
1814 1.1.1.2 christos if (val < 0 || val > 15)
1815 1.1.1.2 christos goto fail;
1816 1.1.1.2 christos break;
1817 1.1.1.2 christos case IMM0_4BY2:
1818 1.1.1.2 christos case IMM1_4BY2:
1819 1.1.1.2 christos if (val < 0 || val > 15 * 2)
1820 1.1.1.2 christos goto fail;
1821 1.1.1.2 christos break;
1822 1.1.1.2 christos case IMM0_4BY4:
1823 1.1.1.2 christos case IMM1_4BY4:
1824 1.1.1.2 christos if (val < 0 || val > 15 * 4)
1825 1.1.1.2 christos goto fail;
1826 1.1.1.2 christos break;
1827 1.1.1.2 christos default:
1828 1.1.1.2 christos break;
1829 1.1 skrll }
1830 1.1 skrll }
1831 1.1 skrll }
1832 1.1 skrll if ( !SH_MERGE_ARCH_SET_VALID (valid_arch, this_try->arch))
1833 1.1 skrll goto fail;
1834 1.1 skrll valid_arch = SH_MERGE_ARCH_SET (valid_arch, this_try->arch);
1835 1.1 skrll return this_try;
1836 1.1 skrll fail:
1837 1.1 skrll ;
1838 1.1 skrll }
1839 1.1 skrll
1840 1.1 skrll return 0;
1841 1.1 skrll }
1842 1.1.1.5 christos
1843 1.1.1.5 christos static void
1844 1.1 skrll insert (char *where, bfd_reloc_code_real_type how, int pcrel,
1845 1.1 skrll sh_operand_info *op)
1846 1.1 skrll {
1847 1.1 skrll fix_new_exp (frag_now,
1848 1.1 skrll where - frag_now->fr_literal,
1849 1.1 skrll 2,
1850 1.1 skrll &op->immediate,
1851 1.1 skrll pcrel,
1852 1.1 skrll how);
1853 1.1 skrll }
1854 1.1.1.5 christos
1855 1.1.1.5 christos static void
1856 1.1 skrll insert4 (char * where, bfd_reloc_code_real_type how, int pcrel,
1857 1.1 skrll sh_operand_info * op)
1858 1.1 skrll {
1859 1.1 skrll fix_new_exp (frag_now,
1860 1.1 skrll where - frag_now->fr_literal,
1861 1.1 skrll 4,
1862 1.1 skrll & op->immediate,
1863 1.1 skrll pcrel,
1864 1.1 skrll how);
1865 1.1 skrll }
1866 1.1 skrll static void
1867 1.1 skrll build_relax (sh_opcode_info *opcode, sh_operand_info *op)
1868 1.1 skrll {
1869 1.1 skrll int high_byte = target_big_endian ? 0 : 1;
1870 1.1 skrll char *p;
1871 1.1 skrll
1872 1.1 skrll if (opcode->arg[0] == A_BDISP8)
1873 1.1 skrll {
1874 1.1 skrll int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
1875 1.1 skrll p = frag_var (rs_machine_dependent,
1876 1.1 skrll md_relax_table[C (what, COND32)].rlx_length,
1877 1.1 skrll md_relax_table[C (what, COND8)].rlx_length,
1878 1.1 skrll C (what, 0),
1879 1.1 skrll op->immediate.X_add_symbol,
1880 1.1 skrll op->immediate.X_add_number,
1881 1.1 skrll 0);
1882 1.1 skrll p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
1883 1.1 skrll }
1884 1.1 skrll else if (opcode->arg[0] == A_BDISP12)
1885 1.1 skrll {
1886 1.1 skrll p = frag_var (rs_machine_dependent,
1887 1.1 skrll md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
1888 1.1 skrll md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
1889 1.1 skrll C (UNCOND_JUMP, 0),
1890 1.1 skrll op->immediate.X_add_symbol,
1891 1.1 skrll op->immediate.X_add_number,
1892 1.1 skrll 0);
1893 1.1 skrll p[high_byte] = (opcode->nibbles[0] << 4);
1894 1.1 skrll }
1895 1.1 skrll
1896 1.1 skrll }
1897 1.1 skrll
1898 1.1 skrll /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
1899 1.1 skrll
1900 1.1 skrll static char *
1901 1.1 skrll insert_loop_bounds (char *output, sh_operand_info *operand)
1902 1.1 skrll {
1903 1.1 skrll symbolS *end_sym;
1904 1.1 skrll
1905 1.1 skrll /* Since the low byte of the opcode will be overwritten by the reloc, we
1906 1.1 skrll can just stash the high byte into both bytes and ignore endianness. */
1907 1.1 skrll output[0] = 0x8c;
1908 1.1 skrll output[1] = 0x8c;
1909 1.1 skrll insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1910 1.1 skrll insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1911 1.1 skrll
1912 1.1 skrll if (sh_relax)
1913 1.1.1.5 christos {
1914 1.1 skrll static int count = 0;
1915 1.1 skrll char name[11];
1916 1.1 skrll
1917 1.1 skrll /* If the last loop insn is a two-byte-insn, it is in danger of being
1918 1.1 skrll swapped with the insn after it. To prevent this, create a new
1919 1.1 skrll symbol - complete with SH_LABEL reloc - after the last loop insn.
1920 1.1 skrll If the last loop insn is four bytes long, the symbol will be
1921 1.1 skrll right in the middle, but four byte insns are not swapped anyways. */
1922 1.1 skrll /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
1923 1.1 skrll Hence a 9 digit number should be enough to count all REPEATs. */
1924 1.1 skrll sprintf (name, "_R%x", count++ & 0x3fffffff);
1925 1.1 skrll end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
1926 1.1 skrll /* Make this a local symbol. */
1927 1.1 skrll #ifdef OBJ_COFF
1928 1.1 skrll SF_SET_LOCAL (end_sym);
1929 1.1 skrll #endif /* OBJ_COFF */
1930 1.1 skrll symbol_table_insert (end_sym);
1931 1.1 skrll end_sym->sy_value = operand[1].immediate;
1932 1.1 skrll end_sym->sy_value.X_add_number += 2;
1933 1.1 skrll fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
1934 1.1 skrll }
1935 1.1 skrll
1936 1.1 skrll output = frag_more (2);
1937 1.1 skrll output[0] = 0x8e;
1938 1.1 skrll output[1] = 0x8e;
1939 1.1 skrll insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
1940 1.1 skrll insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
1941 1.1 skrll
1942 1.1 skrll return frag_more (2);
1943 1.1 skrll }
1944 1.1 skrll
1945 1.1 skrll /* Now we know what sort of opcodes it is, let's build the bytes. */
1946 1.1 skrll
1947 1.1 skrll static unsigned int
1948 1.1.1.2 christos build_Mytes (sh_opcode_info *opcode, sh_operand_info *operand)
1949 1.1 skrll {
1950 1.1 skrll int indx;
1951 1.1 skrll char nbuf[8];
1952 1.1 skrll char *output;
1953 1.1 skrll unsigned int size = 2;
1954 1.1.1.2 christos int low_byte = target_big_endian ? 1 : 0;
1955 1.1.1.2 christos int max_index = 4;
1956 1.1.1.2 christos bfd_reloc_code_real_type r_type;
1957 1.1.1.2 christos #ifdef OBJ_ELF
1958 1.1 skrll int unhandled_pic = 0;
1959 1.1 skrll #endif
1960 1.1 skrll
1961 1.1 skrll nbuf[0] = 0;
1962 1.1 skrll nbuf[1] = 0;
1963 1.1 skrll nbuf[2] = 0;
1964 1.1 skrll nbuf[3] = 0;
1965 1.1 skrll nbuf[4] = 0;
1966 1.1 skrll nbuf[5] = 0;
1967 1.1 skrll nbuf[6] = 0;
1968 1.1.1.2 christos nbuf[7] = 0;
1969 1.1.1.2 christos
1970 1.1.1.2 christos #ifdef OBJ_ELF
1971 1.1.1.2 christos for (indx = 0; indx < 3; indx++)
1972 1.1.1.2 christos if (opcode->arg[indx] == A_IMM
1973 1.1.1.2 christos && operand[indx].type == A_IMM
1974 1.1.1.2 christos && (operand[indx].immediate.X_op == O_PIC_reloc
1975 1.1.1.2 christos || sh_PIC_related_p (operand[indx].immediate.X_add_symbol)
1976 1.1.1.2 christos || sh_PIC_related_p (operand[indx].immediate.X_op_symbol)))
1977 1.1.1.2 christos unhandled_pic = 1;
1978 1.1 skrll #endif
1979 1.1 skrll
1980 1.1 skrll if (SH_MERGE_ARCH_SET (opcode->arch, arch_op32))
1981 1.1 skrll {
1982 1.1 skrll output = frag_more (4);
1983 1.1 skrll size = 4;
1984 1.1 skrll max_index = 8;
1985 1.1 skrll }
1986 1.1 skrll else
1987 1.1.1.2 christos output = frag_more (2);
1988 1.1 skrll
1989 1.1.1.2 christos for (indx = 0; indx < max_index; indx++)
1990 1.1 skrll {
1991 1.1 skrll sh_nibble_type i = opcode->nibbles[indx];
1992 1.1.1.2 christos if (i < 16)
1993 1.1 skrll {
1994 1.1 skrll nbuf[indx] = i;
1995 1.1 skrll }
1996 1.1 skrll else
1997 1.1 skrll {
1998 1.1 skrll switch (i)
1999 1.1 skrll {
2000 1.1.1.2 christos case REG_N:
2001 1.1 skrll case REG_N_D:
2002 1.1 skrll nbuf[indx] = reg_n;
2003 1.1.1.2 christos break;
2004 1.1 skrll case REG_M:
2005 1.1 skrll nbuf[indx] = reg_m;
2006 1.1 skrll break;
2007 1.1 skrll case SDT_REG_N:
2008 1.1.1.2 christos if (reg_n < 2 || reg_n > 5)
2009 1.1 skrll as_bad (_("Invalid register: 'r%d'"), reg_n);
2010 1.1 skrll nbuf[indx] = (reg_n & 3) | 4;
2011 1.1.1.2 christos break;
2012 1.1 skrll case REG_NM:
2013 1.1 skrll nbuf[indx] = reg_n | (reg_m >> 2);
2014 1.1.1.2 christos break;
2015 1.1 skrll case REG_B:
2016 1.1 skrll nbuf[indx] = reg_b | 0x08;
2017 1.1.1.2 christos break;
2018 1.1 skrll case REG_N_B01:
2019 1.1 skrll nbuf[indx] = reg_n | 0x01;
2020 1.1.1.2 christos break;
2021 1.1.1.6 christos case IMM0_3s:
2022 1.1 skrll nbuf[indx] |= 0x08;
2023 1.1 skrll /* Fall through. */
2024 1.1 skrll case IMM0_3c:
2025 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM3, 0, operand);
2026 1.1.1.2 christos break;
2027 1.1.1.6 christos case IMM0_3Us:
2028 1.1 skrll nbuf[indx] |= 0x80;
2029 1.1 skrll /* Fall through. */
2030 1.1 skrll case IMM0_3Uc:
2031 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM3U, 0, operand);
2032 1.1 skrll break;
2033 1.1 skrll case DISP0_12:
2034 1.1 skrll insert (output + 2, BFD_RELOC_SH_DISP12, 0, operand);
2035 1.1 skrll break;
2036 1.1 skrll case DISP0_12BY2:
2037 1.1 skrll insert (output + 2, BFD_RELOC_SH_DISP12BY2, 0, operand);
2038 1.1 skrll break;
2039 1.1 skrll case DISP0_12BY4:
2040 1.1 skrll insert (output + 2, BFD_RELOC_SH_DISP12BY4, 0, operand);
2041 1.1 skrll break;
2042 1.1 skrll case DISP0_12BY8:
2043 1.1 skrll insert (output + 2, BFD_RELOC_SH_DISP12BY8, 0, operand);
2044 1.1 skrll break;
2045 1.1 skrll case DISP1_12:
2046 1.1 skrll insert (output + 2, BFD_RELOC_SH_DISP12, 0, operand+1);
2047 1.1 skrll break;
2048 1.1 skrll case DISP1_12BY2:
2049 1.1 skrll insert (output + 2, BFD_RELOC_SH_DISP12BY2, 0, operand+1);
2050 1.1 skrll break;
2051 1.1 skrll case DISP1_12BY4:
2052 1.1 skrll insert (output + 2, BFD_RELOC_SH_DISP12BY4, 0, operand+1);
2053 1.1 skrll break;
2054 1.1 skrll case DISP1_12BY8:
2055 1.1 skrll insert (output + 2, BFD_RELOC_SH_DISP12BY8, 0, operand+1);
2056 1.1 skrll break;
2057 1.1 skrll case IMM0_20_4:
2058 1.1.1.2 christos break;
2059 1.1.1.2 christos case IMM0_20:
2060 1.1.1.2 christos r_type = BFD_RELOC_SH_DISP20;
2061 1.1.1.2 christos #ifdef OBJ_ELF
2062 1.1.1.2 christos if (sh_check_fixup (&operand->immediate, &r_type))
2063 1.1.1.2 christos as_bad (_("Invalid PIC expression."));
2064 1.1.1.2 christos unhandled_pic = 0;
2065 1.1 skrll #endif
2066 1.1 skrll insert4 (output, r_type, 0, operand);
2067 1.1 skrll break;
2068 1.1 skrll case IMM0_20BY8:
2069 1.1 skrll insert4 (output, BFD_RELOC_SH_DISP20BY8, 0, operand);
2070 1.1 skrll break;
2071 1.1 skrll case IMM0_4BY4:
2072 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
2073 1.1 skrll break;
2074 1.1 skrll case IMM0_4BY2:
2075 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
2076 1.1 skrll break;
2077 1.1 skrll case IMM0_4:
2078 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
2079 1.1 skrll break;
2080 1.1 skrll case IMM1_4BY4:
2081 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
2082 1.1 skrll break;
2083 1.1 skrll case IMM1_4BY2:
2084 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
2085 1.1 skrll break;
2086 1.1 skrll case IMM1_4:
2087 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
2088 1.1 skrll break;
2089 1.1 skrll case IMM0_8BY4:
2090 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
2091 1.1 skrll break;
2092 1.1 skrll case IMM0_8BY2:
2093 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
2094 1.1 skrll break;
2095 1.1 skrll case IMM0_8:
2096 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
2097 1.1 skrll break;
2098 1.1 skrll case IMM1_8BY4:
2099 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
2100 1.1 skrll break;
2101 1.1 skrll case IMM1_8BY2:
2102 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
2103 1.1 skrll break;
2104 1.1 skrll case IMM1_8:
2105 1.1 skrll insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
2106 1.1 skrll break;
2107 1.1 skrll case PCRELIMM_8BY4:
2108 1.1 skrll insert (output, BFD_RELOC_SH_PCRELIMM8BY4,
2109 1.1 skrll operand->type != A_DISP_PC_ABS, operand);
2110 1.1 skrll break;
2111 1.1 skrll case PCRELIMM_8BY2:
2112 1.1 skrll insert (output, BFD_RELOC_SH_PCRELIMM8BY2,
2113 1.1 skrll operand->type != A_DISP_PC_ABS, operand);
2114 1.1 skrll break;
2115 1.1.1.2 christos case REPEAT:
2116 1.1 skrll output = insert_loop_bounds (output, operand);
2117 1.1 skrll nbuf[indx] = opcode->nibbles[3];
2118 1.1 skrll operand += 2;
2119 1.1 skrll break;
2120 1.1 skrll default:
2121 1.1 skrll printf (_("failed for %d\n"), i);
2122 1.1 skrll }
2123 1.1.1.2 christos }
2124 1.1.1.2 christos }
2125 1.1.1.2 christos #ifdef OBJ_ELF
2126 1.1.1.2 christos if (unhandled_pic)
2127 1.1 skrll as_bad (_("misplaced PIC operand"));
2128 1.1 skrll #endif
2129 1.1 skrll if (!target_big_endian)
2130 1.1 skrll {
2131 1.1 skrll output[1] = (nbuf[0] << 4) | (nbuf[1]);
2132 1.1 skrll output[0] = (nbuf[2] << 4) | (nbuf[3]);
2133 1.1 skrll }
2134 1.1 skrll else
2135 1.1 skrll {
2136 1.1 skrll output[0] = (nbuf[0] << 4) | (nbuf[1]);
2137 1.1 skrll output[1] = (nbuf[2] << 4) | (nbuf[3]);
2138 1.1 skrll }
2139 1.1 skrll if (SH_MERGE_ARCH_SET (opcode->arch, arch_op32))
2140 1.1 skrll {
2141 1.1 skrll if (!target_big_endian)
2142 1.1 skrll {
2143 1.1 skrll output[3] = (nbuf[4] << 4) | (nbuf[5]);
2144 1.1 skrll output[2] = (nbuf[6] << 4) | (nbuf[7]);
2145 1.1 skrll }
2146 1.1 skrll else
2147 1.1 skrll {
2148 1.1 skrll output[2] = (nbuf[4] << 4) | (nbuf[5]);
2149 1.1 skrll output[3] = (nbuf[6] << 4) | (nbuf[7]);
2150 1.1 skrll }
2151 1.1 skrll }
2152 1.1 skrll return size;
2153 1.1 skrll }
2154 1.1 skrll
2155 1.1 skrll /* Find an opcode at the start of *STR_P in the hash table, and set
2156 1.1 skrll *STR_P to the first character after the last one read. */
2157 1.1 skrll
2158 1.1 skrll static sh_opcode_info *
2159 1.1 skrll find_cooked_opcode (char **str_p)
2160 1.1 skrll {
2161 1.1 skrll char *str = *str_p;
2162 1.1 skrll unsigned char *op_start;
2163 1.1.1.2 christos unsigned char *op_end;
2164 1.1 skrll char name[20];
2165 1.1 skrll unsigned int nlen = 0;
2166 1.1 skrll
2167 1.1 skrll /* Drop leading whitespace. */
2168 1.1 skrll while (*str == ' ')
2169 1.1 skrll str++;
2170 1.1 skrll
2171 1.1 skrll /* Find the op code end.
2172 1.1 skrll The pre-processor will eliminate whitespace in front of
2173 1.1 skrll any '@' after the first argument; we may be called from
2174 1.1 skrll assemble_ppi, so the opcode might be terminated by an '@'. */
2175 1.1.1.2 christos for (op_start = op_end = (unsigned char *) str;
2176 1.1 skrll *op_end
2177 1.1 skrll && nlen < sizeof (name) - 1
2178 1.1 skrll && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
2179 1.1 skrll op_end++)
2180 1.1 skrll {
2181 1.1 skrll unsigned char c = op_start[nlen];
2182 1.1 skrll
2183 1.1 skrll /* The machine independent code will convert CMP/EQ into cmp/EQ
2184 1.1 skrll because it thinks the '/' is the end of the symbol. Moreover,
2185 1.1 skrll all but the first sub-insn is a parallel processing insn won't
2186 1.1 skrll be capitalized. Instead of hacking up the machine independent
2187 1.1 skrll code, we just deal with it here. */
2188 1.1 skrll c = TOLOWER (c);
2189 1.1 skrll name[nlen] = c;
2190 1.1 skrll nlen++;
2191 1.1 skrll }
2192 1.1 skrll
2193 1.1 skrll name[nlen] = 0;
2194 1.1 skrll *str_p = (char *) op_end;
2195 1.1 skrll
2196 1.1 skrll if (nlen == 0)
2197 1.1 skrll as_bad (_("can't find opcode "));
2198 1.1 skrll
2199 1.1 skrll return (sh_opcode_info *) hash_find (opcode_hash_control, name);
2200 1.1 skrll }
2201 1.1 skrll
2202 1.1 skrll /* Assemble a parallel processing insn. */
2203 1.1 skrll #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
2204 1.1 skrll
2205 1.1 skrll static unsigned int
2206 1.1 skrll assemble_ppi (char *op_end, sh_opcode_info *opcode)
2207 1.1 skrll {
2208 1.1 skrll int movx = 0;
2209 1.1 skrll int movy = 0;
2210 1.1 skrll int cond = 0;
2211 1.1 skrll int field_b = 0;
2212 1.1 skrll char *output;
2213 1.1 skrll int move_code;
2214 1.1 skrll unsigned int size;
2215 1.1 skrll
2216 1.1 skrll for (;;)
2217 1.1 skrll {
2218 1.1 skrll sh_operand_info operand[3];
2219 1.1 skrll
2220 1.1 skrll /* Some insn ignore one or more register fields, e.g. psts machl,a0.
2221 1.1 skrll Make sure we encode a defined insn pattern. */
2222 1.1 skrll reg_x = 0;
2223 1.1 skrll reg_y = 0;
2224 1.1 skrll reg_n = 0;
2225 1.1 skrll
2226 1.1 skrll if (opcode->arg[0] != A_END)
2227 1.1 skrll op_end = get_operands (opcode, op_end, operand);
2228 1.1 skrll try_another_opcode:
2229 1.1 skrll opcode = get_specific (opcode, operand);
2230 1.1 skrll if (opcode == 0)
2231 1.1 skrll {
2232 1.1 skrll /* Couldn't find an opcode which matched the operands. */
2233 1.1 skrll char *where = frag_more (2);
2234 1.1 skrll size = 2;
2235 1.1 skrll
2236 1.1 skrll where[0] = 0x0;
2237 1.1 skrll where[1] = 0x0;
2238 1.1 skrll as_bad (_("invalid operands for opcode"));
2239 1.1 skrll return size;
2240 1.1 skrll }
2241 1.1 skrll
2242 1.1 skrll if (opcode->nibbles[0] != PPI)
2243 1.1 skrll as_bad (_("insn can't be combined with parallel processing insn"));
2244 1.1 skrll
2245 1.1 skrll switch (opcode->nibbles[1])
2246 1.1 skrll {
2247 1.1 skrll
2248 1.1 skrll case NOPX:
2249 1.1 skrll if (movx)
2250 1.1 skrll as_bad (_("multiple movx specifications"));
2251 1.1 skrll movx = DDT_BASE;
2252 1.1 skrll break;
2253 1.1 skrll case NOPY:
2254 1.1 skrll if (movy)
2255 1.1 skrll as_bad (_("multiple movy specifications"));
2256 1.1 skrll movy = DDT_BASE;
2257 1.1 skrll break;
2258 1.1 skrll
2259 1.1 skrll case MOVX_NOPY:
2260 1.1 skrll if (movx)
2261 1.1 skrll as_bad (_("multiple movx specifications"));
2262 1.1 skrll if ((reg_n < 4 || reg_n > 5)
2263 1.1 skrll && (reg_n < 0 || reg_n > 1))
2264 1.1 skrll as_bad (_("invalid movx address register"));
2265 1.1 skrll if (movy && movy != DDT_BASE)
2266 1.1 skrll as_bad (_("insn cannot be combined with non-nopy"));
2267 1.1 skrll movx = ((((reg_n & 1) != 0) << 9)
2268 1.1 skrll + (((reg_n & 4) == 0) << 8)
2269 1.1 skrll + (reg_x << 6)
2270 1.1 skrll + (opcode->nibbles[2] << 4)
2271 1.1 skrll + opcode->nibbles[3]
2272 1.1 skrll + DDT_BASE);
2273 1.1 skrll break;
2274 1.1 skrll
2275 1.1 skrll case MOVY_NOPX:
2276 1.1 skrll if (movy)
2277 1.1 skrll as_bad (_("multiple movy specifications"));
2278 1.1 skrll if ((reg_n < 6 || reg_n > 7)
2279 1.1 skrll && (reg_n < 2 || reg_n > 3))
2280 1.1 skrll as_bad (_("invalid movy address register"));
2281 1.1 skrll if (movx && movx != DDT_BASE)
2282 1.1 skrll as_bad (_("insn cannot be combined with non-nopx"));
2283 1.1 skrll movy = ((((reg_n & 1) != 0) << 8)
2284 1.1 skrll + (((reg_n & 4) == 0) << 9)
2285 1.1 skrll + (reg_y << 6)
2286 1.1 skrll + (opcode->nibbles[2] << 4)
2287 1.1 skrll + opcode->nibbles[3]
2288 1.1 skrll + DDT_BASE);
2289 1.1 skrll break;
2290 1.1 skrll
2291 1.1 skrll case MOVX:
2292 1.1 skrll if (movx)
2293 1.1 skrll as_bad (_("multiple movx specifications"));
2294 1.1 skrll if (movy & 0x2ac)
2295 1.1 skrll as_bad (_("previous movy requires nopx"));
2296 1.1 skrll if (reg_n < 4 || reg_n > 5)
2297 1.1 skrll as_bad (_("invalid movx address register"));
2298 1.1 skrll if (opcode->nibbles[2] & 8)
2299 1.1 skrll {
2300 1.1 skrll if (reg_m == A_A1_NUM)
2301 1.1 skrll movx = 1 << 7;
2302 1.1 skrll else if (reg_m != A_A0_NUM)
2303 1.1 skrll as_bad (_("invalid movx dsp register"));
2304 1.1 skrll }
2305 1.1 skrll else
2306 1.1 skrll {
2307 1.1 skrll if (reg_x > 1)
2308 1.1 skrll as_bad (_("invalid movx dsp register"));
2309 1.1 skrll movx = reg_x << 7;
2310 1.1 skrll }
2311 1.1 skrll movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
2312 1.1 skrll break;
2313 1.1 skrll
2314 1.1 skrll case MOVY:
2315 1.1 skrll if (movy)
2316 1.1 skrll as_bad (_("multiple movy specifications"));
2317 1.1 skrll if (movx & 0x153)
2318 1.1 skrll as_bad (_("previous movx requires nopy"));
2319 1.1 skrll if (opcode->nibbles[2] & 8)
2320 1.1 skrll {
2321 1.1 skrll /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
2322 1.1 skrll so add 8 more. */
2323 1.1 skrll movy = 8;
2324 1.1 skrll if (reg_m == A_A1_NUM)
2325 1.1 skrll movy += 1 << 6;
2326 1.1 skrll else if (reg_m != A_A0_NUM)
2327 1.1 skrll as_bad (_("invalid movy dsp register"));
2328 1.1 skrll }
2329 1.1 skrll else
2330 1.1 skrll {
2331 1.1 skrll if (reg_y > 1)
2332 1.1 skrll as_bad (_("invalid movy dsp register"));
2333 1.1 skrll movy = reg_y << 6;
2334 1.1 skrll }
2335 1.1 skrll if (reg_n < 6 || reg_n > 7)
2336 1.1 skrll as_bad (_("invalid movy address register"));
2337 1.1 skrll movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
2338 1.1 skrll break;
2339 1.1 skrll
2340 1.1 skrll case PSH:
2341 1.1 skrll if (operand[0].immediate.X_op != O_constant)
2342 1.1 skrll as_bad (_("dsp immediate shift value not constant"));
2343 1.1 skrll field_b = ((opcode->nibbles[2] << 12)
2344 1.1 skrll | (operand[0].immediate.X_add_number & 127) << 4
2345 1.1 skrll | reg_n);
2346 1.1 skrll break;
2347 1.1 skrll case PPI3NC:
2348 1.1 skrll if (cond)
2349 1.1 skrll {
2350 1.1 skrll opcode++;
2351 1.1 skrll goto try_another_opcode;
2352 1.1 skrll }
2353 1.1 skrll /* Fall through. */
2354 1.1 skrll case PPI3:
2355 1.1 skrll if (field_b)
2356 1.1 skrll as_bad (_("multiple parallel processing specifications"));
2357 1.1 skrll field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2358 1.1 skrll + (reg_x << 6) + (reg_y << 4) + reg_n);
2359 1.1 skrll switch (opcode->nibbles[4])
2360 1.1 skrll {
2361 1.1 skrll case HEX_0:
2362 1.1 skrll case HEX_XX00:
2363 1.1 skrll case HEX_00YY:
2364 1.1 skrll break;
2365 1.1 skrll case HEX_1:
2366 1.1 skrll case HEX_4:
2367 1.1 skrll field_b += opcode->nibbles[4] << 4;
2368 1.1 skrll break;
2369 1.1 skrll default:
2370 1.1 skrll abort ();
2371 1.1 skrll }
2372 1.1 skrll break;
2373 1.1 skrll case PDC:
2374 1.1 skrll if (cond)
2375 1.1 skrll as_bad (_("multiple condition specifications"));
2376 1.1 skrll cond = opcode->nibbles[2] << 8;
2377 1.1 skrll if (*op_end)
2378 1.1 skrll goto skip_cond_check;
2379 1.1 skrll break;
2380 1.1 skrll case PPIC:
2381 1.1 skrll if (field_b)
2382 1.1 skrll as_bad (_("multiple parallel processing specifications"));
2383 1.1 skrll field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2384 1.1 skrll + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
2385 1.1 skrll cond = 0;
2386 1.1 skrll switch (opcode->nibbles[4])
2387 1.1 skrll {
2388 1.1 skrll case HEX_0:
2389 1.1 skrll case HEX_XX00:
2390 1.1 skrll case HEX_00YY:
2391 1.1 skrll break;
2392 1.1 skrll case HEX_1:
2393 1.1 skrll case HEX_4:
2394 1.1 skrll field_b += opcode->nibbles[4] << 4;
2395 1.1 skrll break;
2396 1.1 skrll default:
2397 1.1 skrll abort ();
2398 1.1 skrll }
2399 1.1 skrll break;
2400 1.1 skrll case PMUL:
2401 1.1 skrll if (field_b)
2402 1.1 skrll {
2403 1.1 skrll if ((field_b & 0xef00) == 0xa100)
2404 1.1 skrll field_b -= 0x8100;
2405 1.1 skrll /* pclr Dz pmuls Se,Sf,Dg */
2406 1.1 skrll else if ((field_b & 0xff00) == 0x8d00
2407 1.1 skrll && (SH_MERGE_ARCH_SET_VALID (valid_arch, arch_sh4al_dsp_up)))
2408 1.1 skrll {
2409 1.1 skrll valid_arch = SH_MERGE_ARCH_SET (valid_arch, arch_sh4al_dsp_up);
2410 1.1 skrll field_b -= 0x8cf0;
2411 1.1 skrll }
2412 1.1 skrll else
2413 1.1 skrll as_bad (_("insn cannot be combined with pmuls"));
2414 1.1 skrll switch (field_b & 0xf)
2415 1.1 skrll {
2416 1.1 skrll case A_X0_NUM:
2417 1.1 skrll field_b += 0 - A_X0_NUM;
2418 1.1 skrll break;
2419 1.1 skrll case A_Y0_NUM:
2420 1.1 skrll field_b += 1 - A_Y0_NUM;
2421 1.1 skrll break;
2422 1.1 skrll case A_A0_NUM:
2423 1.1 skrll field_b += 2 - A_A0_NUM;
2424 1.1 skrll break;
2425 1.1 skrll case A_A1_NUM:
2426 1.1 skrll field_b += 3 - A_A1_NUM;
2427 1.1 skrll break;
2428 1.1 skrll default:
2429 1.1 skrll as_bad (_("bad combined pmuls output operand"));
2430 1.1 skrll }
2431 1.1 skrll /* Generate warning if the destination register for padd / psub
2432 1.1 skrll and pmuls is the same ( only for A0 or A1 ).
2433 1.1 skrll If the last nibble is 1010 then A0 is used in both
2434 1.1 skrll padd / psub and pmuls. If it is 1111 then A1 is used
2435 1.1 skrll as destination register in both padd / psub and pmuls. */
2436 1.1 skrll
2437 1.1 skrll if ((((field_b | reg_efg) & 0x000F) == 0x000A)
2438 1.1 skrll || (((field_b | reg_efg) & 0x000F) == 0x000F))
2439 1.1 skrll as_warn (_("destination register is same for parallel insns"));
2440 1.1 skrll }
2441 1.1 skrll field_b += 0x4000 + reg_efg;
2442 1.1 skrll break;
2443 1.1 skrll default:
2444 1.1 skrll abort ();
2445 1.1 skrll }
2446 1.1 skrll if (cond)
2447 1.1 skrll {
2448 1.1 skrll as_bad (_("condition not followed by conditionalizable insn"));
2449 1.1 skrll cond = 0;
2450 1.1 skrll }
2451 1.1 skrll if (! *op_end)
2452 1.1 skrll break;
2453 1.1 skrll skip_cond_check:
2454 1.1 skrll opcode = find_cooked_opcode (&op_end);
2455 1.1 skrll if (opcode == NULL)
2456 1.1 skrll {
2457 1.1 skrll (as_bad
2458 1.1 skrll (_("unrecognized characters at end of parallel processing insn")));
2459 1.1 skrll break;
2460 1.1 skrll }
2461 1.1 skrll }
2462 1.1 skrll
2463 1.1 skrll move_code = movx | movy;
2464 1.1 skrll if (field_b)
2465 1.1 skrll {
2466 1.1 skrll /* Parallel processing insn. */
2467 1.1 skrll unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
2468 1.1 skrll
2469 1.1 skrll output = frag_more (4);
2470 1.1 skrll size = 4;
2471 1.1 skrll if (! target_big_endian)
2472 1.1 skrll {
2473 1.1 skrll output[3] = ppi_code >> 8;
2474 1.1 skrll output[2] = ppi_code;
2475 1.1 skrll }
2476 1.1 skrll else
2477 1.1 skrll {
2478 1.1 skrll output[2] = ppi_code >> 8;
2479 1.1 skrll output[3] = ppi_code;
2480 1.1 skrll }
2481 1.1 skrll move_code |= 0xf800;
2482 1.1 skrll }
2483 1.1 skrll else
2484 1.1 skrll {
2485 1.1 skrll /* Just a double data transfer. */
2486 1.1 skrll output = frag_more (2);
2487 1.1 skrll size = 2;
2488 1.1 skrll }
2489 1.1 skrll if (! target_big_endian)
2490 1.1 skrll {
2491 1.1 skrll output[1] = move_code >> 8;
2492 1.1 skrll output[0] = move_code;
2493 1.1 skrll }
2494 1.1 skrll else
2495 1.1 skrll {
2496 1.1 skrll output[0] = move_code >> 8;
2497 1.1 skrll output[1] = move_code;
2498 1.1 skrll }
2499 1.1 skrll return size;
2500 1.1 skrll }
2501 1.1 skrll
2502 1.1 skrll /* This is the guts of the machine-dependent assembler. STR points to a
2503 1.1 skrll machine dependent instruction. This function is supposed to emit
2504 1.1 skrll the frags/bytes it assembles to. */
2505 1.1 skrll
2506 1.1 skrll void
2507 1.1 skrll md_assemble (char *str)
2508 1.1 skrll {
2509 1.1 skrll char *op_end;
2510 1.1 skrll sh_operand_info operand[3];
2511 1.1 skrll sh_opcode_info *opcode;
2512 1.1 skrll unsigned int size = 0;
2513 1.1 skrll char *initial_str = str;
2514 1.1 skrll
2515 1.1 skrll opcode = find_cooked_opcode (&str);
2516 1.1 skrll op_end = str;
2517 1.1 skrll
2518 1.1 skrll if (opcode == NULL)
2519 1.1 skrll {
2520 1.1 skrll /* The opcode is not in the hash table.
2521 1.1 skrll This means we definitely have an assembly failure,
2522 1.1 skrll but the instruction may be valid in another CPU variant.
2523 1.1 skrll In this case emit something better than 'unknown opcode'.
2524 1.1 skrll Search the full table in sh-opc.h to check. */
2525 1.1 skrll
2526 1.1 skrll char *name = initial_str;
2527 1.1 skrll int name_length = 0;
2528 1.1 skrll const sh_opcode_info *op;
2529 1.1 skrll int found = 0;
2530 1.1 skrll
2531 1.1 skrll /* identify opcode in string */
2532 1.1 skrll while (ISSPACE (*name))
2533 1.1 skrll {
2534 1.1 skrll name++;
2535 1.1 skrll }
2536 1.1 skrll while (!ISSPACE (name[name_length]))
2537 1.1 skrll {
2538 1.1 skrll name_length++;
2539 1.1 skrll }
2540 1.1 skrll
2541 1.1 skrll /* search for opcode in full list */
2542 1.1 skrll for (op = sh_table; op->name; op++)
2543 1.1 skrll {
2544 1.1 skrll if (strncasecmp (op->name, name, name_length) == 0
2545 1.1 skrll && op->name[name_length] == '\0')
2546 1.1 skrll {
2547 1.1 skrll found = 1;
2548 1.1 skrll break;
2549 1.1 skrll }
2550 1.1 skrll }
2551 1.1 skrll
2552 1.1 skrll if ( found )
2553 1.1 skrll {
2554 1.1 skrll as_bad (_("opcode not valid for this cpu variant"));
2555 1.1 skrll }
2556 1.1 skrll else
2557 1.1 skrll {
2558 1.1 skrll as_bad (_("unknown opcode"));
2559 1.1 skrll }
2560 1.1 skrll return;
2561 1.1 skrll }
2562 1.1 skrll
2563 1.1 skrll if (sh_relax
2564 1.1 skrll && ! seg_info (now_seg)->tc_segment_info_data.in_code)
2565 1.1 skrll {
2566 1.1 skrll /* Output a CODE reloc to tell the linker that the following
2567 1.1 skrll bytes are instructions, not data. */
2568 1.1 skrll fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2569 1.1 skrll BFD_RELOC_SH_CODE);
2570 1.1 skrll seg_info (now_seg)->tc_segment_info_data.in_code = 1;
2571 1.1 skrll }
2572 1.1 skrll
2573 1.1 skrll if (opcode->nibbles[0] == PPI)
2574 1.1 skrll {
2575 1.1 skrll size = assemble_ppi (op_end, opcode);
2576 1.1 skrll }
2577 1.1 skrll else
2578 1.1 skrll {
2579 1.1 skrll if (opcode->arg[0] == A_BDISP12
2580 1.1 skrll || opcode->arg[0] == A_BDISP8)
2581 1.1 skrll {
2582 1.1 skrll /* Since we skip get_specific here, we have to check & update
2583 1.1 skrll valid_arch now. */
2584 1.1 skrll if (SH_MERGE_ARCH_SET_VALID (valid_arch, opcode->arch))
2585 1.1 skrll valid_arch = SH_MERGE_ARCH_SET (valid_arch, opcode->arch);
2586 1.1 skrll else
2587 1.1 skrll as_bad (_("Delayed branches not available on SH1"));
2588 1.1 skrll parse_exp (op_end + 1, &operand[0]);
2589 1.1 skrll build_relax (opcode, &operand[0]);
2590 1.1 skrll
2591 1.1 skrll /* All branches are currently 16 bit. */
2592 1.1 skrll size = 2;
2593 1.1 skrll }
2594 1.1 skrll else
2595 1.1 skrll {
2596 1.1 skrll if (opcode->arg[0] == A_END)
2597 1.1 skrll {
2598 1.1 skrll /* Ignore trailing whitespace. If there is any, it has already
2599 1.1 skrll been compressed to a single space. */
2600 1.1 skrll if (*op_end == ' ')
2601 1.1 skrll op_end++;
2602 1.1 skrll }
2603 1.1 skrll else
2604 1.1 skrll {
2605 1.1 skrll op_end = get_operands (opcode, op_end, operand);
2606 1.1 skrll }
2607 1.1 skrll opcode = get_specific (opcode, operand);
2608 1.1 skrll
2609 1.1 skrll if (opcode == 0)
2610 1.1 skrll {
2611 1.1 skrll /* Couldn't find an opcode which matched the operands. */
2612 1.1 skrll char *where = frag_more (2);
2613 1.1 skrll size = 2;
2614 1.1 skrll
2615 1.1 skrll where[0] = 0x0;
2616 1.1 skrll where[1] = 0x0;
2617 1.1 skrll as_bad (_("invalid operands for opcode"));
2618 1.1 skrll }
2619 1.1 skrll else
2620 1.1 skrll {
2621 1.1 skrll if (*op_end)
2622 1.1 skrll as_bad (_("excess operands: '%s'"), op_end);
2623 1.1 skrll
2624 1.1 skrll size = build_Mytes (opcode, operand);
2625 1.1 skrll }
2626 1.1 skrll }
2627 1.1 skrll }
2628 1.1 skrll
2629 1.1 skrll dwarf2_emit_insn (size);
2630 1.1 skrll }
2631 1.1 skrll
2632 1.1 skrll /* This routine is called each time a label definition is seen. It
2633 1.1 skrll emits a BFD_RELOC_SH_LABEL reloc if necessary. */
2634 1.1 skrll
2635 1.1 skrll void
2636 1.1 skrll sh_frob_label (symbolS *sym)
2637 1.1 skrll {
2638 1.1 skrll static fragS *last_label_frag;
2639 1.1 skrll static int last_label_offset;
2640 1.1 skrll
2641 1.1 skrll if (sh_relax
2642 1.1 skrll && seg_info (now_seg)->tc_segment_info_data.in_code)
2643 1.1 skrll {
2644 1.1 skrll int offset;
2645 1.1 skrll
2646 1.1 skrll offset = frag_now_fix ();
2647 1.1 skrll if (frag_now != last_label_frag
2648 1.1 skrll || offset != last_label_offset)
2649 1.1 skrll {
2650 1.1 skrll fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
2651 1.1 skrll last_label_frag = frag_now;
2652 1.1 skrll last_label_offset = offset;
2653 1.1 skrll }
2654 1.1 skrll }
2655 1.1 skrll
2656 1.1 skrll dwarf2_emit_label (sym);
2657 1.1 skrll }
2658 1.1 skrll
2659 1.1 skrll /* This routine is called when the assembler is about to output some
2660 1.1 skrll data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
2661 1.1 skrll
2662 1.1 skrll void
2663 1.1 skrll sh_flush_pending_output (void)
2664 1.1 skrll {
2665 1.1 skrll if (sh_relax
2666 1.1 skrll && seg_info (now_seg)->tc_segment_info_data.in_code)
2667 1.1 skrll {
2668 1.1 skrll fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2669 1.1 skrll BFD_RELOC_SH_DATA);
2670 1.1 skrll seg_info (now_seg)->tc_segment_info_data.in_code = 0;
2671 1.1 skrll }
2672 1.1 skrll }
2673 1.1 skrll
2674 1.1 skrll symbolS *
2675 1.1 skrll md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
2676 1.1 skrll {
2677 1.1 skrll return 0;
2678 1.1 skrll }
2679 1.1 skrll
2680 1.1.1.5 christos /* Various routines to kill one day. */
2681 1.1 skrll
2682 1.1 skrll const char *
2683 1.1 skrll md_atof (int type, char *litP, int *sizeP)
2684 1.1 skrll {
2685 1.1 skrll return ieee_md_atof (type, litP, sizeP, target_big_endian);
2686 1.1 skrll }
2687 1.1 skrll
2688 1.1 skrll /* Handle the .uses pseudo-op. This pseudo-op is used just before a
2689 1.1 skrll call instruction. It refers to a label of the instruction which
2690 1.1 skrll loads the register which the call uses. We use it to generate a
2691 1.1 skrll special reloc for the linker. */
2692 1.1 skrll
2693 1.1 skrll static void
2694 1.1 skrll s_uses (int ignore ATTRIBUTE_UNUSED)
2695 1.1 skrll {
2696 1.1 skrll expressionS ex;
2697 1.1 skrll
2698 1.1 skrll if (! sh_relax)
2699 1.1 skrll as_warn (_(".uses pseudo-op seen when not relaxing"));
2700 1.1 skrll
2701 1.1 skrll expression (&ex);
2702 1.1 skrll
2703 1.1 skrll if (ex.X_op != O_symbol || ex.X_add_number != 0)
2704 1.1 skrll {
2705 1.1 skrll as_bad (_("bad .uses format"));
2706 1.1 skrll ignore_rest_of_line ();
2707 1.1 skrll return;
2708 1.1 skrll }
2709 1.1 skrll
2710 1.1 skrll fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
2711 1.1 skrll
2712 1.1 skrll demand_empty_rest_of_line ();
2713 1.1 skrll }
2714 1.1 skrll
2715 1.1 skrll enum options
2717 1.1 skrll {
2718 1.1 skrll OPTION_RELAX = OPTION_MD_BASE,
2719 1.1 skrll OPTION_BIG,
2720 1.1 skrll OPTION_LITTLE,
2721 1.1 skrll OPTION_SMALL,
2722 1.1 skrll OPTION_DSP,
2723 1.1 skrll OPTION_ISA,
2724 1.1.1.2 christos OPTION_RENESAS,
2725 1.1.1.2 christos OPTION_ALLOW_REG_PREFIX,
2726 1.1.1.2 christos OPTION_H_TICK_HEX,
2727 1.1 skrll #ifdef OBJ_ELF
2728 1.1 skrll OPTION_FDPIC,
2729 1.1 skrll #endif
2730 1.1 skrll OPTION_DUMMY /* Not used. This is just here to make it easy to add and subtract options from this enum. */
2731 1.1 skrll };
2732 1.1 skrll
2733 1.1 skrll const char *md_shortopts = "";
2734 1.1 skrll struct option md_longopts[] =
2735 1.1 skrll {
2736 1.1 skrll {"relax", no_argument, NULL, OPTION_RELAX},
2737 1.1 skrll {"big", no_argument, NULL, OPTION_BIG},
2738 1.1 skrll {"little", no_argument, NULL, OPTION_LITTLE},
2739 1.1 skrll /* The next two switches are here because the
2740 1.1 skrll generic parts of the linker testsuite uses them. */
2741 1.1 skrll {"EB", no_argument, NULL, OPTION_BIG},
2742 1.1 skrll {"EL", no_argument, NULL, OPTION_LITTLE},
2743 1.1 skrll {"small", no_argument, NULL, OPTION_SMALL},
2744 1.1 skrll {"dsp", no_argument, NULL, OPTION_DSP},
2745 1.1 skrll {"isa", required_argument, NULL, OPTION_ISA},
2746 1.1 skrll {"renesas", no_argument, NULL, OPTION_RENESAS},
2747 1.1 skrll {"allow-reg-prefix", no_argument, NULL, OPTION_ALLOW_REG_PREFIX},
2748 1.1.1.2 christos
2749 1.1.1.2 christos { "h-tick-hex", no_argument, NULL, OPTION_H_TICK_HEX },
2750 1.1.1.2 christos
2751 1.1.1.2 christos #ifdef OBJ_ELF
2752 1.1 skrll {"fdpic", no_argument, NULL, OPTION_FDPIC},
2753 1.1 skrll #endif
2754 1.1 skrll
2755 1.1 skrll {NULL, no_argument, NULL, 0}
2756 1.1 skrll };
2757 1.1.1.5 christos size_t md_longopts_size = sizeof (md_longopts);
2758 1.1 skrll
2759 1.1 skrll int
2760 1.1 skrll md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED)
2761 1.1 skrll {
2762 1.1 skrll switch (c)
2763 1.1 skrll {
2764 1.1 skrll case OPTION_RELAX:
2765 1.1 skrll sh_relax = 1;
2766 1.1 skrll break;
2767 1.1 skrll
2768 1.1 skrll case OPTION_BIG:
2769 1.1 skrll target_big_endian = 1;
2770 1.1 skrll break;
2771 1.1 skrll
2772 1.1 skrll case OPTION_LITTLE:
2773 1.1 skrll target_big_endian = 0;
2774 1.1 skrll break;
2775 1.1 skrll
2776 1.1 skrll case OPTION_SMALL:
2777 1.1 skrll sh_small = 1;
2778 1.1 skrll break;
2779 1.1 skrll
2780 1.1 skrll case OPTION_DSP:
2781 1.1 skrll preset_target_arch = arch_sh_up & ~(arch_sh_sp_fpu|arch_sh_dp_fpu);
2782 1.1 skrll break;
2783 1.1 skrll
2784 1.1 skrll case OPTION_RENESAS:
2785 1.1 skrll dont_adjust_reloc_32 = 1;
2786 1.1 skrll break;
2787 1.1 skrll
2788 1.1 skrll case OPTION_ALLOW_REG_PREFIX:
2789 1.1 skrll allow_dollar_register_prefix = 1;
2790 1.1 skrll break;
2791 1.1 skrll
2792 1.1 skrll case OPTION_ISA:
2793 1.1 skrll if (strcasecmp (arg, "dsp") == 0)
2794 1.1 skrll preset_target_arch = arch_sh_up & ~(arch_sh_sp_fpu|arch_sh_dp_fpu);
2795 1.1 skrll else if (strcasecmp (arg, "fp") == 0)
2796 1.1 skrll preset_target_arch = arch_sh_up & ~arch_sh_has_dsp;
2797 1.1 skrll else if (strcasecmp (arg, "any") == 0)
2798 1.1 skrll preset_target_arch = arch_sh_up;
2799 1.1 skrll else
2800 1.1 skrll {
2801 1.1 skrll extern const bfd_arch_info_type bfd_sh_arch;
2802 1.1 skrll bfd_arch_info_type const *bfd_arch = &bfd_sh_arch;
2803 1.1 skrll
2804 1.1 skrll preset_target_arch = 0;
2805 1.1.1.4 christos for (; bfd_arch; bfd_arch=bfd_arch->next)
2806 1.1 skrll {
2807 1.1 skrll int len = strlen(bfd_arch->printable_name);
2808 1.1 skrll
2809 1.1 skrll if (strncasecmp (bfd_arch->printable_name, arg, len) != 0)
2810 1.1 skrll continue;
2811 1.1 skrll
2812 1.1 skrll if (arg[len] == '\0')
2813 1.1 skrll preset_target_arch =
2814 1.1 skrll sh_get_arch_from_bfd_mach (bfd_arch->mach);
2815 1.1 skrll else if (strcasecmp(&arg[len], "-up") == 0)
2816 1.1 skrll preset_target_arch =
2817 1.1 skrll sh_get_arch_up_from_bfd_mach (bfd_arch->mach);
2818 1.1 skrll else
2819 1.1.1.4 christos continue;
2820 1.1 skrll break;
2821 1.1.1.2 christos }
2822 1.1 skrll
2823 1.1 skrll if (!preset_target_arch)
2824 1.1 skrll as_bad (_("Invalid argument to --isa option: %s"), arg);
2825 1.1 skrll }
2826 1.1 skrll break;
2827 1.1 skrll
2828 1.1 skrll case OPTION_H_TICK_HEX:
2829 1.1.1.2 christos enable_h_tick_hex = 1;
2830 1.1.1.2 christos break;
2831 1.1.1.2 christos
2832 1.1.1.2 christos #ifdef OBJ_ELF
2833 1.1.1.2 christos case OPTION_FDPIC:
2834 1.1.1.2 christos sh_fdpic = TRUE;
2835 1.1 skrll break;
2836 1.1 skrll #endif /* OBJ_ELF */
2837 1.1 skrll
2838 1.1 skrll default:
2839 1.1 skrll return 0;
2840 1.1 skrll }
2841 1.1 skrll
2842 1.1 skrll return 1;
2843 1.1 skrll }
2844 1.1 skrll
2845 1.1 skrll void
2846 1.1 skrll md_show_usage (FILE *stream)
2847 1.1 skrll {
2848 1.1 skrll fprintf (stream, _("\
2849 1.1 skrll SH options:\n\
2850 1.1 skrll --little generate little endian code\n\
2851 1.1 skrll --big generate big endian code\n\
2852 1.1 skrll --relax alter jump instructions for long displacements\n\
2853 1.1 skrll --renesas disable optimization with section symbol for\n\
2854 1.1 skrll compatibility with Renesas assembler.\n\
2855 1.1 skrll --small align sections to 4 byte boundaries, not 16\n\
2856 1.1 skrll --dsp enable sh-dsp insns, and disable floating-point ISAs.\n\
2857 1.1 skrll --allow-reg-prefix allow '$' as a register name prefix.\n\
2858 1.1 skrll --isa=[any use most appropriate isa\n\
2859 1.1 skrll | dsp same as '-dsp'\n\
2860 1.1 skrll | fp"));
2861 1.1 skrll {
2862 1.1 skrll extern const bfd_arch_info_type bfd_sh_arch;
2863 1.1.1.7 christos bfd_arch_info_type const *bfd_arch = &bfd_sh_arch;
2864 1.1.1.7 christos
2865 1.1.1.7 christos for (; bfd_arch; bfd_arch=bfd_arch->next)
2866 1.1.1.7 christos {
2867 1.1 skrll fprintf (stream, "\n | %s", bfd_arch->printable_name);
2868 1.1 skrll fprintf (stream, "\n | %s-up", bfd_arch->printable_name);
2869 1.1.1.2 christos }
2870 1.1.1.2 christos }
2871 1.1.1.2 christos fprintf (stream, "]\n");
2872 1.1.1.2 christos #ifdef OBJ_ELF
2873 1.1 skrll fprintf (stream, _("\
2874 1.1 skrll --fdpic generate an FDPIC object file\n"));
2875 1.1 skrll #endif /* OBJ_ELF */
2876 1.1 skrll }
2877 1.1 skrll
2878 1.1 skrll /* This struct is used to pass arguments to sh_count_relocs through
2880 1.1 skrll bfd_map_over_sections. */
2881 1.1 skrll
2882 1.1 skrll struct sh_count_relocs
2883 1.1 skrll {
2884 1.1 skrll /* Symbol we are looking for. */
2885 1.1 skrll symbolS *sym;
2886 1.1 skrll /* Count of relocs found. */
2887 1.1 skrll int count;
2888 1.1 skrll };
2889 1.1 skrll
2890 1.1 skrll /* Count the number of fixups in a section which refer to a particular
2891 1.1 skrll symbol. This is called via bfd_map_over_sections. */
2892 1.1 skrll
2893 1.1 skrll static void
2894 1.1 skrll sh_count_relocs (bfd *abfd ATTRIBUTE_UNUSED, segT sec, void *data)
2895 1.1 skrll {
2896 1.1 skrll struct sh_count_relocs *info = (struct sh_count_relocs *) data;
2897 1.1 skrll segment_info_type *seginfo;
2898 1.1 skrll symbolS *sym;
2899 1.1 skrll fixS *fix;
2900 1.1 skrll
2901 1.1 skrll seginfo = seg_info (sec);
2902 1.1 skrll if (seginfo == NULL)
2903 1.1 skrll return;
2904 1.1 skrll
2905 1.1 skrll sym = info->sym;
2906 1.1 skrll for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2907 1.1 skrll {
2908 1.1 skrll if (fix->fx_addsy == sym)
2909 1.1 skrll {
2910 1.1 skrll ++info->count;
2911 1.1 skrll fix->fx_tcbit = 1;
2912 1.1 skrll }
2913 1.1 skrll }
2914 1.1 skrll }
2915 1.1 skrll
2916 1.1 skrll /* Handle the count relocs for a particular section.
2917 1.1 skrll This is called via bfd_map_over_sections. */
2918 1.1 skrll
2919 1.1 skrll static void
2920 1.1 skrll sh_frob_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec,
2921 1.1 skrll void *ignore ATTRIBUTE_UNUSED)
2922 1.1 skrll {
2923 1.1 skrll segment_info_type *seginfo;
2924 1.1 skrll fixS *fix;
2925 1.1 skrll
2926 1.1 skrll seginfo = seg_info (sec);
2927 1.1 skrll if (seginfo == NULL)
2928 1.1 skrll return;
2929 1.1 skrll
2930 1.1 skrll for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2931 1.1 skrll {
2932 1.1 skrll symbolS *sym;
2933 1.1 skrll
2934 1.1 skrll sym = fix->fx_addsy;
2935 1.1 skrll /* Check for a local_symbol. */
2936 1.1 skrll if (sym && sym->bsym == NULL)
2937 1.1 skrll {
2938 1.1 skrll struct local_symbol *ls = (struct local_symbol *)sym;
2939 1.1 skrll /* See if it's been converted. If so, canonicalize. */
2940 1.1 skrll if (local_symbol_converted_p (ls))
2941 1.1 skrll fix->fx_addsy = local_symbol_get_real_symbol (ls);
2942 1.1 skrll }
2943 1.1 skrll }
2944 1.1 skrll
2945 1.1 skrll for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
2946 1.1 skrll {
2947 1.1 skrll symbolS *sym;
2948 1.1 skrll bfd_vma val;
2949 1.1 skrll fixS *fscan;
2950 1.1 skrll struct sh_count_relocs info;
2951 1.1 skrll
2952 1.1 skrll if (fix->fx_r_type != BFD_RELOC_SH_USES)
2953 1.1 skrll continue;
2954 1.1 skrll
2955 1.1 skrll /* The BFD_RELOC_SH_USES reloc should refer to a defined local
2956 1.1 skrll symbol in the same section. */
2957 1.1 skrll sym = fix->fx_addsy;
2958 1.1 skrll if (sym == NULL
2959 1.1 skrll || fix->fx_subsy != NULL
2960 1.1 skrll || fix->fx_addnumber != 0
2961 1.1 skrll || S_GET_SEGMENT (sym) != sec
2962 1.1 skrll || S_IS_EXTERNAL (sym))
2963 1.1 skrll {
2964 1.1 skrll as_warn_where (fix->fx_file, fix->fx_line,
2965 1.1 skrll _(".uses does not refer to a local symbol in the same section"));
2966 1.1 skrll continue;
2967 1.1 skrll }
2968 1.1 skrll
2969 1.1 skrll /* Look through the fixups again, this time looking for one
2970 1.1 skrll at the same location as sym. */
2971 1.1 skrll val = S_GET_VALUE (sym);
2972 1.1 skrll for (fscan = seginfo->fix_root;
2973 1.1 skrll fscan != NULL;
2974 1.1 skrll fscan = fscan->fx_next)
2975 1.1 skrll if (val == fscan->fx_frag->fr_address + fscan->fx_where
2976 1.1 skrll && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
2977 1.1 skrll && fscan->fx_r_type != BFD_RELOC_SH_CODE
2978 1.1 skrll && fscan->fx_r_type != BFD_RELOC_SH_DATA
2979 1.1 skrll && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
2980 1.1 skrll break;
2981 1.1 skrll if (fscan == NULL)
2982 1.1 skrll {
2983 1.1 skrll as_warn_where (fix->fx_file, fix->fx_line,
2984 1.1 skrll _("can't find fixup pointed to by .uses"));
2985 1.1 skrll continue;
2986 1.1 skrll }
2987 1.1 skrll
2988 1.1 skrll if (fscan->fx_tcbit)
2989 1.1 skrll {
2990 1.1 skrll /* We've already done this one. */
2991 1.1 skrll continue;
2992 1.1 skrll }
2993 1.1 skrll
2994 1.1 skrll /* The variable fscan should also be a fixup to a local symbol
2995 1.1 skrll in the same section. */
2996 1.1 skrll sym = fscan->fx_addsy;
2997 1.1 skrll if (sym == NULL
2998 1.1 skrll || fscan->fx_subsy != NULL
2999 1.1 skrll || fscan->fx_addnumber != 0
3000 1.1 skrll || S_GET_SEGMENT (sym) != sec
3001 1.1 skrll || S_IS_EXTERNAL (sym))
3002 1.1 skrll {
3003 1.1 skrll as_warn_where (fix->fx_file, fix->fx_line,
3004 1.1 skrll _(".uses target does not refer to a local symbol in the same section"));
3005 1.1 skrll continue;
3006 1.1 skrll }
3007 1.1 skrll
3008 1.1 skrll /* Now we look through all the fixups of all the sections,
3009 1.1 skrll counting the number of times we find a reference to sym. */
3010 1.1 skrll info.sym = sym;
3011 1.1 skrll info.count = 0;
3012 1.1 skrll bfd_map_over_sections (stdoutput, sh_count_relocs, &info);
3013 1.1 skrll
3014 1.1 skrll if (info.count < 1)
3015 1.1 skrll abort ();
3016 1.1 skrll
3017 1.1 skrll /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
3018 1.1 skrll We have already adjusted the value of sym to include the
3019 1.1 skrll fragment address, so we undo that adjustment here. */
3020 1.1 skrll subseg_change (sec, 0);
3021 1.1 skrll fix_new (fscan->fx_frag,
3022 1.1 skrll S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
3023 1.1 skrll 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
3024 1.1 skrll }
3025 1.1 skrll }
3026 1.1 skrll
3027 1.1 skrll /* This function is called after the symbol table has been completed,
3028 1.1 skrll but before the relocs or section contents have been written out.
3029 1.1 skrll If we have seen any .uses pseudo-ops, they point to an instruction
3030 1.1 skrll which loads a register with the address of a function. We look
3031 1.1 skrll through the fixups to find where the function address is being
3032 1.1 skrll loaded from. We then generate a COUNT reloc giving the number of
3033 1.1 skrll times that function address is referred to. The linker uses this
3034 1.1 skrll information when doing relaxing, to decide when it can eliminate
3035 1.1 skrll the stored function address entirely. */
3036 1.1 skrll
3037 1.1 skrll void
3038 1.1 skrll sh_frob_file (void)
3039 1.1 skrll {
3040 1.1 skrll if (! sh_relax)
3041 1.1 skrll return;
3042 1.1 skrll
3043 1.1 skrll bfd_map_over_sections (stdoutput, sh_frob_section, NULL);
3044 1.1 skrll }
3045 1.1 skrll
3046 1.1 skrll /* Called after relaxing. Set the correct sizes of the fragments, and
3047 1.1 skrll create relocs so that md_apply_fix will fill in the correct values. */
3048 1.1 skrll
3049 1.1 skrll void
3050 1.1 skrll md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT seg, fragS *fragP)
3051 1.1 skrll {
3052 1.1 skrll int donerelax = 0;
3053 1.1 skrll
3054 1.1 skrll switch (fragP->fr_subtype)
3055 1.1 skrll {
3056 1.1 skrll case C (COND_JUMP, COND8):
3057 1.1 skrll case C (COND_JUMP_DELAY, COND8):
3058 1.1 skrll subseg_change (seg, 0);
3059 1.1 skrll fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3060 1.1 skrll 1, BFD_RELOC_SH_PCDISP8BY2);
3061 1.1 skrll fragP->fr_fix += 2;
3062 1.1 skrll fragP->fr_var = 0;
3063 1.1 skrll break;
3064 1.1 skrll
3065 1.1 skrll case C (UNCOND_JUMP, UNCOND12):
3066 1.1 skrll subseg_change (seg, 0);
3067 1.1 skrll fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3068 1.1 skrll 1, BFD_RELOC_SH_PCDISP12BY2);
3069 1.1 skrll fragP->fr_fix += 2;
3070 1.1 skrll fragP->fr_var = 0;
3071 1.1 skrll break;
3072 1.1 skrll
3073 1.1 skrll case C (UNCOND_JUMP, UNCOND32):
3074 1.1 skrll case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3075 1.1 skrll if (fragP->fr_symbol == NULL)
3076 1.1 skrll as_bad_where (fragP->fr_file, fragP->fr_line,
3077 1.1 skrll _("displacement overflows 12-bit field"));
3078 1.1 skrll else if (S_IS_DEFINED (fragP->fr_symbol))
3079 1.1 skrll as_bad_where (fragP->fr_file, fragP->fr_line,
3080 1.1 skrll _("displacement to defined symbol %s overflows 12-bit field"),
3081 1.1 skrll S_GET_NAME (fragP->fr_symbol));
3082 1.1 skrll else
3083 1.1 skrll as_bad_where (fragP->fr_file, fragP->fr_line,
3084 1.1 skrll _("displacement to undefined symbol %s overflows 12-bit field"),
3085 1.1 skrll S_GET_NAME (fragP->fr_symbol));
3086 1.1 skrll /* Stabilize this frag, so we don't trip an assert. */
3087 1.1 skrll fragP->fr_fix += fragP->fr_var;
3088 1.1 skrll fragP->fr_var = 0;
3089 1.1 skrll break;
3090 1.1 skrll
3091 1.1 skrll case C (COND_JUMP, COND12):
3092 1.1 skrll case C (COND_JUMP_DELAY, COND12):
3093 1.1 skrll /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
3094 1.1 skrll /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
3095 1.1 skrll was due to gas incorrectly relaxing an out-of-range conditional
3096 1.1 skrll branch with delay slot. It turned:
3097 1.1 skrll bf.s L6 (slot mov.l r12,@(44,r0))
3098 1.1 skrll into:
3099 1.1 skrll
3100 1.1 skrll 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
3101 1.1 skrll 30: 00 09 nop
3102 1.1 skrll 32: 10 cb mov.l r12,@(44,r0)
3103 1.1 skrll Therefore, branches with delay slots have to be handled
3104 1.1 skrll differently from ones without delay slots. */
3105 1.1 skrll {
3106 1.1 skrll unsigned char *buffer =
3107 1.1 skrll (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
3108 1.1 skrll int highbyte = target_big_endian ? 0 : 1;
3109 1.1 skrll int lowbyte = target_big_endian ? 1 : 0;
3110 1.1 skrll int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
3111 1.1 skrll
3112 1.1 skrll /* Toggle the true/false bit of the bcond. */
3113 1.1 skrll buffer[highbyte] ^= 0x2;
3114 1.1 skrll
3115 1.1 skrll /* If this is a delayed branch, we may not put the bra in the
3116 1.1 skrll slot. So we change it to a non-delayed branch, like that:
3117 1.1 skrll b! cond slot_label; bra disp; slot_label: slot_insn
3118 1.1 skrll ??? We should try if swapping the conditional branch and
3119 1.1 skrll its delay-slot insn already makes the branch reach. */
3120 1.1 skrll
3121 1.1 skrll /* Build a relocation to six / four bytes farther on. */
3122 1.1 skrll subseg_change (seg, 0);
3123 1.1 skrll fix_new (fragP, fragP->fr_fix, 2, section_symbol (seg),
3124 1.1 skrll fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
3125 1.1 skrll 1, BFD_RELOC_SH_PCDISP8BY2);
3126 1.1 skrll
3127 1.1 skrll /* Set up a jump instruction. */
3128 1.1 skrll buffer[highbyte + 2] = 0xa0;
3129 1.1 skrll buffer[lowbyte + 2] = 0;
3130 1.1 skrll fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
3131 1.1 skrll fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
3132 1.1 skrll
3133 1.1 skrll if (delay)
3134 1.1 skrll {
3135 1.1 skrll buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
3136 1.1 skrll fragP->fr_fix += 4;
3137 1.1 skrll }
3138 1.1 skrll else
3139 1.1 skrll {
3140 1.1 skrll /* Fill in a NOP instruction. */
3141 1.1 skrll buffer[highbyte + 4] = 0x0;
3142 1.1 skrll buffer[lowbyte + 4] = 0x9;
3143 1.1 skrll
3144 1.1 skrll fragP->fr_fix += 6;
3145 1.1 skrll }
3146 1.1 skrll fragP->fr_var = 0;
3147 1.1 skrll donerelax = 1;
3148 1.1 skrll }
3149 1.1 skrll break;
3150 1.1 skrll
3151 1.1 skrll case C (COND_JUMP, COND32):
3152 1.1 skrll case C (COND_JUMP_DELAY, COND32):
3153 1.1 skrll case C (COND_JUMP, UNDEF_WORD_DISP):
3154 1.1 skrll case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3155 1.1 skrll if (fragP->fr_symbol == NULL)
3156 1.1 skrll as_bad_where (fragP->fr_file, fragP->fr_line,
3157 1.1 skrll _("displacement overflows 8-bit field"));
3158 1.1 skrll else if (S_IS_DEFINED (fragP->fr_symbol))
3159 1.1 skrll as_bad_where (fragP->fr_file, fragP->fr_line,
3160 1.1 skrll _("displacement to defined symbol %s overflows 8-bit field"),
3161 1.1 skrll S_GET_NAME (fragP->fr_symbol));
3162 1.1 skrll else
3163 1.1 skrll as_bad_where (fragP->fr_file, fragP->fr_line,
3164 1.1 skrll _("displacement to undefined symbol %s overflows 8-bit field "),
3165 1.1 skrll S_GET_NAME (fragP->fr_symbol));
3166 1.1 skrll /* Stabilize this frag, so we don't trip an assert. */
3167 1.1 skrll fragP->fr_fix += fragP->fr_var;
3168 1.1 skrll fragP->fr_var = 0;
3169 1.1 skrll break;
3170 1.1 skrll
3171 1.1 skrll default:
3172 1.1 skrll abort ();
3173 1.1 skrll }
3174 1.1 skrll
3175 1.1 skrll if (donerelax && !sh_relax)
3176 1.1 skrll as_warn_where (fragP->fr_file, fragP->fr_line,
3177 1.1 skrll _("overflow in branch to %s; converted into longer instruction sequence"),
3178 1.1 skrll (fragP->fr_symbol != NULL
3179 1.1 skrll ? S_GET_NAME (fragP->fr_symbol)
3180 1.1 skrll : ""));
3181 1.1 skrll }
3182 1.1 skrll
3183 1.1 skrll valueT
3184 1.1 skrll md_section_align (segT seg ATTRIBUTE_UNUSED, valueT size)
3185 1.1 skrll {
3186 1.1.1.4 christos #ifdef OBJ_ELF
3187 1.1 skrll return size;
3188 1.1 skrll #else /* ! OBJ_ELF */
3189 1.1 skrll return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
3190 1.1 skrll & -(1 << bfd_get_section_alignment (stdoutput, seg)));
3191 1.1 skrll #endif /* ! OBJ_ELF */
3192 1.1 skrll }
3193 1.1 skrll
3194 1.1 skrll /* This static variable is set by s_uacons to tell sh_cons_align that
3195 1.1 skrll the expression does not need to be aligned. */
3196 1.1 skrll
3197 1.1 skrll static int sh_no_align_cons = 0;
3198 1.1 skrll
3199 1.1 skrll /* This handles the unaligned space allocation pseudo-ops, such as
3200 1.1 skrll .uaword. .uaword is just like .word, but the value does not need
3201 1.1 skrll to be aligned. */
3202 1.1 skrll
3203 1.1 skrll static void
3204 1.1 skrll s_uacons (int bytes)
3205 1.1 skrll {
3206 1.1 skrll /* Tell sh_cons_align not to align this value. */
3207 1.1 skrll sh_no_align_cons = 1;
3208 1.1 skrll cons (bytes);
3209 1.1 skrll }
3210 1.1 skrll
3211 1.1 skrll /* If a .word, et. al., pseud-op is seen, warn if the value is not
3212 1.1 skrll aligned correctly. Note that this can cause warnings to be issued
3213 1.1 skrll when assembling initialized structured which were declared with the
3214 1.1 skrll packed attribute. FIXME: Perhaps we should require an option to
3215 1.1 skrll enable this warning? */
3216 1.1 skrll
3217 1.1 skrll void
3218 1.1 skrll sh_cons_align (int nbytes)
3219 1.1 skrll {
3220 1.1 skrll int nalign;
3221 1.1 skrll
3222 1.1 skrll if (sh_no_align_cons)
3223 1.1 skrll {
3224 1.1 skrll /* This is an unaligned pseudo-op. */
3225 1.1 skrll sh_no_align_cons = 0;
3226 1.1 skrll return;
3227 1.1 skrll }
3228 1.1 skrll
3229 1.1 skrll nalign = 0;
3230 1.1 skrll while ((nbytes & 1) == 0)
3231 1.1 skrll {
3232 1.1 skrll ++nalign;
3233 1.1 skrll nbytes >>= 1;
3234 1.1 skrll }
3235 1.1 skrll
3236 1.1 skrll if (nalign == 0)
3237 1.1 skrll return;
3238 1.1 skrll
3239 1.1 skrll if (now_seg == absolute_section)
3240 1.1 skrll {
3241 1.1 skrll if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3242 1.1.1.2 christos as_warn (_("misaligned data"));
3243 1.1.1.2 christos return;
3244 1.1 skrll }
3245 1.1 skrll
3246 1.1 skrll frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
3247 1.1 skrll (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3248 1.1 skrll
3249 1.1 skrll record_alignment (now_seg, nalign);
3250 1.1 skrll }
3251 1.1 skrll
3252 1.1 skrll /* When relaxing, we need to output a reloc for any .align directive
3253 1.1 skrll that requests alignment to a four byte boundary or larger. This is
3254 1.1 skrll also where we check for misaligned data. */
3255 1.1 skrll
3256 1.1 skrll void
3257 1.1 skrll sh_handle_align (fragS *frag)
3258 1.1 skrll {
3259 1.1 skrll int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
3260 1.1 skrll
3261 1.1 skrll if (frag->fr_type == rs_align_code)
3262 1.1 skrll {
3263 1.1 skrll static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
3264 1.1 skrll static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
3265 1.1 skrll
3266 1.1 skrll char *p = frag->fr_literal + frag->fr_fix;
3267 1.1 skrll
3268 1.1 skrll if (bytes & 1)
3269 1.1 skrll {
3270 1.1 skrll *p++ = 0;
3271 1.1 skrll bytes--;
3272 1.1 skrll frag->fr_fix += 1;
3273 1.1 skrll }
3274 1.1 skrll
3275 1.1 skrll if (target_big_endian)
3276 1.1 skrll {
3277 1.1 skrll memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
3278 1.1 skrll frag->fr_var = sizeof big_nop_pattern;
3279 1.1 skrll }
3280 1.1 skrll else
3281 1.1 skrll {
3282 1.1 skrll memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
3283 1.1 skrll frag->fr_var = sizeof little_nop_pattern;
3284 1.1 skrll }
3285 1.1 skrll }
3286 1.1 skrll else if (frag->fr_type == rs_align_test)
3287 1.1 skrll {
3288 1.1 skrll if (bytes != 0)
3289 1.1 skrll as_bad_where (frag->fr_file, frag->fr_line, _("misaligned data"));
3290 1.1 skrll }
3291 1.1 skrll
3292 1.1 skrll if (sh_relax
3293 1.1 skrll && (frag->fr_type == rs_align
3294 1.1 skrll || frag->fr_type == rs_align_code)
3295 1.1 skrll && frag->fr_address + frag->fr_fix > 0
3296 1.1 skrll && frag->fr_offset > 1
3297 1.1 skrll && now_seg != bss_section)
3298 1.1 skrll fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
3299 1.1 skrll BFD_RELOC_SH_ALIGN);
3300 1.1 skrll }
3301 1.1 skrll
3302 1.1 skrll /* See whether the relocation should be resolved locally. */
3303 1.1 skrll
3304 1.1 skrll static bfd_boolean
3305 1.1 skrll sh_local_pcrel (fixS *fix)
3306 1.1 skrll {
3307 1.1 skrll return (! sh_relax
3308 1.1 skrll && (fix->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
3309 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
3310 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
3311 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
3312 1.1 skrll || fix->fx_r_type == BFD_RELOC_8_PCREL
3313 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_SWITCH16
3314 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_SWITCH32));
3315 1.1 skrll }
3316 1.1 skrll
3317 1.1 skrll /* See whether we need to force a relocation into the output file.
3318 1.1 skrll This is used to force out switch and PC relative relocations when
3319 1.1 skrll relaxing. */
3320 1.1 skrll
3321 1.1 skrll int
3322 1.1 skrll sh_force_relocation (fixS *fix)
3323 1.1 skrll {
3324 1.1 skrll /* These relocations can't make it into a DSO, so no use forcing
3325 1.1 skrll them for global symbols. */
3326 1.1 skrll if (sh_local_pcrel (fix))
3327 1.1 skrll return 0;
3328 1.1 skrll
3329 1.1 skrll /* Make sure some relocations get emitted. */
3330 1.1 skrll if (fix->fx_r_type == BFD_RELOC_SH_LOOP_START
3331 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_LOOP_END
3332 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_TLS_GD_32
3333 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_TLS_LD_32
3334 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_TLS_IE_32
3335 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_TLS_LDO_32
3336 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_TLS_LE_32
3337 1.1 skrll || generic_force_reloc (fix))
3338 1.1 skrll return 1;
3339 1.1 skrll
3340 1.1 skrll if (! sh_relax)
3341 1.1 skrll return 0;
3342 1.1 skrll
3343 1.1 skrll return (fix->fx_pcrel
3344 1.1 skrll || SWITCH_TABLE (fix)
3345 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_COUNT
3346 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_ALIGN
3347 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_CODE
3348 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_DATA
3349 1.1 skrll || fix->fx_r_type == BFD_RELOC_SH_LABEL);
3350 1.1 skrll }
3351 1.1 skrll
3352 1.1 skrll #ifdef OBJ_ELF
3353 1.1 skrll bfd_boolean
3354 1.1.1.2 christos sh_fix_adjustable (fixS *fixP)
3355 1.1 skrll {
3356 1.1.1.2 christos if (fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL
3357 1.1.1.2 christos || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
3358 1.1.1.2 christos || fixP->fx_r_type == BFD_RELOC_SH_GOT20
3359 1.1.1.2 christos || fixP->fx_r_type == BFD_RELOC_SH_GOTPC
3360 1.1.1.2 christos || fixP->fx_r_type == BFD_RELOC_SH_GOTFUNCDESC
3361 1.1 skrll || fixP->fx_r_type == BFD_RELOC_SH_GOTFUNCDESC20
3362 1.1 skrll || fixP->fx_r_type == BFD_RELOC_SH_GOTOFFFUNCDESC
3363 1.1 skrll || fixP->fx_r_type == BFD_RELOC_SH_GOTOFFFUNCDESC20
3364 1.1 skrll || fixP->fx_r_type == BFD_RELOC_SH_FUNCDESC
3365 1.1 skrll || ((fixP->fx_r_type == BFD_RELOC_32) && dont_adjust_reloc_32)
3366 1.1 skrll || fixP->fx_r_type == BFD_RELOC_RVA)
3367 1.1 skrll return 0;
3368 1.1 skrll
3369 1.1 skrll /* We need the symbol name for the VTABLE entries */
3370 1.1 skrll if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3371 1.1 skrll || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3372 1.1 skrll return 0;
3373 1.1 skrll
3374 1.1 skrll return 1;
3375 1.1 skrll }
3376 1.1 skrll
3377 1.1 skrll void
3378 1.1 skrll sh_elf_final_processing (void)
3379 1.1 skrll {
3380 1.1.1.7 christos int val;
3381 1.1 skrll
3382 1.1 skrll /* Set file-specific flags to indicate if this code needs
3383 1.1 skrll a processor with the sh-dsp / sh2e ISA to execute. */
3384 1.1.1.2 christos val = sh_find_elf_flags (valid_arch);
3385 1.1.1.2 christos
3386 1.1.1.2 christos elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
3387 1.1.1.2 christos elf_elfheader (stdoutput)->e_flags |= val;
3388 1.1.1.2 christos
3389 1.1.1.2 christos if (sh_fdpic)
3390 1.1.1.2 christos elf_elfheader (stdoutput)->e_flags |= EF_SH_FDPIC;
3391 1.1.1.2 christos }
3392 1.1.1.2 christos #endif
3393 1.1.1.2 christos
3394 1.1.1.2 christos #ifdef TE_UCLINUX
3395 1.1.1.2 christos /* Return the target format for uClinux. */
3396 1.1.1.2 christos
3397 1.1.1.2 christos const char *
3398 1.1.1.2 christos sh_uclinux_target_format (void)
3399 1.1.1.2 christos {
3400 1.1 skrll if (sh_fdpic)
3401 1.1 skrll return (!target_big_endian ? "elf32-sh-fdpic" : "elf32-shbig-fdpic");
3402 1.1 skrll else
3403 1.1 skrll return (!target_big_endian ? "elf32-shl" : "elf32-sh");
3404 1.1 skrll }
3405 1.1 skrll #endif
3406 1.1 skrll
3407 1.1 skrll /* Apply fixup FIXP to SIZE-byte field BUF given that VAL is its
3408 1.1 skrll assembly-time value. If we're generating a reloc for FIXP,
3409 1.1 skrll see whether the addend should be stored in-place or whether
3410 1.1 skrll it should be in an ELF r_addend field. */
3411 1.1 skrll
3412 1.1 skrll static void
3413 1.1 skrll apply_full_field_fix (fixS *fixP, char *buf, bfd_vma val, int size)
3414 1.1 skrll {
3415 1.1 skrll reloc_howto_type *howto;
3416 1.1 skrll
3417 1.1 skrll if (fixP->fx_addsy != NULL || fixP->fx_pcrel)
3418 1.1 skrll {
3419 1.1 skrll howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
3420 1.1 skrll if (howto && !howto->partial_inplace)
3421 1.1 skrll {
3422 1.1 skrll fixP->fx_addnumber = val;
3423 1.1 skrll return;
3424 1.1 skrll }
3425 1.1 skrll }
3426 1.1 skrll md_number_to_chars (buf, val, size);
3427 1.1 skrll }
3428 1.1 skrll
3429 1.1 skrll /* Apply a fixup to the object file. */
3430 1.1 skrll
3431 1.1 skrll void
3432 1.1 skrll md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3433 1.1 skrll {
3434 1.1 skrll char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3435 1.1 skrll int lowbyte = target_big_endian ? 1 : 0;
3436 1.1 skrll int highbyte = target_big_endian ? 0 : 1;
3437 1.1 skrll long val = (long) *valP;
3438 1.1 skrll long max, min;
3439 1.1 skrll int shift;
3440 1.1 skrll
3441 1.1 skrll /* A difference between two symbols, the second of which is in the
3442 1.1 skrll current section, is transformed in a PC-relative relocation to
3443 1.1 skrll the other symbol. We have to adjust the relocation type here. */
3444 1.1 skrll if (fixP->fx_pcrel)
3445 1.1 skrll {
3446 1.1 skrll switch (fixP->fx_r_type)
3447 1.1 skrll {
3448 1.1 skrll default:
3449 1.1 skrll break;
3450 1.1 skrll
3451 1.1 skrll case BFD_RELOC_32:
3452 1.1 skrll fixP->fx_r_type = BFD_RELOC_32_PCREL;
3453 1.1 skrll break;
3454 1.1 skrll
3455 1.1 skrll /* Currently, we only support 32-bit PCREL relocations.
3456 1.1 skrll We'd need a new reloc type to handle 16_PCREL, and
3457 1.1 skrll 8_PCREL is already taken for R_SH_SWITCH8, which
3458 1.1 skrll apparently does something completely different than what
3459 1.1 skrll we need. FIXME. */
3460 1.1 skrll case BFD_RELOC_16:
3461 1.1 skrll bfd_set_error (bfd_error_bad_value);
3462 1.1 skrll return;
3463 1.1 skrll
3464 1.1 skrll case BFD_RELOC_8:
3465 1.1 skrll bfd_set_error (bfd_error_bad_value);
3466 1.1 skrll return;
3467 1.1 skrll }
3468 1.1 skrll }
3469 1.1 skrll
3470 1.1 skrll /* The function adjust_reloc_syms won't convert a reloc against a weak
3471 1.1 skrll symbol into a reloc against a section, but bfd_install_relocation
3472 1.1 skrll will screw up if the symbol is defined, so we have to adjust val here
3473 1.1 skrll to avoid the screw up later.
3474 1.1 skrll
3475 1.1 skrll For ordinary relocs, this does not happen for ELF, since for ELF,
3476 1.1 skrll bfd_install_relocation uses the "special function" field of the
3477 1.1 skrll howto, and does not execute the code that needs to be undone, as long
3478 1.1 skrll as the special function does not return bfd_reloc_continue.
3479 1.1 skrll It can happen for GOT- and PLT-type relocs the way they are
3480 1.1 skrll described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
3481 1.1 skrll doesn't matter here since those relocs don't use VAL; see below. */
3482 1.1 skrll if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3483 1.1 skrll && fixP->fx_addsy != NULL
3484 1.1 skrll && S_IS_WEAK (fixP->fx_addsy))
3485 1.1 skrll val -= S_GET_VALUE (fixP->fx_addsy);
3486 1.1 skrll
3487 1.1 skrll if (SWITCH_TABLE (fixP))
3488 1.1 skrll val -= S_GET_VALUE (fixP->fx_subsy);
3489 1.1 skrll
3490 1.1 skrll max = min = 0;
3491 1.1 skrll shift = 0;
3492 1.1 skrll switch (fixP->fx_r_type)
3493 1.1 skrll {
3494 1.1 skrll case BFD_RELOC_SH_IMM3:
3495 1.1 skrll max = 0x7;
3496 1.1 skrll * buf = (* buf & 0xf8) | (val & 0x7);
3497 1.1 skrll break;
3498 1.1 skrll case BFD_RELOC_SH_IMM3U:
3499 1.1 skrll max = 0x7;
3500 1.1 skrll * buf = (* buf & 0x8f) | ((val & 0x7) << 4);
3501 1.1 skrll break;
3502 1.1 skrll case BFD_RELOC_SH_DISP12:
3503 1.1 skrll max = 0xfff;
3504 1.1 skrll buf[lowbyte] = val & 0xff;
3505 1.1 skrll buf[highbyte] |= (val >> 8) & 0x0f;
3506 1.1 skrll break;
3507 1.1 skrll case BFD_RELOC_SH_DISP12BY2:
3508 1.1 skrll max = 0xfff;
3509 1.1 skrll shift = 1;
3510 1.1 skrll buf[lowbyte] = (val >> 1) & 0xff;
3511 1.1 skrll buf[highbyte] |= (val >> 9) & 0x0f;
3512 1.1 skrll break;
3513 1.1 skrll case BFD_RELOC_SH_DISP12BY4:
3514 1.1 skrll max = 0xfff;
3515 1.1 skrll shift = 2;
3516 1.1 skrll buf[lowbyte] = (val >> 2) & 0xff;
3517 1.1 skrll buf[highbyte] |= (val >> 10) & 0x0f;
3518 1.1 skrll break;
3519 1.1 skrll case BFD_RELOC_SH_DISP12BY8:
3520 1.1 skrll max = 0xfff;
3521 1.1 skrll shift = 3;
3522 1.1 skrll buf[lowbyte] = (val >> 3) & 0xff;
3523 1.1 skrll buf[highbyte] |= (val >> 11) & 0x0f;
3524 1.1 skrll break;
3525 1.1 skrll case BFD_RELOC_SH_DISP20:
3526 1.1 skrll if (! target_big_endian)
3527 1.1 skrll abort();
3528 1.1 skrll max = 0x7ffff;
3529 1.1 skrll min = -0x80000;
3530 1.1 skrll buf[1] = (buf[1] & 0x0f) | ((val >> 12) & 0xf0);
3531 1.1 skrll buf[2] = (val >> 8) & 0xff;
3532 1.1 skrll buf[3] = val & 0xff;
3533 1.1 skrll break;
3534 1.1 skrll case BFD_RELOC_SH_DISP20BY8:
3535 1.1 skrll if (!target_big_endian)
3536 1.1 skrll abort();
3537 1.1 skrll max = 0x7ffff;
3538 1.1 skrll min = -0x80000;
3539 1.1 skrll shift = 8;
3540 1.1 skrll buf[1] = (buf[1] & 0x0f) | ((val >> 20) & 0xf0);
3541 1.1 skrll buf[2] = (val >> 16) & 0xff;
3542 1.1 skrll buf[3] = (val >> 8) & 0xff;
3543 1.1 skrll break;
3544 1.1 skrll
3545 1.1 skrll case BFD_RELOC_SH_IMM4:
3546 1.1 skrll max = 0xf;
3547 1.1 skrll *buf = (*buf & 0xf0) | (val & 0xf);
3548 1.1 skrll break;
3549 1.1 skrll
3550 1.1 skrll case BFD_RELOC_SH_IMM4BY2:
3551 1.1 skrll max = 0xf;
3552 1.1 skrll shift = 1;
3553 1.1 skrll *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
3554 1.1 skrll break;
3555 1.1 skrll
3556 1.1 skrll case BFD_RELOC_SH_IMM4BY4:
3557 1.1 skrll max = 0xf;
3558 1.1 skrll shift = 2;
3559 1.1 skrll *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
3560 1.1 skrll break;
3561 1.1 skrll
3562 1.1 skrll case BFD_RELOC_SH_IMM8BY2:
3563 1.1 skrll max = 0xff;
3564 1.1 skrll shift = 1;
3565 1.1 skrll *buf = val >> 1;
3566 1.1 skrll break;
3567 1.1 skrll
3568 1.1 skrll case BFD_RELOC_SH_IMM8BY4:
3569 1.1 skrll max = 0xff;
3570 1.1 skrll shift = 2;
3571 1.1 skrll *buf = val >> 2;
3572 1.1 skrll break;
3573 1.1 skrll
3574 1.1 skrll case BFD_RELOC_8:
3575 1.1 skrll case BFD_RELOC_SH_IMM8:
3576 1.1 skrll /* Sometimes the 8 bit value is sign extended (e.g., add) and
3577 1.1 skrll sometimes it is not (e.g., and). We permit any 8 bit value.
3578 1.1 skrll Note that adding further restrictions may invalidate
3579 1.1 skrll reasonable looking assembly code, such as ``and -0x1,r0''. */
3580 1.1 skrll max = 0xff;
3581 1.1 skrll min = -0xff;
3582 1.1 skrll *buf++ = val;
3583 1.1 skrll break;
3584 1.1 skrll
3585 1.1 skrll case BFD_RELOC_SH_PCRELIMM8BY4:
3586 1.1 skrll /* If we are dealing with a known destination ... */
3587 1.1 skrll if ((fixP->fx_addsy == NULL || S_IS_DEFINED (fixP->fx_addsy))
3588 1.1 skrll && (fixP->fx_subsy == NULL || S_IS_DEFINED (fixP->fx_addsy)))
3589 1.1 skrll {
3590 1.1 skrll /* Don't silently move the destination due to misalignment.
3591 1.1 skrll The absolute address is the fragment base plus the offset into
3592 1.1 skrll the fragment plus the pc relative offset to the label. */
3593 1.1 skrll if ((fixP->fx_frag->fr_address + fixP->fx_where + val) & 3)
3594 1.1 skrll as_bad_where (fixP->fx_file, fixP->fx_line,
3595 1.1 skrll _("offset to unaligned destination"));
3596 1.1 skrll
3597 1.1 skrll /* The displacement cannot be zero or backward even if aligned.
3598 1.1 skrll Allow -2 because val has already been adjusted somewhere. */
3599 1.1 skrll if (val < -2)
3600 1.1 skrll as_bad_where (fixP->fx_file, fixP->fx_line, _("negative offset"));
3601 1.1 skrll }
3602 1.1 skrll
3603 1.1 skrll /* The lower two bits of the PC are cleared before the
3604 1.1 skrll displacement is added in. We can assume that the destination
3605 1.1 skrll is on a 4 byte boundary. If this instruction is also on a 4
3606 1.1 skrll byte boundary, then we want
3607 1.1 skrll (target - here) / 4
3608 1.1 skrll and target - here is a multiple of 4.
3609 1.1 skrll Otherwise, we are on a 2 byte boundary, and we want
3610 1.1 skrll (target - (here - 2)) / 4
3611 1.1 skrll and target - here is not a multiple of 4. Computing
3612 1.1 skrll (target - (here - 2)) / 4 == (target - here + 2) / 4
3613 1.1 skrll works for both cases, since in the first case the addition of
3614 1.1 skrll 2 will be removed by the division. target - here is in the
3615 1.1 skrll variable val. */
3616 1.1 skrll val = (val + 2) / 4;
3617 1.1 skrll if (val & ~0xff)
3618 1.1 skrll as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3619 1.1 skrll buf[lowbyte] = val;
3620 1.1 skrll break;
3621 1.1 skrll
3622 1.1 skrll case BFD_RELOC_SH_PCRELIMM8BY2:
3623 1.1 skrll val /= 2;
3624 1.1 skrll if (val & ~0xff)
3625 1.1 skrll as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3626 1.1 skrll buf[lowbyte] = val;
3627 1.1 skrll break;
3628 1.1 skrll
3629 1.1 skrll case BFD_RELOC_SH_PCDISP8BY2:
3630 1.1 skrll val /= 2;
3631 1.1 skrll if (val < -0x80 || val > 0x7f)
3632 1.1 skrll as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3633 1.1 skrll buf[lowbyte] = val;
3634 1.1 skrll break;
3635 1.1 skrll
3636 1.1 skrll case BFD_RELOC_SH_PCDISP12BY2:
3637 1.1 skrll val /= 2;
3638 1.1 skrll if (val < -0x800 || val > 0x7ff)
3639 1.1 skrll as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3640 1.1 skrll buf[lowbyte] = val & 0xff;
3641 1.1 skrll buf[highbyte] |= (val >> 8) & 0xf;
3642 1.1 skrll break;
3643 1.1 skrll
3644 1.1 skrll case BFD_RELOC_32:
3645 1.1 skrll case BFD_RELOC_32_PCREL:
3646 1.1 skrll apply_full_field_fix (fixP, buf, val, 4);
3647 1.1 skrll break;
3648 1.1 skrll
3649 1.1 skrll case BFD_RELOC_16:
3650 1.1 skrll apply_full_field_fix (fixP, buf, val, 2);
3651 1.1 skrll break;
3652 1.1 skrll
3653 1.1 skrll case BFD_RELOC_SH_USES:
3654 1.1 skrll /* Pass the value into sh_reloc(). */
3655 1.1 skrll fixP->fx_addnumber = val;
3656 1.1 skrll break;
3657 1.1 skrll
3658 1.1 skrll case BFD_RELOC_SH_COUNT:
3659 1.1 skrll case BFD_RELOC_SH_ALIGN:
3660 1.1 skrll case BFD_RELOC_SH_CODE:
3661 1.1 skrll case BFD_RELOC_SH_DATA:
3662 1.1 skrll case BFD_RELOC_SH_LABEL:
3663 1.1 skrll /* Nothing to do here. */
3664 1.1 skrll break;
3665 1.1 skrll
3666 1.1 skrll case BFD_RELOC_SH_LOOP_START:
3667 1.1 skrll case BFD_RELOC_SH_LOOP_END:
3668 1.1 skrll
3669 1.1 skrll case BFD_RELOC_VTABLE_INHERIT:
3670 1.1 skrll case BFD_RELOC_VTABLE_ENTRY:
3671 1.1 skrll fixP->fx_done = 0;
3672 1.1 skrll return;
3673 1.1 skrll
3674 1.1 skrll #ifdef OBJ_ELF
3675 1.1 skrll case BFD_RELOC_32_PLT_PCREL:
3676 1.1 skrll /* Make the jump instruction point to the address of the operand. At
3677 1.1 skrll runtime we merely add the offset to the actual PLT entry. */
3678 1.1 skrll * valP = 0xfffffffc;
3679 1.1 skrll val = fixP->fx_offset;
3680 1.1 skrll if (fixP->fx_subsy)
3681 1.1 skrll val -= S_GET_VALUE (fixP->fx_subsy);
3682 1.1 skrll apply_full_field_fix (fixP, buf, val, 4);
3683 1.1 skrll break;
3684 1.1 skrll
3685 1.1 skrll case BFD_RELOC_SH_GOTPC:
3686 1.1 skrll /* This is tough to explain. We end up with this one if we have
3687 1.1 skrll operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
3688 1.1 skrll The goal here is to obtain the absolute address of the GOT,
3689 1.1 skrll and it is strongly preferable from a performance point of
3690 1.1 skrll view to avoid using a runtime relocation for this. There are
3691 1.1 skrll cases where you have something like:
3692 1.1 skrll
3693 1.1 skrll .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
3694 1.1 skrll
3695 1.1 skrll and here no correction would be required. Internally in the
3696 1.1 skrll assembler we treat operands of this form as not being pcrel
3697 1.1 skrll since the '.' is explicitly mentioned, and I wonder whether
3698 1.1 skrll it would simplify matters to do it this way. Who knows. In
3699 1.1 skrll earlier versions of the PIC patches, the pcrel_adjust field
3700 1.1 skrll was used to store the correction, but since the expression is
3701 1.1 skrll not pcrel, I felt it would be confusing to do it this way. */
3702 1.1 skrll * valP -= 1;
3703 1.1 skrll apply_full_field_fix (fixP, buf, val, 4);
3704 1.1 skrll break;
3705 1.1 skrll
3706 1.1 skrll case BFD_RELOC_SH_TLS_GD_32:
3707 1.1 skrll case BFD_RELOC_SH_TLS_LD_32:
3708 1.1.1.2 christos case BFD_RELOC_SH_TLS_IE_32:
3709 1.1 skrll S_SET_THREAD_LOCAL (fixP->fx_addsy);
3710 1.1.1.2 christos /* Fallthrough */
3711 1.1.1.2 christos case BFD_RELOC_32_GOT_PCREL:
3712 1.1.1.2 christos case BFD_RELOC_SH_GOT20:
3713 1.1.1.2 christos case BFD_RELOC_SH_GOTPLT32:
3714 1.1.1.2 christos case BFD_RELOC_SH_GOTFUNCDESC:
3715 1.1 skrll case BFD_RELOC_SH_GOTFUNCDESC20:
3716 1.1 skrll case BFD_RELOC_SH_GOTOFFFUNCDESC:
3717 1.1 skrll case BFD_RELOC_SH_GOTOFFFUNCDESC20:
3718 1.1 skrll case BFD_RELOC_SH_FUNCDESC:
3719 1.1 skrll * valP = 0; /* Fully resolved at runtime. No addend. */
3720 1.1 skrll apply_full_field_fix (fixP, buf, 0, 4);
3721 1.1 skrll break;
3722 1.1 skrll
3723 1.1 skrll case BFD_RELOC_SH_TLS_LDO_32:
3724 1.1.1.2 christos case BFD_RELOC_SH_TLS_LE_32:
3725 1.1 skrll S_SET_THREAD_LOCAL (fixP->fx_addsy);
3726 1.1 skrll /* Fallthrough */
3727 1.1 skrll case BFD_RELOC_32_GOTOFF:
3728 1.1 skrll case BFD_RELOC_SH_GOTOFF20:
3729 1.1 skrll apply_full_field_fix (fixP, buf, val, 4);
3730 1.1 skrll break;
3731 1.1 skrll #endif
3732 1.1 skrll
3733 1.1 skrll default:
3734 1.1 skrll abort ();
3735 1.1 skrll }
3736 1.1 skrll
3737 1.1 skrll if (shift != 0)
3738 1.1 skrll {
3739 1.1 skrll if ((val & ((1 << shift) - 1)) != 0)
3740 1.1 skrll as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
3741 1.1 skrll if (val >= 0)
3742 1.1 skrll val >>= shift;
3743 1.1.1.2 christos else
3744 1.1.1.2 christos val = ((val >> shift)
3745 1.1.1.2 christos | ((long) -1 & ~ ((long) -1 >> shift)));
3746 1.1 skrll }
3747 1.1 skrll
3748 1.1 skrll /* Extend sign for 64-bit host. */
3749 1.1.1.6 christos val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
3750 1.1 skrll if (max != 0 && (val < min || val > max))
3751 1.1 skrll as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
3752 1.1 skrll else if (max != 0)
3753 1.1 skrll /* Stop the generic code from trying to overflow check the value as well.
3754 1.1 skrll It may not have the correct value anyway, as we do not store val back
3755 1.1 skrll into *valP. */
3756 1.1 skrll fixP->fx_no_overflow = 1;
3757 1.1 skrll
3758 1.1 skrll if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
3759 1.1 skrll fixP->fx_done = 1;
3760 1.1 skrll }
3761 1.1 skrll
3762 1.1 skrll /* Called just before address relaxation. Return the length
3763 1.1 skrll by which a fragment must grow to reach it's destination. */
3764 1.1 skrll
3765 1.1 skrll int
3766 1.1 skrll md_estimate_size_before_relax (fragS *fragP, segT segment_type)
3767 1.1 skrll {
3768 1.1 skrll int what;
3769 1.1 skrll
3770 1.1 skrll switch (fragP->fr_subtype)
3771 1.1 skrll {
3772 1.1 skrll default:
3773 1.1 skrll abort ();
3774 1.1 skrll
3775 1.1 skrll case C (UNCOND_JUMP, UNDEF_DISP):
3776 1.1 skrll /* Used to be a branch to somewhere which was unknown. */
3777 1.1 skrll if (!fragP->fr_symbol)
3778 1.1 skrll {
3779 1.1 skrll fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3780 1.1 skrll }
3781 1.1 skrll else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3782 1.1 skrll {
3783 1.1 skrll fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
3784 1.1 skrll }
3785 1.1 skrll else
3786 1.1 skrll {
3787 1.1 skrll fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
3788 1.1 skrll }
3789 1.1 skrll break;
3790 1.1 skrll
3791 1.1 skrll case C (COND_JUMP, UNDEF_DISP):
3792 1.1 skrll case C (COND_JUMP_DELAY, UNDEF_DISP):
3793 1.1 skrll what = GET_WHAT (fragP->fr_subtype);
3794 1.1 skrll /* Used to be a branch to somewhere which was unknown. */
3795 1.1 skrll if (fragP->fr_symbol
3796 1.1 skrll && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3797 1.1 skrll {
3798 1.1 skrll /* Got a symbol and it's defined in this segment, become byte
3799 1.1 skrll sized - maybe it will fix up. */
3800 1.1.1.6 christos fragP->fr_subtype = C (what, COND8);
3801 1.1 skrll }
3802 1.1 skrll else if (fragP->fr_symbol)
3803 1.1 skrll {
3804 1.1 skrll /* It's got a segment, but it's not ours, so it will always be long. */
3805 1.1 skrll fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
3806 1.1 skrll }
3807 1.1 skrll else
3808 1.1 skrll {
3809 1.1 skrll /* We know the abs value. */
3810 1.1 skrll fragP->fr_subtype = C (what, COND8);
3811 1.1 skrll }
3812 1.1 skrll break;
3813 1.1 skrll
3814 1.1 skrll case C (UNCOND_JUMP, UNCOND12):
3815 1.1 skrll case C (UNCOND_JUMP, UNCOND32):
3816 1.1 skrll case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3817 1.1 skrll case C (COND_JUMP, COND8):
3818 1.1 skrll case C (COND_JUMP, COND12):
3819 1.1 skrll case C (COND_JUMP, COND32):
3820 1.1 skrll case C (COND_JUMP, UNDEF_WORD_DISP):
3821 1.1 skrll case C (COND_JUMP_DELAY, COND8):
3822 1.1 skrll case C (COND_JUMP_DELAY, COND12):
3823 1.1 skrll case C (COND_JUMP_DELAY, COND32):
3824 1.1 skrll case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3825 1.1 skrll /* When relaxing a section for the second time, we don't need to
3826 1.1 skrll do anything besides return the current size. */
3827 1.1 skrll break;
3828 1.1 skrll }
3829 1.1 skrll
3830 1.1 skrll fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
3831 1.1 skrll return fragP->fr_var;
3832 1.1 skrll }
3833 1.1 skrll
3834 1.1 skrll /* Put number into target byte order. */
3835 1.1 skrll
3836 1.1 skrll void
3837 1.1 skrll md_number_to_chars (char *ptr, valueT use, int nbytes)
3838 1.1 skrll {
3839 1.1 skrll if (! target_big_endian)
3840 1.1 skrll number_to_chars_littleendian (ptr, use, nbytes);
3841 1.1 skrll else
3842 1.1 skrll number_to_chars_bigendian (ptr, use, nbytes);
3843 1.1 skrll }
3844 1.1 skrll
3845 1.1 skrll /* This version is used in obj-coff.c eg. for the sh-hms target. */
3846 1.1 skrll
3847 1.1 skrll long
3848 1.1 skrll md_pcrel_from (fixS *fixP)
3849 1.1 skrll {
3850 1.1 skrll return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
3851 1.1 skrll }
3852 1.1 skrll
3853 1.1 skrll long
3854 1.1 skrll md_pcrel_from_section (fixS *fixP, segT sec)
3855 1.1 skrll {
3856 1.1 skrll if (! sh_local_pcrel (fixP)
3857 1.1 skrll && fixP->fx_addsy != (symbolS *) NULL
3858 1.1 skrll && (generic_force_reloc (fixP)
3859 1.1 skrll || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3860 1.1 skrll {
3861 1.1 skrll /* The symbol is undefined (or is defined but not in this section,
3862 1.1 skrll or we're not sure about it being the final definition). Let the
3863 1.1 skrll linker figure it out. We need to adjust the subtraction of a
3864 1.1 skrll symbol to the position of the relocated data, though. */
3865 1.1 skrll return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
3866 1.1 skrll }
3867 1.1 skrll
3868 1.1 skrll return md_pcrel_from (fixP);
3869 1.1 skrll }
3870 1.1 skrll
3871 1.1 skrll /* Create a reloc. */
3872 1.1 skrll
3873 1.1 skrll arelent *
3874 1.1 skrll tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
3875 1.1.1.5 christos {
3876 1.1.1.5 christos arelent *rel;
3877 1.1 skrll bfd_reloc_code_real_type r_type;
3878 1.1 skrll
3879 1.1 skrll rel = XNEW (arelent);
3880 1.1 skrll rel->sym_ptr_ptr = XNEW (asymbol *);
3881 1.1 skrll *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3882 1.1 skrll rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3883 1.1 skrll
3884 1.1 skrll r_type = fixp->fx_r_type;
3885 1.1.1.4 christos
3886 1.1 skrll if (SWITCH_TABLE (fixp))
3887 1.1 skrll {
3888 1.1 skrll *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
3889 1.1 skrll rel->addend = rel->address - S_GET_VALUE(fixp->fx_subsy);
3890 1.1 skrll if (r_type == BFD_RELOC_16)
3891 1.1 skrll r_type = BFD_RELOC_SH_SWITCH16;
3892 1.1 skrll else if (r_type == BFD_RELOC_8)
3893 1.1 skrll r_type = BFD_RELOC_8_PCREL;
3894 1.1 skrll else if (r_type == BFD_RELOC_32)
3895 1.1 skrll r_type = BFD_RELOC_SH_SWITCH32;
3896 1.1 skrll else
3897 1.1 skrll abort ();
3898 1.1 skrll }
3899 1.1 skrll else if (r_type == BFD_RELOC_SH_USES)
3900 1.1 skrll rel->addend = fixp->fx_addnumber;
3901 1.1 skrll else if (r_type == BFD_RELOC_SH_COUNT)
3902 1.1 skrll rel->addend = fixp->fx_offset;
3903 1.1 skrll else if (r_type == BFD_RELOC_SH_ALIGN)
3904 1.1 skrll rel->addend = fixp->fx_offset;
3905 1.1 skrll else if (r_type == BFD_RELOC_VTABLE_INHERIT
3906 1.1 skrll || r_type == BFD_RELOC_VTABLE_ENTRY)
3907 1.1 skrll rel->addend = fixp->fx_offset;
3908 1.1 skrll else if (r_type == BFD_RELOC_SH_LOOP_START
3909 1.1 skrll || r_type == BFD_RELOC_SH_LOOP_END)
3910 1.1 skrll rel->addend = fixp->fx_offset;
3911 1.1 skrll else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
3912 1.1 skrll {
3913 1.1 skrll rel->addend = 0;
3914 1.1 skrll rel->address = rel->addend = fixp->fx_offset;
3915 1.1 skrll }
3916 1.1 skrll else
3917 1.1 skrll rel->addend = fixp->fx_addnumber;
3918 1.1 skrll
3919 1.1 skrll rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
3920 1.1 skrll
3921 1.1 skrll if (rel->howto == NULL)
3922 1.1 skrll {
3923 1.1 skrll as_bad_where (fixp->fx_file, fixp->fx_line,
3924 1.1.1.2 christos _("Cannot represent relocation type %s"),
3925 1.1 skrll bfd_get_reloc_code_name (r_type));
3926 1.1 skrll /* Set howto to a garbage value so that we can keep going. */
3927 1.1 skrll rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3928 1.1 skrll gas_assert (rel->howto != NULL);
3929 1.1 skrll }
3930 1.1 skrll #ifdef OBJ_ELF
3931 1.1 skrll else if (rel->howto->type == R_SH_IND12W)
3932 1.1 skrll rel->addend += fixp->fx_offset - 4;
3933 1.1 skrll #endif
3934 1.1 skrll
3935 1.1 skrll return rel;
3936 1.1.1.5 christos }
3937 1.1 skrll
3938 1.1 skrll #ifdef OBJ_ELF
3939 1.1 skrll inline static char *
3940 1.1 skrll sh_end_of_match (char *cont, const char *what)
3941 1.1 skrll {
3942 1.1 skrll int len = strlen (what);
3943 1.1 skrll
3944 1.1 skrll if (strncasecmp (cont, what, strlen (what)) == 0
3945 1.1 skrll && ! is_part_of_name (cont[len]))
3946 1.1 skrll return cont + len;
3947 1.1 skrll
3948 1.1 skrll return NULL;
3949 1.1 skrll }
3950 1.1 skrll
3951 1.1 skrll int
3952 1.1 skrll sh_parse_name (char const *name,
3953 1.1 skrll expressionS *exprP,
3954 1.1 skrll enum expr_mode mode,
3955 1.1 skrll char *nextcharP)
3956 1.1 skrll {
3957 1.1 skrll char *next = input_line_pointer;
3958 1.1 skrll char *next_end;
3959 1.1 skrll int reloc_type;
3960 1.1 skrll segT segment;
3961 1.1 skrll
3962 1.1 skrll exprP->X_op_symbol = NULL;
3963 1.1 skrll
3964 1.1 skrll if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
3965 1.1 skrll {
3966 1.1 skrll if (! GOT_symbol)
3967 1.1 skrll GOT_symbol = symbol_find_or_make (name);
3968 1.1 skrll
3969 1.1 skrll exprP->X_add_symbol = GOT_symbol;
3970 1.1 skrll no_suffix:
3971 1.1 skrll /* If we have an absolute symbol or a reg, then we know its
3972 1.1 skrll value now. */
3973 1.1 skrll segment = S_GET_SEGMENT (exprP->X_add_symbol);
3974 1.1 skrll if (mode != expr_defer && segment == absolute_section)
3975 1.1 skrll {
3976 1.1 skrll exprP->X_op = O_constant;
3977 1.1 skrll exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
3978 1.1 skrll exprP->X_add_symbol = NULL;
3979 1.1 skrll }
3980 1.1 skrll else if (mode != expr_defer && segment == reg_section)
3981 1.1 skrll {
3982 1.1 skrll exprP->X_op = O_register;
3983 1.1 skrll exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
3984 1.1 skrll exprP->X_add_symbol = NULL;
3985 1.1 skrll }
3986 1.1 skrll else
3987 1.1 skrll {
3988 1.1 skrll exprP->X_op = O_symbol;
3989 1.1 skrll exprP->X_add_number = 0;
3990 1.1 skrll }
3991 1.1 skrll
3992 1.1 skrll return 1;
3993 1.1 skrll }
3994 1.1 skrll
3995 1.1 skrll exprP->X_add_symbol = symbol_find_or_make (name);
3996 1.1 skrll
3997 1.1 skrll if (*nextcharP != '@')
3998 1.1 skrll goto no_suffix;
3999 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "GOTOFF")))
4000 1.1 skrll reloc_type = BFD_RELOC_32_GOTOFF;
4001 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "GOTPLT")))
4002 1.1 skrll reloc_type = BFD_RELOC_SH_GOTPLT32;
4003 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "GOT")))
4004 1.1 skrll reloc_type = BFD_RELOC_32_GOT_PCREL;
4005 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "PLT")))
4006 1.1 skrll reloc_type = BFD_RELOC_32_PLT_PCREL;
4007 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "TLSGD")))
4008 1.1 skrll reloc_type = BFD_RELOC_SH_TLS_GD_32;
4009 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "TLSLDM")))
4010 1.1 skrll reloc_type = BFD_RELOC_SH_TLS_LD_32;
4011 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "GOTTPOFF")))
4012 1.1 skrll reloc_type = BFD_RELOC_SH_TLS_IE_32;
4013 1.1.1.2 christos else if ((next_end = sh_end_of_match (next + 1, "TPOFF")))
4014 1.1.1.2 christos reloc_type = BFD_RELOC_SH_TLS_LE_32;
4015 1.1.1.2 christos else if ((next_end = sh_end_of_match (next + 1, "DTPOFF")))
4016 1.1.1.2 christos reloc_type = BFD_RELOC_SH_TLS_LDO_32;
4017 1.1.1.2 christos else if ((next_end = sh_end_of_match (next + 1, "PCREL")))
4018 1.1.1.2 christos reloc_type = BFD_RELOC_32_PCREL;
4019 1.1.1.2 christos else if ((next_end = sh_end_of_match (next + 1, "GOTFUNCDESC")))
4020 1.1.1.2 christos reloc_type = BFD_RELOC_SH_GOTFUNCDESC;
4021 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "GOTOFFFUNCDESC")))
4022 1.1 skrll reloc_type = BFD_RELOC_SH_GOTOFFFUNCDESC;
4023 1.1 skrll else if ((next_end = sh_end_of_match (next + 1, "FUNCDESC")))
4024 1.1 skrll reloc_type = BFD_RELOC_SH_FUNCDESC;
4025 1.1 skrll else
4026 1.1 skrll goto no_suffix;
4027 1.1 skrll
4028 1.1 skrll *input_line_pointer = *nextcharP;
4029 1.1 skrll input_line_pointer = next_end;
4030 1.1 skrll *nextcharP = *input_line_pointer;
4031 1.1 skrll *input_line_pointer = '\0';
4032 1.1 skrll
4033 1.1 skrll exprP->X_op = O_PIC_reloc;
4034 1.1 skrll exprP->X_add_number = 0;
4035 1.1 skrll exprP->X_md = reloc_type;
4036 1.1 skrll
4037 1.1 skrll return 1;
4038 1.1 skrll }
4039 1.1 skrll
4040 1.1 skrll void
4041 1.1 skrll sh_cfi_frame_initial_instructions (void)
4042 1.1 skrll {
4043 1.1 skrll cfi_add_CFA_def_cfa (15, 0);
4044 1.1 skrll }
4045 1.1 skrll
4046 1.1 skrll int
4047 1.1 skrll sh_regname_to_dw2regnum (char *regname)
4048 1.1 skrll {
4049 1.1.1.5 christos unsigned int regnum = -1;
4050 1.1 skrll unsigned int i;
4051 1.1 skrll const char *p;
4052 1.1 skrll char *q;
4053 1.1 skrll static struct { const char *name; int dw2regnum; } regnames[] =
4054 1.1 skrll {
4055 1.1 skrll { "pr", 17 }, { "t", 18 }, { "gbr", 19 }, { "mach", 20 },
4056 1.1 skrll { "macl", 21 }, { "fpul", 23 }
4057 1.1 skrll };
4058 1.1 skrll
4059 1.1 skrll for (i = 0; i < ARRAY_SIZE (regnames); ++i)
4060 1.1 skrll if (strcmp (regnames[i].name, regname) == 0)
4061 1.1 skrll return regnames[i].dw2regnum;
4062 1.1 skrll
4063 1.1 skrll if (regname[0] == 'r')
4064 1.1 skrll {
4065 1.1 skrll p = regname + 1;
4066 1.1 skrll regnum = strtoul (p, &q, 10);
4067 1.1 skrll if (p == q || *q || regnum >= 16)
4068 1.1 skrll return -1;
4069 1.1 skrll }
4070 1.1 skrll else if (regname[0] == 'f' && regname[1] == 'r')
4071 1.1 skrll {
4072 1.1 skrll p = regname + 2;
4073 1.1 skrll regnum = strtoul (p, &q, 10);
4074 1.1 skrll if (p == q || *q || regnum >= 16)
4075 1.1 skrll return -1;
4076 1.1 skrll regnum += 25;
4077 1.1 skrll }
4078 1.1 skrll else if (regname[0] == 'x' && regname[1] == 'd')
4079 1.1 skrll {
4080 1.1 skrll p = regname + 2;
4081 1.1 skrll regnum = strtoul (p, &q, 10);
4082 1.1 skrll if (p == q || *q || regnum >= 8)
4083 1.1 skrll return -1;
4084 1.1 skrll regnum += 87;
4085 }
4086 return regnum;
4087 }
4088 #endif /* OBJ_ELF */
4089