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"
26 #include "elf/common.h"
28 #include "rl78-defs.h"
29 #include "filenames.h"
34 const char comment_chars[] = ";";
35 /* Note that input_file.c hand checks for '#' at the beginning of the
36 first line of the input file. This is because the compiler outputs
37 #NO_APP at the beginning of its output. */
38 const char line_comment_chars[] = "#";
39 /* Use something that isn't going to be needed by any expressions or
41 const char line_separator_chars[] = "@";
43 const char EXP_CHARS[] = "eE";
44 const char FLT_CHARS[] = "dD";
46 /* ELF flags to set in the output file header. */
47 static int elf_flags = 0;
49 /*------------------------------------------------------------------*/
51 char * rl78_lex_start;
54 typedef struct rl78_bytesT
67 char type; /* RL78REL_*. */
80 fixS *link_relax_fixP;
85 static rl78_bytesT rl78_bytes;
88 rl78_relax (int type, int pos)
90 rl78_bytes.relax[rl78_bytes.n_relax].type = type;
91 rl78_bytes.relax[rl78_bytes.n_relax].field_pos = pos;
92 rl78_bytes.relax[rl78_bytes.n_relax].val_ofs = rl78_bytes.n_base + rl78_bytes.n_ops;
93 rl78_bytes.n_relax ++;
97 rl78_linkrelax_addr16 (void)
99 rl78_bytes.link_relax |= RL78_RELAXA_ADDR16;
103 rl78_linkrelax_branch (void)
105 rl78_relax (RL78_RELAX_BRANCH, 0);
106 rl78_bytes.link_relax |= RL78_RELAXA_BRA;
110 rl78_fixup (expressionS exp, int offsetbits, int nbits, int type)
112 rl78_bytes.fixups[rl78_bytes.n_fixups].exp = exp;
113 rl78_bytes.fixups[rl78_bytes.n_fixups].offset = offsetbits;
114 rl78_bytes.fixups[rl78_bytes.n_fixups].nbits = nbits;
115 rl78_bytes.fixups[rl78_bytes.n_fixups].type = type;
116 rl78_bytes.fixups[rl78_bytes.n_fixups].reloc = exp.X_md;
117 rl78_bytes.n_fixups ++;
120 #define rl78_field_fixup(exp, offset, nbits, type) \
121 rl78_fixup (exp, offset + 8 * rl78_bytes.n_prefix), nbits, type)
123 #define rl78_op_fixup(exp, offset, nbits, type) \
124 rl78_fixup (exp, offset + 8 * (rl78_bytes.n_prefix + rl78_bytes.n_base), nbits, type)
129 rl78_bytes.prefix[0] = p;
130 rl78_bytes.n_prefix = 1;
136 return rl78_bytes.n_prefix;
142 rl78_bytes.base[0] = b1;
143 rl78_bytes.n_base = 1;
147 rl78_base2 (int b1, int b2)
149 rl78_bytes.base[0] = b1;
150 rl78_bytes.base[1] = b2;
151 rl78_bytes.n_base = 2;
155 rl78_base3 (int b1, int b2, int b3)
157 rl78_bytes.base[0] = b1;
158 rl78_bytes.base[1] = b2;
159 rl78_bytes.base[2] = b3;
160 rl78_bytes.n_base = 3;
164 rl78_base4 (int b1, int b2, int b3, int b4)
166 rl78_bytes.base[0] = b1;
167 rl78_bytes.base[1] = b2;
168 rl78_bytes.base[2] = b3;
169 rl78_bytes.base[3] = b4;
170 rl78_bytes.n_base = 4;
173 #define F_PRECISION 2
176 rl78_op (expressionS exp, int nbytes, int type)
180 if ((exp.X_op == O_constant || exp.X_op == O_big)
181 && type != RL78REL_PCREL)
183 if (exp.X_op == O_big && exp.X_add_number <= 0)
186 char * ip = rl78_bytes.ops + rl78_bytes.n_ops;
188 gen_to_words (w, F_PRECISION, 8);
193 rl78_bytes.n_ops += 4;
197 v = exp.X_add_number;
200 rl78_bytes.ops[rl78_bytes.n_ops++] =v & 0xff;
209 && exp.X_md == BFD_RELOC_RL78_CODE)
213 && (exp.X_md == BFD_RELOC_RL78_LO16
214 || exp.X_md == BFD_RELOC_RL78_HI16))
215 as_bad (_("16-bit relocation used in 8-bit operand"));
218 && exp.X_md == BFD_RELOC_RL78_HI8)
219 as_bad (_("8-bit relocation used in 16-bit operand"));
221 rl78_op_fixup (exp, rl78_bytes.n_ops * 8, nbytes * 8, type);
222 memset (rl78_bytes.ops + rl78_bytes.n_ops, 0, nbytes);
223 rl78_bytes.n_ops += nbytes;
227 /* This gets complicated when the field spans bytes, because fields
228 are numbered from the MSB of the first byte as zero, and bits are
229 stored LSB towards the LSB of the byte. Thus, a simple four-bit
230 insertion of 12 at position 4 of 0x00 yields: 0x0b. A three-bit
231 insertion of b'MXL at position 7 is like this:
233 - - - - - - - - - - - - - - - -
237 rl78_field (int val, int pos, int sz)
244 if (val < 0 || val >= (1 << sz))
245 as_bad (_("Value %d doesn't fit in unsigned %d-bit field"), val, sz);
250 if (val < -(1 << (sz - 1)) || val >= (1 << (sz - 1)))
251 as_bad (_("Value %d doesn't fit in signed %d-bit field"), val, sz);
254 /* This code points at 'M' in the above example. */
258 while (bitp + sz > 8)
263 svalm = val >> (sz - ssz);
264 svalm = svalm & ((1 << ssz) - 1);
265 svalm = svalm << (8 - bitp - ssz);
266 gas_assert (bytep < rl78_bytes.n_base);
267 rl78_bytes.base[bytep] |= svalm;
273 valm = val & ((1 << sz) - 1);
274 valm = valm << (8 - bitp - sz);
275 gas_assert (bytep < rl78_bytes.n_base);
276 rl78_bytes.base[bytep] |= valm;
279 /*------------------------------------------------------------------*/
283 OPTION_RELAX = OPTION_MD_BASE,
288 OPTION_32BIT_DOUBLES,
289 OPTION_64BIT_DOUBLES,
292 #define RL78_SHORTOPTS ""
293 const char * md_shortopts = RL78_SHORTOPTS;
295 /* Assembler options. */
296 struct option md_longopts[] =
298 {"relax", no_argument, NULL, OPTION_RELAX},
299 {"norelax", no_argument, NULL, OPTION_NORELAX},
300 {"mg10", no_argument, NULL, OPTION_G10},
301 {"mg13", no_argument, NULL, OPTION_G13},
302 {"mg14", no_argument, NULL, OPTION_G14},
303 {"mrl78", no_argument, NULL, OPTION_G14},
304 {"m32bit-doubles", no_argument, NULL, OPTION_32BIT_DOUBLES},
305 {"m64bit-doubles", no_argument, NULL, OPTION_64BIT_DOUBLES},
306 {NULL, no_argument, NULL, 0}
308 size_t md_longopts_size = sizeof (md_longopts);
311 md_parse_option (int c, char * arg ATTRIBUTE_UNUSED)
323 elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
324 elf_flags |= E_FLAG_RL78_G10;
328 elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
329 elf_flags |= E_FLAG_RL78_G13;
333 elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
334 elf_flags |= E_FLAG_RL78_G14;
337 case OPTION_32BIT_DOUBLES:
338 elf_flags &= ~ E_FLAG_RL78_64BIT_DOUBLES;
341 case OPTION_64BIT_DOUBLES:
342 elf_flags |= E_FLAG_RL78_64BIT_DOUBLES;
351 return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G10;
357 return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G13;
363 return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G14;
367 md_show_usage (FILE * stream)
369 fprintf (stream, _(" RL78 specific command line options:\n"));
370 fprintf (stream, _(" --mrelax Enable link time relaxation\n"));
371 fprintf (stream, _(" --mg10 Enable support for G10 variant\n"));
372 fprintf (stream, _(" --mg13 Selects the G13 core.\n"));
373 fprintf (stream, _(" --mg14 Selects the G14 core [default]\n"));
374 fprintf (stream, _(" --mrl78 Alias for --mg14\n"));
375 fprintf (stream, _(" --m32bit-doubles [default]\n"));
376 fprintf (stream, _(" --m64bit-doubles Source code uses 64-bit doubles\n"));
380 s_bss (int ignore ATTRIBUTE_UNUSED)
384 temp = get_absolute_expression ();
385 subseg_set (bss_section, (subsegT) temp);
386 demand_empty_rest_of_line ();
390 rl78_float_cons (int ignore ATTRIBUTE_UNUSED)
392 if (elf_flags & E_FLAG_RL78_64BIT_DOUBLES)
393 return float_cons ('d');
394 return float_cons ('f');
397 /* The target specific pseudo-ops which we support. */
398 const pseudo_typeS md_pseudo_table[] =
400 /* Our "standard" pseudos. */
401 { "double", rl78_float_cons, 'd' },
403 { "3byte", cons, 3 },
407 /* End of list marker. */
411 static symbolS * rl78_abs_sym = NULL;
416 rl78_abs_sym = symbol_make ("__rl78_abs__");
424 /* Set the ELF specific flags. */
426 rl78_elf_final_processing (void)
428 elf_elfheader (stdoutput)->e_flags |= elf_flags;
431 /* Write a value out to the object file, using the appropriate endianness. */
433 md_number_to_chars (char * buf, valueT val, int n)
435 number_to_chars_littleendian (buf, val, n);
439 require_end_of_expr (const char *fname)
441 while (* input_line_pointer == ' '
442 || * input_line_pointer == '\t')
443 input_line_pointer ++;
445 if (! * input_line_pointer
446 || strchr ("\n\r,", * input_line_pointer)
447 || strchr (comment_chars, * input_line_pointer)
448 || strchr (line_comment_chars, * input_line_pointer)
449 || strchr (line_separator_chars, * input_line_pointer))
452 as_bad (_("%%%s() must be outermost term in expression"), fname);
462 { "code", BFD_RELOC_RL78_CODE },
463 { "lo16", BFD_RELOC_RL78_LO16 },
464 { "hi16", BFD_RELOC_RL78_HI16 },
465 { "hi8", BFD_RELOC_RL78_HI8 },
470 md_operand (expressionS * exp ATTRIBUTE_UNUSED)
475 for (i = 0; reloc_functions[i].fname; i++)
477 int flen = strlen (reloc_functions[i].fname);
479 if (input_line_pointer[0] == '%'
480 && strncasecmp (input_line_pointer + 1, reloc_functions[i].fname, flen) == 0
481 && input_line_pointer[flen + 1] == '(')
483 reloc = reloc_functions[i].reloc;
484 input_line_pointer += flen + 2;
492 if (* input_line_pointer == ')')
493 input_line_pointer ++;
497 require_end_of_expr (reloc_functions[i].fname);
501 rl78_frag_init (fragS * fragP)
503 if (rl78_bytes.n_relax || rl78_bytes.link_relax)
505 fragP->tc_frag_data = malloc (sizeof (rl78_bytesT));
506 memcpy (fragP->tc_frag_data, & rl78_bytes, sizeof (rl78_bytesT));
509 fragP->tc_frag_data = 0;
512 /* When relaxing, we need to output a reloc for any .align directive
513 so that we can retain this alignment as we adjust opcode sizes. */
515 rl78_handle_align (fragS * frag)
518 && (frag->fr_type == rs_align
519 || frag->fr_type == rs_align_code)
520 && frag->fr_address + frag->fr_fix > 0
521 && frag->fr_offset > 0
522 && now_seg != bss_section)
524 fix_new (frag, frag->fr_fix, 0,
525 &abs_symbol, RL78_RELAXA_ALIGN + frag->fr_offset,
526 0, BFD_RELOC_RL78_RELAX);
527 /* For the purposes of relaxation, this relocation is attached
528 to the byte *after* the alignment - i.e. the byte that must
530 fix_new (frag->fr_next, 0, 0,
531 &abs_symbol, RL78_RELAXA_ELIGN + frag->fr_offset,
532 0, BFD_RELOC_RL78_RELAX);
537 md_atof (int type, char * litP, int * sizeP)
539 return ieee_md_atof (type, litP, sizeP, target_big_endian);
543 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
548 #define APPEND(B, N_B) \
549 if (rl78_bytes.N_B) \
551 memcpy (bytes + idx, rl78_bytes.B, rl78_bytes.N_B); \
552 idx += rl78_bytes.N_B; \
557 md_assemble (char * str)
560 fragS * frag_then = frag_now;
566 /*printf("\033[32mASM: %s\033[0m\n", str);*/
568 dwarf2_emit_insn (0);
570 memset (& rl78_bytes, 0, sizeof (rl78_bytes));
572 rl78_lex_init (str, str + strlen (str));
576 /* This simplifies the relaxation code. */
577 if (rl78_bytes.n_relax || rl78_bytes.link_relax)
579 int olen = rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops;
580 /* We do it this way because we want the frag to have the
581 rl78_bytes in it, which we initialize above. The extra bytes
583 bytes = frag_more (olen + 3);
584 frag_then = frag_now;
585 frag_variant (rs_machine_dependent,
586 olen /* max_chars */,
592 frag_then->fr_opcode = bytes;
593 frag_then->fr_fix = olen + (bytes - frag_then->fr_literal);
594 frag_then->fr_subtype = olen;
595 frag_then->fr_var = 0;
599 bytes = frag_more (rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops);
600 frag_then = frag_now;
603 APPEND (prefix, n_prefix);
604 APPEND (base, n_base);
607 if (rl78_bytes.link_relax)
611 f = fix_new (frag_then,
612 (char *) bytes - frag_then->fr_literal,
615 rl78_bytes.link_relax | rl78_bytes.n_fixups,
617 BFD_RELOC_RL78_RELAX);
618 frag_then->tc_frag_data->link_relax_fixP = f;
621 for (i = 0; i < rl78_bytes.n_fixups; i ++)
623 /* index: [nbytes][type] */
624 static int reloc_map[5][4] =
627 { BFD_RELOC_8, BFD_RELOC_8_PCREL },
628 { BFD_RELOC_16, BFD_RELOC_16_PCREL },
629 { BFD_RELOC_24, BFD_RELOC_24_PCREL },
630 { BFD_RELOC_32, BFD_RELOC_32_PCREL },
634 idx = rl78_bytes.fixups[i].offset / 8;
635 rel = reloc_map [rl78_bytes.fixups[i].nbits / 8][(int) rl78_bytes.fixups[i].type];
637 if (rl78_bytes.fixups[i].reloc)
638 rel = rl78_bytes.fixups[i].reloc;
640 if (frag_then->tc_frag_data)
641 exp = & frag_then->tc_frag_data->fixups[i].exp;
643 exp = & rl78_bytes.fixups[i].exp;
645 f = fix_new_exp (frag_then,
646 (char *) bytes + idx - frag_then->fr_literal,
647 rl78_bytes.fixups[i].nbits / 8,
649 rl78_bytes.fixups[i].type == RL78REL_PCREL ? 1 : 0,
651 if (frag_then->tc_frag_data)
652 frag_then->tc_frag_data->fixups[i].fixP = f;
657 rl78_cons_fix_new (fragS * frag,
662 bfd_reloc_code_real_type type;
680 as_bad (_("unsupported constant size %d\n"), size);
686 case BFD_RELOC_RL78_CODE:
690 case BFD_RELOC_RL78_LO16:
691 case BFD_RELOC_RL78_HI16:
694 /* Fixups to assembler generated expressions do not use %hi or %lo. */
696 as_bad (_("%%hi16/%%lo16 only applies to .short or .hword"));
701 case BFD_RELOC_RL78_HI8:
704 /* Fixups to assembler generated expressions do not use %hi or %lo. */
706 as_bad (_("%%hi8 only applies to .byte"));
715 if (exp->X_op == O_subtract && exp->X_op_symbol)
717 if (size != 4 && size != 2 && size != 1)
718 as_bad (_("difference of two symbols only supported with .long, .short, or .byte"));
720 type = BFD_RELOC_RL78_DIFF;
723 fixP = fix_new_exp (frag, where, (int) size, exp, 0, type);
726 /* These are intended to have values larger than the container,
727 since the backend puts only the portion we need in it.
728 However, we don't have a backend-specific reloc for them as
729 they're handled with complex relocations. */
730 case BFD_RELOC_RL78_LO16:
731 case BFD_RELOC_RL78_HI16:
732 case BFD_RELOC_RL78_HI8:
733 fixP->fx_no_overflow = 1;
741 /*----------------------------------------------------------------------*/
742 /* To recap: we estimate everything based on md_estimate_size, then
743 adjust based on rl78_relax_frag. When it all settles, we call
744 md_convert frag to update the bytes. The relaxation types and
745 relocations are in fragP->tc_frag_data, which is a copy of that
748 Our scheme is as follows: fr_fix has the size of the smallest
749 opcode (like BRA.S). We store the number of total bytes we need in
750 fr_subtype. When we're done relaxing, we use fr_subtype and the
751 existing opcode bytes to figure out what actual opcode we need to
752 put in there. If the fixup isn't resolvable now, we use the
755 #define TRACE_RELAX 0
756 #define tprintf if (TRACE_RELAX) printf
772 /* We're looking for these types of relaxations:
774 BT 00110001 sbit0cc1 addr---- (cc is 10 (BF) or 01 (BT))
775 B~T 00110001 sbit0cc1 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
777 BT sfr 00110001 sbit0cc0 sfr----- addr----
778 BT ES: 00010001 00101110 sbit0cc1 addr----
781 B~C 110111cc 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
783 BH 01100001 110c0011 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
784 B~H 01100001 110c0011 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
787 /* Given the opcode bytes at OP, figure out which opcode it is and
788 return the type of opcode. We use this to re-encode the opcode as
789 a different size later. */
792 rl78_opcode_type (char * ops)
794 unsigned char *op = (unsigned char *)ops;
797 && ((op[1] & 0x0f) == 0x05
798 || (op[1] & 0x0f) == 0x03))
802 && ((op[1] & 0x0f) == 0x04
803 || (op[1] & 0x0f) == 0x02))
808 && ((op[2] & 0x0f) == 0x05
809 || (op[2] & 0x0f) == 0x03))
812 if ((op[0] & 0xfc) == 0xdc)
816 && (op[1] & 0xef) == 0xc3)
820 && (op[1] & 0xcf) == 0xc8)
824 && (op[1] & 0xef) == 0xe3)
830 if ((op[0] & 0xec) == 0xec)
836 /* Returns zero if *addrP has the target address. Else returns nonzero
837 if we cannot compute the target address yet. */
840 rl78_frag_fix_value (fragS * fragP,
848 rl78_bytesT * b = fragP->tc_frag_data;
849 expressionS * exp = & b->fixups[which].exp;
851 if (need_diff && exp->X_op != O_subtract)
854 if (exp->X_add_symbol)
856 if (S_FORCE_RELOC (exp->X_add_symbol, 1))
858 if (S_GET_SEGMENT (exp->X_add_symbol) != segment)
860 addr += S_GET_VALUE (exp->X_add_symbol);
863 if (exp->X_op_symbol)
865 if (exp->X_op != O_subtract)
867 if (S_FORCE_RELOC (exp->X_op_symbol, 1))
869 if (S_GET_SEGMENT (exp->X_op_symbol) != segment)
871 addr -= S_GET_VALUE (exp->X_op_symbol);
875 addr += exp->X_add_number;
880 /* Estimate how big the opcode is after this relax pass. The return
881 value is the difference between fr_fix and the actual size. We
882 compute the total size in rl78_relax_frag and store it in fr_subtype,
883 so we only need to subtract fx_fix and return it. */
886 md_estimate_size_before_relax (fragS * fragP ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED)
891 /* This is the size of the opcode that's accounted for in fr_fix. */
892 opfixsize = fragP->fr_fix - (fragP->fr_opcode - fragP->fr_literal);
893 /* This is the size of the opcode that isn't. */
894 delta = (fragP->fr_subtype - opfixsize);
896 tprintf (" -> opfixsize %d delta %d\n", opfixsize, delta);
900 /* Given the new addresses for this relax pass, figure out how big
901 each opcode must be. We store the total number of bytes needed in
902 fr_subtype. The return value is the difference between the size
903 after the last pass and the size after this pass, so we use the old
904 fr_subtype to calculate the difference. */
907 rl78_relax_frag (segT segment ATTRIBUTE_UNUSED, fragS * fragP, long stretch)
909 addressT addr0, sym_addr;
912 int oldsize = fragP->fr_subtype;
913 int newsize = oldsize;
917 mypc = fragP->fr_address + (fragP->fr_opcode - fragP->fr_literal);
919 /* If we ever get more than one reloc per opcode, this is the one
923 optype = rl78_opcode_type (fragP->fr_opcode);
924 /* Try to get the target address. */
925 if (rl78_frag_fix_value (fragP, segment, ri, & addr0,
926 fragP->tc_frag_data->relax[ri].type != RL78_RELAX_BRANCH,
929 /* If we don't expect the linker to do relaxing, don't emit
930 expanded opcodes that only the linker will relax. */
932 return newsize - oldsize;
934 /* If we don't, we must use the maximum size for the linker. */
935 switch (fragP->tc_frag_data->relax[ri].type)
937 case RL78_RELAX_BRANCH:
963 fragP->fr_subtype = newsize;
964 tprintf (" -> new %d old %d delta %d (external)\n", newsize, oldsize, newsize-oldsize);
965 return newsize - oldsize;
971 switch (fragP->tc_frag_data->relax[ri].type)
973 case RL78_RELAX_BRANCH:
974 disp = (int) addr0 - (int) mypc;
979 if (disp >= -128 && (disp - (oldsize-2)) <= 127)
986 if (disp >= -128 && (disp - (oldsize-3)) <= 127)
992 if (disp >= -128 && (disp - (oldsize-1)) <= 127)
998 if (disp >= -128 && (disp - (oldsize-2)) <= 127)
1013 /* This prevents infinite loops in align-heavy sources. */
1014 if (newsize < oldsize)
1016 if (fragP->tc_frag_data->times_shrank > 10
1017 && fragP->tc_frag_data->times_grown > 10)
1019 if (fragP->tc_frag_data->times_shrank < 20)
1020 fragP->tc_frag_data->times_shrank ++;
1022 else if (newsize > oldsize)
1024 if (fragP->tc_frag_data->times_grown < 20)
1025 fragP->tc_frag_data->times_grown ++;
1028 fragP->fr_subtype = newsize;
1029 tprintf (" -> new %d old %d delta %d\n", newsize, oldsize, newsize-oldsize);
1030 return newsize - oldsize;
1033 /* This lets us test for the opcode type and the desired size in a
1034 switch statement. */
1035 #define OPCODE(type,size) ((type) * 16 + (size))
1037 /* Given the opcode stored in fr_opcode and the number of bytes we
1038 think we need, encode a new opcode. We stored a pointer to the
1039 fixup for this opcode in the tc_frag_data structure. If we can do
1040 the fixup here, we change the relocation type to "none" (we test
1041 for that in tc_gen_reloc) else we change it to the right type for
1042 the new (biggest) opcode. */
1045 md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
1046 segT segment ATTRIBUTE_UNUSED,
1047 fragS * fragP ATTRIBUTE_UNUSED)
1049 rl78_bytesT * rl78b = fragP->tc_frag_data;
1050 addressT addr0, mypc;
1052 int reloc_type, reloc_adjust;
1053 char * op = fragP->fr_opcode;
1056 int fi = (rl78b->n_fixups > 1) ? 1 : 0;
1057 fixS * fix = rl78b->fixups[fi].fixP;
1059 /* If we ever get more than one reloc per opcode, this is the one
1063 /* We used a new frag for this opcode, so the opcode address should
1064 be the frag address. */
1065 mypc = fragP->fr_address + (fragP->fr_opcode - fragP->fr_literal);
1066 tprintf ("\033[32mmypc: 0x%x\033[0m\n", (int)mypc);
1068 /* Try to get the target address. If we fail here, we just use the
1070 if (rl78_frag_fix_value (fragP, segment, 0, & addr0,
1071 fragP->tc_frag_data->relax[ri].type != RL78_RELAX_BRANCH, 0))
1073 /* We don't know the target address. */
1077 tprintf ("unknown addr ? - %x = ?\n", (int)mypc);
1081 /* We know the target address, and it's in addr0. */
1082 disp = (int) addr0 - (int) mypc;
1083 tprintf ("known addr %x - %x = %d\n", (int)addr0, (int)mypc, disp);
1089 reloc_type = BFD_RELOC_NONE;
1092 switch (fragP->tc_frag_data->relax[ri].type)
1094 case RL78_RELAX_BRANCH:
1095 switch (OPCODE (rl78_opcode_type (fragP->fr_opcode), fragP->fr_subtype))
1098 case OPCODE (OT_bt, 3): /* BT A,$ - no change. */
1101 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1104 case OPCODE (OT_bt, 6): /* BT A,$ - long version. */
1106 op[1] ^= 0x06; /* toggle conditional. */
1107 op[2] = 3; /* displacement over long branch. */
1109 op[3] = 0xEE; /* BR $!addr20 */
1110 op[4] = disp & 0xff;
1112 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1116 case OPCODE (OT_bt_sfr, 4): /* BT PSW,$ - no change. */
1119 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1122 case OPCODE (OT_bt_sfr, 7): /* BT PSW,$ - long version. */
1124 op[1] ^= 0x06; /* toggle conditional. */
1125 op[3] = 3; /* displacement over long branch. */
1127 op[4] = 0xEE; /* BR $!addr20 */
1128 op[5] = disp & 0xff;
1130 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1134 case OPCODE (OT_bt_es, 4): /* BT ES:[HL],$ - no change. */
1137 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1140 case OPCODE (OT_bt_es, 7): /* BT PSW,$ - long version. */
1142 op[2] ^= 0x06; /* toggle conditional. */
1143 op[3] = 3; /* displacement over long branch. */
1145 op[4] = 0xEE; /* BR $!addr20 */
1146 op[5] = disp & 0xff;
1148 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1152 case OPCODE (OT_bc, 2): /* BC $ - no change. */
1155 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1158 case OPCODE (OT_bc, 5): /* BC $ - long version. */
1160 op[0] ^= 0x02; /* toggle conditional. */
1163 op[2] = 0xEE; /* BR $!addr20 */
1164 op[3] = disp & 0xff;
1166 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1170 case OPCODE (OT_bh, 3): /* BH $ - no change. */
1173 reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
1176 case OPCODE (OT_bh, 6): /* BC $ - long version. */
1178 op[1] ^= 0x10; /* toggle conditional. */
1181 op[3] = 0xEE; /* BR $!addr20 */
1182 op[4] = disp & 0xff;
1184 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1188 case OPCODE (OT_sk, 2): /* SK<cond> - no change */
1189 reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
1193 reloc_type = fix ? fix->fx_r_type : BFD_RELOC_NONE;
1199 if (rl78b->n_fixups)
1201 reloc_type = fix->fx_r_type;
1207 if (rl78b->n_fixups)
1210 fix->fx_r_type = reloc_type;
1211 fix->fx_where += reloc_adjust;
1214 case BFD_RELOC_NONE:
1220 case BFD_RELOC_16_PCREL:
1226 fragP->fr_fix = fragP->fr_subtype + (fragP->fr_opcode - fragP->fr_literal);
1227 tprintf ("fragP->fr_fix now %ld (%d + (%p - %p)\n", (long) fragP->fr_fix,
1228 fragP->fr_subtype, fragP->fr_opcode, fragP->fr_literal);
1231 tprintf ("compare 0x%lx vs 0x%lx - 0x%lx = 0x%lx (%p)\n",
1232 (long)fragP->fr_fix,
1233 (long)fragP->fr_next->fr_address, (long)fragP->fr_address,
1234 (long)(fragP->fr_next->fr_address - fragP->fr_address),
1237 if (fragP->fr_next != NULL
1238 && ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
1240 as_bad (_("bad frag at %p : fix %ld addr %ld %ld \n"), fragP,
1241 (long) fragP->fr_fix,
1242 (long) fragP->fr_address, (long) fragP->fr_next->fr_address);
1245 /* End of relaxation code.
1246 ----------------------------------------------------------------------*/
1250 tc_gen_reloc (asection * seg ATTRIBUTE_UNUSED, fixS * fixp)
1252 static arelent * reloc[8];
1255 if (fixp->fx_r_type == BFD_RELOC_NONE)
1261 if (fixp->fx_r_type == BFD_RELOC_RL78_RELAX && !linkrelax)
1268 && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1270 fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy);
1271 fixp->fx_subsy = NULL;
1274 reloc[0] = (arelent *) xmalloc (sizeof (arelent));
1275 reloc[0]->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1276 * reloc[0]->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1277 reloc[0]->address = fixp->fx_frag->fr_address + fixp->fx_where;
1278 reloc[0]->addend = fixp->fx_offset;
1280 if (fixp->fx_r_type == BFD_RELOC_RL78_32_OP
1283 fixp->fx_r_type = BFD_RELOC_RL78_DIFF;
1286 #define OPX(REL,SYM,ADD) \
1287 reloc[rp] = (arelent *) xmalloc (sizeof (arelent)); \
1288 reloc[rp]->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); \
1289 reloc[rp]->howto = bfd_reloc_type_lookup (stdoutput, REL); \
1290 reloc[rp]->addend = ADD; \
1291 * reloc[rp]->sym_ptr_ptr = SYM; \
1292 reloc[rp]->address = fixp->fx_frag->fr_address + fixp->fx_where; \
1294 #define OPSYM(SYM) OPX(BFD_RELOC_RL78_SYM, SYM, 0)
1296 /* FIXME: We cannot do the normal thing for an immediate value reloc,
1297 ie creating a RL78_SYM reloc in the *ABS* section with an offset
1298 equal to the immediate value we want to store. This fails because
1299 the reloc processing in bfd_perform_relocation and bfd_install_relocation
1300 will short circuit such relocs and never pass them on to the special
1301 reloc processing code. So instead we create a RL78_SYM reloc against
1302 the __rl78_abs__ symbol and arrange for the linker scripts to place
1303 this symbol at address 0. */
1304 #define OPIMM(IMM) OPX (BFD_RELOC_RL78_SYM, symbol_get_bfdsym (rl78_abs_sym), IMM)
1306 #define OP(OP) OPX(BFD_RELOC_RL78_##OP, *reloc[0]->sym_ptr_ptr, 0)
1307 #define SYM0() reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_RL78_SYM)
1311 /* Certain BFD relocations cannot be translated directly into
1312 a single (non-Red Hat) RL78 relocation, but instead need
1313 multiple RL78 relocations - handle them here. */
1314 switch (fixp->fx_r_type)
1316 case BFD_RELOC_RL78_DIFF:
1318 OPSYM (symbol_get_bfdsym (fixp->fx_subsy));
1321 switch (fixp->fx_size)
1335 case BFD_RELOC_RL78_NEG32:
1341 case BFD_RELOC_RL78_CODE:
1342 reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_RL78_16U);
1346 case BFD_RELOC_RL78_LO16:
1353 case BFD_RELOC_RL78_HI16:
1360 case BFD_RELOC_RL78_HI8:
1370 reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1379 rl78_validate_fix_sub (struct fix * f)
1381 /* We permit the subtraction of two symbols in a few cases. */
1382 /* mov #sym1-sym2, R3 */
1383 if (f->fx_r_type == BFD_RELOC_RL78_32_OP)
1385 /* .long sym1-sym2 */
1386 if (f->fx_r_type == BFD_RELOC_RL78_DIFF
1388 && (f->fx_size == 4 || f->fx_size == 2 || f->fx_size == 1))
1394 md_pcrel_from_section (fixS * fixP, segT sec)
1398 if (fixP->fx_addsy != NULL
1399 && (! S_IS_DEFINED (fixP->fx_addsy)
1400 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
1401 /* The symbol is undefined (or is defined but not in this section).
1402 Let the linker figure it out. */
1405 rv = fixP->fx_frag->fr_address + fixP->fx_where;
1406 switch (fixP->fx_r_type)
1408 case BFD_RELOC_8_PCREL:
1411 case BFD_RELOC_16_PCREL:
1421 md_apply_fix (struct fix * f ATTRIBUTE_UNUSED,
1422 valueT * t ATTRIBUTE_UNUSED,
1423 segT s ATTRIBUTE_UNUSED)
1428 /* We always defer overflow checks for these to the linker, as it
1429 needs to do PLT stuff. */
1430 if (f->fx_r_type == BFD_RELOC_RL78_CODE)
1431 f->fx_no_overflow = 1;
1433 if (f->fx_addsy && S_FORCE_RELOC (f->fx_addsy, 1))
1435 if (f->fx_subsy && S_FORCE_RELOC (f->fx_subsy, 1))
1438 op = f->fx_frag->fr_literal + f->fx_where;
1439 val = (unsigned long) * t;
1441 if (f->fx_addsy == NULL)
1444 switch (f->fx_r_type)
1446 case BFD_RELOC_NONE:
1449 case BFD_RELOC_RL78_RELAX:
1453 case BFD_RELOC_8_PCREL:
1454 if ((long)val < -128 || (long)val > 127)
1455 as_bad_where (f->fx_file, f->fx_line,
1456 _("value of %ld too large for 8-bit branch"),
1460 case BFD_RELOC_RL78_SADDR: /* We need to store the 8 LSB, but this works. */
1464 case BFD_RELOC_16_PCREL:
1465 if ((long)val < -32768 || (long)val > 32767)
1466 as_bad_where (f->fx_file, f->fx_line,
1467 _("value of %ld too large for 16-bit branch"),
1471 case BFD_RELOC_RL78_CODE:
1489 case BFD_RELOC_RL78_DIFF:
1499 case BFD_RELOC_RL78_HI8:
1504 case BFD_RELOC_RL78_HI16:
1510 case BFD_RELOC_RL78_LO16:
1516 as_bad (_("Unknown reloc in md_apply_fix: %s"),
1517 bfd_get_reloc_code_name (f->fx_r_type));
1524 md_section_align (segT segment, valueT size)
1526 int align = bfd_get_section_alignment (stdoutput, segment);
1527 return ((size + (1 << align) - 1) & -(1 << align));