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