1 /* tc-rl78.c -- Assembler for the Renesas RL78
2 Copyright (C) 2011-2016 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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 3, or (at your option)
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.
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 the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22 #include "struc-symbol.h"
23 #include "safe-ctype.h"
24 #include "dwarf2dbg.h"
25 #include "elf/common.h"
27 #include "rl78-defs.h"
28 #include "filenames.h"
33 const char comment_chars[] = ";";
34 /* Note that input_file.c hand checks for '#' at the beginning of the
35 first line of the input file. This is because the compiler outputs
36 #NO_APP at the beginning of its output. */
37 const char line_comment_chars[] = "#";
38 /* Use something that isn't going to be needed by any expressions or
40 const char line_separator_chars[] = "@";
42 const char EXP_CHARS[] = "eE";
43 const char FLT_CHARS[] = "dD";
45 /* ELF flags to set in the output file header. */
46 static int elf_flags = 0;
48 /*------------------------------------------------------------------*/
50 char * rl78_lex_start;
53 typedef struct rl78_bytesT
66 char type; /* RL78REL_*. */
79 fixS *link_relax_fixP;
84 static rl78_bytesT rl78_bytes;
87 rl78_relax (int type, int pos)
89 rl78_bytes.relax[rl78_bytes.n_relax].type = type;
90 rl78_bytes.relax[rl78_bytes.n_relax].field_pos = pos;
91 rl78_bytes.relax[rl78_bytes.n_relax].val_ofs = rl78_bytes.n_base + rl78_bytes.n_ops;
92 rl78_bytes.n_relax ++;
96 rl78_linkrelax_addr16 (void)
98 rl78_bytes.link_relax |= RL78_RELAXA_ADDR16;
102 rl78_linkrelax_branch (void)
104 rl78_relax (RL78_RELAX_BRANCH, 0);
105 rl78_bytes.link_relax |= RL78_RELAXA_BRA;
109 rl78_fixup (expressionS exp, int offsetbits, int nbits, int type)
111 rl78_bytes.fixups[rl78_bytes.n_fixups].exp = exp;
112 rl78_bytes.fixups[rl78_bytes.n_fixups].offset = offsetbits;
113 rl78_bytes.fixups[rl78_bytes.n_fixups].nbits = nbits;
114 rl78_bytes.fixups[rl78_bytes.n_fixups].type = type;
115 rl78_bytes.fixups[rl78_bytes.n_fixups].reloc = exp.X_md;
116 rl78_bytes.n_fixups ++;
119 #define rl78_field_fixup(exp, offset, nbits, type) \
120 rl78_fixup (exp, offset + 8 * rl78_bytes.n_prefix), nbits, type)
122 #define rl78_op_fixup(exp, offset, nbits, type) \
123 rl78_fixup (exp, offset + 8 * (rl78_bytes.n_prefix + rl78_bytes.n_base), nbits, type)
128 rl78_bytes.prefix[0] = p;
129 rl78_bytes.n_prefix = 1;
133 rl78_has_prefix (void)
135 return rl78_bytes.n_prefix;
141 rl78_bytes.base[0] = b1;
142 rl78_bytes.n_base = 1;
146 rl78_base2 (int b1, int b2)
148 rl78_bytes.base[0] = b1;
149 rl78_bytes.base[1] = b2;
150 rl78_bytes.n_base = 2;
154 rl78_base3 (int b1, int b2, int b3)
156 rl78_bytes.base[0] = b1;
157 rl78_bytes.base[1] = b2;
158 rl78_bytes.base[2] = b3;
159 rl78_bytes.n_base = 3;
163 rl78_base4 (int b1, int b2, int b3, int b4)
165 rl78_bytes.base[0] = b1;
166 rl78_bytes.base[1] = b2;
167 rl78_bytes.base[2] = b3;
168 rl78_bytes.base[3] = b4;
169 rl78_bytes.n_base = 4;
172 #define F_PRECISION 2
175 rl78_op (expressionS exp, int nbytes, int type)
179 if ((exp.X_op == O_constant || exp.X_op == O_big)
180 && type != RL78REL_PCREL)
182 if (exp.X_op == O_big && exp.X_add_number <= 0)
185 char * ip = rl78_bytes.ops + rl78_bytes.n_ops;
187 gen_to_words (w, F_PRECISION, 8);
192 rl78_bytes.n_ops += 4;
196 v = exp.X_add_number;
199 rl78_bytes.ops[rl78_bytes.n_ops++] =v & 0xff;
208 && exp.X_md == BFD_RELOC_RL78_CODE)
212 && (exp.X_md == BFD_RELOC_RL78_LO16
213 || exp.X_md == BFD_RELOC_RL78_HI16))
214 as_bad (_("16-bit relocation used in 8-bit operand"));
217 && exp.X_md == BFD_RELOC_RL78_HI8)
218 as_bad (_("8-bit relocation used in 16-bit operand"));
220 rl78_op_fixup (exp, rl78_bytes.n_ops * 8, nbytes * 8, type);
221 memset (rl78_bytes.ops + rl78_bytes.n_ops, 0, nbytes);
222 rl78_bytes.n_ops += nbytes;
226 /* This gets complicated when the field spans bytes, because fields
227 are numbered from the MSB of the first byte as zero, and bits are
228 stored LSB towards the LSB of the byte. Thus, a simple four-bit
229 insertion of 12 at position 4 of 0x00 yields: 0x0b. A three-bit
230 insertion of b'MXL at position 7 is like this:
232 - - - - - - - - - - - - - - - -
236 rl78_field (int val, int pos, int sz)
243 if (val < 0 || val >= (1 << sz))
244 as_bad (_("Value %d doesn't fit in unsigned %d-bit field"), val, sz);
249 if (val < -(1 << (sz - 1)) || val >= (1 << (sz - 1)))
250 as_bad (_("Value %d doesn't fit in signed %d-bit field"), val, sz);
253 /* This code points at 'M' in the above example. */
257 while (bitp + sz > 8)
262 svalm = val >> (sz - ssz);
263 svalm = svalm & ((1 << ssz) - 1);
264 svalm = svalm << (8 - bitp - ssz);
265 gas_assert (bytep < rl78_bytes.n_base);
266 rl78_bytes.base[bytep] |= svalm;
272 valm = val & ((1 << sz) - 1);
273 valm = valm << (8 - bitp - sz);
274 gas_assert (bytep < rl78_bytes.n_base);
275 rl78_bytes.base[bytep] |= valm;
278 /*------------------------------------------------------------------*/
282 OPTION_RELAX = OPTION_MD_BASE,
287 OPTION_32BIT_DOUBLES,
288 OPTION_64BIT_DOUBLES,
291 #define RL78_SHORTOPTS ""
292 const char * md_shortopts = RL78_SHORTOPTS;
294 /* Assembler options. */
295 struct option md_longopts[] =
297 {"relax", no_argument, NULL, OPTION_RELAX},
298 {"norelax", no_argument, NULL, OPTION_NORELAX},
299 {"mg10", no_argument, NULL, OPTION_G10},
300 {"mg13", no_argument, NULL, OPTION_G13},
301 {"mg14", no_argument, NULL, OPTION_G14},
302 {"mrl78", no_argument, NULL, OPTION_G14},
303 {"m32bit-doubles", no_argument, NULL, OPTION_32BIT_DOUBLES},
304 {"m64bit-doubles", no_argument, NULL, OPTION_64BIT_DOUBLES},
305 {NULL, no_argument, NULL, 0}
307 size_t md_longopts_size = sizeof (md_longopts);
310 md_parse_option (int c, const char * arg ATTRIBUTE_UNUSED)
322 elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
323 elf_flags |= E_FLAG_RL78_G10;
327 elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
328 elf_flags |= E_FLAG_RL78_G13;
332 elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
333 elf_flags |= E_FLAG_RL78_G14;
336 case OPTION_32BIT_DOUBLES:
337 elf_flags &= ~ E_FLAG_RL78_64BIT_DOUBLES;
340 case OPTION_64BIT_DOUBLES:
341 elf_flags |= E_FLAG_RL78_64BIT_DOUBLES;
350 return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G10;
356 return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G13;
362 return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G14;
366 md_show_usage (FILE * stream)
368 fprintf (stream, _(" RL78 specific command line options:\n"));
369 fprintf (stream, _(" --mrelax Enable link time relaxation\n"));
370 fprintf (stream, _(" --mg10 Enable support for G10 variant\n"));
371 fprintf (stream, _(" --mg13 Selects the G13 core.\n"));
372 fprintf (stream, _(" --mg14 Selects the G14 core [default]\n"));
373 fprintf (stream, _(" --mrl78 Alias for --mg14\n"));
374 fprintf (stream, _(" --m32bit-doubles [default]\n"));
375 fprintf (stream, _(" --m64bit-doubles Source code uses 64-bit doubles\n"));
379 s_bss (int ignore ATTRIBUTE_UNUSED)
383 temp = get_absolute_expression ();
384 subseg_set (bss_section, (subsegT) temp);
385 demand_empty_rest_of_line ();
389 rl78_float_cons (int ignore ATTRIBUTE_UNUSED)
391 if (elf_flags & E_FLAG_RL78_64BIT_DOUBLES)
392 return float_cons ('d');
393 return float_cons ('f');
396 /* The target specific pseudo-ops which we support. */
397 const pseudo_typeS md_pseudo_table[] =
399 /* Our "standard" pseudos. */
400 { "double", rl78_float_cons, 'd' },
402 { "3byte", cons, 3 },
406 /* End of list marker. */
410 static symbolS * rl78_abs_sym = NULL;
415 rl78_abs_sym = symbol_make ("__rl78_abs__");
423 /* Set the ELF specific flags. */
425 rl78_elf_final_processing (void)
427 elf_elfheader (stdoutput)->e_flags |= elf_flags;
430 /* Write a value out to the object file, using the appropriate endianness. */
432 md_number_to_chars (char * buf, valueT val, int n)
434 number_to_chars_littleendian (buf, val, n);
438 require_end_of_expr (const char *fname)
440 while (* input_line_pointer == ' '
441 || * input_line_pointer == '\t')
442 input_line_pointer ++;
444 if (! * input_line_pointer
445 || strchr ("\n\r,", * input_line_pointer)
446 || strchr (comment_chars, * input_line_pointer)
447 || strchr (line_comment_chars, * input_line_pointer)
448 || strchr (line_separator_chars, * input_line_pointer))
451 as_bad (_("%%%s() must be outermost term in expression"), fname);
461 { "code", BFD_RELOC_RL78_CODE },
462 { "lo16", BFD_RELOC_RL78_LO16 },
463 { "hi16", BFD_RELOC_RL78_HI16 },
464 { "hi8", BFD_RELOC_RL78_HI8 },
469 md_operand (expressionS * exp ATTRIBUTE_UNUSED)
474 for (i = 0; reloc_functions[i].fname; i++)
476 int flen = strlen (reloc_functions[i].fname);
478 if (input_line_pointer[0] == '%'
479 && strncasecmp (input_line_pointer + 1, reloc_functions[i].fname, flen) == 0
480 && input_line_pointer[flen + 1] == '(')
482 reloc = reloc_functions[i].reloc;
483 input_line_pointer += flen + 2;
491 if (* input_line_pointer == ')')
492 input_line_pointer ++;
496 require_end_of_expr (reloc_functions[i].fname);
500 rl78_frag_init (fragS * fragP)
502 if (rl78_bytes.n_relax || rl78_bytes.link_relax)
504 fragP->tc_frag_data = XNEW (rl78_bytesT);
505 memcpy (fragP->tc_frag_data, & rl78_bytes, sizeof (rl78_bytesT));
508 fragP->tc_frag_data = 0;
511 /* When relaxing, we need to output a reloc for any .align directive
512 so that we can retain this alignment as we adjust opcode sizes. */
514 rl78_handle_align (fragS * frag)
517 && (frag->fr_type == rs_align
518 || frag->fr_type == rs_align_code)
519 && frag->fr_address + frag->fr_fix > 0
520 && frag->fr_offset > 0
521 && now_seg != bss_section)
523 fix_new (frag, frag->fr_fix, 0,
524 &abs_symbol, RL78_RELAXA_ALIGN + frag->fr_offset,
525 0, BFD_RELOC_RL78_RELAX);
526 /* For the purposes of relaxation, this relocation is attached
527 to the byte *after* the alignment - i.e. the byte that must
529 fix_new (frag->fr_next, 0, 0,
530 &abs_symbol, RL78_RELAXA_ELIGN + frag->fr_offset,
531 0, BFD_RELOC_RL78_RELAX);
536 md_atof (int type, char * litP, int * sizeP)
538 return ieee_md_atof (type, litP, sizeP, target_big_endian);
542 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
547 #define APPEND(B, N_B) \
548 if (rl78_bytes.N_B) \
550 memcpy (bytes + idx, rl78_bytes.B, rl78_bytes.N_B); \
551 idx += rl78_bytes.N_B; \
556 md_assemble (char * str)
559 fragS * frag_then = frag_now;
565 /*printf("\033[32mASM: %s\033[0m\n", str);*/
567 dwarf2_emit_insn (0);
569 memset (& rl78_bytes, 0, sizeof (rl78_bytes));
571 rl78_lex_init (str, str + strlen (str));
575 /* This simplifies the relaxation code. */
576 if (rl78_bytes.n_relax || rl78_bytes.link_relax)
578 int olen = rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops;
579 /* We do it this way because we want the frag to have the
580 rl78_bytes in it, which we initialize above. The extra bytes
582 bytes = frag_more (olen + 3);
583 frag_then = frag_now;
584 frag_variant (rs_machine_dependent,
585 olen /* max_chars */,
591 frag_then->fr_opcode = bytes;
592 frag_then->fr_fix = olen + (bytes - frag_then->fr_literal);
593 frag_then->fr_subtype = olen;
594 frag_then->fr_var = 0;
598 bytes = frag_more (rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops);
599 frag_then = frag_now;
602 APPEND (prefix, n_prefix);
603 APPEND (base, n_base);
606 if (rl78_bytes.link_relax)
610 f = fix_new (frag_then,
611 (char *) bytes - frag_then->fr_literal,
614 rl78_bytes.link_relax | rl78_bytes.n_fixups,
616 BFD_RELOC_RL78_RELAX);
617 frag_then->tc_frag_data->link_relax_fixP = f;
620 for (i = 0; i < rl78_bytes.n_fixups; i ++)
622 /* index: [nbytes][type] */
623 static int reloc_map[5][4] =
626 { BFD_RELOC_8, BFD_RELOC_8_PCREL },
627 { BFD_RELOC_16, BFD_RELOC_16_PCREL },
628 { BFD_RELOC_24, BFD_RELOC_24_PCREL },
629 { BFD_RELOC_32, BFD_RELOC_32_PCREL },
633 idx = rl78_bytes.fixups[i].offset / 8;
634 rel = reloc_map [rl78_bytes.fixups[i].nbits / 8][(int) rl78_bytes.fixups[i].type];
636 if (rl78_bytes.fixups[i].reloc)
637 rel = rl78_bytes.fixups[i].reloc;
639 if (frag_then->tc_frag_data)
640 exp = & frag_then->tc_frag_data->fixups[i].exp;
642 exp = & rl78_bytes.fixups[i].exp;
644 f = fix_new_exp (frag_then,
645 (char *) bytes + idx - frag_then->fr_literal,
646 rl78_bytes.fixups[i].nbits / 8,
648 rl78_bytes.fixups[i].type == RL78REL_PCREL ? 1 : 0,
650 if (frag_then->tc_frag_data)
651 frag_then->tc_frag_data->fixups[i].fixP = f;
656 rl78_cons_fix_new (fragS * frag,
661 bfd_reloc_code_real_type type;
679 as_bad (_("unsupported constant size %d\n"), size);
685 case BFD_RELOC_RL78_CODE:
689 case BFD_RELOC_RL78_LO16:
690 case BFD_RELOC_RL78_HI16:
693 /* Fixups to assembler generated expressions do not use %hi or %lo. */
695 as_bad (_("%%hi16/%%lo16 only applies to .short or .hword"));
700 case BFD_RELOC_RL78_HI8:
703 /* Fixups to assembler generated expressions do not use %hi or %lo. */
705 as_bad (_("%%hi8 only applies to .byte"));
714 if (exp->X_op == O_subtract && exp->X_op_symbol)
716 if (size != 4 && size != 2 && size != 1)
717 as_bad (_("difference of two symbols only supported with .long, .short, or .byte"));
719 type = BFD_RELOC_RL78_DIFF;
722 fixP = fix_new_exp (frag, where, (int) size, exp, 0, type);
725 /* These are intended to have values larger than the container,
726 since the backend puts only the portion we need in it.
727 However, we don't have a backend-specific reloc for them as
728 they're handled with complex relocations. */
729 case BFD_RELOC_RL78_LO16:
730 case BFD_RELOC_RL78_HI16:
731 case BFD_RELOC_RL78_HI8:
732 fixP->fx_no_overflow = 1;
740 /*----------------------------------------------------------------------*/
741 /* To recap: we estimate everything based on md_estimate_size, then
742 adjust based on rl78_relax_frag. When it all settles, we call
743 md_convert frag to update the bytes. The relaxation types and
744 relocations are in fragP->tc_frag_data, which is a copy of that
747 Our scheme is as follows: fr_fix has the size of the smallest
748 opcode (like BRA.S). We store the number of total bytes we need in
749 fr_subtype. When we're done relaxing, we use fr_subtype and the
750 existing opcode bytes to figure out what actual opcode we need to
751 put in there. If the fixup isn't resolvable now, we use the
754 #define TRACE_RELAX 0
755 #define tprintf if (TRACE_RELAX) printf
771 /* We're looking for these types of relaxations:
773 BT 00110001 sbit0cc1 addr---- (cc is 10 (BF) or 01 (BT))
774 B~T 00110001 sbit0cc1 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
776 BT sfr 00110001 sbit0cc0 sfr----- addr----
777 BT ES: 00010001 00101110 sbit0cc1 addr----
780 B~C 110111cc 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
782 BH 01100001 110c0011 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
783 B~H 01100001 110c0011 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
786 /* Given the opcode bytes at OP, figure out which opcode it is and
787 return the type of opcode. We use this to re-encode the opcode as
788 a different size later. */
791 rl78_opcode_type (char * ops)
793 unsigned char *op = (unsigned char *)ops;
796 && ((op[1] & 0x0f) == 0x05
797 || (op[1] & 0x0f) == 0x03))
801 && ((op[1] & 0x0f) == 0x04
802 || (op[1] & 0x0f) == 0x02))
807 && ((op[2] & 0x0f) == 0x05
808 || (op[2] & 0x0f) == 0x03))
811 if ((op[0] & 0xfc) == 0xdc)
815 && (op[1] & 0xef) == 0xc3)
819 && (op[1] & 0xcf) == 0xc8)
823 && (op[1] & 0xef) == 0xe3)
829 if ((op[0] & 0xec) == 0xec)
835 /* Returns zero if *addrP has the target address. Else returns nonzero
836 if we cannot compute the target address yet. */
839 rl78_frag_fix_value (fragS * fragP,
847 rl78_bytesT * b = fragP->tc_frag_data;
848 expressionS * exp = & b->fixups[which].exp;
850 if (need_diff && exp->X_op != O_subtract)
853 if (exp->X_add_symbol)
855 if (S_FORCE_RELOC (exp->X_add_symbol, 1))
857 if (S_GET_SEGMENT (exp->X_add_symbol) != segment)
859 addr += S_GET_VALUE (exp->X_add_symbol);
862 if (exp->X_op_symbol)
864 if (exp->X_op != O_subtract)
866 if (S_FORCE_RELOC (exp->X_op_symbol, 1))
868 if (S_GET_SEGMENT (exp->X_op_symbol) != segment)
870 addr -= S_GET_VALUE (exp->X_op_symbol);
874 addr += exp->X_add_number;
879 /* Estimate how big the opcode is after this relax pass. The return
880 value is the difference between fr_fix and the actual size. We
881 compute the total size in rl78_relax_frag and store it in fr_subtype,
882 so we only need to subtract fx_fix and return it. */
885 md_estimate_size_before_relax (fragS * fragP ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED)
890 /* This is the size of the opcode that's accounted for in fr_fix. */
891 opfixsize = fragP->fr_fix - (fragP->fr_opcode - fragP->fr_literal);
892 /* This is the size of the opcode that isn't. */
893 delta = (fragP->fr_subtype - opfixsize);
895 tprintf (" -> opfixsize %d delta %d\n", opfixsize, delta);
899 /* Given the new addresses for this relax pass, figure out how big
900 each opcode must be. We store the total number of bytes needed in
901 fr_subtype. The return value is the difference between the size
902 after the last pass and the size after this pass, so we use the old
903 fr_subtype to calculate the difference. */
906 rl78_relax_frag (segT segment ATTRIBUTE_UNUSED, fragS * fragP, long stretch)
908 addressT addr0, sym_addr;
911 int oldsize = fragP->fr_subtype;
912 int newsize = oldsize;
916 mypc = fragP->fr_address + (fragP->fr_opcode - fragP->fr_literal);
918 /* If we ever get more than one reloc per opcode, this is the one
922 optype = rl78_opcode_type (fragP->fr_opcode);
923 /* Try to get the target address. */
924 if (rl78_frag_fix_value (fragP, segment, ri, & addr0,
925 fragP->tc_frag_data->relax[ri].type != RL78_RELAX_BRANCH,
928 /* If we don't expect the linker to do relaxing, don't emit
929 expanded opcodes that only the linker will relax. */
931 return newsize - oldsize;
933 /* If we don't, we must use the maximum size for the linker. */
934 switch (fragP->tc_frag_data->relax[ri].type)
936 case RL78_RELAX_BRANCH:
962 fragP->fr_subtype = newsize;
963 tprintf (" -> new %d old %d delta %d (external)\n", newsize, oldsize, newsize-oldsize);
964 return newsize - oldsize;
970 switch (fragP->tc_frag_data->relax[ri].type)
972 case RL78_RELAX_BRANCH:
973 disp = (int) addr0 - (int) mypc;
978 if (disp >= -128 && (disp - (oldsize-2)) <= 127)
985 if (disp >= -128 && (disp - (oldsize-3)) <= 127)
991 if (disp >= -128 && (disp - (oldsize-1)) <= 127)
997 if (disp >= -128 && (disp - (oldsize-2)) <= 127)
1012 /* This prevents infinite loops in align-heavy sources. */
1013 if (newsize < oldsize)
1015 if (fragP->tc_frag_data->times_shrank > 10
1016 && fragP->tc_frag_data->times_grown > 10)
1018 if (fragP->tc_frag_data->times_shrank < 20)
1019 fragP->tc_frag_data->times_shrank ++;
1021 else if (newsize > oldsize)
1023 if (fragP->tc_frag_data->times_grown < 20)
1024 fragP->tc_frag_data->times_grown ++;
1027 fragP->fr_subtype = newsize;
1028 tprintf (" -> new %d old %d delta %d\n", newsize, oldsize, newsize-oldsize);
1029 return newsize - oldsize;
1032 /* This lets us test for the opcode type and the desired size in a
1033 switch statement. */
1034 #define OPCODE(type,size) ((type) * 16 + (size))
1036 /* Given the opcode stored in fr_opcode and the number of bytes we
1037 think we need, encode a new opcode. We stored a pointer to the
1038 fixup for this opcode in the tc_frag_data structure. If we can do
1039 the fixup here, we change the relocation type to "none" (we test
1040 for that in tc_gen_reloc) else we change it to the right type for
1041 the new (biggest) opcode. */
1044 md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
1045 segT segment ATTRIBUTE_UNUSED,
1046 fragS * fragP ATTRIBUTE_UNUSED)
1048 rl78_bytesT * rl78b = fragP->tc_frag_data;
1049 addressT addr0, mypc;
1051 int reloc_type, reloc_adjust;
1052 char * op = fragP->fr_opcode;
1055 int fi = (rl78b->n_fixups > 1) ? 1 : 0;
1056 fixS * fix = rl78b->fixups[fi].fixP;
1058 /* If we ever get more than one reloc per opcode, this is the one
1062 /* We used a new frag for this opcode, so the opcode address should
1063 be the frag address. */
1064 mypc = fragP->fr_address + (fragP->fr_opcode - fragP->fr_literal);
1065 tprintf ("\033[32mmypc: 0x%x\033[0m\n", (int)mypc);
1067 /* Try to get the target address. If we fail here, we just use the
1069 if (rl78_frag_fix_value (fragP, segment, 0, & addr0,
1070 fragP->tc_frag_data->relax[ri].type != RL78_RELAX_BRANCH, 0))
1072 /* We don't know the target address. */
1076 tprintf ("unknown addr ? - %x = ?\n", (int)mypc);
1080 /* We know the target address, and it's in addr0. */
1081 disp = (int) addr0 - (int) mypc;
1082 tprintf ("known addr %x - %x = %d\n", (int)addr0, (int)mypc, disp);
1088 reloc_type = BFD_RELOC_NONE;
1091 switch (fragP->tc_frag_data->relax[ri].type)
1093 case RL78_RELAX_BRANCH:
1094 switch (OPCODE (rl78_opcode_type (fragP->fr_opcode), fragP->fr_subtype))
1097 case OPCODE (OT_bt, 3): /* BT A,$ - no change. */
1100 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1103 case OPCODE (OT_bt, 6): /* BT A,$ - long version. */
1105 op[1] ^= 0x06; /* toggle conditional. */
1106 op[2] = 3; /* displacement over long branch. */
1108 op[3] = 0xEE; /* BR $!addr20 */
1109 op[4] = disp & 0xff;
1111 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1115 case OPCODE (OT_bt_sfr, 4): /* BT PSW,$ - no change. */
1118 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1121 case OPCODE (OT_bt_sfr, 7): /* BT PSW,$ - long version. */
1123 op[1] ^= 0x06; /* toggle conditional. */
1124 op[3] = 3; /* displacement over long branch. */
1126 op[4] = 0xEE; /* BR $!addr20 */
1127 op[5] = disp & 0xff;
1129 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1133 case OPCODE (OT_bt_es, 4): /* BT ES:[HL],$ - no change. */
1136 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1139 case OPCODE (OT_bt_es, 7): /* BT PSW,$ - long version. */
1141 op[2] ^= 0x06; /* toggle conditional. */
1142 op[3] = 3; /* displacement over long branch. */
1144 op[4] = 0xEE; /* BR $!addr20 */
1145 op[5] = disp & 0xff;
1147 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1151 case OPCODE (OT_bc, 2): /* BC $ - no change. */
1154 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1157 case OPCODE (OT_bc, 5): /* BC $ - long version. */
1159 op[0] ^= 0x02; /* toggle conditional. */
1162 op[2] = 0xEE; /* BR $!addr20 */
1163 op[3] = disp & 0xff;
1165 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1169 case OPCODE (OT_bh, 3): /* BH $ - no change. */
1172 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1175 case OPCODE (OT_bh, 6): /* BC $ - long version. */
1177 op[1] ^= 0x10; /* toggle conditional. */
1180 op[3] = 0xEE; /* BR $!addr20 */
1181 op[4] = disp & 0xff;
1183 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1187 case OPCODE (OT_sk, 2): /* SK<cond> - no change */
1188 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1192 reloc_type = fix ? fix->fx_r_type : BFD_RELOC_NONE;
1198 if (rl78b->n_fixups)
1200 reloc_type = fix->fx_r_type;
1206 if (rl78b->n_fixups)
1209 fix->fx_r_type = reloc_type;
1210 fix->fx_where += reloc_adjust;
1213 case BFD_RELOC_NONE:
1219 case BFD_RELOC_16_PCREL:
1225 fragP->fr_fix = fragP->fr_subtype + (fragP->fr_opcode - fragP->fr_literal);
1226 tprintf ("fragP->fr_fix now %ld (%d + (%p - %p)\n", (long) fragP->fr_fix,
1227 fragP->fr_subtype, fragP->fr_opcode, fragP->fr_literal);
1230 tprintf ("compare 0x%lx vs 0x%lx - 0x%lx = 0x%lx (%p)\n",
1231 (long)fragP->fr_fix,
1232 (long)fragP->fr_next->fr_address, (long)fragP->fr_address,
1233 (long)(fragP->fr_next->fr_address - fragP->fr_address),
1236 if (fragP->fr_next != NULL
1237 && ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
1239 as_bad (_("bad frag at %p : fix %ld addr %ld %ld \n"), fragP,
1240 (long) fragP->fr_fix,
1241 (long) fragP->fr_address, (long) fragP->fr_next->fr_address);
1244 /* End of relaxation code.
1245 ----------------------------------------------------------------------*/
1249 tc_gen_reloc (asection * seg ATTRIBUTE_UNUSED, fixS * fixp)
1251 static arelent * reloc[8];
1254 if (fixp->fx_r_type == BFD_RELOC_NONE)
1260 if (fixp->fx_r_type == BFD_RELOC_RL78_RELAX && !linkrelax)
1267 && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1269 fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy);
1270 fixp->fx_subsy = NULL;
1273 reloc[0] = XNEW (arelent);
1274 reloc[0]->sym_ptr_ptr = XNEW (asymbol *);
1275 * reloc[0]->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1276 reloc[0]->address = fixp->fx_frag->fr_address + fixp->fx_where;
1277 reloc[0]->addend = fixp->fx_offset;
1279 if (fixp->fx_r_type == BFD_RELOC_RL78_32_OP
1282 fixp->fx_r_type = BFD_RELOC_RL78_DIFF;
1285 #define OPX(REL,SYM,ADD) \
1286 reloc[rp] = XNEW (arelent); \
1287 reloc[rp]->sym_ptr_ptr = XNEW (asymbol *); \
1288 reloc[rp]->howto = bfd_reloc_type_lookup (stdoutput, REL); \
1289 reloc[rp]->addend = ADD; \
1290 * reloc[rp]->sym_ptr_ptr = SYM; \
1291 reloc[rp]->address = fixp->fx_frag->fr_address + fixp->fx_where; \
1293 #define OPSYM(SYM) OPX(BFD_RELOC_RL78_SYM, SYM, 0)
1295 /* FIXME: We cannot do the normal thing for an immediate value reloc,
1296 ie creating a RL78_SYM reloc in the *ABS* section with an offset
1297 equal to the immediate value we want to store. This fails because
1298 the reloc processing in bfd_perform_relocation and bfd_install_relocation
1299 will short circuit such relocs and never pass them on to the special
1300 reloc processing code. So instead we create a RL78_SYM reloc against
1301 the __rl78_abs__ symbol and arrange for the linker scripts to place
1302 this symbol at address 0. */
1303 #define OPIMM(IMM) OPX (BFD_RELOC_RL78_SYM, symbol_get_bfdsym (rl78_abs_sym), IMM)
1305 #define OP(OP) OPX(BFD_RELOC_RL78_##OP, *reloc[0]->sym_ptr_ptr, 0)
1306 #define SYM0() reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_RL78_SYM)
1310 /* Certain BFD relocations cannot be translated directly into
1311 a single (non-Red Hat) RL78 relocation, but instead need
1312 multiple RL78 relocations - handle them here. */
1313 switch (fixp->fx_r_type)
1315 case BFD_RELOC_RL78_DIFF:
1317 OPSYM (symbol_get_bfdsym (fixp->fx_subsy));
1320 switch (fixp->fx_size)
1334 case BFD_RELOC_RL78_NEG32:
1340 case BFD_RELOC_RL78_CODE:
1341 reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_RL78_16U);
1345 case BFD_RELOC_RL78_LO16:
1352 case BFD_RELOC_RL78_HI16:
1359 case BFD_RELOC_RL78_HI8:
1369 reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1378 rl78_validate_fix_sub (struct fix * f)
1380 /* We permit the subtraction of two symbols in a few cases. */
1381 /* mov #sym1-sym2, R3 */
1382 if (f->fx_r_type == BFD_RELOC_RL78_32_OP)
1384 /* .long sym1-sym2 */
1385 if (f->fx_r_type == BFD_RELOC_RL78_DIFF
1387 && (f->fx_size == 4 || f->fx_size == 2 || f->fx_size == 1))
1393 md_pcrel_from_section (fixS * fixP, segT sec)
1397 if (fixP->fx_addsy != NULL
1398 && (! S_IS_DEFINED (fixP->fx_addsy)
1399 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
1400 /* The symbol is undefined (or is defined but not in this section).
1401 Let the linker figure it out. */
1404 rv = fixP->fx_frag->fr_address + fixP->fx_where;
1405 switch (fixP->fx_r_type)
1407 case BFD_RELOC_8_PCREL:
1410 case BFD_RELOC_16_PCREL:
1420 md_apply_fix (struct fix * f ATTRIBUTE_UNUSED,
1421 valueT * t ATTRIBUTE_UNUSED,
1422 segT s ATTRIBUTE_UNUSED)
1427 /* We always defer overflow checks for these to the linker, as it
1428 needs to do PLT stuff. */
1429 if (f->fx_r_type == BFD_RELOC_RL78_CODE)
1430 f->fx_no_overflow = 1;
1432 if (f->fx_addsy && S_FORCE_RELOC (f->fx_addsy, 1))
1434 if (f->fx_subsy && S_FORCE_RELOC (f->fx_subsy, 1))
1437 op = f->fx_frag->fr_literal + f->fx_where;
1438 val = (unsigned long) * t;
1440 if (f->fx_addsy == NULL)
1443 switch (f->fx_r_type)
1445 case BFD_RELOC_NONE:
1448 case BFD_RELOC_RL78_RELAX:
1452 case BFD_RELOC_8_PCREL:
1453 if ((long)val < -128 || (long)val > 127)
1454 as_bad_where (f->fx_file, f->fx_line,
1455 _("value of %ld too large for 8-bit branch"),
1459 case BFD_RELOC_RL78_SADDR: /* We need to store the 8 LSB, but this works. */
1463 case BFD_RELOC_16_PCREL:
1464 if ((long)val < -32768 || (long)val > 32767)
1465 as_bad_where (f->fx_file, f->fx_line,
1466 _("value of %ld too large for 16-bit branch"),
1470 case BFD_RELOC_RL78_CODE:
1488 case BFD_RELOC_RL78_DIFF:
1498 case BFD_RELOC_RL78_HI8:
1503 case BFD_RELOC_RL78_HI16:
1509 case BFD_RELOC_RL78_LO16:
1515 as_bad (_("Unknown reloc in md_apply_fix: %s"),
1516 bfd_get_reloc_code_name (f->fx_r_type));
1523 md_section_align (segT segment, valueT size)
1525 int align = bfd_get_section_alignment (stdoutput, segment);
1526 return ((size + (1 << align) - 1) & -(1 << align));