Home | History | Annotate | Line # | Download | only in config
tc-rl78.c revision 1.8
      1 /* tc-rl78.c -- Assembler for the Renesas RL78
      2    Copyright (C) 2011-2022 Free Software Foundation, Inc.
      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 3, 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 the Free
     18    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
     19    02110-1301, USA.  */
     20 
     21 #include "as.h"
     22 #include "safe-ctype.h"
     23 #include "dwarf2dbg.h"
     24 #include "elf/common.h"
     25 #include "elf/rl78.h"
     26 #include "rl78-defs.h"
     27 #include "filenames.h"
     28 #include "listing.h"
     29 #include "sb.h"
     30 #include "macro.h"
     31 
     32 const char comment_chars[]        = ";";
     33 /* Note that input_file.c hand checks for '#' at the beginning of the
     34    first line of the input file.  This is because the compiler outputs
     35    #NO_APP at the beginning of its output.  */
     36 const char line_comment_chars[]   = "#";
     37 /* Use something that isn't going to be needed by any expressions or
     38    other syntax.  */
     39 const char line_separator_chars[] = "@";
     40 
     41 const char EXP_CHARS[]            = "eE";
     42 const char FLT_CHARS[]            = "dD";
     43 
     44 /* ELF flags to set in the output file header.  */
     45 static int elf_flags = 0;
     46 
     47 /*------------------------------------------------------------------*/
     48 
     49 char * rl78_lex_start;
     50 char * rl78_lex_end;
     51 
     52 typedef struct rl78_bytesT
     53 {
     54   char prefix[1];
     55   int n_prefix;
     56   char base[4];
     57   int n_base;
     58   char ops[8];
     59   int n_ops;
     60   struct
     61   {
     62     expressionS  exp;
     63     char         offset;
     64     char         nbits;
     65     char         type; /* RL78REL_*.  */
     66     int          reloc;
     67     fixS *       fixP;
     68   } fixups[2];
     69   int n_fixups;
     70   struct
     71   {
     72     char type;
     73     char field_pos;
     74     char val_ofs;
     75   } relax[2];
     76   int n_relax;
     77   int link_relax;
     78   fixS *link_relax_fixP;
     79   char times_grown;
     80   char times_shrank;
     81 } rl78_bytesT;
     82 
     83 static rl78_bytesT rl78_bytes;
     84 
     85 void
     86 rl78_relax (int type, int pos)
     87 {
     88   rl78_bytes.relax[rl78_bytes.n_relax].type = type;
     89   rl78_bytes.relax[rl78_bytes.n_relax].field_pos = pos;
     90   rl78_bytes.relax[rl78_bytes.n_relax].val_ofs = rl78_bytes.n_base + rl78_bytes.n_ops;
     91   rl78_bytes.n_relax ++;
     92 }
     93 
     94 void
     95 rl78_linkrelax_addr16 (void)
     96 {
     97   rl78_bytes.link_relax |= RL78_RELAXA_ADDR16;
     98 }
     99 
    100 void
    101 rl78_linkrelax_branch (void)
    102 {
    103   rl78_relax (RL78_RELAX_BRANCH, 0);
    104   rl78_bytes.link_relax |= RL78_RELAXA_BRA;
    105 }
    106 
    107 static void
    108 rl78_fixup (expressionS exp, int offsetbits, int nbits, int type)
    109 {
    110   rl78_bytes.fixups[rl78_bytes.n_fixups].exp = exp;
    111   rl78_bytes.fixups[rl78_bytes.n_fixups].offset = offsetbits;
    112   rl78_bytes.fixups[rl78_bytes.n_fixups].nbits = nbits;
    113   rl78_bytes.fixups[rl78_bytes.n_fixups].type = type;
    114   rl78_bytes.fixups[rl78_bytes.n_fixups].reloc = exp.X_md;
    115   rl78_bytes.n_fixups ++;
    116 }
    117 
    118 #define rl78_field_fixup(exp, offset, nbits, type)	\
    119   rl78_fixup (exp, offset + 8 * rl78_bytes.n_prefix), nbits, type)
    120 
    121 #define rl78_op_fixup(exp, offset, nbits, type)		\
    122   rl78_fixup (exp, offset + 8 * (rl78_bytes.n_prefix + rl78_bytes.n_base), nbits, type)
    123 
    124 void
    125 rl78_prefix (int p)
    126 {
    127   rl78_bytes.prefix[0] = p;
    128   rl78_bytes.n_prefix = 1;
    129 }
    130 
    131 int
    132 rl78_has_prefix (void)
    133 {
    134   return rl78_bytes.n_prefix;
    135 }
    136 
    137 void
    138 rl78_base1 (int b1)
    139 {
    140   rl78_bytes.base[0] = b1;
    141   rl78_bytes.n_base = 1;
    142 }
    143 
    144 void
    145 rl78_base2 (int b1, int b2)
    146 {
    147   rl78_bytes.base[0] = b1;
    148   rl78_bytes.base[1] = b2;
    149   rl78_bytes.n_base = 2;
    150 }
    151 
    152 void
    153 rl78_base3 (int b1, int b2, int b3)
    154 {
    155   rl78_bytes.base[0] = b1;
    156   rl78_bytes.base[1] = b2;
    157   rl78_bytes.base[2] = b3;
    158   rl78_bytes.n_base = 3;
    159 }
    160 
    161 void
    162 rl78_base4 (int b1, int b2, int b3, int b4)
    163 {
    164   rl78_bytes.base[0] = b1;
    165   rl78_bytes.base[1] = b2;
    166   rl78_bytes.base[2] = b3;
    167   rl78_bytes.base[3] = b4;
    168   rl78_bytes.n_base = 4;
    169 }
    170 
    171 #define F_PRECISION 2
    172 
    173 void
    174 rl78_op (expressionS exp, int nbytes, int type)
    175 {
    176   int v = 0;
    177 
    178   if ((exp.X_op == O_constant || exp.X_op == O_big)
    179       && type != RL78REL_PCREL)
    180     {
    181       if (exp.X_op == O_big && exp.X_add_number <= 0)
    182 	{
    183 	  LITTLENUM_TYPE w[2];
    184 	  char * ip = rl78_bytes.ops + rl78_bytes.n_ops;
    185 
    186 	  gen_to_words (w, F_PRECISION, 8);
    187 	  ip[3] = w[0] >> 8;
    188 	  ip[2] = w[0];
    189 	  ip[1] = w[1] >> 8;
    190 	  ip[0] = w[1];
    191 	  rl78_bytes.n_ops += 4;
    192 	}
    193       else
    194 	{
    195 	  v = exp.X_add_number;
    196 	  while (nbytes)
    197 	    {
    198 	      rl78_bytes.ops[rl78_bytes.n_ops++] =v & 0xff;
    199 	      v >>= 8;
    200 	      nbytes --;
    201 	    }
    202 	}
    203     }
    204   else
    205     {
    206       if (nbytes > 2
    207 	  && exp.X_md == BFD_RELOC_RL78_CODE)
    208 	exp.X_md = 0;
    209 
    210       if (nbytes == 1
    211 	  && (exp.X_md == BFD_RELOC_RL78_LO16
    212 	      || exp.X_md == BFD_RELOC_RL78_HI16))
    213 	as_bad (_("16-bit relocation used in 8-bit operand"));
    214 
    215       if (nbytes == 2
    216 	  && exp.X_md == BFD_RELOC_RL78_HI8)
    217 	as_bad (_("8-bit relocation used in 16-bit operand"));
    218 
    219       rl78_op_fixup (exp, rl78_bytes.n_ops * 8, nbytes * 8, type);
    220       memset (rl78_bytes.ops + rl78_bytes.n_ops, 0, nbytes);
    221       rl78_bytes.n_ops += nbytes;
    222     }
    223 }
    224 
    225 /* This gets complicated when the field spans bytes, because fields
    226    are numbered from the MSB of the first byte as zero, and bits are
    227    stored LSB towards the LSB of the byte.  Thus, a simple four-bit
    228    insertion of 12 at position 4 of 0x00 yields: 0x0b.  A three-bit
    229    insertion of b'MXL at position 7 is like this:
    230 
    231      - - - -  - - - -   - - - -  - - - -
    232                     M   X L               */
    233 
    234 void
    235 rl78_field (int val, int pos, int sz)
    236 {
    237   int valm;
    238   int bytep, bitp;
    239 
    240   if (sz > 0)
    241     {
    242       if (val < 0 || val >= (1 << sz))
    243 	as_bad (_("Value %d doesn't fit in unsigned %d-bit field"), val, sz);
    244     }
    245   else
    246     {
    247       sz = - sz;
    248       if (val < -(1 << (sz - 1)) || val >= (1 << (sz - 1)))
    249 	as_bad (_("Value %d doesn't fit in signed %d-bit field"), val, sz);
    250     }
    251 
    252   /* This code points at 'M' in the above example.  */
    253   bytep = pos / 8;
    254   bitp = pos % 8;
    255 
    256   while (bitp + sz > 8)
    257     {
    258       int ssz = 8 - bitp;
    259       int svalm;
    260 
    261       svalm = val >> (sz - ssz);
    262       svalm = svalm & ((1 << ssz) - 1);
    263       svalm = svalm << (8 - bitp - ssz);
    264       gas_assert (bytep < rl78_bytes.n_base);
    265       rl78_bytes.base[bytep] |= svalm;
    266 
    267       bitp = 0;
    268       sz -= ssz;
    269       bytep ++;
    270     }
    271   valm = val & ((1 << sz) - 1);
    272   valm = valm << (8 - bitp - sz);
    273   gas_assert (bytep < rl78_bytes.n_base);
    274   rl78_bytes.base[bytep] |= valm;
    275 }
    276 
    277 /*------------------------------------------------------------------*/
    278 
    279 enum options
    280 {
    281   OPTION_RELAX = OPTION_MD_BASE,
    282   OPTION_NORELAX,
    283   OPTION_G10,
    284   OPTION_G13,
    285   OPTION_G14,
    286   OPTION_32BIT_DOUBLES,
    287   OPTION_64BIT_DOUBLES,
    288 };
    289 
    290 #define RL78_SHORTOPTS ""
    291 const char * md_shortopts = RL78_SHORTOPTS;
    292 
    293 /* Assembler options.  */
    294 struct option md_longopts[] =
    295 {
    296   {"relax", no_argument, NULL, OPTION_RELAX},
    297   {"norelax", no_argument, NULL, OPTION_NORELAX},
    298   {"mg10", no_argument, NULL, OPTION_G10},
    299   {"mg13", no_argument, NULL, OPTION_G13},
    300   {"mg14", no_argument, NULL, OPTION_G14},
    301   {"mrl78", no_argument, NULL, OPTION_G14},
    302   {"m32bit-doubles", no_argument, NULL, OPTION_32BIT_DOUBLES},
    303   {"m64bit-doubles", no_argument, NULL, OPTION_64BIT_DOUBLES},
    304   {NULL, no_argument, NULL, 0}
    305 };
    306 size_t md_longopts_size = sizeof (md_longopts);
    307 
    308 int
    309 md_parse_option (int c, const char * arg ATTRIBUTE_UNUSED)
    310 {
    311   switch (c)
    312     {
    313     case OPTION_RELAX:
    314       linkrelax = 1;
    315       return 1;
    316     case OPTION_NORELAX:
    317       linkrelax = 0;
    318       return 1;
    319 
    320     case OPTION_G10:
    321       elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
    322       elf_flags |= E_FLAG_RL78_G10;
    323       return 1;
    324 
    325     case OPTION_G13:
    326       elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
    327       elf_flags |= E_FLAG_RL78_G13;
    328       return 1;
    329 
    330     case OPTION_G14:
    331       elf_flags &= ~ E_FLAG_RL78_CPU_MASK;
    332       elf_flags |= E_FLAG_RL78_G14;
    333       return 1;
    334 
    335     case OPTION_32BIT_DOUBLES:
    336       elf_flags &= ~ E_FLAG_RL78_64BIT_DOUBLES;
    337       return 1;
    338 
    339     case OPTION_64BIT_DOUBLES:
    340       elf_flags |= E_FLAG_RL78_64BIT_DOUBLES;
    341       return 1;
    342     }
    343   return 0;
    344 }
    345 
    346 int
    347 rl78_isa_g10 (void)
    348 {
    349   return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G10;
    350 }
    351 
    352 int
    353 rl78_isa_g13 (void)
    354 {
    355   return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G13;
    356 }
    357 
    358 int
    359 rl78_isa_g14 (void)
    360 {
    361   return (elf_flags & E_FLAG_RL78_CPU_MASK) == E_FLAG_RL78_G14;
    362 }
    363 
    364 void
    365 md_show_usage (FILE * stream)
    366 {
    367   fprintf (stream, _(" RL78 specific command line options:\n"));
    368   fprintf (stream, _("  --mrelax          Enable link time relaxation\n"));
    369   fprintf (stream, _("  --mg10            Enable support for G10 variant\n"));
    370   fprintf (stream, _("  --mg13            Selects the G13 core.\n"));
    371   fprintf (stream, _("  --mg14            Selects the G14 core [default]\n"));
    372   fprintf (stream, _("  --mrl78           Alias for --mg14\n"));
    373   fprintf (stream, _("  --m32bit-doubles  [default]\n"));
    374   fprintf (stream, _("  --m64bit-doubles  Source code uses 64-bit doubles\n"));
    375 }
    376 
    377 static void
    378 s_bss (int ignore ATTRIBUTE_UNUSED)
    379 {
    380   int temp;
    381 
    382   temp = get_absolute_expression ();
    383   subseg_set (bss_section, (subsegT) temp);
    384   demand_empty_rest_of_line ();
    385 }
    386 
    387 static void
    388 rl78_float_cons (int ignore ATTRIBUTE_UNUSED)
    389 {
    390   if (elf_flags & E_FLAG_RL78_64BIT_DOUBLES)
    391     return float_cons ('d');
    392   return float_cons ('f');
    393 }
    394 
    395 /* The target specific pseudo-ops which we support.  */
    396 const pseudo_typeS md_pseudo_table[] =
    397 {
    398   /* Our "standard" pseudos.  */
    399   { "double", rl78_float_cons,	'd' },
    400   { "bss",    s_bss, 		0 },
    401   { "3byte",  cons,		3 },
    402   { "int",    cons,		4 },
    403   { "word",   cons,		4 },
    404 
    405   /* End of list marker.  */
    406   { NULL, 	NULL, 		0 }
    407 };
    408 
    409 static symbolS * rl78_abs_sym = NULL;
    410 
    411 void
    412 md_begin (void)
    413 {
    414   rl78_abs_sym = symbol_make ("__rl78_abs__");
    415 }
    416 
    417 void
    418 rl78_md_end (void)
    419 {
    420 }
    421 
    422 /* Set the ELF specific flags.  */
    423 void
    424 rl78_elf_final_processing (void)
    425 {
    426   elf_elfheader (stdoutput)->e_flags |= elf_flags;
    427 }
    428 
    429 /* Write a value out to the object file, using the appropriate endianness.  */
    430 void
    431 md_number_to_chars (char * buf, valueT val, int n)
    432 {
    433   number_to_chars_littleendian (buf, val, n);
    434 }
    435 
    436 static void
    437 require_end_of_expr (const char *fname)
    438 {
    439   while (* input_line_pointer == ' '
    440 	 || * input_line_pointer == '\t')
    441     input_line_pointer ++;
    442 
    443   if (! * input_line_pointer
    444       || strchr ("\n\r,", * input_line_pointer)
    445       || strchr (comment_chars, * input_line_pointer)
    446       || strchr (line_comment_chars, * input_line_pointer)
    447       || strchr (line_separator_chars, * input_line_pointer))
    448     return;
    449 
    450   as_bad (_("%%%s() must be outermost term in expression"), fname);
    451 }
    452 
    453 static struct
    454 {
    455   const char * fname;
    456   int    reloc;
    457 }
    458 reloc_functions[] =
    459 {
    460   { "code", BFD_RELOC_RL78_CODE },
    461   { "lo16", BFD_RELOC_RL78_LO16 },
    462   { "hi16", BFD_RELOC_RL78_HI16 },
    463   { "hi8",  BFD_RELOC_RL78_HI8 },
    464   { 0, 0 }
    465 };
    466 
    467 void
    468 md_operand (expressionS * exp ATTRIBUTE_UNUSED)
    469 {
    470   int reloc = 0;
    471   int i;
    472 
    473   for (i = 0; reloc_functions[i].fname; i++)
    474     {
    475       int flen = strlen (reloc_functions[i].fname);
    476 
    477       if (input_line_pointer[0] == '%'
    478 	  && strncasecmp (input_line_pointer + 1, reloc_functions[i].fname, flen) == 0
    479 	  && input_line_pointer[flen + 1] == '(')
    480 	{
    481 	  reloc = reloc_functions[i].reloc;
    482 	  input_line_pointer += flen + 2;
    483 	  break;
    484 	}
    485     }
    486   if (reloc == 0)
    487     return;
    488 
    489   expression (exp);
    490   if (* input_line_pointer == ')')
    491     input_line_pointer ++;
    492 
    493   exp->X_md = reloc;
    494 
    495   require_end_of_expr (reloc_functions[i].fname);
    496 }
    497 
    498 void
    499 rl78_frag_init (fragS * fragP)
    500 {
    501   if (rl78_bytes.n_relax || rl78_bytes.link_relax)
    502     {
    503       fragP->tc_frag_data = XNEW (rl78_bytesT);
    504       memcpy (fragP->tc_frag_data, & rl78_bytes, sizeof (rl78_bytesT));
    505     }
    506   else
    507     fragP->tc_frag_data = 0;
    508 }
    509 
    510 /* When relaxing, we need to output a reloc for any .align directive
    511    so that we can retain this alignment as we adjust opcode sizes.  */
    512 void
    513 rl78_handle_align (fragS * frag)
    514 {
    515   if (linkrelax
    516       && (frag->fr_type == rs_align
    517 	  || frag->fr_type == rs_align_code)
    518       && frag->fr_address + frag->fr_fix > 0
    519       && frag->fr_offset > 0
    520       && now_seg != bss_section)
    521     {
    522       fix_new (frag, frag->fr_fix, 0,
    523 	       &abs_symbol, RL78_RELAXA_ALIGN + frag->fr_offset,
    524 	       0, BFD_RELOC_RL78_RELAX);
    525       /* For the purposes of relaxation, this relocation is attached
    526 	 to the byte *after* the alignment - i.e. the byte that must
    527 	 remain aligned.  */
    528       fix_new (frag->fr_next, 0, 0,
    529 	       &abs_symbol, RL78_RELAXA_ELIGN + frag->fr_offset,
    530 	       0, BFD_RELOC_RL78_RELAX);
    531     }
    532 }
    533 
    534 const char *
    535 md_atof (int type, char * litP, int * sizeP)
    536 {
    537   return ieee_md_atof (type, litP, sizeP, target_big_endian);
    538 }
    539 
    540 symbolS *
    541 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
    542 {
    543   return NULL;
    544 }
    545 
    546 #define APPEND(B, N_B)				       \
    547   if (rl78_bytes.N_B)				       \
    548     {						       \
    549       memcpy (bytes + idx, rl78_bytes.B, rl78_bytes.N_B);  \
    550       idx += rl78_bytes.N_B;			       \
    551     }
    552 
    553 
    554 void
    555 md_assemble (char * str)
    556 {
    557   char * bytes;
    558   fragS * frag_then = frag_now;
    559   int idx = 0;
    560   int i;
    561   int rel;
    562   expressionS  *exp;
    563 
    564   /*printf("\033[32mASM: %s\033[0m\n", str);*/
    565 
    566   dwarf2_emit_insn (0);
    567 
    568   memset (& rl78_bytes, 0, sizeof (rl78_bytes));
    569 
    570   rl78_lex_init (str, str + strlen (str));
    571 
    572   rl78_parse ();
    573 
    574   /* This simplifies the relaxation code.  */
    575   if (rl78_bytes.n_relax || rl78_bytes.link_relax)
    576     {
    577       int olen = rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops;
    578       /* We do it this way because we want the frag to have the
    579 	 rl78_bytes in it, which we initialize above.  The extra bytes
    580 	 are for relaxing.  */
    581       bytes = frag_more (olen + 3);
    582       frag_then = frag_now;
    583       frag_variant (rs_machine_dependent,
    584 		    olen /* max_chars */,
    585 		    0 /* var */,
    586 		    olen /* subtype */,
    587 		    0 /* symbol */,
    588 		    0 /* offset */,
    589 		    0 /* opcode */);
    590       frag_then->fr_opcode = bytes;
    591       frag_then->fr_fix = olen + (bytes - frag_then->fr_literal);
    592       frag_then->fr_subtype = olen;
    593       frag_then->fr_var = 0;
    594     }
    595   else
    596     {
    597       bytes = frag_more (rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops);
    598       frag_then = frag_now;
    599     }
    600 
    601   APPEND (prefix, n_prefix);
    602   APPEND (base, n_base);
    603   APPEND (ops, n_ops);
    604 
    605   if (rl78_bytes.link_relax)
    606     {
    607       fixS * f;
    608 
    609       f = fix_new (frag_then,
    610 		   (char *) bytes - frag_then->fr_literal,
    611 		   0,
    612 		   abs_section_sym,
    613 		   rl78_bytes.link_relax | rl78_bytes.n_fixups,
    614 		   0,
    615 		   BFD_RELOC_RL78_RELAX);
    616       frag_then->tc_frag_data->link_relax_fixP = f;
    617     }
    618 
    619   for (i = 0; i < rl78_bytes.n_fixups; i ++)
    620     {
    621       /* index: [nbytes][type] */
    622       static int reloc_map[5][4] =
    623 	{
    624 	  { 0,            0 },
    625 	  { BFD_RELOC_8,  BFD_RELOC_8_PCREL },
    626 	  { BFD_RELOC_16, BFD_RELOC_16_PCREL },
    627 	  { BFD_RELOC_24, BFD_RELOC_24_PCREL },
    628 	  { BFD_RELOC_32, BFD_RELOC_32_PCREL },
    629 	};
    630       fixS * f;
    631 
    632       idx = rl78_bytes.fixups[i].offset / 8;
    633       rel = reloc_map [rl78_bytes.fixups[i].nbits / 8][(int) rl78_bytes.fixups[i].type];
    634 
    635       if (rl78_bytes.fixups[i].reloc)
    636 	rel = rl78_bytes.fixups[i].reloc;
    637 
    638       if (frag_then->tc_frag_data)
    639 	exp = & frag_then->tc_frag_data->fixups[i].exp;
    640       else
    641 	exp = & rl78_bytes.fixups[i].exp;
    642 
    643       f = fix_new_exp (frag_then,
    644 		       (char *) bytes + idx - frag_then->fr_literal,
    645 		       rl78_bytes.fixups[i].nbits / 8,
    646 		       exp,
    647 		       rl78_bytes.fixups[i].type == RL78REL_PCREL ? 1 : 0,
    648 		       rel);
    649       if (frag_then->tc_frag_data)
    650 	frag_then->tc_frag_data->fixups[i].fixP = f;
    651     }
    652 }
    653 
    654 void
    655 rl78_cons_fix_new (fragS *	frag,
    656 		 int		where,
    657 		 int		size,
    658 		 expressionS *  exp)
    659 {
    660   bfd_reloc_code_real_type type;
    661   fixS *fixP;
    662 
    663   switch (size)
    664     {
    665     case 1:
    666       type = BFD_RELOC_8;
    667       break;
    668     case 2:
    669       type = BFD_RELOC_16;
    670       break;
    671     case 3:
    672       type = BFD_RELOC_24;
    673       break;
    674     case 4:
    675       type = BFD_RELOC_32;
    676       break;
    677     default:
    678       as_bad (_("unsupported constant size %d\n"), size);
    679       return;
    680     }
    681 
    682   switch (exp->X_md)
    683     {
    684     case BFD_RELOC_RL78_CODE:
    685       if (size == 2)
    686 	type = exp->X_md;
    687       break;
    688     case BFD_RELOC_RL78_LO16:
    689     case BFD_RELOC_RL78_HI16:
    690       if (size != 2)
    691 	{
    692 	  /* Fixups to assembler generated expressions do not use %hi or %lo.  */
    693 	  if (frag->fr_file)
    694 	    as_bad (_("%%hi16/%%lo16 only applies to .short or .hword"));
    695 	}
    696       else
    697 	type = exp->X_md;
    698       break;
    699     case BFD_RELOC_RL78_HI8:
    700       if (size != 1)
    701 	{
    702 	  /* Fixups to assembler generated expressions do not use %hi or %lo.  */
    703 	  if (frag->fr_file)
    704 	    as_bad (_("%%hi8 only applies to .byte"));
    705 	}
    706       else
    707 	type = exp->X_md;
    708       break;
    709     default:
    710       break;
    711     }
    712 
    713   if (exp->X_op == O_subtract && exp->X_op_symbol)
    714     {
    715       if (size != 4 && size != 2 && size != 1)
    716 	as_bad (_("difference of two symbols only supported with .long, .short, or .byte"));
    717       else
    718 	type = BFD_RELOC_RL78_DIFF;
    719     }
    720 
    721   fixP = fix_new_exp (frag, where, (int) size, exp, 0, type);
    722   switch (exp->X_md)
    723     {
    724       /* These are intended to have values larger than the container,
    725 	 since the backend puts only the portion we need in it.
    726 	 However, we don't have a backend-specific reloc for them as
    727 	 they're handled with complex relocations.  */
    728     case BFD_RELOC_RL78_LO16:
    729     case BFD_RELOC_RL78_HI16:
    730     case BFD_RELOC_RL78_HI8:
    731       fixP->fx_no_overflow = 1;
    732       break;
    733     default:
    734       break;
    735     }
    736 }
    737 
    738 
    739 /*----------------------------------------------------------------------*/
    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
    745    rl78_bytes.
    746 
    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
    752    maximal size.  */
    753 
    754 #define TRACE_RELAX 0
    755 #define tprintf if (TRACE_RELAX) printf
    756 
    757 
    758 typedef enum
    759 {
    760   OT_other,
    761   OT_bt,
    762   OT_bt_sfr,
    763   OT_bt_es,
    764   OT_bc,
    765   OT_bh,
    766   OT_sk,
    767   OT_call,
    768   OT_br,
    769 } op_type_T;
    770 
    771 /* We're looking for these types of relaxations:
    772 
    773    BT		00110001 sbit0cc1 addr----	(cc is 10 (BF) or 01 (BT))
    774    B~T		00110001 sbit0cc1 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
    775 
    776    BT sfr	00110001 sbit0cc0 sfr----- addr----
    777    BT ES:	00010001 00101110 sbit0cc1 addr----
    778 
    779    BC		110111cc addr----
    780    B~C		110111cc 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
    781 
    782    BH		01100001 110c0011 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
    783    B~H		01100001 110c0011 00000011 11101110 pcrel16- -------- (BR $!pcrel20)
    784 */
    785 
    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.  */
    789 
    790 static op_type_T
    791 rl78_opcode_type (char * ops)
    792 {
    793   unsigned char *op = (unsigned char *)ops;
    794 
    795   if (op[0] == 0x31
    796       && ((op[1] & 0x0f) == 0x05
    797 	  || (op[1] & 0x0f) == 0x03))
    798     return OT_bt;
    799 
    800   if (op[0] == 0x31
    801       && ((op[1] & 0x0f) == 0x04
    802 	  || (op[1] & 0x0f) == 0x02))
    803     return OT_bt_sfr;
    804 
    805   if (op[0] == 0x11
    806       && op[1] == 0x31
    807       && ((op[2] & 0x0f) == 0x05
    808 	  || (op[2] & 0x0f) == 0x03))
    809     return OT_bt_es;
    810 
    811   if ((op[0] & 0xfc) == 0xdc)
    812     return OT_bc;
    813 
    814   if (op[0] == 0x61
    815       && (op[1] & 0xef) == 0xc3)
    816     return OT_bh;
    817 
    818   if (op[0] == 0x61
    819       && (op[1] & 0xcf) == 0xc8)
    820     return OT_sk;
    821 
    822   if (op[0] == 0x61
    823       && (op[1] & 0xef) == 0xe3)
    824     return OT_sk;
    825 
    826   if (op[0] == 0xfc)
    827     return OT_call;
    828 
    829   if ((op[0] & 0xec) == 0xec)
    830     return OT_br;
    831 
    832   return OT_other;
    833 }
    834 
    835 /* Returns zero if *addrP has the target address.  Else returns nonzero
    836    if we cannot compute the target address yet.  */
    837 
    838 static int
    839 rl78_frag_fix_value (fragS *    fragP,
    840 		     segT       segment,
    841 		     int        which,
    842 		     addressT * addrP,
    843 		     int        need_diff,
    844 		     addressT * sym_addr)
    845 {
    846   addressT addr = 0;
    847   rl78_bytesT * b = fragP->tc_frag_data;
    848   expressionS * exp = & b->fixups[which].exp;
    849 
    850   if (need_diff && exp->X_op != O_subtract)
    851     return 1;
    852 
    853   if (exp->X_add_symbol)
    854     {
    855       if (S_FORCE_RELOC (exp->X_add_symbol, 1))
    856 	return 1;
    857       if (S_GET_SEGMENT (exp->X_add_symbol) != segment)
    858 	return 1;
    859       addr += S_GET_VALUE (exp->X_add_symbol);
    860     }
    861 
    862   if (exp->X_op_symbol)
    863     {
    864       if (exp->X_op != O_subtract)
    865 	return 1;
    866       if (S_FORCE_RELOC (exp->X_op_symbol, 1))
    867 	return 1;
    868       if (S_GET_SEGMENT (exp->X_op_symbol) != segment)
    869 	return 1;
    870       addr -= S_GET_VALUE (exp->X_op_symbol);
    871     }
    872   if (sym_addr)
    873     * sym_addr = addr;
    874   addr += exp->X_add_number;
    875   * addrP = addr;
    876   return 0;
    877 }
    878 
    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.  */
    883 
    884 int
    885 md_estimate_size_before_relax (fragS * fragP ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED)
    886 {
    887   int opfixsize;
    888   int delta;
    889 
    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);
    894 
    895   tprintf (" -> opfixsize %d delta %d\n", opfixsize, delta);
    896   return delta;
    897 }
    898 
    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.  */
    904 
    905 int
    906 rl78_relax_frag (segT segment ATTRIBUTE_UNUSED, fragS * fragP, long stretch)
    907 {
    908   addressT addr0, sym_addr;
    909   addressT mypc;
    910   int disp;
    911   int oldsize = fragP->fr_subtype;
    912   int newsize = oldsize;
    913   op_type_T optype;
    914   int ri;
    915 
    916   mypc = fragP->fr_address + (fragP->fr_opcode - fragP->fr_literal);
    917 
    918   /* If we ever get more than one reloc per opcode, this is the one
    919      we're relaxing.  */
    920   ri = 0;
    921 
    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,
    926 			   & sym_addr))
    927     {
    928       /* If we don't expect the linker to do relaxing, don't emit
    929 	 expanded opcodes that only the linker will relax.  */
    930       if (!linkrelax)
    931 	return newsize - oldsize;
    932 
    933       /* If we don't, we must use the maximum size for the linker.  */
    934       switch (fragP->tc_frag_data->relax[ri].type)
    935 	{
    936 	case RL78_RELAX_BRANCH:
    937 	  switch (optype)
    938 	    {
    939 	    case OT_bt:
    940 	      newsize = 6;
    941 	      break;
    942 	    case OT_bt_sfr:
    943 	    case OT_bt_es:
    944 	      newsize = 7;
    945 	      break;
    946 	    case OT_bc:
    947 	      newsize = 5;
    948 	      break;
    949 	    case OT_bh:
    950 	      newsize = 6;
    951 	      break;
    952 	    case OT_sk:
    953 	      newsize = 2;
    954 	      break;
    955 	    default:
    956 	      newsize = oldsize;
    957 	      break;
    958 	    }
    959 	  break;
    960 
    961 	}
    962       fragP->fr_subtype = newsize;
    963       tprintf (" -> new %d old %d delta %d (external)\n", newsize, oldsize, newsize-oldsize);
    964       return newsize - oldsize;
    965     }
    966 
    967   if (sym_addr > mypc)
    968     addr0 += stretch;
    969 
    970   switch (fragP->tc_frag_data->relax[ri].type)
    971     {
    972     case  RL78_RELAX_BRANCH:
    973       disp = (int) addr0 - (int) mypc;
    974 
    975       switch (optype)
    976 	{
    977 	case OT_bt:
    978 	  if (disp >= -128 && (disp - (oldsize-2)) <= 127)
    979 	    newsize = 3;
    980 	  else
    981 	    newsize = 6;
    982 	  break;
    983 	case OT_bt_sfr:
    984 	case OT_bt_es:
    985 	  if (disp >= -128 && (disp - (oldsize-3)) <= 127)
    986 	    newsize = 4;
    987 	  else
    988 	    newsize = 7;
    989 	  break;
    990 	case OT_bc:
    991 	  if (disp >= -128 && (disp - (oldsize-1)) <= 127)
    992 	    newsize = 2;
    993 	  else
    994 	    newsize = 5;
    995 	  break;
    996 	case OT_bh:
    997 	  if (disp >= -128 && (disp - (oldsize-2)) <= 127)
    998 	    newsize = 3;
    999 	  else
   1000 	    newsize = 6;
   1001 	  break;
   1002 	case OT_sk:
   1003 	  newsize = 2;
   1004 	  break;
   1005 	default:
   1006 	  newsize = oldsize;
   1007 	  break;
   1008 	}
   1009       break;
   1010     }
   1011 
   1012   /* This prevents infinite loops in align-heavy sources.  */
   1013   if (newsize < oldsize)
   1014     {
   1015       if (fragP->tc_frag_data->times_shrank > 10
   1016          && fragP->tc_frag_data->times_grown > 10)
   1017        newsize = oldsize;
   1018       if (fragP->tc_frag_data->times_shrank < 20)
   1019        fragP->tc_frag_data->times_shrank ++;
   1020     }
   1021   else if (newsize > oldsize)
   1022     {
   1023       if (fragP->tc_frag_data->times_grown < 20)
   1024        fragP->tc_frag_data->times_grown ++;
   1025     }
   1026 
   1027   fragP->fr_subtype = newsize;
   1028   tprintf (" -> new %d old %d delta %d\n", newsize, oldsize, newsize-oldsize);
   1029   return newsize - oldsize;
   1030 }
   1031 
   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))
   1035 
   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.  */
   1042 
   1043 void
   1044 md_convert_frag (bfd *   abfd ATTRIBUTE_UNUSED,
   1045 		 segT    segment ATTRIBUTE_UNUSED,
   1046 		 fragS * fragP ATTRIBUTE_UNUSED)
   1047 {
   1048   rl78_bytesT * rl78b = fragP->tc_frag_data;
   1049   addressT addr0, mypc;
   1050   int disp;
   1051   int reloc_type, reloc_adjust;
   1052   char * op = fragP->fr_opcode;
   1053   int keep_reloc = 0;
   1054   int ri;
   1055   int fi = (rl78b->n_fixups > 1) ? 1 : 0;
   1056   fixS * fix = rl78b->fixups[fi].fixP;
   1057 
   1058   /* If we ever get more than one reloc per opcode, this is the one
   1059      we're relaxing.  */
   1060   ri = 0;
   1061 
   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);
   1066 
   1067   /* Try to get the target address.  If we fail here, we just use the
   1068      largest format.  */
   1069   if (rl78_frag_fix_value (fragP, segment, 0, & addr0,
   1070 			   fragP->tc_frag_data->relax[ri].type != RL78_RELAX_BRANCH, 0))
   1071     {
   1072       /* We don't know the target address.  */
   1073       keep_reloc = 1;
   1074       addr0 = 0;
   1075       disp = 0;
   1076       tprintf ("unknown addr ? - %x = ?\n", (int)mypc);
   1077     }
   1078   else
   1079     {
   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);
   1083     }
   1084 
   1085   if (linkrelax)
   1086     keep_reloc = 1;
   1087 
   1088   reloc_type = BFD_RELOC_NONE;
   1089   reloc_adjust = 0;
   1090 
   1091   switch (fragP->tc_frag_data->relax[ri].type)
   1092     {
   1093     case RL78_RELAX_BRANCH:
   1094       switch (OPCODE (rl78_opcode_type (fragP->fr_opcode), fragP->fr_subtype))
   1095 	{
   1096 
   1097 	case OPCODE (OT_bt, 3): /* BT A,$ - no change.  */
   1098 	  disp -= 3;
   1099 	  op[2] = disp;
   1100 	  reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
   1101 	  break;
   1102 
   1103 	case OPCODE (OT_bt, 6): /* BT A,$ - long version.  */
   1104 	  disp -= 3;
   1105 	  op[1] ^= 0x06; /* toggle conditional.  */
   1106 	  op[2] = 3; /* displacement over long branch.  */
   1107 	  disp -= 3;
   1108 	  op[3] = 0xEE; /* BR $!addr20 */
   1109 	  op[4] = disp & 0xff;
   1110 	  op[5] = disp >> 8;
   1111 	  reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
   1112 	  reloc_adjust = 2;
   1113 	  break;
   1114 
   1115 	case OPCODE (OT_bt_sfr, 4): /* BT PSW,$ - no change.  */
   1116 	  disp -= 4;
   1117 	  op[3] = disp;
   1118 	  reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
   1119 	  break;
   1120 
   1121 	case OPCODE (OT_bt_sfr, 7): /* BT PSW,$ - long version.  */
   1122 	  disp -= 4;
   1123 	  op[1] ^= 0x06; /* toggle conditional.  */
   1124 	  op[3] = 3; /* displacement over long branch.  */
   1125 	  disp -= 3;
   1126 	  op[4] = 0xEE; /* BR $!addr20 */
   1127 	  op[5] = disp & 0xff;
   1128 	  op[6] = disp >> 8;
   1129 	  reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
   1130 	  reloc_adjust = 2;
   1131 	  break;
   1132 
   1133 	case OPCODE (OT_bt_es, 4): /* BT ES:[HL],$ - no change.  */
   1134 	  disp -= 4;
   1135 	  op[3] = disp;
   1136 	  reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
   1137 	  break;
   1138 
   1139 	case OPCODE (OT_bt_es, 7): /* BT PSW,$ - long version.  */
   1140 	  disp -= 4;
   1141 	  op[2] ^= 0x06; /* toggle conditional.  */
   1142 	  op[3] = 3; /* displacement over long branch.  */
   1143 	  disp -= 3;
   1144 	  op[4] = 0xEE; /* BR $!addr20 */
   1145 	  op[5] = disp & 0xff;
   1146 	  op[6] = disp >> 8;
   1147 	  reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
   1148 	  reloc_adjust = 2;
   1149 	  break;
   1150 
   1151 	case OPCODE (OT_bc, 2): /* BC $ - no change.  */
   1152 	  disp -= 2;
   1153 	  op[1] = disp;
   1154 	  reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
   1155 	  break;
   1156 
   1157 	case OPCODE (OT_bc, 5): /* BC $ - long version.  */
   1158 	  disp -= 2;
   1159 	  op[0] ^= 0x02; /* toggle conditional.  */
   1160 	  op[1] = 3;
   1161 	  disp -= 3;
   1162 	  op[2] = 0xEE; /* BR $!addr20 */
   1163 	  op[3] = disp & 0xff;
   1164 	  op[4] = disp >> 8;
   1165 	  reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
   1166 	  reloc_adjust = 2;
   1167 	  break;
   1168 
   1169 	case OPCODE (OT_bh, 3): /* BH $ - no change.  */
   1170 	  disp -= 3;
   1171 	  op[2] = disp;
   1172 	  reloc_type = keep_reloc ? BFD_RELOC_8_PCREL : BFD_RELOC_NONE;
   1173 	  break;
   1174 
   1175 	case OPCODE (OT_bh, 6): /* BC $ - long version.  */
   1176 	  disp -= 3;
   1177 	  op[1] ^= 0x10; /* toggle conditional.  */
   1178 	  op[2] = 3;
   1179 	  disp -= 3;
   1180 	  op[3] = 0xEE; /* BR $!addr20 */
   1181 	  op[4] = disp & 0xff;
   1182 	  op[5] = disp >> 8;
   1183 	  reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
   1184 	  reloc_adjust = 2;
   1185 	  break;
   1186 
   1187 	case OPCODE (OT_sk, 2): /* SK<cond> - no change */
   1188 	  reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE;
   1189 	  break;
   1190 
   1191 	default:
   1192 	  reloc_type = fix ? fix->fx_r_type : BFD_RELOC_NONE;
   1193 	  break;
   1194 	}
   1195       break;
   1196 
   1197     default:
   1198       if (rl78b->n_fixups)
   1199 	{
   1200 	  reloc_type = fix->fx_r_type;
   1201 	  reloc_adjust = 0;
   1202 	}
   1203       break;
   1204     }
   1205 
   1206   if (rl78b->n_fixups)
   1207     {
   1208 
   1209       fix->fx_r_type = reloc_type;
   1210       fix->fx_where += reloc_adjust;
   1211       switch (reloc_type)
   1212 	{
   1213 	case BFD_RELOC_NONE:
   1214 	  fix->fx_size = 0;
   1215 	  break;
   1216 	case BFD_RELOC_8:
   1217 	  fix->fx_size = 1;
   1218 	  break;
   1219 	case BFD_RELOC_16_PCREL:
   1220 	  fix->fx_size = 2;
   1221 	  break;
   1222 	}
   1223     }
   1224 
   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);
   1228   fragP->fr_var = 0;
   1229 
   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),
   1234 	   fragP->fr_next);
   1235 
   1236   if (fragP->fr_next != NULL
   1237       && fragP->fr_next->fr_address - fragP->fr_address != fragP->fr_fix)
   1238     as_bad (_("bad frag at %p : fix %ld addr %ld %ld \n"), fragP,
   1239 	    (long) fragP->fr_fix,
   1240 	    (long) fragP->fr_address, (long) fragP->fr_next->fr_address);
   1241 }
   1242 
   1243 /* End of relaxation code.
   1244   ----------------------------------------------------------------------*/
   1245 
   1246 
   1248 arelent **
   1249 tc_gen_reloc (asection * seg ATTRIBUTE_UNUSED, fixS * fixp)
   1250 {
   1251   static arelent * reloc[8];
   1252   int rp;
   1253 
   1254   if (fixp->fx_r_type == BFD_RELOC_NONE)
   1255     {
   1256       reloc[0] = NULL;
   1257       return reloc;
   1258     }
   1259 
   1260   if (fixp->fx_r_type == BFD_RELOC_RL78_RELAX && !linkrelax)
   1261     {
   1262       reloc[0] = NULL;
   1263       return reloc;
   1264     }
   1265 
   1266   if (fixp->fx_subsy
   1267       && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
   1268     {
   1269       fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy);
   1270       fixp->fx_subsy = NULL;
   1271     }
   1272 
   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;
   1278 
   1279   if (fixp->fx_r_type == BFD_RELOC_RL78_32_OP
   1280       && fixp->fx_subsy)
   1281     {
   1282       fixp->fx_r_type = BFD_RELOC_RL78_DIFF;
   1283     }
   1284 
   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;	\
   1292   reloc[++rp] = NULL
   1293 #define OPSYM(SYM) OPX(BFD_RELOC_RL78_SYM, SYM, 0)
   1294 
   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)
   1304 
   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)
   1307 
   1308   rp = 1;
   1309 
   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)
   1314     {
   1315     case BFD_RELOC_RL78_DIFF:
   1316       SYM0 ();
   1317       OPSYM (symbol_get_bfdsym (fixp->fx_subsy));
   1318       OP(OP_SUBTRACT);
   1319 
   1320       switch (fixp->fx_size)
   1321 	{
   1322 	case 1:
   1323 	  OP(ABS8);
   1324 	  break;
   1325 	case 2:
   1326 	  OP (ABS16);
   1327 	  break;
   1328 	case 4:
   1329 	  OP (ABS32);
   1330 	  break;
   1331 	}
   1332       break;
   1333 
   1334     case BFD_RELOC_RL78_NEG32:
   1335       SYM0 ();
   1336       OP (OP_NEG);
   1337       OP (ABS32);
   1338       break;
   1339 
   1340     case BFD_RELOC_RL78_CODE:
   1341       reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_RL78_16U);
   1342       reloc[1] = NULL;
   1343       break;
   1344 
   1345     case BFD_RELOC_RL78_LO16:
   1346       SYM0 ();
   1347       OPIMM (0xffff);
   1348       OP (OP_AND);
   1349       OP (ABS16);
   1350       break;
   1351 
   1352     case BFD_RELOC_RL78_HI16:
   1353       SYM0 ();
   1354       OPIMM (16);
   1355       OP (OP_SHRA);
   1356       OP (ABS16);
   1357       break;
   1358 
   1359     case BFD_RELOC_RL78_HI8:
   1360       SYM0 ();
   1361       OPIMM (16);
   1362       OP (OP_SHRA);
   1363       OPIMM (0xff);
   1364       OP (OP_AND);
   1365       OP (ABS8);
   1366       break;
   1367 
   1368     default:
   1369       reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
   1370       reloc[1] = NULL;
   1371       break;
   1372     }
   1373 
   1374   return reloc;
   1375 }
   1376 
   1377 int
   1378 rl78_validate_fix_sub (struct fix * f)
   1379 {
   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)
   1383     return 1;
   1384   /* .long sym1-sym2 */
   1385   if (f->fx_r_type == BFD_RELOC_RL78_DIFF
   1386       && ! f->fx_pcrel
   1387       && (f->fx_size == 4 || f->fx_size == 2 || f->fx_size == 1))
   1388     return 1;
   1389   return 0;
   1390 }
   1391 
   1392 long
   1393 md_pcrel_from_section (fixS * fixP, segT sec)
   1394 {
   1395   long rv;
   1396 
   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.  */
   1402     return 0;
   1403 
   1404   rv = fixP->fx_frag->fr_address + fixP->fx_where;
   1405   switch (fixP->fx_r_type)
   1406     {
   1407     case BFD_RELOC_8_PCREL:
   1408       rv += 1;
   1409       break;
   1410     case BFD_RELOC_16_PCREL:
   1411       rv += 2;
   1412       break;
   1413     default:
   1414       break;
   1415     }
   1416   return rv;
   1417 }
   1418 
   1419 void
   1420 md_apply_fix (struct fix * f ATTRIBUTE_UNUSED,
   1421 	      valueT *     t ATTRIBUTE_UNUSED,
   1422 	      segT         s ATTRIBUTE_UNUSED)
   1423 {
   1424   char * op;
   1425   unsigned long val;
   1426 
   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;
   1431 
   1432   if (f->fx_addsy && S_FORCE_RELOC (f->fx_addsy, 1))
   1433     return;
   1434   if (f->fx_subsy && S_FORCE_RELOC (f->fx_subsy, 1))
   1435     return;
   1436 
   1437   op = f->fx_frag->fr_literal + f->fx_where;
   1438   val = (unsigned long) * t;
   1439 
   1440   if (f->fx_addsy == NULL)
   1441     f->fx_done = 1;
   1442 
   1443   switch (f->fx_r_type)
   1444     {
   1445     case BFD_RELOC_NONE:
   1446       break;
   1447 
   1448     case BFD_RELOC_RL78_RELAX:
   1449       f->fx_done = 0;
   1450       break;
   1451 
   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"),
   1456 		      val);
   1457       /* Fall through.  */
   1458     case BFD_RELOC_8:
   1459     case BFD_RELOC_RL78_SADDR: /* We need to store the 8 LSB, but this works.  */
   1460       op[0] = val;
   1461       break;
   1462 
   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"),
   1467 		      val);
   1468       /* Fall through.  */
   1469     case BFD_RELOC_16:
   1470     case BFD_RELOC_RL78_CODE:
   1471       op[0] = val;
   1472       op[1] = val >> 8;
   1473       break;
   1474 
   1475     case BFD_RELOC_24:
   1476       op[0] = val;
   1477       op[1] = val >> 8;
   1478       op[2] = val >> 16;
   1479       break;
   1480 
   1481     case BFD_RELOC_32:
   1482       op[0] = val;
   1483       op[1] = val >> 8;
   1484       op[2] = val >> 16;
   1485       op[3] = val >> 24;
   1486       break;
   1487 
   1488     case BFD_RELOC_RL78_DIFF:
   1489       op[0] = val;
   1490       if (f->fx_size > 1)
   1491 	op[1] = val >> 8;
   1492       if (f->fx_size > 2)
   1493 	op[2] = val >> 16;
   1494       if (f->fx_size > 3)
   1495 	op[3] = val >> 24;
   1496       break;
   1497 
   1498     case BFD_RELOC_RL78_HI8:
   1499       val = val >> 16;
   1500       op[0] = val;
   1501       break;
   1502 
   1503     case BFD_RELOC_RL78_HI16:
   1504       val = val >> 16;
   1505       op[0] = val;
   1506       op[1] = val >> 8;
   1507       break;
   1508 
   1509     case BFD_RELOC_RL78_LO16:
   1510       op[0] = val;
   1511       op[1] = val >> 8;
   1512       break;
   1513 
   1514     default:
   1515       as_bad (_("Unknown reloc in md_apply_fix: %s"),
   1516 	      bfd_get_reloc_code_name (f->fx_r_type));
   1517       break;
   1518     }
   1519 
   1520 }
   1521 
   1522 valueT
   1523 md_section_align (segT segment, valueT size)
   1524 {
   1525   int align = bfd_section_alignment (segment);
   1526   return ((size + (1 << align) - 1) & -(1 << align));
   1527 }
   1528