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