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