Home | History | Annotate | Download | only in bfd
      1 /* Renesas RX specific support for 32-bit ELF.
      2    Copyright (C) 2008-2016 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program 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 of the License, or
      9    (at your option) any later version.
     10 
     11    This program 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 this program; if not, write to the Free Software
     18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
     19 
     20 #include "sysdep.h"
     21 #include "bfd.h"
     22 #include "bfd_stdint.h"
     23 #include "libbfd.h"
     24 #include "elf-bfd.h"
     25 #include "elf/rx.h"
     26 #include "libiberty.h"
     27 #include "elf32-rx.h"
     28 
     29 #define RX_OPCODE_BIG_ENDIAN 0
     30 
     31 /* This is a meta-target that's used only with objcopy, to avoid the
     32    endian-swap we would otherwise get.  We check for this in
     33    rx_elf_object_p().  */
     34 const bfd_target rx_elf32_be_ns_vec;
     35 const bfd_target rx_elf32_be_vec;
     36 
     37 #ifdef DEBUG
     38 char * rx_get_reloc (long);
     39 void rx_dump_symtab (bfd *, void *, void *);
     40 #endif
     41 
     42 #define RXREL(n,sz,bit,shift,complain,pcrel)				     \
     43   HOWTO (R_RX_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
     44 	 bfd_elf_generic_reloc, "R_RX_" #n, FALSE, 0, ~0, FALSE)
     45 
     46 /* Note that the relocations around 0x7f are internal to this file;
     47    feel free to move them as needed to avoid conflicts with published
     48    relocation numbers.  */
     49 
     50 static reloc_howto_type rx_elf_howto_table [] =
     51 {
     52   RXREL (NONE,         3,  0, 0, dont,     FALSE),
     53   RXREL (DIR32,        2, 32, 0, signed,   FALSE),
     54   RXREL (DIR24S,       2, 24, 0, signed,   FALSE),
     55   RXREL (DIR16,        1, 16, 0, dont,     FALSE),
     56   RXREL (DIR16U,       1, 16, 0, unsigned, FALSE),
     57   RXREL (DIR16S,       1, 16, 0, signed,   FALSE),
     58   RXREL (DIR8,         0,  8, 0, dont,     FALSE),
     59   RXREL (DIR8U,        0,  8, 0, unsigned, FALSE),
     60   RXREL (DIR8S,        0,  8, 0, signed,   FALSE),
     61   RXREL (DIR24S_PCREL, 2, 24, 0, signed,   TRUE),
     62   RXREL (DIR16S_PCREL, 1, 16, 0, signed,   TRUE),
     63   RXREL (DIR8S_PCREL,  0,  8, 0, signed,   TRUE),
     64   RXREL (DIR16UL,      1, 16, 2, unsigned, FALSE),
     65   RXREL (DIR16UW,      1, 16, 1, unsigned, FALSE),
     66   RXREL (DIR8UL,       0,  8, 2, unsigned, FALSE),
     67   RXREL (DIR8UW,       0,  8, 1, unsigned, FALSE),
     68   RXREL (DIR32_REV,    1, 16, 0, dont,     FALSE),
     69   RXREL (DIR16_REV,    1, 16, 0, dont,     FALSE),
     70   RXREL (DIR3U_PCREL,  0,  3, 0, dont,     TRUE),
     71 
     72   EMPTY_HOWTO (0x13),
     73   EMPTY_HOWTO (0x14),
     74   EMPTY_HOWTO (0x15),
     75   EMPTY_HOWTO (0x16),
     76   EMPTY_HOWTO (0x17),
     77   EMPTY_HOWTO (0x18),
     78   EMPTY_HOWTO (0x19),
     79   EMPTY_HOWTO (0x1a),
     80   EMPTY_HOWTO (0x1b),
     81   EMPTY_HOWTO (0x1c),
     82   EMPTY_HOWTO (0x1d),
     83   EMPTY_HOWTO (0x1e),
     84   EMPTY_HOWTO (0x1f),
     85 
     86   RXREL (RH_3_PCREL, 0,  3, 0, signed,   TRUE),
     87   RXREL (RH_16_OP,   1, 16, 0, signed,   FALSE),
     88   RXREL (RH_24_OP,   2, 24, 0, signed,   FALSE),
     89   RXREL (RH_32_OP,   2, 32, 0, signed,   FALSE),
     90   RXREL (RH_24_UNS,  2, 24, 0, unsigned, FALSE),
     91   RXREL (RH_8_NEG,   0,  8, 0, signed,   FALSE),
     92   RXREL (RH_16_NEG,  1, 16, 0, signed,   FALSE),
     93   RXREL (RH_24_NEG,  2, 24, 0, signed,   FALSE),
     94   RXREL (RH_32_NEG,  2, 32, 0, signed,   FALSE),
     95   RXREL (RH_DIFF,    2, 32, 0, signed,   FALSE),
     96   RXREL (RH_GPRELB,  1, 16, 0, unsigned, FALSE),
     97   RXREL (RH_GPRELW,  1, 16, 0, unsigned, FALSE),
     98   RXREL (RH_GPRELL,  1, 16, 0, unsigned, FALSE),
     99   RXREL (RH_RELAX,   0,  0, 0, dont,     FALSE),
    100 
    101   EMPTY_HOWTO (0x2e),
    102   EMPTY_HOWTO (0x2f),
    103   EMPTY_HOWTO (0x30),
    104   EMPTY_HOWTO (0x31),
    105   EMPTY_HOWTO (0x32),
    106   EMPTY_HOWTO (0x33),
    107   EMPTY_HOWTO (0x34),
    108   EMPTY_HOWTO (0x35),
    109   EMPTY_HOWTO (0x36),
    110   EMPTY_HOWTO (0x37),
    111   EMPTY_HOWTO (0x38),
    112   EMPTY_HOWTO (0x39),
    113   EMPTY_HOWTO (0x3a),
    114   EMPTY_HOWTO (0x3b),
    115   EMPTY_HOWTO (0x3c),
    116   EMPTY_HOWTO (0x3d),
    117   EMPTY_HOWTO (0x3e),
    118   EMPTY_HOWTO (0x3f),
    119   EMPTY_HOWTO (0x40),
    120 
    121   RXREL (ABS32,        2, 32, 0, dont,     FALSE),
    122   RXREL (ABS24S,       2, 24, 0, signed,   FALSE),
    123   RXREL (ABS16,        1, 16, 0, dont,     FALSE),
    124   RXREL (ABS16U,       1, 16, 0, unsigned, FALSE),
    125   RXREL (ABS16S,       1, 16, 0, signed,   FALSE),
    126   RXREL (ABS8,         0,  8, 0, dont,     FALSE),
    127   RXREL (ABS8U,        0,  8, 0, unsigned, FALSE),
    128   RXREL (ABS8S,        0,  8, 0, signed,   FALSE),
    129   RXREL (ABS24S_PCREL, 2, 24, 0, signed,   TRUE),
    130   RXREL (ABS16S_PCREL, 1, 16, 0, signed,   TRUE),
    131   RXREL (ABS8S_PCREL,  0,  8, 0, signed,   TRUE),
    132   RXREL (ABS16UL,      1, 16, 0, unsigned, FALSE),
    133   RXREL (ABS16UW,      1, 16, 0, unsigned, FALSE),
    134   RXREL (ABS8UL,       0,  8, 0, unsigned, FALSE),
    135   RXREL (ABS8UW,       0,  8, 0, unsigned, FALSE),
    136   RXREL (ABS32_REV,    2, 32, 0, dont,     FALSE),
    137   RXREL (ABS16_REV,    1, 16, 0, dont,     FALSE),
    138 
    139 #define STACK_REL_P(x) ((x) <= R_RX_ABS16_REV && (x) >= R_RX_ABS32)
    140 
    141   EMPTY_HOWTO (0x52),
    142   EMPTY_HOWTO (0x53),
    143   EMPTY_HOWTO (0x54),
    144   EMPTY_HOWTO (0x55),
    145   EMPTY_HOWTO (0x56),
    146   EMPTY_HOWTO (0x57),
    147   EMPTY_HOWTO (0x58),
    148   EMPTY_HOWTO (0x59),
    149   EMPTY_HOWTO (0x5a),
    150   EMPTY_HOWTO (0x5b),
    151   EMPTY_HOWTO (0x5c),
    152   EMPTY_HOWTO (0x5d),
    153   EMPTY_HOWTO (0x5e),
    154   EMPTY_HOWTO (0x5f),
    155   EMPTY_HOWTO (0x60),
    156   EMPTY_HOWTO (0x61),
    157   EMPTY_HOWTO (0x62),
    158   EMPTY_HOWTO (0x63),
    159   EMPTY_HOWTO (0x64),
    160   EMPTY_HOWTO (0x65),
    161   EMPTY_HOWTO (0x66),
    162   EMPTY_HOWTO (0x67),
    163   EMPTY_HOWTO (0x68),
    164   EMPTY_HOWTO (0x69),
    165   EMPTY_HOWTO (0x6a),
    166   EMPTY_HOWTO (0x6b),
    167   EMPTY_HOWTO (0x6c),
    168   EMPTY_HOWTO (0x6d),
    169   EMPTY_HOWTO (0x6e),
    170   EMPTY_HOWTO (0x6f),
    171   EMPTY_HOWTO (0x70),
    172   EMPTY_HOWTO (0x71),
    173   EMPTY_HOWTO (0x72),
    174   EMPTY_HOWTO (0x73),
    175   EMPTY_HOWTO (0x74),
    176   EMPTY_HOWTO (0x75),
    177   EMPTY_HOWTO (0x76),
    178   EMPTY_HOWTO (0x77),
    179 
    180   /* These are internal.  */
    181   /* A 5-bit unsigned displacement to a B/W/L address, at bit position 8/12.  */
    182   /* ---- ----   4--- 3210.  */
    183 #define R_RX_RH_ABS5p8B 0x78
    184   RXREL (RH_ABS5p8B,   0,  0, 0, dont,     FALSE),
    185 #define R_RX_RH_ABS5p8W 0x79
    186   RXREL (RH_ABS5p8W,   0,  0, 0, dont,     FALSE),
    187 #define R_RX_RH_ABS5p8L 0x7a
    188   RXREL (RH_ABS5p8L,   0,  0, 0, dont,     FALSE),
    189   /* A 5-bit unsigned displacement to a B/W/L address, at bit position 5/12.  */
    190   /* ---- -432   1--- 0---.  */
    191 #define R_RX_RH_ABS5p5B 0x7b
    192   RXREL (RH_ABS5p5B,   0,  0, 0, dont,     FALSE),
    193 #define R_RX_RH_ABS5p5W 0x7c
    194   RXREL (RH_ABS5p5W,   0,  0, 0, dont,     FALSE),
    195 #define R_RX_RH_ABS5p5L 0x7d
    196   RXREL (RH_ABS5p5L,   0,  0, 0, dont,     FALSE),
    197   /* A 4-bit unsigned immediate at bit position 8.  */
    198 #define R_RX_RH_UIMM4p8 0x7e
    199   RXREL (RH_UIMM4p8,   0,  0, 0, dont,     FALSE),
    200   /* A 4-bit negative unsigned immediate at bit position 8.  */
    201 #define R_RX_RH_UNEG4p8 0x7f
    202   RXREL (RH_UNEG4p8,   0,  0, 0, dont,     FALSE),
    203   /* End of internal relocs.  */
    204 
    205   RXREL (SYM,       2, 32, 0, dont, FALSE),
    206   RXREL (OPneg,     2, 32, 0, dont, FALSE),
    207   RXREL (OPadd,     2, 32, 0, dont, FALSE),
    208   RXREL (OPsub,     2, 32, 0, dont, FALSE),
    209   RXREL (OPmul,     2, 32, 0, dont, FALSE),
    210   RXREL (OPdiv,     2, 32, 0, dont, FALSE),
    211   RXREL (OPshla,    2, 32, 0, dont, FALSE),
    212   RXREL (OPshra,    2, 32, 0, dont, FALSE),
    213   RXREL (OPsctsize, 2, 32, 0, dont, FALSE),
    214   RXREL (OPscttop,  2, 32, 0, dont, FALSE),
    215   RXREL (OPand,     2, 32, 0, dont, FALSE),
    216   RXREL (OPor,      2, 32, 0, dont, FALSE),
    217   RXREL (OPxor,     2, 32, 0, dont, FALSE),
    218   RXREL (OPnot,     2, 32, 0, dont, FALSE),
    219   RXREL (OPmod,     2, 32, 0, dont, FALSE),
    220   RXREL (OPromtop,  2, 32, 0, dont, FALSE),
    221   RXREL (OPramtop,  2, 32, 0, dont, FALSE)
    222 };
    223 
    224 /* Map BFD reloc types to RX ELF reloc types.  */
    226 
    227 struct rx_reloc_map
    228 {
    229   bfd_reloc_code_real_type  bfd_reloc_val;
    230   unsigned int              rx_reloc_val;
    231 };
    232 
    233 static const struct rx_reloc_map rx_reloc_map [] =
    234 {
    235   { BFD_RELOC_NONE,		R_RX_NONE },
    236   { BFD_RELOC_8,		R_RX_DIR8S },
    237   { BFD_RELOC_16,		R_RX_DIR16S },
    238   { BFD_RELOC_24,		R_RX_DIR24S },
    239   { BFD_RELOC_32,		R_RX_DIR32 },
    240   { BFD_RELOC_RX_16_OP,		R_RX_DIR16 },
    241   { BFD_RELOC_RX_DIR3U_PCREL,	R_RX_DIR3U_PCREL },
    242   { BFD_RELOC_8_PCREL,		R_RX_DIR8S_PCREL },
    243   { BFD_RELOC_16_PCREL,		R_RX_DIR16S_PCREL },
    244   { BFD_RELOC_24_PCREL,		R_RX_DIR24S_PCREL },
    245   { BFD_RELOC_RX_8U,		R_RX_DIR8U },
    246   { BFD_RELOC_RX_16U,		R_RX_DIR16U },
    247   { BFD_RELOC_RX_24U,		R_RX_RH_24_UNS },
    248   { BFD_RELOC_RX_NEG8,		R_RX_RH_8_NEG },
    249   { BFD_RELOC_RX_NEG16,		R_RX_RH_16_NEG },
    250   { BFD_RELOC_RX_NEG24,		R_RX_RH_24_NEG },
    251   { BFD_RELOC_RX_NEG32,		R_RX_RH_32_NEG },
    252   { BFD_RELOC_RX_DIFF,		R_RX_RH_DIFF },
    253   { BFD_RELOC_RX_GPRELB,	R_RX_RH_GPRELB },
    254   { BFD_RELOC_RX_GPRELW,	R_RX_RH_GPRELW },
    255   { BFD_RELOC_RX_GPRELL,	R_RX_RH_GPRELL },
    256   { BFD_RELOC_RX_RELAX,		R_RX_RH_RELAX },
    257   { BFD_RELOC_RX_SYM,		R_RX_SYM },
    258   { BFD_RELOC_RX_OP_SUBTRACT,	R_RX_OPsub },
    259   { BFD_RELOC_RX_OP_NEG,	R_RX_OPneg },
    260   { BFD_RELOC_RX_ABS8,		R_RX_ABS8 },
    261   { BFD_RELOC_RX_ABS16,		R_RX_ABS16 },
    262   { BFD_RELOC_RX_ABS16_REV,	R_RX_ABS16_REV },
    263   { BFD_RELOC_RX_ABS32,		R_RX_ABS32 },
    264   { BFD_RELOC_RX_ABS32_REV,	R_RX_ABS32_REV },
    265   { BFD_RELOC_RX_ABS16UL,	R_RX_ABS16UL },
    266   { BFD_RELOC_RX_ABS16UW,	R_RX_ABS16UW },
    267   { BFD_RELOC_RX_ABS16U,	R_RX_ABS16U }
    268 };
    269 
    270 #define BIGE(abfd)       ((abfd)->xvec->byteorder == BFD_ENDIAN_BIG)
    271 
    272 static reloc_howto_type *
    273 rx_reloc_type_lookup (bfd *                    abfd ATTRIBUTE_UNUSED,
    274 		      bfd_reloc_code_real_type code)
    275 {
    276   unsigned int i;
    277 
    278   if (code == BFD_RELOC_RX_32_OP)
    279     return rx_elf_howto_table + R_RX_DIR32;
    280 
    281   for (i = ARRAY_SIZE (rx_reloc_map); i--;)
    282     if (rx_reloc_map [i].bfd_reloc_val == code)
    283       return rx_elf_howto_table + rx_reloc_map[i].rx_reloc_val;
    284 
    285   return NULL;
    286 }
    287 
    288 static reloc_howto_type *
    289 rx_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, const char * r_name)
    290 {
    291   unsigned int i;
    292 
    293   for (i = 0; i < ARRAY_SIZE (rx_elf_howto_table); i++)
    294     if (rx_elf_howto_table[i].name != NULL
    295 	&& strcasecmp (rx_elf_howto_table[i].name, r_name) == 0)
    296       return rx_elf_howto_table + i;
    297 
    298   return NULL;
    299 }
    300 
    301 /* Set the howto pointer for an RX ELF reloc.  */
    302 
    303 static void
    304 rx_info_to_howto_rela (bfd *               abfd ATTRIBUTE_UNUSED,
    305 		       arelent *           cache_ptr,
    306 		       Elf_Internal_Rela * dst)
    307 {
    308   unsigned int r_type;
    309 
    310   r_type = ELF32_R_TYPE (dst->r_info);
    311   if (r_type >= (unsigned int) R_RX_max)
    312     {
    313       _bfd_error_handler (_("%B: invalid RX reloc number: %d"), abfd, r_type);
    314       r_type = 0;
    315     }
    316   cache_ptr->howto = rx_elf_howto_table + r_type;
    317 }
    318 
    319 static bfd_vma
    321 get_symbol_value (const char *            name,
    322 		  struct bfd_link_info *  info,
    323 		  bfd *                   input_bfd,
    324 		  asection *              input_section,
    325 		  int			  offset)
    326 {
    327   bfd_vma value = 0;
    328   struct bfd_link_hash_entry * h;
    329 
    330   h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
    331 
    332   if (h == NULL
    333       || (h->type != bfd_link_hash_defined
    334 	  && h->type != bfd_link_hash_defweak))
    335     (*info->callbacks->undefined_symbol)
    336       (info, name, input_bfd, input_section, offset, TRUE);
    337   else
    338     value = (h->u.def.value
    339 	     + h->u.def.section->output_section->vma
    340 	     + h->u.def.section->output_offset);
    341 
    342   return value;
    343 }
    344 
    345 static bfd_vma
    346 get_symbol_value_maybe (const char *            name,
    347 			struct bfd_link_info *  info)
    348 {
    349   bfd_vma value = 0;
    350   struct bfd_link_hash_entry * h;
    351 
    352   h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
    353 
    354   if (h == NULL
    355       || (h->type != bfd_link_hash_defined
    356 	  && h->type != bfd_link_hash_defweak))
    357     return 0;
    358   else
    359     value = (h->u.def.value
    360 	     + h->u.def.section->output_section->vma
    361 	     + h->u.def.section->output_offset);
    362 
    363   return value;
    364 }
    365 
    366 static bfd_vma
    367 get_gp (struct bfd_link_info *  info,
    368 	bfd *                   abfd,
    369 	asection *              sec,
    370 	int			offset)
    371 {
    372   static bfd_boolean cached = FALSE;
    373   static bfd_vma     cached_value = 0;
    374 
    375   if (!cached)
    376     {
    377       cached_value = get_symbol_value ("__gp", info, abfd, sec, offset);
    378       cached = TRUE;
    379     }
    380   return cached_value;
    381 }
    382 
    383 static bfd_vma
    384 get_romstart (struct bfd_link_info *  info,
    385 	      bfd *                   abfd,
    386 	      asection *              sec,
    387 	      int		      offset)
    388 {
    389   static bfd_boolean cached = FALSE;
    390   static bfd_vma     cached_value = 0;
    391 
    392   if (!cached)
    393     {
    394       cached_value = get_symbol_value ("_start", info, abfd, sec, offset);
    395       cached = TRUE;
    396     }
    397   return cached_value;
    398 }
    399 
    400 static bfd_vma
    401 get_ramstart (struct bfd_link_info *  info,
    402 	      bfd *                   abfd,
    403 	      asection *              sec,
    404 	      int		      offset)
    405 {
    406   static bfd_boolean cached = FALSE;
    407   static bfd_vma     cached_value = 0;
    408 
    409   if (!cached)
    410     {
    411       cached_value = get_symbol_value ("__datastart", info, abfd, sec, offset);
    412       cached = TRUE;
    413     }
    414   return cached_value;
    415 }
    416 
    417 #define NUM_STACK_ENTRIES 16
    418 static int32_t rx_stack [ NUM_STACK_ENTRIES ];
    419 static unsigned int rx_stack_top;
    420 
    421 #define RX_STACK_PUSH(val)			\
    422   do						\
    423     {						\
    424       if (rx_stack_top < NUM_STACK_ENTRIES)	\
    425         rx_stack [rx_stack_top ++] = (val);	\
    426       else					\
    427         r = bfd_reloc_dangerous;		\
    428     }						\
    429   while (0)
    430 
    431 #define RX_STACK_POP(dest)			\
    432   do						\
    433     {						\
    434       if (rx_stack_top > 0)			\
    435         (dest) = rx_stack [-- rx_stack_top];	\
    436       else					\
    437         (dest) = 0, r = bfd_reloc_dangerous;	\
    438     }						\
    439   while (0)
    440 
    441 /* Relocate an RX ELF section.
    442    There is some attempt to make this function usable for many architectures,
    443    both USE_REL and USE_RELA ['twould be nice if such a critter existed],
    444    if only to serve as a learning tool.
    445 
    446    The RELOCATE_SECTION function is called by the new ELF backend linker
    447    to handle the relocations for a section.
    448 
    449    The relocs are always passed as Rela structures; if the section
    450    actually uses Rel structures, the r_addend field will always be
    451    zero.
    452 
    453    This function is responsible for adjusting the section contents as
    454    necessary, and (if using Rela relocs and generating a relocatable
    455    output file) adjusting the reloc addend as necessary.
    456 
    457    This function does not have to worry about setting the reloc
    458    address or the reloc symbol index.
    459 
    460    LOCAL_SYMS is a pointer to the swapped in local symbols.
    461 
    462    LOCAL_SECTIONS is an array giving the section in the input file
    463    corresponding to the st_shndx field of each local symbol.
    464 
    465    The global hash table entry for the global symbols can be found
    466    via elf_sym_hashes (input_bfd).
    467 
    468    When generating relocatable output, this function must handle
    469    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
    470    going to be the section symbol corresponding to the output
    471    section, which means that the addend must be adjusted
    472    accordingly.  */
    473 
    474 static bfd_boolean
    475 rx_elf_relocate_section
    476     (bfd *                   output_bfd,
    477      struct bfd_link_info *  info,
    478      bfd *                   input_bfd,
    479      asection *              input_section,
    480      bfd_byte *              contents,
    481      Elf_Internal_Rela *     relocs,
    482      Elf_Internal_Sym *      local_syms,
    483      asection **             local_sections)
    484 {
    485   Elf_Internal_Shdr *           symtab_hdr;
    486   struct elf_link_hash_entry ** sym_hashes;
    487   Elf_Internal_Rela *           rel;
    488   Elf_Internal_Rela *           relend;
    489   bfd_boolean			pid_mode;
    490   bfd_boolean			saw_subtract = FALSE;
    491   const char *			table_default_cache = NULL;
    492   bfd_vma			table_start_cache = 0;
    493   bfd_vma			table_end_cache = 0;
    494 
    495   if (elf_elfheader (output_bfd)->e_flags & E_FLAG_RX_PID)
    496     pid_mode = TRUE;
    497   else
    498     pid_mode = FALSE;
    499 
    500   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
    501   sym_hashes = elf_sym_hashes (input_bfd);
    502   relend     = relocs + input_section->reloc_count;
    503   for (rel = relocs; rel < relend; rel ++)
    504     {
    505       reloc_howto_type *           howto;
    506       unsigned long                r_symndx;
    507       Elf_Internal_Sym *           sym;
    508       asection *                   sec;
    509       struct elf_link_hash_entry * h;
    510       bfd_vma                      relocation;
    511       bfd_reloc_status_type        r;
    512       const char *                 name = NULL;
    513       bfd_boolean                  unresolved_reloc = TRUE;
    514       int                          r_type;
    515 
    516       r_type = ELF32_R_TYPE (rel->r_info);
    517       r_symndx = ELF32_R_SYM (rel->r_info);
    518 
    519       howto  = rx_elf_howto_table + ELF32_R_TYPE (rel->r_info);
    520       h      = NULL;
    521       sym    = NULL;
    522       sec    = NULL;
    523       relocation = 0;
    524 
    525       if (rx_stack_top == 0)
    526 	saw_subtract = FALSE;
    527 
    528       if (r_symndx < symtab_hdr->sh_info)
    529 	{
    530 	  sym = local_syms + r_symndx;
    531 	  sec = local_sections [r_symndx];
    532 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel);
    533 
    534 	  name = bfd_elf_string_from_elf_section
    535 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
    536 	  name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
    537 	}
    538       else
    539 	{
    540 	  bfd_boolean warned, ignored;
    541 
    542 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
    543 				   r_symndx, symtab_hdr, sym_hashes, h,
    544 				   sec, relocation, unresolved_reloc,
    545 				   warned, ignored);
    546 
    547 	  name = h->root.root.string;
    548 	}
    549 
    550       if (strncmp (name, "$tableentry$default$", 20) == 0)
    551 	{
    552 	  bfd_vma entry_vma;
    553 	  int idx;
    554 	  char *buf;
    555 
    556 	  if (table_default_cache != name)
    557 	    {
    558 
    559 	      /* All relocs for a given table should be to the same
    560 		 (weak) default symbol) so we can use it to detect a
    561 		 cache miss.  We use the offset into the table to find
    562 		 the "real" symbol.  Calculate and store the table's
    563 		 offset here.  */
    564 
    565 	      table_default_cache = name;
    566 
    567 	      /* We have already done error checking in rx_table_find().  */
    568 
    569 	      buf = (char *) malloc (13 + strlen (name + 20));
    570 
    571 	      sprintf (buf, "$tablestart$%s", name + 20);
    572 	      table_start_cache = get_symbol_value (buf,
    573 						    info,
    574 						    input_bfd,
    575 						    input_section,
    576 						    rel->r_offset);
    577 
    578 	      sprintf (buf, "$tableend$%s", name + 20);
    579 	      table_end_cache = get_symbol_value (buf,
    580 						  info,
    581 						  input_bfd,
    582 						  input_section,
    583 						  rel->r_offset);
    584 
    585 	      free (buf);
    586 	    }
    587 
    588 	  entry_vma = (input_section->output_section->vma
    589 		       + input_section->output_offset
    590 		       + rel->r_offset);
    591 
    592 	  if (table_end_cache <= entry_vma || entry_vma < table_start_cache)
    593 	    {
    594 	      _bfd_error_handler (_("%B:%A: table entry %s outside table"),
    595 				  input_bfd, input_section,
    596 				  name);
    597 	    }
    598 	  else if ((int) (entry_vma - table_start_cache) % 4)
    599 	    {
    600 	      _bfd_error_handler (_("%B:%A: table entry %s not word-aligned within table"),
    601 				  input_bfd, input_section,
    602 				  name);
    603 	    }
    604 	  else
    605 	    {
    606 	      idx = (int) (entry_vma - table_start_cache) / 4;
    607 
    608 	      /* This will look like $tableentry$<N>$<name> */
    609 	      buf = (char *) malloc (12 + 20 + strlen (name + 20));
    610 	      sprintf (buf, "$tableentry$%d$%s", idx, name + 20);
    611 
    612 	      h = (struct elf_link_hash_entry *) bfd_link_hash_lookup (info->hash, buf, FALSE, FALSE, TRUE);
    613 
    614 	      if (h)
    615 		{
    616 		  relocation = (h->root.u.def.value
    617 				+ h->root.u.def.section->output_section->vma
    618 				+ h->root.u.def.section->output_offset);;
    619 		}
    620 
    621 	      free (buf);
    622 	    }
    623 	}
    624 
    625       if (sec != NULL && discarded_section (sec))
    626 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
    627 					 rel, 1, relend, howto, 0, contents);
    628 
    629       if (bfd_link_relocatable (info))
    630 	{
    631 	  /* This is a relocatable link.  We don't have to change
    632              anything, unless the reloc is against a section symbol,
    633              in which case we have to adjust according to where the
    634              section symbol winds up in the output section.  */
    635 	  if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
    636 	    rel->r_addend += sec->output_offset;
    637 	  continue;
    638 	}
    639 
    640       if (h != NULL && h->root.type == bfd_link_hash_undefweak)
    641 	/* If the symbol is undefined and weak
    642 	   then the relocation resolves to zero.  */
    643 	relocation = 0;
    644       else
    645 	{
    646 	  if (howto->pc_relative)
    647 	    {
    648 	      relocation -= (input_section->output_section->vma
    649 			     + input_section->output_offset
    650 			     + rel->r_offset);
    651 	      if (r_type != R_RX_RH_3_PCREL
    652 		  && r_type != R_RX_DIR3U_PCREL)
    653 		relocation ++;
    654 	    }
    655 
    656 	  relocation += rel->r_addend;
    657 	}
    658 
    659       r = bfd_reloc_ok;
    660 
    661 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
    662 #define ALIGN(m)   if (relocation & m) r = bfd_reloc_other;
    663 #define OP(i)      (contents[rel->r_offset + (i)])
    664 #define WARN_REDHAT(type) \
    665       _bfd_error_handler (_("%B:%A: Warning: deprecated Red Hat reloc " type " detected against: %s."), \
    666       input_bfd, input_section, name)
    667 
    668       /* Check for unsafe relocs in PID mode.  These are any relocs where
    669 	 an absolute address is being computed.  There are special cases
    670 	 for relocs against symbols that are known to be referenced in
    671 	 crt0.o before the PID base address register has been initialised.  */
    672 #define UNSAFE_FOR_PID							\
    673   do									\
    674     {									\
    675       if (pid_mode							\
    676           && sec != NULL						\
    677 	  && sec->flags & SEC_READONLY					\
    678 	  && !(input_section->flags & SEC_DEBUGGING)			\
    679 	  && strcmp (name, "__pid_base") != 0				\
    680 	  && strcmp (name, "__gp") != 0					\
    681 	  && strcmp (name, "__romdatastart") != 0			\
    682 	  && !saw_subtract)						\
    683 	_bfd_error_handler (_("%B(%A): unsafe PID relocation %s at 0x%08lx (against %s in %s)"), \
    684 			    input_bfd, input_section, howto->name,	\
    685 			    input_section->output_section->vma + input_section->output_offset + rel->r_offset, \
    686 			    name, sec->name);				\
    687     }									\
    688   while (0)
    689 
    690       /* Opcode relocs are always big endian.  Data relocs are bi-endian.  */
    691       switch (r_type)
    692 	{
    693 	case R_RX_NONE:
    694 	  break;
    695 
    696 	case R_RX_RH_RELAX:
    697 	  break;
    698 
    699 	case R_RX_RH_3_PCREL:
    700 	  WARN_REDHAT ("RX_RH_3_PCREL");
    701 	  RANGE (3, 10);
    702 	  OP (0) &= 0xf8;
    703 	  OP (0) |= relocation & 0x07;
    704 	  break;
    705 
    706 	case R_RX_RH_8_NEG:
    707 	  WARN_REDHAT ("RX_RH_8_NEG");
    708 	  relocation = - relocation;
    709 	case R_RX_DIR8S_PCREL:
    710 	  UNSAFE_FOR_PID;
    711 	  RANGE (-128, 127);
    712 	  OP (0) = relocation;
    713 	  break;
    714 
    715 	case R_RX_DIR8S:
    716 	  UNSAFE_FOR_PID;
    717 	  RANGE (-128, 255);
    718 	  OP (0) = relocation;
    719 	  break;
    720 
    721 	case R_RX_DIR8U:
    722 	  UNSAFE_FOR_PID;
    723 	  RANGE (0, 255);
    724 	  OP (0) = relocation;
    725 	  break;
    726 
    727 	case R_RX_RH_16_NEG:
    728 	  WARN_REDHAT ("RX_RH_16_NEG");
    729 	  relocation = - relocation;
    730 	case R_RX_DIR16S_PCREL:
    731 	  UNSAFE_FOR_PID;
    732 	  RANGE (-32768, 32767);
    733 #if RX_OPCODE_BIG_ENDIAN
    734 #else
    735 	  OP (0) = relocation;
    736 	  OP (1) = relocation >> 8;
    737 #endif
    738 	  break;
    739 
    740 	case R_RX_RH_16_OP:
    741 	  WARN_REDHAT ("RX_RH_16_OP");
    742 	  UNSAFE_FOR_PID;
    743 	  RANGE (-32768, 32767);
    744 #if RX_OPCODE_BIG_ENDIAN
    745 	  OP (1) = relocation;
    746 	  OP (0) = relocation >> 8;
    747 #else
    748 	  OP (0) = relocation;
    749 	  OP (1) = relocation >> 8;
    750 #endif
    751 	  break;
    752 
    753 	case R_RX_DIR16S:
    754 	  UNSAFE_FOR_PID;
    755 	  RANGE (-32768, 65535);
    756 	  if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
    757 	    {
    758 	      OP (1) = relocation;
    759 	      OP (0) = relocation >> 8;
    760 	    }
    761 	  else
    762 	    {
    763 	      OP (0) = relocation;
    764 	      OP (1) = relocation >> 8;
    765 	    }
    766 	  break;
    767 
    768 	case R_RX_DIR16U:
    769 	  UNSAFE_FOR_PID;
    770 	  RANGE (0, 65536);
    771 #if RX_OPCODE_BIG_ENDIAN
    772 	  OP (1) = relocation;
    773 	  OP (0) = relocation >> 8;
    774 #else
    775 	  OP (0) = relocation;
    776 	  OP (1) = relocation >> 8;
    777 #endif
    778 	  break;
    779 
    780 	case R_RX_DIR16:
    781 	  UNSAFE_FOR_PID;
    782 	  RANGE (-32768, 65536);
    783 #if RX_OPCODE_BIG_ENDIAN
    784 	  OP (1) = relocation;
    785 	  OP (0) = relocation >> 8;
    786 #else
    787 	  OP (0) = relocation;
    788 	  OP (1) = relocation >> 8;
    789 #endif
    790 	  break;
    791 
    792 	case R_RX_DIR16_REV:
    793 	  UNSAFE_FOR_PID;
    794 	  RANGE (-32768, 65536);
    795 #if RX_OPCODE_BIG_ENDIAN
    796 	  OP (0) = relocation;
    797 	  OP (1) = relocation >> 8;
    798 #else
    799 	  OP (1) = relocation;
    800 	  OP (0) = relocation >> 8;
    801 #endif
    802 	  break;
    803 
    804 	case R_RX_DIR3U_PCREL:
    805 	  RANGE (3, 10);
    806 	  OP (0) &= 0xf8;
    807 	  OP (0) |= relocation & 0x07;
    808 	  break;
    809 
    810 	case R_RX_RH_24_NEG:
    811 	  UNSAFE_FOR_PID;
    812 	  WARN_REDHAT ("RX_RH_24_NEG");
    813 	  relocation = - relocation;
    814 	case R_RX_DIR24S_PCREL:
    815 	  RANGE (-0x800000, 0x7fffff);
    816 #if RX_OPCODE_BIG_ENDIAN
    817 	  OP (2) = relocation;
    818 	  OP (1) = relocation >> 8;
    819 	  OP (0) = relocation >> 16;
    820 #else
    821 	  OP (0) = relocation;
    822 	  OP (1) = relocation >> 8;
    823 	  OP (2) = relocation >> 16;
    824 #endif
    825 	  break;
    826 
    827 	case R_RX_RH_24_OP:
    828 	  UNSAFE_FOR_PID;
    829 	  WARN_REDHAT ("RX_RH_24_OP");
    830 	  RANGE (-0x800000, 0x7fffff);
    831 #if RX_OPCODE_BIG_ENDIAN
    832 	  OP (2) = relocation;
    833 	  OP (1) = relocation >> 8;
    834 	  OP (0) = relocation >> 16;
    835 #else
    836 	  OP (0) = relocation;
    837 	  OP (1) = relocation >> 8;
    838 	  OP (2) = relocation >> 16;
    839 #endif
    840 	  break;
    841 
    842 	case R_RX_DIR24S:
    843 	  UNSAFE_FOR_PID;
    844 	  RANGE (-0x800000, 0x7fffff);
    845 	  if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
    846 	    {
    847 	      OP (2) = relocation;
    848 	      OP (1) = relocation >> 8;
    849 	      OP (0) = relocation >> 16;
    850 	    }
    851 	  else
    852 	    {
    853 	      OP (0) = relocation;
    854 	      OP (1) = relocation >> 8;
    855 	      OP (2) = relocation >> 16;
    856 	    }
    857 	  break;
    858 
    859 	case R_RX_RH_24_UNS:
    860 	  UNSAFE_FOR_PID;
    861 	  WARN_REDHAT ("RX_RH_24_UNS");
    862 	  RANGE (0, 0xffffff);
    863 #if RX_OPCODE_BIG_ENDIAN
    864 	  OP (2) = relocation;
    865 	  OP (1) = relocation >> 8;
    866 	  OP (0) = relocation >> 16;
    867 #else
    868 	  OP (0) = relocation;
    869 	  OP (1) = relocation >> 8;
    870 	  OP (2) = relocation >> 16;
    871 #endif
    872 	  break;
    873 
    874 	case R_RX_RH_32_NEG:
    875 	  UNSAFE_FOR_PID;
    876 	  WARN_REDHAT ("RX_RH_32_NEG");
    877 	  relocation = - relocation;
    878 #if RX_OPCODE_BIG_ENDIAN
    879 	  OP (3) = relocation;
    880 	  OP (2) = relocation >> 8;
    881 	  OP (1) = relocation >> 16;
    882 	  OP (0) = relocation >> 24;
    883 #else
    884 	  OP (0) = relocation;
    885 	  OP (1) = relocation >> 8;
    886 	  OP (2) = relocation >> 16;
    887 	  OP (3) = relocation >> 24;
    888 #endif
    889 	  break;
    890 
    891 	case R_RX_RH_32_OP:
    892 	  UNSAFE_FOR_PID;
    893 	  WARN_REDHAT ("RX_RH_32_OP");
    894 #if RX_OPCODE_BIG_ENDIAN
    895 	  OP (3) = relocation;
    896 	  OP (2) = relocation >> 8;
    897 	  OP (1) = relocation >> 16;
    898 	  OP (0) = relocation >> 24;
    899 #else
    900 	  OP (0) = relocation;
    901 	  OP (1) = relocation >> 8;
    902 	  OP (2) = relocation >> 16;
    903 	  OP (3) = relocation >> 24;
    904 #endif
    905 	  break;
    906 
    907 	case R_RX_DIR32:
    908 	  if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
    909 	    {
    910 	      OP (3) = relocation;
    911 	      OP (2) = relocation >> 8;
    912 	      OP (1) = relocation >> 16;
    913 	      OP (0) = relocation >> 24;
    914 	    }
    915 	  else
    916 	    {
    917 	      OP (0) = relocation;
    918 	      OP (1) = relocation >> 8;
    919 	      OP (2) = relocation >> 16;
    920 	      OP (3) = relocation >> 24;
    921 	    }
    922 	  break;
    923 
    924 	case R_RX_DIR32_REV:
    925 	  if (BIGE (output_bfd))
    926 	    {
    927 	      OP (0) = relocation;
    928 	      OP (1) = relocation >> 8;
    929 	      OP (2) = relocation >> 16;
    930 	      OP (3) = relocation >> 24;
    931 	    }
    932 	  else
    933 	    {
    934 	      OP (3) = relocation;
    935 	      OP (2) = relocation >> 8;
    936 	      OP (1) = relocation >> 16;
    937 	      OP (0) = relocation >> 24;
    938 	    }
    939 	  break;
    940 
    941 	case R_RX_RH_DIFF:
    942 	  {
    943 	    bfd_vma val;
    944 	    WARN_REDHAT ("RX_RH_DIFF");
    945 	    val = bfd_get_32 (output_bfd, & OP (0));
    946 	    val -= relocation;
    947 	    bfd_put_32 (output_bfd, val, & OP (0));
    948 	  }
    949 	  break;
    950 
    951 	case R_RX_RH_GPRELB:
    952 	  WARN_REDHAT ("RX_RH_GPRELB");
    953 	  relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
    954 	  RANGE (0, 65535);
    955 #if RX_OPCODE_BIG_ENDIAN
    956 	  OP (1) = relocation;
    957 	  OP (0) = relocation >> 8;
    958 #else
    959 	  OP (0) = relocation;
    960 	  OP (1) = relocation >> 8;
    961 #endif
    962 	  break;
    963 
    964 	case R_RX_RH_GPRELW:
    965 	  WARN_REDHAT ("RX_RH_GPRELW");
    966 	  relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
    967 	  ALIGN (1);
    968 	  relocation >>= 1;
    969 	  RANGE (0, 65535);
    970 #if RX_OPCODE_BIG_ENDIAN
    971 	  OP (1) = relocation;
    972 	  OP (0) = relocation >> 8;
    973 #else
    974 	  OP (0) = relocation;
    975 	  OP (1) = relocation >> 8;
    976 #endif
    977 	  break;
    978 
    979 	case R_RX_RH_GPRELL:
    980 	  WARN_REDHAT ("RX_RH_GPRELL");
    981 	  relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
    982 	  ALIGN (3);
    983 	  relocation >>= 2;
    984 	  RANGE (0, 65535);
    985 #if RX_OPCODE_BIG_ENDIAN
    986 	  OP (1) = relocation;
    987 	  OP (0) = relocation >> 8;
    988 #else
    989 	  OP (0) = relocation;
    990 	  OP (1) = relocation >> 8;
    991 #endif
    992 	  break;
    993 
    994 	/* Internal relocations just for relaxation:  */
    995 	case R_RX_RH_ABS5p5B:
    996 	  RX_STACK_POP (relocation);
    997 	  RANGE (0, 31);
    998 	  OP (0) &= 0xf8;
    999 	  OP (0) |= relocation >> 2;
   1000 	  OP (1) &= 0x77;
   1001 	  OP (1) |= (relocation << 6) & 0x80;
   1002 	  OP (1) |= (relocation << 3) & 0x08;
   1003 	  break;
   1004 
   1005 	case R_RX_RH_ABS5p5W:
   1006 	  RX_STACK_POP (relocation);
   1007 	  RANGE (0, 62);
   1008 	  ALIGN (1);
   1009 	  relocation >>= 1;
   1010 	  OP (0) &= 0xf8;
   1011 	  OP (0) |= relocation >> 2;
   1012 	  OP (1) &= 0x77;
   1013 	  OP (1) |= (relocation << 6) & 0x80;
   1014 	  OP (1) |= (relocation << 3) & 0x08;
   1015 	  break;
   1016 
   1017 	case R_RX_RH_ABS5p5L:
   1018 	  RX_STACK_POP (relocation);
   1019 	  RANGE (0, 124);
   1020 	  ALIGN (3);
   1021 	  relocation >>= 2;
   1022 	  OP (0) &= 0xf8;
   1023 	  OP (0) |= relocation >> 2;
   1024 	  OP (1) &= 0x77;
   1025 	  OP (1) |= (relocation << 6) & 0x80;
   1026 	  OP (1) |= (relocation << 3) & 0x08;
   1027 	  break;
   1028 
   1029 	case R_RX_RH_ABS5p8B:
   1030 	  RX_STACK_POP (relocation);
   1031 	  RANGE (0, 31);
   1032 	  OP (0) &= 0x70;
   1033 	  OP (0) |= (relocation << 3) & 0x80;
   1034 	  OP (0) |= relocation & 0x0f;
   1035 	  break;
   1036 
   1037 	case R_RX_RH_ABS5p8W:
   1038 	  RX_STACK_POP (relocation);
   1039 	  RANGE (0, 62);
   1040 	  ALIGN (1);
   1041 	  relocation >>= 1;
   1042 	  OP (0) &= 0x70;
   1043 	  OP (0) |= (relocation << 3) & 0x80;
   1044 	  OP (0) |= relocation & 0x0f;
   1045 	  break;
   1046 
   1047 	case R_RX_RH_ABS5p8L:
   1048 	  RX_STACK_POP (relocation);
   1049 	  RANGE (0, 124);
   1050 	  ALIGN (3);
   1051 	  relocation >>= 2;
   1052 	  OP (0) &= 0x70;
   1053 	  OP (0) |= (relocation << 3) & 0x80;
   1054 	  OP (0) |= relocation & 0x0f;
   1055 	  break;
   1056 
   1057 	case R_RX_RH_UIMM4p8:
   1058 	  RANGE (0, 15);
   1059 	  OP (0) &= 0x0f;
   1060 	  OP (0) |= relocation << 4;
   1061 	  break;
   1062 
   1063 	case R_RX_RH_UNEG4p8:
   1064 	  RANGE (-15, 0);
   1065 	  OP (0) &= 0x0f;
   1066 	  OP (0) |= (-relocation) << 4;
   1067 	  break;
   1068 
   1069 	  /* Complex reloc handling:  */
   1070 
   1071 	case R_RX_ABS32:
   1072 	  UNSAFE_FOR_PID;
   1073 	  RX_STACK_POP (relocation);
   1074 #if RX_OPCODE_BIG_ENDIAN
   1075 	  OP (3) = relocation;
   1076 	  OP (2) = relocation >> 8;
   1077 	  OP (1) = relocation >> 16;
   1078 	  OP (0) = relocation >> 24;
   1079 #else
   1080 	  OP (0) = relocation;
   1081 	  OP (1) = relocation >> 8;
   1082 	  OP (2) = relocation >> 16;
   1083 	  OP (3) = relocation >> 24;
   1084 #endif
   1085 	  break;
   1086 
   1087 	case R_RX_ABS32_REV:
   1088 	  UNSAFE_FOR_PID;
   1089 	  RX_STACK_POP (relocation);
   1090 #if RX_OPCODE_BIG_ENDIAN
   1091 	  OP (0) = relocation;
   1092 	  OP (1) = relocation >> 8;
   1093 	  OP (2) = relocation >> 16;
   1094 	  OP (3) = relocation >> 24;
   1095 #else
   1096 	  OP (3) = relocation;
   1097 	  OP (2) = relocation >> 8;
   1098 	  OP (1) = relocation >> 16;
   1099 	  OP (0) = relocation >> 24;
   1100 #endif
   1101 	  break;
   1102 
   1103 	case R_RX_ABS24S_PCREL:
   1104 	case R_RX_ABS24S:
   1105 	  UNSAFE_FOR_PID;
   1106 	  RX_STACK_POP (relocation);
   1107 	  RANGE (-0x800000, 0x7fffff);
   1108 	  if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
   1109 	    {
   1110 	      OP (2) = relocation;
   1111 	      OP (1) = relocation >> 8;
   1112 	      OP (0) = relocation >> 16;
   1113 	    }
   1114 	  else
   1115 	    {
   1116 	      OP (0) = relocation;
   1117 	      OP (1) = relocation >> 8;
   1118 	      OP (2) = relocation >> 16;
   1119 	    }
   1120 	  break;
   1121 
   1122 	case R_RX_ABS16:
   1123 	  UNSAFE_FOR_PID;
   1124 	  RX_STACK_POP (relocation);
   1125 	  RANGE (-32768, 65535);
   1126 #if RX_OPCODE_BIG_ENDIAN
   1127 	  OP (1) = relocation;
   1128 	  OP (0) = relocation >> 8;
   1129 #else
   1130 	  OP (0) = relocation;
   1131 	  OP (1) = relocation >> 8;
   1132 #endif
   1133 	  break;
   1134 
   1135 	case R_RX_ABS16_REV:
   1136 	  UNSAFE_FOR_PID;
   1137 	  RX_STACK_POP (relocation);
   1138 	  RANGE (-32768, 65535);
   1139 #if RX_OPCODE_BIG_ENDIAN
   1140 	  OP (0) = relocation;
   1141 	  OP (1) = relocation >> 8;
   1142 #else
   1143 	  OP (1) = relocation;
   1144 	  OP (0) = relocation >> 8;
   1145 #endif
   1146 	  break;
   1147 
   1148 	case R_RX_ABS16S_PCREL:
   1149 	case R_RX_ABS16S:
   1150 	  RX_STACK_POP (relocation);
   1151 	  RANGE (-32768, 32767);
   1152 	  if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
   1153 	    {
   1154 	      OP (1) = relocation;
   1155 	      OP (0) = relocation >> 8;
   1156 	    }
   1157 	  else
   1158 	    {
   1159 	      OP (0) = relocation;
   1160 	      OP (1) = relocation >> 8;
   1161 	    }
   1162 	  break;
   1163 
   1164 	case R_RX_ABS16U:
   1165 	  UNSAFE_FOR_PID;
   1166 	  RX_STACK_POP (relocation);
   1167 	  RANGE (0, 65536);
   1168 #if RX_OPCODE_BIG_ENDIAN
   1169 	  OP (1) = relocation;
   1170 	  OP (0) = relocation >> 8;
   1171 #else
   1172 	  OP (0) = relocation;
   1173 	  OP (1) = relocation >> 8;
   1174 #endif
   1175 	  break;
   1176 
   1177 	case R_RX_ABS16UL:
   1178 	  UNSAFE_FOR_PID;
   1179 	  RX_STACK_POP (relocation);
   1180 	  relocation >>= 2;
   1181 	  RANGE (0, 65536);
   1182 #if RX_OPCODE_BIG_ENDIAN
   1183 	  OP (1) = relocation;
   1184 	  OP (0) = relocation >> 8;
   1185 #else
   1186 	  OP (0) = relocation;
   1187 	  OP (1) = relocation >> 8;
   1188 #endif
   1189 	  break;
   1190 
   1191 	case R_RX_ABS16UW:
   1192 	  UNSAFE_FOR_PID;
   1193 	  RX_STACK_POP (relocation);
   1194 	  relocation >>= 1;
   1195 	  RANGE (0, 65536);
   1196 #if RX_OPCODE_BIG_ENDIAN
   1197 	  OP (1) = relocation;
   1198 	  OP (0) = relocation >> 8;
   1199 #else
   1200 	  OP (0) = relocation;
   1201 	  OP (1) = relocation >> 8;
   1202 #endif
   1203 	  break;
   1204 
   1205 	case R_RX_ABS8:
   1206 	  UNSAFE_FOR_PID;
   1207 	  RX_STACK_POP (relocation);
   1208 	  RANGE (-128, 255);
   1209 	  OP (0) = relocation;
   1210 	  break;
   1211 
   1212 	case R_RX_ABS8U:
   1213 	  UNSAFE_FOR_PID;
   1214 	  RX_STACK_POP (relocation);
   1215 	  RANGE (0, 255);
   1216 	  OP (0) = relocation;
   1217 	  break;
   1218 
   1219 	case R_RX_ABS8UL:
   1220 	  UNSAFE_FOR_PID;
   1221 	  RX_STACK_POP (relocation);
   1222 	  relocation >>= 2;
   1223 	  RANGE (0, 255);
   1224 	  OP (0) = relocation;
   1225 	  break;
   1226 
   1227 	case R_RX_ABS8UW:
   1228 	  UNSAFE_FOR_PID;
   1229 	  RX_STACK_POP (relocation);
   1230 	  relocation >>= 1;
   1231 	  RANGE (0, 255);
   1232 	  OP (0) = relocation;
   1233 	  break;
   1234 
   1235 	case R_RX_ABS8S:
   1236 	  UNSAFE_FOR_PID;
   1237 	case R_RX_ABS8S_PCREL:
   1238 	  RX_STACK_POP (relocation);
   1239 	  RANGE (-128, 127);
   1240 	  OP (0) = relocation;
   1241 	  break;
   1242 
   1243 	case R_RX_SYM:
   1244 	  if (r_symndx < symtab_hdr->sh_info)
   1245 	    RX_STACK_PUSH (sec->output_section->vma
   1246 			   + sec->output_offset
   1247 			   + sym->st_value
   1248 			   + rel->r_addend);
   1249 	  else
   1250 	    {
   1251 	      if (h != NULL
   1252 		  && (h->root.type == bfd_link_hash_defined
   1253 		      || h->root.type == bfd_link_hash_defweak))
   1254 		RX_STACK_PUSH (h->root.u.def.value
   1255 			       + sec->output_section->vma
   1256 			       + sec->output_offset
   1257 			       + rel->r_addend);
   1258 	      else
   1259 		_bfd_error_handler (_("Warning: RX_SYM reloc with an unknown symbol"));
   1260 	    }
   1261 	  break;
   1262 
   1263 	case R_RX_OPneg:
   1264 	  {
   1265 	    int32_t tmp;
   1266 
   1267 	    saw_subtract = TRUE;
   1268 	    RX_STACK_POP (tmp);
   1269 	    tmp = - tmp;
   1270 	    RX_STACK_PUSH (tmp);
   1271 	  }
   1272 	  break;
   1273 
   1274 	case R_RX_OPadd:
   1275 	  {
   1276 	    int32_t tmp1, tmp2;
   1277 
   1278 	    RX_STACK_POP (tmp1);
   1279 	    RX_STACK_POP (tmp2);
   1280 	    tmp1 += tmp2;
   1281 	    RX_STACK_PUSH (tmp1);
   1282 	  }
   1283 	  break;
   1284 
   1285 	case R_RX_OPsub:
   1286 	  {
   1287 	    int32_t tmp1, tmp2;
   1288 
   1289 	    saw_subtract = TRUE;
   1290 	    RX_STACK_POP (tmp1);
   1291 	    RX_STACK_POP (tmp2);
   1292 	    tmp2 -= tmp1;
   1293 	    RX_STACK_PUSH (tmp2);
   1294 	  }
   1295 	  break;
   1296 
   1297 	case R_RX_OPmul:
   1298 	  {
   1299 	    int32_t tmp1, tmp2;
   1300 
   1301 	    RX_STACK_POP (tmp1);
   1302 	    RX_STACK_POP (tmp2);
   1303 	    tmp1 *= tmp2;
   1304 	    RX_STACK_PUSH (tmp1);
   1305 	  }
   1306 	  break;
   1307 
   1308 	case R_RX_OPdiv:
   1309 	  {
   1310 	    int32_t tmp1, tmp2;
   1311 
   1312 	    RX_STACK_POP (tmp1);
   1313 	    RX_STACK_POP (tmp2);
   1314 	    tmp1 /= tmp2;
   1315 	    RX_STACK_PUSH (tmp1);
   1316 	  }
   1317 	  break;
   1318 
   1319 	case R_RX_OPshla:
   1320 	  {
   1321 	    int32_t tmp1, tmp2;
   1322 
   1323 	    RX_STACK_POP (tmp1);
   1324 	    RX_STACK_POP (tmp2);
   1325 	    tmp1 <<= tmp2;
   1326 	    RX_STACK_PUSH (tmp1);
   1327 	  }
   1328 	  break;
   1329 
   1330 	case R_RX_OPshra:
   1331 	  {
   1332 	    int32_t tmp1, tmp2;
   1333 
   1334 	    RX_STACK_POP (tmp1);
   1335 	    RX_STACK_POP (tmp2);
   1336 	    tmp1 >>= tmp2;
   1337 	    RX_STACK_PUSH (tmp1);
   1338 	  }
   1339 	  break;
   1340 
   1341 	case R_RX_OPsctsize:
   1342 	  RX_STACK_PUSH (input_section->size);
   1343 	  break;
   1344 
   1345 	case R_RX_OPscttop:
   1346 	  RX_STACK_PUSH (input_section->output_section->vma);
   1347 	  break;
   1348 
   1349 	case R_RX_OPand:
   1350 	  {
   1351 	    int32_t tmp1, tmp2;
   1352 
   1353 	    RX_STACK_POP (tmp1);
   1354 	    RX_STACK_POP (tmp2);
   1355 	    tmp1 &= tmp2;
   1356 	    RX_STACK_PUSH (tmp1);
   1357 	  }
   1358 	  break;
   1359 
   1360 	case R_RX_OPor:
   1361 	  {
   1362 	    int32_t tmp1, tmp2;
   1363 
   1364 	    RX_STACK_POP (tmp1);
   1365 	    RX_STACK_POP (tmp2);
   1366 	    tmp1 |= tmp2;
   1367 	    RX_STACK_PUSH (tmp1);
   1368 	  }
   1369 	  break;
   1370 
   1371 	case R_RX_OPxor:
   1372 	  {
   1373 	    int32_t tmp1, tmp2;
   1374 
   1375 	    RX_STACK_POP (tmp1);
   1376 	    RX_STACK_POP (tmp2);
   1377 	    tmp1 ^= tmp2;
   1378 	    RX_STACK_PUSH (tmp1);
   1379 	  }
   1380 	  break;
   1381 
   1382 	case R_RX_OPnot:
   1383 	  {
   1384 	    int32_t tmp;
   1385 
   1386 	    RX_STACK_POP (tmp);
   1387 	    tmp = ~ tmp;
   1388 	    RX_STACK_PUSH (tmp);
   1389 	  }
   1390 	  break;
   1391 
   1392 	case R_RX_OPmod:
   1393 	  {
   1394 	    int32_t tmp1, tmp2;
   1395 
   1396 	    RX_STACK_POP (tmp1);
   1397 	    RX_STACK_POP (tmp2);
   1398 	    tmp1 %= tmp2;
   1399 	    RX_STACK_PUSH (tmp1);
   1400 	  }
   1401 	  break;
   1402 
   1403 	case R_RX_OPromtop:
   1404 	  RX_STACK_PUSH (get_romstart (info, input_bfd, input_section, rel->r_offset));
   1405 	  break;
   1406 
   1407 	case R_RX_OPramtop:
   1408 	  RX_STACK_PUSH (get_ramstart (info, input_bfd, input_section, rel->r_offset));
   1409 	  break;
   1410 
   1411 	default:
   1412 	  r = bfd_reloc_notsupported;
   1413 	  break;
   1414 	}
   1415 
   1416       if (r != bfd_reloc_ok)
   1417 	{
   1418 	  const char * msg = NULL;
   1419 
   1420 	  switch (r)
   1421 	    {
   1422 	    case bfd_reloc_overflow:
   1423 	      /* Catch the case of a missing function declaration
   1424 		 and emit a more helpful error message.  */
   1425 	      if (r_type == R_RX_DIR24S_PCREL)
   1426 		msg = _("%B(%A): error: call to undefined function '%s'");
   1427 	      else
   1428 		(*info->callbacks->reloc_overflow)
   1429 		  (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
   1430 		   input_bfd, input_section, rel->r_offset);
   1431 	      break;
   1432 
   1433 	    case bfd_reloc_undefined:
   1434 	      (*info->callbacks->undefined_symbol)
   1435 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
   1436 	      break;
   1437 
   1438 	    case bfd_reloc_other:
   1439 	      msg = _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
   1440 	      break;
   1441 
   1442 	    case bfd_reloc_outofrange:
   1443 	      msg = _("%B(%A): internal error: out of range error");
   1444 	      break;
   1445 
   1446 	    case bfd_reloc_notsupported:
   1447 	      msg = _("%B(%A): internal error: unsupported relocation error");
   1448 	      break;
   1449 
   1450 	    case bfd_reloc_dangerous:
   1451 	      msg = _("%B(%A): internal error: dangerous relocation");
   1452 	      break;
   1453 
   1454 	    default:
   1455 	      msg = _("%B(%A): internal error: unknown error");
   1456 	      break;
   1457 	    }
   1458 
   1459 	  if (msg)
   1460 	    _bfd_error_handler (msg, input_bfd, input_section, name);
   1461 	}
   1462     }
   1463 
   1464   return TRUE;
   1465 }
   1466 
   1467 /* Relaxation Support.  */
   1469 
   1470 /* Progression of relocations from largest operand size to smallest
   1471    operand size.  */
   1472 
   1473 static int
   1474 next_smaller_reloc (int r)
   1475 {
   1476   switch (r)
   1477     {
   1478     case R_RX_DIR32:		return R_RX_DIR24S;
   1479     case R_RX_DIR24S:		return R_RX_DIR16S;
   1480     case R_RX_DIR16S:		return R_RX_DIR8S;
   1481     case R_RX_DIR8S:		return R_RX_NONE;
   1482 
   1483     case R_RX_DIR16:		return R_RX_DIR8;
   1484     case R_RX_DIR8:		return R_RX_NONE;
   1485 
   1486     case R_RX_DIR16U:		return R_RX_DIR8U;
   1487     case R_RX_DIR8U:		return R_RX_NONE;
   1488 
   1489     case R_RX_DIR24S_PCREL:	return R_RX_DIR16S_PCREL;
   1490     case R_RX_DIR16S_PCREL:	return R_RX_DIR8S_PCREL;
   1491     case R_RX_DIR8S_PCREL:	return R_RX_DIR3U_PCREL;
   1492 
   1493     case R_RX_DIR16UL:		return R_RX_DIR8UL;
   1494     case R_RX_DIR8UL:		return R_RX_NONE;
   1495     case R_RX_DIR16UW:		return R_RX_DIR8UW;
   1496     case R_RX_DIR8UW:		return R_RX_NONE;
   1497 
   1498     case R_RX_RH_32_OP:		return R_RX_RH_24_OP;
   1499     case R_RX_RH_24_OP:		return R_RX_RH_16_OP;
   1500     case R_RX_RH_16_OP:		return R_RX_DIR8;
   1501 
   1502     case R_RX_ABS32:		return R_RX_ABS24S;
   1503     case R_RX_ABS24S:		return R_RX_ABS16S;
   1504     case R_RX_ABS16:		return R_RX_ABS8;
   1505     case R_RX_ABS16U:		return R_RX_ABS8U;
   1506     case R_RX_ABS16S:		return R_RX_ABS8S;
   1507     case R_RX_ABS8:		return R_RX_NONE;
   1508     case R_RX_ABS8U:		return R_RX_NONE;
   1509     case R_RX_ABS8S:		return R_RX_NONE;
   1510     case R_RX_ABS24S_PCREL:	return R_RX_ABS16S_PCREL;
   1511     case R_RX_ABS16S_PCREL:	return R_RX_ABS8S_PCREL;
   1512     case R_RX_ABS8S_PCREL:	return R_RX_NONE;
   1513     case R_RX_ABS16UL:		return R_RX_ABS8UL;
   1514     case R_RX_ABS16UW:		return R_RX_ABS8UW;
   1515     case R_RX_ABS8UL:		return R_RX_NONE;
   1516     case R_RX_ABS8UW:		return R_RX_NONE;
   1517     }
   1518   return r;
   1519 };
   1520 
   1521 /* Delete some bytes from a section while relaxing.  */
   1522 
   1523 static bfd_boolean
   1524 elf32_rx_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
   1525 			     Elf_Internal_Rela *alignment_rel, int force_snip,
   1526 			     Elf_Internal_Rela *irelstart)
   1527 {
   1528   Elf_Internal_Shdr * symtab_hdr;
   1529   unsigned int        sec_shndx;
   1530   bfd_byte *          contents;
   1531   Elf_Internal_Rela * irel;
   1532   Elf_Internal_Rela * irelend;
   1533   Elf_Internal_Sym *  isym;
   1534   Elf_Internal_Sym *  isymend;
   1535   bfd_vma             toaddr;
   1536   unsigned int        symcount;
   1537   struct elf_link_hash_entry ** sym_hashes;
   1538   struct elf_link_hash_entry ** end_hashes;
   1539 
   1540   if (!alignment_rel)
   1541     force_snip = 1;
   1542 
   1543   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   1544 
   1545   contents = elf_section_data (sec)->this_hdr.contents;
   1546 
   1547   /* The deletion must stop at the next alignment boundary, if
   1548      ALIGNMENT_REL is non-NULL.  */
   1549   toaddr = sec->size;
   1550   if (alignment_rel)
   1551     toaddr = alignment_rel->r_offset;
   1552 
   1553   BFD_ASSERT (toaddr > addr);
   1554 
   1555   /* Actually delete the bytes.  */
   1556   memmove (contents + addr, contents + addr + count,
   1557 	   (size_t) (toaddr - addr - count));
   1558 
   1559   /* If we don't have an alignment marker to worry about, we can just
   1560      shrink the section.  Otherwise, we have to fill in the newly
   1561      created gap with NOP insns (0x03).  */
   1562   if (force_snip)
   1563     sec->size -= count;
   1564   else
   1565     memset (contents + toaddr - count, 0x03, count);
   1566 
   1567   irel = irelstart;
   1568   BFD_ASSERT (irel != NULL || sec->reloc_count == 0);
   1569   irelend = irel + sec->reloc_count;
   1570 
   1571   /* Adjust all the relocs.  */
   1572   for (; irel < irelend; irel++)
   1573     {
   1574       /* Get the new reloc address.  */
   1575       if (irel->r_offset > addr
   1576 	  && (irel->r_offset < toaddr
   1577 	      || (force_snip && irel->r_offset == toaddr)))
   1578 	irel->r_offset -= count;
   1579 
   1580       /* If we see an ALIGN marker at the end of the gap, we move it
   1581 	 to the beginning of the gap, since marking these gaps is what
   1582 	 they're for.  */
   1583       if (irel->r_offset == toaddr
   1584 	  && ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
   1585 	  && irel->r_addend & RX_RELAXA_ALIGN)
   1586 	irel->r_offset -= count;
   1587     }
   1588 
   1589   /* Adjust the local symbols defined in this section.  */
   1590   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1591   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   1592   isymend = isym + symtab_hdr->sh_info;
   1593 
   1594   for (; isym < isymend; isym++)
   1595     {
   1596       /* If the symbol is in the range of memory we just moved, we
   1597 	 have to adjust its value.  */
   1598       if (isym->st_shndx == sec_shndx
   1599 	  && isym->st_value > addr
   1600 	  && isym->st_value < toaddr)
   1601 	isym->st_value -= count;
   1602 
   1603       /* If the symbol *spans* the bytes we just deleted (i.e. it's
   1604 	 *end* is in the moved bytes but it's *start* isn't), then we
   1605 	 must adjust its size.  */
   1606       if (isym->st_shndx == sec_shndx
   1607 	  && isym->st_value < addr
   1608 	  && isym->st_value + isym->st_size > addr
   1609 	  && isym->st_value + isym->st_size < toaddr)
   1610 	isym->st_size -= count;
   1611     }
   1612 
   1613   /* Now adjust the global symbols defined in this section.  */
   1614   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   1615 	      - symtab_hdr->sh_info);
   1616   sym_hashes = elf_sym_hashes (abfd);
   1617   end_hashes = sym_hashes + symcount;
   1618 
   1619   for (; sym_hashes < end_hashes; sym_hashes++)
   1620     {
   1621       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   1622 
   1623       if ((sym_hash->root.type == bfd_link_hash_defined
   1624 	   || sym_hash->root.type == bfd_link_hash_defweak)
   1625 	  && sym_hash->root.u.def.section == sec)
   1626 	{
   1627 	  /* As above, adjust the value if needed.  */
   1628 	  if (sym_hash->root.u.def.value > addr
   1629 	      && sym_hash->root.u.def.value < toaddr)
   1630 	    sym_hash->root.u.def.value -= count;
   1631 
   1632 	  /* As above, adjust the size if needed.  */
   1633 	  if (sym_hash->root.u.def.value < addr
   1634 	      && sym_hash->root.u.def.value + sym_hash->size > addr
   1635 	      && sym_hash->root.u.def.value + sym_hash->size < toaddr)
   1636 	    sym_hash->size -= count;
   1637 	}
   1638     }
   1639 
   1640   return TRUE;
   1641 }
   1642 
   1643 /* Used to sort relocs by address.  If relocs have the same address,
   1644    we maintain their relative order, except that R_RX_RH_RELAX
   1645    alignment relocs must be the first reloc for any given address.  */
   1646 
   1647 static void
   1648 reloc_bubblesort (Elf_Internal_Rela * r, int count)
   1649 {
   1650   int i;
   1651   bfd_boolean again;
   1652   bfd_boolean swappit;
   1653 
   1654   /* This is almost a classic bubblesort.  It's the slowest sort, but
   1655      we're taking advantage of the fact that the relocations are
   1656      mostly in order already (the assembler emits them that way) and
   1657      we need relocs with the same address to remain in the same
   1658      relative order.  */
   1659   again = TRUE;
   1660   while (again)
   1661     {
   1662       again = FALSE;
   1663       for (i = 0; i < count - 1; i ++)
   1664 	{
   1665 	  if (r[i].r_offset > r[i + 1].r_offset)
   1666 	    swappit = TRUE;
   1667 	  else if (r[i].r_offset < r[i + 1].r_offset)
   1668 	    swappit = FALSE;
   1669 	  else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
   1670 		   && (r[i + 1].r_addend & RX_RELAXA_ALIGN))
   1671 	    swappit = TRUE;
   1672 	  else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
   1673 		   && (r[i + 1].r_addend & RX_RELAXA_ELIGN)
   1674 		   && !(ELF32_R_TYPE (r[i].r_info) == R_RX_RH_RELAX
   1675 			&& (r[i].r_addend & RX_RELAXA_ALIGN)))
   1676 	    swappit = TRUE;
   1677 	  else
   1678 	    swappit = FALSE;
   1679 
   1680 	  if (swappit)
   1681 	    {
   1682 	      Elf_Internal_Rela tmp;
   1683 
   1684 	      tmp = r[i];
   1685 	      r[i] = r[i + 1];
   1686 	      r[i + 1] = tmp;
   1687 	      /* If we do move a reloc back, re-scan to see if it
   1688 		 needs to be moved even further back.  This avoids
   1689 		 most of the O(n^2) behavior for our cases.  */
   1690 	      if (i > 0)
   1691 		i -= 2;
   1692 	      again = TRUE;
   1693 	    }
   1694 	}
   1695     }
   1696 }
   1697 
   1698 
   1699 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
   1700   rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
   1701 		       lrel, abfd, sec, link_info, scale)
   1702 
   1703 static bfd_vma
   1704 rx_offset_for_reloc (bfd *                    abfd,
   1705 		     Elf_Internal_Rela *      rel,
   1706 		     Elf_Internal_Shdr *      symtab_hdr,
   1707 		     Elf_External_Sym_Shndx * shndx_buf ATTRIBUTE_UNUSED,
   1708 		     Elf_Internal_Sym *       intsyms,
   1709 		     Elf_Internal_Rela **     lrel,
   1710 		     bfd *                    input_bfd,
   1711 		     asection *               input_section,
   1712 		     struct bfd_link_info *   info,
   1713 		     int *                    scale)
   1714 {
   1715   bfd_vma symval;
   1716   bfd_reloc_status_type r;
   1717 
   1718   *scale = 1;
   1719 
   1720   /* REL is the first of 1..N relocations.  We compute the symbol
   1721      value for each relocation, then combine them if needed.  LREL
   1722      gets a pointer to the last relocation used.  */
   1723   while (1)
   1724     {
   1725       int32_t tmp1, tmp2;
   1726 
   1727       /* Get the value of the symbol referred to by the reloc.  */
   1728       if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
   1729 	{
   1730 	  /* A local symbol.  */
   1731 	  Elf_Internal_Sym *isym;
   1732 	  asection *ssec;
   1733 
   1734 	  isym = intsyms + ELF32_R_SYM (rel->r_info);
   1735 
   1736 	  if (isym->st_shndx == SHN_UNDEF)
   1737 	    ssec = bfd_und_section_ptr;
   1738 	  else if (isym->st_shndx == SHN_ABS)
   1739 	    ssec = bfd_abs_section_ptr;
   1740 	  else if (isym->st_shndx == SHN_COMMON)
   1741 	    ssec = bfd_com_section_ptr;
   1742 	  else
   1743 	    ssec = bfd_section_from_elf_index (abfd,
   1744 					       isym->st_shndx);
   1745 
   1746 	  /* Initial symbol value.  */
   1747 	  symval = isym->st_value;
   1748 
   1749 	  /* GAS may have made this symbol relative to a section, in
   1750 	     which case, we have to add the addend to find the
   1751 	     symbol.  */
   1752 	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   1753 	    symval += rel->r_addend;
   1754 
   1755 	  if (ssec)
   1756 	    {
   1757 	      if ((ssec->flags & SEC_MERGE)
   1758 		  && ssec->sec_info_type == SEC_INFO_TYPE_MERGE)
   1759 		symval = _bfd_merged_section_offset (abfd, & ssec,
   1760 						     elf_section_data (ssec)->sec_info,
   1761 						     symval);
   1762 	    }
   1763 
   1764 	  /* Now make the offset relative to where the linker is putting it.  */
   1765 	  if (ssec)
   1766 	    symval +=
   1767 	      ssec->output_section->vma + ssec->output_offset;
   1768 
   1769 	  symval += rel->r_addend;
   1770 	}
   1771       else
   1772 	{
   1773 	  unsigned long indx;
   1774 	  struct elf_link_hash_entry * h;
   1775 
   1776 	  /* An external symbol.  */
   1777 	  indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
   1778 	  h = elf_sym_hashes (abfd)[indx];
   1779 	  BFD_ASSERT (h != NULL);
   1780 
   1781 	  if (h->root.type != bfd_link_hash_defined
   1782 	      && h->root.type != bfd_link_hash_defweak)
   1783 	    {
   1784 	      /* This appears to be a reference to an undefined
   1785 		 symbol.  Just ignore it--it will be caught by the
   1786 		 regular reloc processing.  */
   1787 	      if (lrel)
   1788 		*lrel = rel;
   1789 	      return 0;
   1790 	    }
   1791 
   1792 	  symval = (h->root.u.def.value
   1793 		    + h->root.u.def.section->output_section->vma
   1794 		    + h->root.u.def.section->output_offset);
   1795 
   1796 	  symval += rel->r_addend;
   1797 	}
   1798 
   1799       switch (ELF32_R_TYPE (rel->r_info))
   1800 	{
   1801 	case R_RX_SYM:
   1802 	  RX_STACK_PUSH (symval);
   1803 	  break;
   1804 
   1805 	case R_RX_OPneg:
   1806 	  RX_STACK_POP (tmp1);
   1807 	  tmp1 = - tmp1;
   1808 	  RX_STACK_PUSH (tmp1);
   1809 	  break;
   1810 
   1811 	case R_RX_OPadd:
   1812 	  RX_STACK_POP (tmp1);
   1813 	  RX_STACK_POP (tmp2);
   1814 	  tmp1 += tmp2;
   1815 	  RX_STACK_PUSH (tmp1);
   1816 	  break;
   1817 
   1818 	case R_RX_OPsub:
   1819 	  RX_STACK_POP (tmp1);
   1820 	  RX_STACK_POP (tmp2);
   1821 	  tmp2 -= tmp1;
   1822 	  RX_STACK_PUSH (tmp2);
   1823 	  break;
   1824 
   1825 	case R_RX_OPmul:
   1826 	  RX_STACK_POP (tmp1);
   1827 	  RX_STACK_POP (tmp2);
   1828 	  tmp1 *= tmp2;
   1829 	  RX_STACK_PUSH (tmp1);
   1830 	  break;
   1831 
   1832 	case R_RX_OPdiv:
   1833 	  RX_STACK_POP (tmp1);
   1834 	  RX_STACK_POP (tmp2);
   1835 	  tmp1 /= tmp2;
   1836 	  RX_STACK_PUSH (tmp1);
   1837 	  break;
   1838 
   1839 	case R_RX_OPshla:
   1840 	  RX_STACK_POP (tmp1);
   1841 	  RX_STACK_POP (tmp2);
   1842 	  tmp1 <<= tmp2;
   1843 	  RX_STACK_PUSH (tmp1);
   1844 	  break;
   1845 
   1846 	case R_RX_OPshra:
   1847 	  RX_STACK_POP (tmp1);
   1848 	  RX_STACK_POP (tmp2);
   1849 	  tmp1 >>= tmp2;
   1850 	  RX_STACK_PUSH (tmp1);
   1851 	  break;
   1852 
   1853 	case R_RX_OPsctsize:
   1854 	  RX_STACK_PUSH (input_section->size);
   1855 	  break;
   1856 
   1857 	case R_RX_OPscttop:
   1858 	  RX_STACK_PUSH (input_section->output_section->vma);
   1859 	  break;
   1860 
   1861 	case R_RX_OPand:
   1862 	  RX_STACK_POP (tmp1);
   1863 	  RX_STACK_POP (tmp2);
   1864 	  tmp1 &= tmp2;
   1865 	  RX_STACK_PUSH (tmp1);
   1866 	  break;
   1867 
   1868 	case R_RX_OPor:
   1869 	  RX_STACK_POP (tmp1);
   1870 	  RX_STACK_POP (tmp2);
   1871 	  tmp1 |= tmp2;
   1872 	  RX_STACK_PUSH (tmp1);
   1873 	  break;
   1874 
   1875 	case R_RX_OPxor:
   1876 	  RX_STACK_POP (tmp1);
   1877 	  RX_STACK_POP (tmp2);
   1878 	  tmp1 ^= tmp2;
   1879 	  RX_STACK_PUSH (tmp1);
   1880 	  break;
   1881 
   1882 	case R_RX_OPnot:
   1883 	  RX_STACK_POP (tmp1);
   1884 	  tmp1 = ~ tmp1;
   1885 	  RX_STACK_PUSH (tmp1);
   1886 	  break;
   1887 
   1888 	case R_RX_OPmod:
   1889 	  RX_STACK_POP (tmp1);
   1890 	  RX_STACK_POP (tmp2);
   1891 	  tmp1 %= tmp2;
   1892 	  RX_STACK_PUSH (tmp1);
   1893 	  break;
   1894 
   1895 	case R_RX_OPromtop:
   1896 	  RX_STACK_PUSH (get_romstart (info, input_bfd, input_section, rel->r_offset));
   1897 	  break;
   1898 
   1899 	case R_RX_OPramtop:
   1900 	  RX_STACK_PUSH (get_ramstart (info, input_bfd, input_section, rel->r_offset));
   1901 	  break;
   1902 
   1903 	case R_RX_DIR16UL:
   1904 	case R_RX_DIR8UL:
   1905 	case R_RX_ABS16UL:
   1906 	case R_RX_ABS8UL:
   1907 	  if (rx_stack_top)
   1908 	    RX_STACK_POP (symval);
   1909 	  if (lrel)
   1910 	    *lrel = rel;
   1911 	  *scale = 4;
   1912 	  return symval;
   1913 
   1914 	case R_RX_DIR16UW:
   1915 	case R_RX_DIR8UW:
   1916 	case R_RX_ABS16UW:
   1917 	case R_RX_ABS8UW:
   1918 	  if (rx_stack_top)
   1919 	    RX_STACK_POP (symval);
   1920 	  if (lrel)
   1921 	    *lrel = rel;
   1922 	  *scale = 2;
   1923 	  return symval;
   1924 
   1925 	default:
   1926 	  if (rx_stack_top)
   1927 	    RX_STACK_POP (symval);
   1928 	  if (lrel)
   1929 	    *lrel = rel;
   1930 	  return symval;
   1931 	}
   1932 
   1933       rel ++;
   1934     }
   1935   /* FIXME.  */
   1936   (void) r;
   1937 }
   1938 
   1939 static void
   1940 move_reloc (Elf_Internal_Rela * irel, Elf_Internal_Rela * srel, int delta)
   1941 {
   1942   bfd_vma old_offset = srel->r_offset;
   1943 
   1944   irel ++;
   1945   while (irel <= srel)
   1946     {
   1947       if (irel->r_offset == old_offset)
   1948 	irel->r_offset += delta;
   1949       irel ++;
   1950     }
   1951 }
   1952 
   1953 /* Relax one section.  */
   1954 
   1955 static bfd_boolean
   1956 elf32_rx_relax_section (bfd *                  abfd,
   1957 			asection *             sec,
   1958 			struct bfd_link_info * link_info,
   1959 			bfd_boolean *          again,
   1960 			bfd_boolean            allow_pcrel3)
   1961 {
   1962   Elf_Internal_Shdr * symtab_hdr;
   1963   Elf_Internal_Shdr * shndx_hdr;
   1964   Elf_Internal_Rela * internal_relocs;
   1965   Elf_Internal_Rela * irel;
   1966   Elf_Internal_Rela * srel;
   1967   Elf_Internal_Rela * irelend;
   1968   Elf_Internal_Rela * next_alignment;
   1969   Elf_Internal_Rela * prev_alignment;
   1970   bfd_byte *          contents = NULL;
   1971   bfd_byte *          free_contents = NULL;
   1972   Elf_Internal_Sym *  intsyms = NULL;
   1973   Elf_Internal_Sym *  free_intsyms = NULL;
   1974   Elf_External_Sym_Shndx * shndx_buf = NULL;
   1975   bfd_vma pc;
   1976   bfd_vma sec_start;
   1977   bfd_vma symval = 0;
   1978   int pcrel = 0;
   1979   int code = 0;
   1980   int section_alignment_glue;
   1981   /* how much to scale the relocation by - 1, 2, or 4.  */
   1982   int scale;
   1983 
   1984   /* Assume nothing changes.  */
   1985   *again = FALSE;
   1986 
   1987   /* We don't have to do anything for a relocatable link, if
   1988      this section does not have relocs, or if this is not a
   1989      code section.  */
   1990   if (bfd_link_relocatable (link_info)
   1991       || (sec->flags & SEC_RELOC) == 0
   1992       || sec->reloc_count == 0
   1993       || (sec->flags & SEC_CODE) == 0)
   1994     return TRUE;
   1995 
   1996   symtab_hdr = & elf_symtab_hdr (abfd);
   1997   if (elf_symtab_shndx_list (abfd))
   1998     shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   1999   else
   2000     shndx_hdr = NULL;
   2001 
   2002   sec_start = sec->output_section->vma + sec->output_offset;
   2003 
   2004   /* Get the section contents.  */
   2005   if (elf_section_data (sec)->this_hdr.contents != NULL)
   2006     contents = elf_section_data (sec)->this_hdr.contents;
   2007   /* Go get them off disk.  */
   2008   else
   2009     {
   2010       if (! bfd_malloc_and_get_section (abfd, sec, &contents))
   2011 	goto error_return;
   2012       elf_section_data (sec)->this_hdr.contents = contents;
   2013     }
   2014 
   2015   /* Read this BFD's symbols.  */
   2016   /* Get cached copy if it exists.  */
   2017   if (symtab_hdr->contents != NULL)
   2018     intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   2019   else
   2020     {
   2021       intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
   2022       symtab_hdr->contents = (bfd_byte *) intsyms;
   2023     }
   2024 
   2025   if (shndx_hdr && shndx_hdr->sh_size != 0)
   2026     {
   2027       bfd_size_type amt;
   2028 
   2029       amt = symtab_hdr->sh_info;
   2030       amt *= sizeof (Elf_External_Sym_Shndx);
   2031       shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   2032       if (shndx_buf == NULL)
   2033 	goto error_return;
   2034       if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
   2035 	  || bfd_bread (shndx_buf, amt, abfd) != amt)
   2036 	goto error_return;
   2037       shndx_hdr->contents = (bfd_byte *) shndx_buf;
   2038     }
   2039 
   2040   /* Get a copy of the native relocations.  */
   2041   /* Note - we ignore the setting of link_info->keep_memory when reading
   2042      in these relocs.  We have to maintain a permanent copy of the relocs
   2043      because we are going to walk over them multiple times, adjusting them
   2044      as bytes are deleted from the section, and with this relaxation
   2045      function itself being called multiple times on the same section...  */
   2046   internal_relocs = _bfd_elf_link_read_relocs
   2047     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, TRUE);
   2048   if (internal_relocs == NULL)
   2049     goto error_return;
   2050 
   2051   /* The RL_ relocs must be just before the operand relocs they go
   2052      with, so we must sort them to guarantee this.  We use bubblesort
   2053      instead of qsort so we can guarantee that relocs with the same
   2054      address remain in the same relative order.  */
   2055   reloc_bubblesort (internal_relocs, sec->reloc_count);
   2056 
   2057   /* Walk through them looking for relaxing opportunities.  */
   2058   irelend = internal_relocs + sec->reloc_count;
   2059 
   2060   /* This will either be NULL or a pointer to the next alignment
   2061      relocation.  */
   2062   next_alignment = internal_relocs;
   2063   /* This will be the previous alignment, although at first it points
   2064      to the first real relocation.  */
   2065   prev_alignment = internal_relocs;
   2066 
   2067   /* We calculate worst case shrinkage caused by alignment directives.
   2068      No fool-proof, but better than either ignoring the problem or
   2069      doing heavy duty analysis of all the alignment markers in all
   2070      input sections.  */
   2071   section_alignment_glue = 0;
   2072   for (irel = internal_relocs; irel < irelend; irel++)
   2073       if (ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
   2074 	  && irel->r_addend & RX_RELAXA_ALIGN)
   2075 	{
   2076 	  int this_glue = 1 << (irel->r_addend & RX_RELAXA_ANUM);
   2077 
   2078 	  if (section_alignment_glue < this_glue)
   2079 	    section_alignment_glue = this_glue;
   2080 	}
   2081   /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
   2082      shrinkage.  */
   2083   section_alignment_glue *= 2;
   2084 
   2085   for (irel = internal_relocs; irel < irelend; irel++)
   2086     {
   2087       unsigned char *insn;
   2088       int nrelocs;
   2089 
   2090       /* The insns we care about are all marked with one of these.  */
   2091       if (ELF32_R_TYPE (irel->r_info) != R_RX_RH_RELAX)
   2092 	continue;
   2093 
   2094       if (irel->r_addend & RX_RELAXA_ALIGN
   2095 	  || next_alignment == internal_relocs)
   2096 	{
   2097 	  /* When we delete bytes, we need to maintain all the alignments
   2098 	     indicated.  In addition, we need to be careful about relaxing
   2099 	     jumps across alignment boundaries - these displacements
   2100 	     *grow* when we delete bytes.  For now, don't shrink
   2101 	     displacements across an alignment boundary, just in case.
   2102 	     Note that this only affects relocations to the same
   2103 	     section.  */
   2104 	  prev_alignment = next_alignment;
   2105 	  next_alignment += 2;
   2106 	  while (next_alignment < irelend
   2107 		 && (ELF32_R_TYPE (next_alignment->r_info) != R_RX_RH_RELAX
   2108 		     || !(next_alignment->r_addend & RX_RELAXA_ELIGN)))
   2109 	    next_alignment ++;
   2110 	  if (next_alignment >= irelend || next_alignment->r_offset == 0)
   2111 	    next_alignment = NULL;
   2112 	}
   2113 
   2114       /* When we hit alignment markers, see if we've shrunk enough
   2115 	 before them to reduce the gap without violating the alignment
   2116 	 requirements.  */
   2117       if (irel->r_addend & RX_RELAXA_ALIGN)
   2118 	{
   2119 	  /* At this point, the next relocation *should* be the ELIGN
   2120 	     end marker.  */
   2121 	  Elf_Internal_Rela *erel = irel + 1;
   2122 	  unsigned int alignment, nbytes;
   2123 
   2124 	  if (ELF32_R_TYPE (erel->r_info) != R_RX_RH_RELAX)
   2125 	    continue;
   2126 	  if (!(erel->r_addend & RX_RELAXA_ELIGN))
   2127 	    continue;
   2128 
   2129 	  alignment = 1 << (irel->r_addend & RX_RELAXA_ANUM);
   2130 
   2131 	  if (erel->r_offset - irel->r_offset < alignment)
   2132 	    continue;
   2133 
   2134 	  nbytes = erel->r_offset - irel->r_offset;
   2135 	  nbytes /= alignment;
   2136 	  nbytes *= alignment;
   2137 
   2138 	  elf32_rx_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment,
   2139 				       erel->r_offset == sec->size, internal_relocs);
   2140 	  *again = TRUE;
   2141 
   2142 	  continue;
   2143 	}
   2144 
   2145       if (irel->r_addend & RX_RELAXA_ELIGN)
   2146 	  continue;
   2147 
   2148       insn = contents + irel->r_offset;
   2149 
   2150       nrelocs = irel->r_addend & RX_RELAXA_RNUM;
   2151 
   2152       /* At this point, we have an insn that is a candidate for linker
   2153 	 relaxation.  There are NRELOCS relocs following that may be
   2154 	 relaxed, although each reloc may be made of more than one
   2155 	 reloc entry (such as gp-rel symbols).  */
   2156 
   2157       /* Get the value of the symbol referred to by the reloc.  Just
   2158          in case this is the last reloc in the list, use the RL's
   2159          addend to choose between this reloc (no addend) or the next
   2160          (yes addend, which means at least one following reloc).  */
   2161 
   2162       /* srel points to the "current" reloction for this insn -
   2163 	 actually the last reloc for a given operand, which is the one
   2164 	 we need to update.  We check the relaxations in the same
   2165 	 order that the relocations happen, so we'll just push it
   2166 	 along as we go.  */
   2167       srel = irel;
   2168 
   2169       pc = sec->output_section->vma + sec->output_offset
   2170 	+ srel->r_offset;
   2171 
   2172 #define GET_RELOC \
   2173       symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
   2174       pcrel = symval - pc + srel->r_addend; \
   2175       nrelocs --;
   2176 
   2177 #define SNIPNR(offset, nbytes) \
   2178       elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0, internal_relocs);
   2179 #define SNIP(offset, nbytes, newtype) \
   2180         SNIPNR (offset, nbytes);						\
   2181 	srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
   2182 
   2183       /* The order of these bit tests must match the order that the
   2184 	 relocs appear in.  Since we sorted those by offset, we can
   2185 	 predict them.  */
   2186 
   2187       /* Note that the numbers in, say, DSP6 are the bit offsets of
   2188 	 the code fields that describe the operand.  Bits number 0 for
   2189 	 the MSB of insn[0].  */
   2190 
   2191       /* DSP* codes:
   2192 	   0  00  [reg]
   2193 	   1  01  dsp:8[reg]
   2194 	   2  10  dsp:16[reg]
   2195 	   3  11  reg  */
   2196       if (irel->r_addend & RX_RELAXA_DSP6)
   2197 	{
   2198 	  GET_RELOC;
   2199 
   2200 	  code = insn[0] & 3;
   2201 	  if (code == 2 && symval/scale <= 255)
   2202 	    {
   2203 	      unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2204 	      insn[0] &= 0xfc;
   2205 	      insn[0] |= 0x01;
   2206 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2207 	      if (newrel != ELF32_R_TYPE (srel->r_info))
   2208 		{
   2209 		  SNIP (3, 1, newrel);
   2210 		  *again = TRUE;
   2211 		}
   2212 	    }
   2213 
   2214 	  else if (code == 1 && symval == 0)
   2215 	    {
   2216 	      insn[0] &= 0xfc;
   2217 	      SNIP (2, 1, R_RX_NONE);
   2218 	      *again = TRUE;
   2219 	    }
   2220 
   2221 	  /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst.  */
   2222 	  else if (code == 1 && symval/scale <= 31
   2223 		   /* Decodable bits.  */
   2224 		   && (insn[0] & 0xcc) == 0xcc
   2225 		   /* Width.  */
   2226 		   && (insn[0] & 0x30) != 0x30
   2227 		   /* Register MSBs.  */
   2228 		   && (insn[1] & 0x88)  == 0x00)
   2229 	    {
   2230 	      int newrel = 0;
   2231 
   2232 	      insn[0] = 0x88 | (insn[0] & 0x30);
   2233 	      /* The register fields are in the right place already.  */
   2234 
   2235 	      /* We can't relax this new opcode.  */
   2236 	      irel->r_addend = 0;
   2237 
   2238 	      switch ((insn[0] & 0x30) >> 4)
   2239 		{
   2240 		case 0:
   2241 		  newrel = R_RX_RH_ABS5p5B;
   2242 		  break;
   2243 		case 1:
   2244 		  newrel = R_RX_RH_ABS5p5W;
   2245 		  break;
   2246 		case 2:
   2247 		  newrel = R_RX_RH_ABS5p5L;
   2248 		  break;
   2249 		}
   2250 
   2251 	      move_reloc (irel, srel, -2);
   2252 	      SNIP (2, 1, newrel);
   2253 	    }
   2254 
   2255 	  /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst.  */
   2256 	  else if (code == 1 && symval/scale <= 31
   2257 		   /* Decodable bits.  */
   2258 		   && (insn[0] & 0xf8) == 0x58
   2259 		   /* Register MSBs.  */
   2260 		   && (insn[1] & 0x88)  == 0x00)
   2261 	    {
   2262 	      int newrel = 0;
   2263 
   2264 	      insn[0] = 0xb0 | ((insn[0] & 0x04) << 1);
   2265 	      /* The register fields are in the right place already.  */
   2266 
   2267 	      /* We can't relax this new opcode.  */
   2268 	      irel->r_addend = 0;
   2269 
   2270 	      switch ((insn[0] & 0x08) >> 3)
   2271 		{
   2272 		case 0:
   2273 		  newrel = R_RX_RH_ABS5p5B;
   2274 		  break;
   2275 		case 1:
   2276 		  newrel = R_RX_RH_ABS5p5W;
   2277 		  break;
   2278 		}
   2279 
   2280 	      move_reloc (irel, srel, -2);
   2281 	      SNIP (2, 1, newrel);
   2282 	    }
   2283 	}
   2284 
   2285       /* A DSP4 operand always follows a DSP6 operand, even if there's
   2286 	 no relocation for it.  We have to read the code out of the
   2287 	 opcode to calculate the offset of the operand.  */
   2288       if (irel->r_addend & RX_RELAXA_DSP4)
   2289 	{
   2290 	  int code6, offset = 0;
   2291 
   2292 	  GET_RELOC;
   2293 
   2294 	  code6 = insn[0] & 0x03;
   2295 	  switch (code6)
   2296 	    {
   2297 	    case 0: offset = 2; break;
   2298 	    case 1: offset = 3; break;
   2299 	    case 2: offset = 4; break;
   2300 	    case 3: offset = 2; break;
   2301 	    }
   2302 
   2303 	  code = (insn[0] & 0x0c) >> 2;
   2304 
   2305 	  if (code == 2 && symval / scale <= 255)
   2306 	    {
   2307 	      unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2308 
   2309 	      insn[0] &= 0xf3;
   2310 	      insn[0] |= 0x04;
   2311 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2312 	      if (newrel != ELF32_R_TYPE (srel->r_info))
   2313 		{
   2314 		  SNIP (offset+1, 1, newrel);
   2315 		  *again = TRUE;
   2316 		}
   2317 	    }
   2318 
   2319 	  else if (code == 1 && symval == 0)
   2320 	    {
   2321 	      insn[0] &= 0xf3;
   2322 	      SNIP (offset, 1, R_RX_NONE);
   2323 	      *again = TRUE;
   2324 	    }
   2325 	  /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
   2326 	  else if (code == 1 && symval/scale <= 31
   2327 		   /* Decodable bits.  */
   2328 		   && (insn[0] & 0xc3) == 0xc3
   2329 		   /* Width.  */
   2330 		   && (insn[0] & 0x30) != 0x30
   2331 		   /* Register MSBs.  */
   2332 		   && (insn[1] & 0x88)  == 0x00)
   2333 	    {
   2334 	      int newrel = 0;
   2335 
   2336 	      insn[0] = 0x80 | (insn[0] & 0x30);
   2337 	      /* The register fields are in the right place already.  */
   2338 
   2339 	      /* We can't relax this new opcode.  */
   2340 	      irel->r_addend = 0;
   2341 
   2342 	      switch ((insn[0] & 0x30) >> 4)
   2343 		{
   2344 		case 0:
   2345 		  newrel = R_RX_RH_ABS5p5B;
   2346 		  break;
   2347 		case 1:
   2348 		  newrel = R_RX_RH_ABS5p5W;
   2349 		  break;
   2350 		case 2:
   2351 		  newrel = R_RX_RH_ABS5p5L;
   2352 		  break;
   2353 		}
   2354 
   2355 	      move_reloc (irel, srel, -2);
   2356 	      SNIP (2, 1, newrel);
   2357 	    }
   2358 	}
   2359 
   2360       /* These always occur alone, but the offset depends on whether
   2361 	 it's a MEMEX opcode (0x06) or not.  */
   2362       if (irel->r_addend & RX_RELAXA_DSP14)
   2363 	{
   2364 	  int offset;
   2365 	  GET_RELOC;
   2366 
   2367 	  if (insn[0] == 0x06)
   2368 	    offset = 3;
   2369 	  else
   2370 	    offset = 4;
   2371 
   2372 	  code = insn[1] & 3;
   2373 
   2374 	  if (code == 2 && symval / scale <= 255)
   2375 	    {
   2376 	      unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2377 
   2378 	      insn[1] &= 0xfc;
   2379 	      insn[1] |= 0x01;
   2380 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2381 	      if (newrel != ELF32_R_TYPE (srel->r_info))
   2382 		{
   2383 		  SNIP (offset, 1, newrel);
   2384 		  *again = TRUE;
   2385 		}
   2386 	    }
   2387 	  else if (code == 1 && symval == 0)
   2388 	    {
   2389 	      insn[1] &= 0xfc;
   2390 	      SNIP (offset, 1, R_RX_NONE);
   2391 	      *again = TRUE;
   2392 	    }
   2393 	}
   2394 
   2395       /* IMM* codes:
   2396 	   0  00  imm:32
   2397 	   1  01  simm:8
   2398 	   2  10  simm:16
   2399 	   3  11  simm:24.  */
   2400 
   2401       /* These always occur alone.  */
   2402       if (irel->r_addend & RX_RELAXA_IMM6)
   2403 	{
   2404 	  long ssymval;
   2405 
   2406 	  GET_RELOC;
   2407 
   2408 	  /* These relocations sign-extend, so we must do signed compares.  */
   2409 	  ssymval = (long) symval;
   2410 
   2411 	  code = insn[0] & 0x03;
   2412 
   2413 	  if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
   2414 	    {
   2415 	      unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2416 
   2417 	      insn[0] &= 0xfc;
   2418 	      insn[0] |= 0x03;
   2419 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2420 	      if (newrel != ELF32_R_TYPE (srel->r_info))
   2421 		{
   2422 		  SNIP (2, 1, newrel);
   2423 		  *again = TRUE;
   2424 		}
   2425 	    }
   2426 
   2427 	  else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
   2428 	    {
   2429 	      unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2430 
   2431 	      insn[0] &= 0xfc;
   2432 	      insn[0] |= 0x02;
   2433 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2434 	      if (newrel != ELF32_R_TYPE (srel->r_info))
   2435 		{
   2436 		  SNIP (2, 1, newrel);
   2437 		  *again = TRUE;
   2438 		}
   2439 	    }
   2440 
   2441 	  /* Special case UIMM8 format: CMP #uimm8,Rdst.  */
   2442 	  else if (code == 2 && ssymval <= 255 && ssymval >= 16
   2443 		   /* Decodable bits.  */
   2444 		   && (insn[0] & 0xfc) == 0x74
   2445 		   /* Decodable bits.  */
   2446 		   && ((insn[1] & 0xf0) == 0x00))
   2447 	    {
   2448 	      int newrel;
   2449 
   2450 	      insn[0] = 0x75;
   2451 	      insn[1] = 0x50 | (insn[1] & 0x0f);
   2452 
   2453 	      /* We can't relax this new opcode.  */
   2454 	      irel->r_addend = 0;
   2455 
   2456 	      if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
   2457 		newrel = R_RX_ABS8U;
   2458 	      else
   2459 		newrel = R_RX_DIR8U;
   2460 
   2461 	      SNIP (2, 1, newrel);
   2462 	      *again = TRUE;
   2463 	    }
   2464 
   2465 	  else if (code == 2 && ssymval <= 127 && ssymval >= -128)
   2466 	    {
   2467 	      unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2468 
   2469 	      insn[0] &= 0xfc;
   2470 	      insn[0] |= 0x01;
   2471 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2472 	      if (newrel != ELF32_R_TYPE (srel->r_info))
   2473 		{
   2474 		  SNIP (2, 1, newrel);
   2475 		  *again = TRUE;
   2476 		}
   2477 	    }
   2478 
   2479 	  /* Special case UIMM4 format: CMP, MUL, AND, OR.  */
   2480 	  else if (code == 1 && ssymval <= 15 && ssymval >= 0
   2481 		   /* Decodable bits and immediate type.  */
   2482 		   && insn[0] == 0x75
   2483 		   /* Decodable bits.  */
   2484 		   && (insn[1] & 0xc0)  == 0x00)
   2485 	    {
   2486 	      static const int newop[4] = { 1, 3, 4, 5 };
   2487 
   2488 	      insn[0] = 0x60 | newop[insn[1] >> 4];
   2489 	      /* The register number doesn't move.  */
   2490 
   2491 	      /* We can't relax this new opcode.  */
   2492 	      irel->r_addend = 0;
   2493 
   2494 	      move_reloc (irel, srel, -1);
   2495 
   2496 	      SNIP (2, 1, R_RX_RH_UIMM4p8);
   2497 	      *again = TRUE;
   2498 	    }
   2499 
   2500 	  /* Special case UIMM4 format: ADD -> ADD/SUB.  */
   2501 	  else if (code == 1 && ssymval <= 15 && ssymval >= -15
   2502 		   /* Decodable bits and immediate type.  */
   2503 		   && insn[0] == 0x71
   2504 		   /* Same register for source and destination.  */
   2505 		   && ((insn[1] >> 4) == (insn[1] & 0x0f)))
   2506 	    {
   2507 	      int newrel;
   2508 
   2509 	      /* Note that we can't turn "add $0,Rs" into a NOP
   2510 		 because the flags need to be set right.  */
   2511 
   2512 	      if (ssymval < 0)
   2513 		{
   2514 		  insn[0] = 0x60; /* Subtract.  */
   2515 		  newrel = R_RX_RH_UNEG4p8;
   2516 		}
   2517 	      else
   2518 		{
   2519 		  insn[0] = 0x62; /* Add.  */
   2520 		  newrel = R_RX_RH_UIMM4p8;
   2521 		}
   2522 
   2523 	      /* The register number is in the right place.  */
   2524 
   2525 	      /* We can't relax this new opcode.  */
   2526 	      irel->r_addend = 0;
   2527 
   2528 	      move_reloc (irel, srel, -1);
   2529 
   2530 	      SNIP (2, 1, newrel);
   2531 	      *again = TRUE;
   2532 	    }
   2533 	}
   2534 
   2535       /* These are either matched with a DSP6 (2-byte base) or an id24
   2536 	 (3-byte base).  */
   2537       if (irel->r_addend & RX_RELAXA_IMM12)
   2538 	{
   2539 	  int dspcode, offset = 0;
   2540 	  long ssymval;
   2541 
   2542 	  GET_RELOC;
   2543 
   2544 	  if ((insn[0] & 0xfc) == 0xfc)
   2545 	    dspcode = 1; /* Just something with one byte operand.  */
   2546 	  else
   2547 	    dspcode = insn[0] & 3;
   2548 	  switch (dspcode)
   2549 	    {
   2550 	    case 0: offset = 2; break;
   2551 	    case 1: offset = 3; break;
   2552 	    case 2: offset = 4; break;
   2553 	    case 3: offset = 2; break;
   2554 	    }
   2555 
   2556 	  /* These relocations sign-extend, so we must do signed compares.  */
   2557 	  ssymval = (long) symval;
   2558 
   2559 	  code = (insn[1] >> 2) & 3;
   2560 	  if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
   2561 	    {
   2562 	      unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2563 
   2564 	      insn[1] &= 0xf3;
   2565 	      insn[1] |= 0x0c;
   2566 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2567 	      if (newrel != ELF32_R_TYPE (srel->r_info))
   2568 		{
   2569 		  SNIP (offset, 1, newrel);
   2570 		  *again = TRUE;
   2571 		}
   2572 	    }
   2573 
   2574 	  else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
   2575 	    {
   2576 	      unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2577 
   2578 	      insn[1] &= 0xf3;
   2579 	      insn[1] |= 0x08;
   2580 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2581 	      if (newrel != ELF32_R_TYPE (srel->r_info))
   2582 		{
   2583 		  SNIP (offset, 1, newrel);
   2584 		  *again = TRUE;
   2585 		}
   2586 	    }
   2587 
   2588 	  /* Special case UIMM8 format: MOV #uimm8,Rdst.  */
   2589 	  else if (code == 2 && ssymval <= 255 && ssymval >= 16
   2590 		   /* Decodable bits.  */
   2591 		   && insn[0] == 0xfb
   2592 		   /* Decodable bits.  */
   2593 		   && ((insn[1] & 0x03) == 0x02))
   2594 	    {
   2595 	      int newrel;
   2596 
   2597 	      insn[0] = 0x75;
   2598 	      insn[1] = 0x40 | (insn[1] >> 4);
   2599 
   2600 	      /* We can't relax this new opcode.  */
   2601 	      irel->r_addend = 0;
   2602 
   2603 	      if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
   2604 		newrel = R_RX_ABS8U;
   2605 	      else
   2606 		newrel = R_RX_DIR8U;
   2607 
   2608 	      SNIP (2, 1, newrel);
   2609 	      *again = TRUE;
   2610 	    }
   2611 
   2612 	  else if (code == 2 && ssymval <= 127 && ssymval >= -128)
   2613 	    {
   2614 	      unsigned int newrel = ELF32_R_TYPE(srel->r_info);
   2615 
   2616 	      insn[1] &= 0xf3;
   2617 	      insn[1] |= 0x04;
   2618 	      newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2619 	      if (newrel != ELF32_R_TYPE(srel->r_info))
   2620 		{
   2621 		  SNIP (offset, 1, newrel);
   2622 		  *again = TRUE;
   2623 		}
   2624 	    }
   2625 
   2626 	  /* Special case UIMM4 format: MOV #uimm4,Rdst.  */
   2627 	  else if (code == 1 && ssymval <= 15 && ssymval >= 0
   2628 		   /* Decodable bits.  */
   2629 		   && insn[0] == 0xfb
   2630 		   /* Decodable bits.  */
   2631 		   && ((insn[1] & 0x03) == 0x02))
   2632 	    {
   2633 	      insn[0] = 0x66;
   2634 	      insn[1] = insn[1] >> 4;
   2635 
   2636 	      /* We can't relax this new opcode.  */
   2637 	      irel->r_addend = 0;
   2638 
   2639 	      move_reloc (irel, srel, -1);
   2640 
   2641 	      SNIP (2, 1, R_RX_RH_UIMM4p8);
   2642 	      *again = TRUE;
   2643 	    }
   2644 	}
   2645 
   2646       if (irel->r_addend & RX_RELAXA_BRA)
   2647 	{
   2648 	  unsigned int newrel = ELF32_R_TYPE (srel->r_info);
   2649 	  int max_pcrel3 = 4;
   2650 	  int alignment_glue = 0;
   2651 
   2652 	  GET_RELOC;
   2653 
   2654 	  /* Branches over alignment chunks are problematic, as
   2655 	     deleting bytes here makes the branch *further* away.  We
   2656 	     can be agressive with branches within this alignment
   2657 	     block, but not branches outside it.  */
   2658 	  if ((prev_alignment == NULL
   2659 	       || symval < (bfd_vma)(sec_start + prev_alignment->r_offset))
   2660 	      && (next_alignment == NULL
   2661 		  || symval > (bfd_vma)(sec_start + next_alignment->r_offset)))
   2662 	    alignment_glue = section_alignment_glue;
   2663 
   2664 	  if (ELF32_R_TYPE(srel[1].r_info) == R_RX_RH_RELAX
   2665 	      && srel[1].r_addend & RX_RELAXA_BRA
   2666 	      && srel[1].r_offset < irel->r_offset + pcrel)
   2667 	    max_pcrel3 ++;
   2668 
   2669 	  newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
   2670 
   2671 	  /* The values we compare PCREL with are not what you'd
   2672 	     expect; they're off by a little to compensate for (1)
   2673 	     where the reloc is relative to the insn, and (2) how much
   2674 	     the insn is going to change when we relax it.  */
   2675 
   2676 	  /* These we have to decode.  */
   2677 	  switch (insn[0])
   2678 	    {
   2679 	    case 0x04: /* BRA pcdsp:24 */
   2680 	      if (-32768 + alignment_glue <= pcrel
   2681 		  && pcrel <= 32765 - alignment_glue)
   2682 		{
   2683 		  insn[0] = 0x38;
   2684 		  SNIP (3, 1, newrel);
   2685 		  *again = TRUE;
   2686 		}
   2687 	      break;
   2688 
   2689 	    case 0x38: /* BRA pcdsp:16 */
   2690 	      if (-128 + alignment_glue <= pcrel
   2691 		  && pcrel <= 127 - alignment_glue)
   2692 		{
   2693 		  insn[0] = 0x2e;
   2694 		  SNIP (2, 1, newrel);
   2695 		  *again = TRUE;
   2696 		}
   2697 	      break;
   2698 
   2699 	    case 0x2e: /* BRA pcdsp:8 */
   2700 	      /* Note that there's a risk here of shortening things so
   2701 		 much that we no longer fit this reloc; it *should*
   2702 		 only happen when you branch across a branch, and that
   2703 		 branch also devolves into BRA.S.  "Real" code should
   2704 		 be OK.  */
   2705 	      if (max_pcrel3 + alignment_glue <= pcrel
   2706 		  && pcrel <= 10 - alignment_glue
   2707 		  && allow_pcrel3)
   2708 		{
   2709 		  insn[0] = 0x08;
   2710 		  SNIP (1, 1, newrel);
   2711 		  move_reloc (irel, srel, -1);
   2712 		  *again = TRUE;
   2713 		}
   2714 	      break;
   2715 
   2716 	    case 0x05: /* BSR pcdsp:24 */
   2717 	      if (-32768 + alignment_glue <= pcrel
   2718 		  && pcrel <= 32765 - alignment_glue)
   2719 		{
   2720 		  insn[0] = 0x39;
   2721 		  SNIP (1, 1, newrel);
   2722 		  *again = TRUE;
   2723 		}
   2724 	      break;
   2725 
   2726 	    case 0x3a: /* BEQ.W pcdsp:16 */
   2727 	    case 0x3b: /* BNE.W pcdsp:16 */
   2728 	      if (-128 + alignment_glue <= pcrel
   2729 		  && pcrel <= 127 - alignment_glue)
   2730 		{
   2731 		  insn[0] = 0x20 | (insn[0] & 1);
   2732 		  SNIP (1, 1, newrel);
   2733 		  *again = TRUE;
   2734 		}
   2735 	      break;
   2736 
   2737 	    case 0x20: /* BEQ.B pcdsp:8 */
   2738 	    case 0x21: /* BNE.B pcdsp:8 */
   2739 	      if (max_pcrel3 + alignment_glue <= pcrel
   2740 		  && pcrel - alignment_glue <= 10
   2741 		  && allow_pcrel3)
   2742 		{
   2743 		  insn[0] = 0x10 | ((insn[0] & 1) << 3);
   2744 		  SNIP (1, 1, newrel);
   2745 		  move_reloc (irel, srel, -1);
   2746 		  *again = TRUE;
   2747 		}
   2748 	      break;
   2749 
   2750 	    case 0x16: /* synthetic BNE dsp24 */
   2751 	    case 0x1e: /* synthetic BEQ dsp24 */
   2752 	      if (-32767 + alignment_glue <= pcrel
   2753 		  && pcrel <= 32766 - alignment_glue
   2754 		  && insn[1] == 0x04)
   2755 		{
   2756 		  if (insn[0] == 0x16)
   2757 		    insn[0] = 0x3b;
   2758 		  else
   2759 		    insn[0] = 0x3a;
   2760 		  /* We snip out the bytes at the end else the reloc
   2761 		     will get moved too, and too much.  */
   2762 		  SNIP (3, 2, newrel);
   2763 		  move_reloc (irel, srel, -1);
   2764 		  *again = TRUE;
   2765 		}
   2766 	      break;
   2767 	    }
   2768 
   2769 	  /* Special case - synthetic conditional branches, pcrel24.
   2770 	     Note that EQ and NE have been handled above.  */
   2771 	  if ((insn[0] & 0xf0) == 0x20
   2772 	      && insn[1] == 0x06
   2773 	      && insn[2] == 0x04
   2774 	      && srel->r_offset != irel->r_offset + 1
   2775 	      && -32767 + alignment_glue <= pcrel
   2776 	      && pcrel <= 32766 - alignment_glue)
   2777 	    {
   2778 	      insn[1] = 0x05;
   2779 	      insn[2] = 0x38;
   2780 	      SNIP (5, 1, newrel);
   2781 	      *again = TRUE;
   2782 	    }
   2783 
   2784 	  /* Special case - synthetic conditional branches, pcrel16 */
   2785 	  if ((insn[0] & 0xf0) == 0x20
   2786 	      && insn[1] == 0x05
   2787 	      && insn[2] == 0x38
   2788 	      && srel->r_offset != irel->r_offset + 1
   2789 	      && -127 + alignment_glue <= pcrel
   2790 	      && pcrel <= 126 - alignment_glue)
   2791 	    {
   2792 	      int cond = (insn[0] & 0x0f) ^ 0x01;
   2793 
   2794 	      insn[0] = 0x20 | cond;
   2795 	      /* By moving the reloc first, we avoid having
   2796 		 delete_bytes move it also.  */
   2797 	      move_reloc (irel, srel, -2);
   2798 	      SNIP (2, 3, newrel);
   2799 	      *again = TRUE;
   2800 	    }
   2801 	}
   2802 
   2803       BFD_ASSERT (nrelocs == 0);
   2804 
   2805       /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
   2806 	 use MOV.bwl #uimm:8, dsp:5[r7] format.  This is tricky
   2807 	 because it may have one or two relocations.  */
   2808       if ((insn[0] & 0xfc) == 0xf8
   2809 	  && (insn[1] & 0x80) == 0x00
   2810 	  && (insn[0] & 0x03) != 0x03)
   2811 	{
   2812 	  int dcode, icode, reg, ioff, dscale, ilen;
   2813 	  bfd_vma disp_val = 0;
   2814 	  long imm_val = 0;
   2815 	  Elf_Internal_Rela * disp_rel = 0;
   2816 	  Elf_Internal_Rela * imm_rel = 0;
   2817 
   2818 	  /* Reset this.  */
   2819 	  srel = irel;
   2820 
   2821 	  dcode = insn[0] & 0x03;
   2822 	  icode = (insn[1] >> 2) & 0x03;
   2823 	  reg = (insn[1] >> 4) & 0x0f;
   2824 
   2825 	  ioff = dcode == 1 ? 3 : dcode == 2 ? 4 : 2;
   2826 
   2827 	  /* Figure out what the dispacement is.  */
   2828 	  if (dcode == 1 || dcode == 2)
   2829 	    {
   2830 	      /* There's a displacement.  See if there's a reloc for it.  */
   2831 	      if (srel[1].r_offset == irel->r_offset + 2)
   2832 		{
   2833 		  GET_RELOC;
   2834 		  disp_val = symval;
   2835 		  disp_rel = srel;
   2836 		}
   2837 	      else
   2838 		{
   2839 		  if (dcode == 1)
   2840 		    disp_val = insn[2];
   2841 		  else
   2842 		    {
   2843 #if RX_OPCODE_BIG_ENDIAN
   2844 		      disp_val = insn[2] * 256 + insn[3];
   2845 #else
   2846 		      disp_val = insn[2] + insn[3] * 256;
   2847 #endif
   2848 		    }
   2849 		  switch (insn[1] & 3)
   2850 		    {
   2851 		    case 1:
   2852 		      disp_val *= 2;
   2853 		      scale = 2;
   2854 		      break;
   2855 		    case 2:
   2856 		      disp_val *= 4;
   2857 		      scale = 4;
   2858 		      break;
   2859 		    }
   2860 		}
   2861 	    }
   2862 
   2863 	  dscale = scale;
   2864 
   2865 	  /* Figure out what the immediate is.  */
   2866 	  if (srel[1].r_offset == irel->r_offset + ioff)
   2867 	    {
   2868 	      GET_RELOC;
   2869 	      imm_val = (long) symval;
   2870 	      imm_rel = srel;
   2871 	    }
   2872 	  else
   2873 	    {
   2874 	      unsigned char * ip = insn + ioff;
   2875 
   2876 	      switch (icode)
   2877 		{
   2878 		case 1:
   2879 		  /* For byte writes, we don't sign extend.  Makes the math easier later.  */
   2880 		  if (scale == 1)
   2881 		    imm_val = ip[0];
   2882 		  else
   2883 		    imm_val = (char) ip[0];
   2884 		  break;
   2885 		case 2:
   2886 #if RX_OPCODE_BIG_ENDIAN
   2887 		  imm_val = ((char) ip[0] << 8) | ip[1];
   2888 #else
   2889 		  imm_val = ((char) ip[1] << 8) | ip[0];
   2890 #endif
   2891 		  break;
   2892 		case 3:
   2893 #if RX_OPCODE_BIG_ENDIAN
   2894 		  imm_val = ((char) ip[0] << 16) | (ip[1] << 8) | ip[2];
   2895 #else
   2896 		  imm_val = ((char) ip[2] << 16) | (ip[1] << 8) | ip[0];
   2897 #endif
   2898 		  break;
   2899 		case 0:
   2900 #if RX_OPCODE_BIG_ENDIAN
   2901 		  imm_val = (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];
   2902 #else
   2903 		  imm_val = (ip[3] << 24) | (ip[2] << 16) | (ip[1] << 8) | ip[0];
   2904 #endif
   2905 		  break;
   2906 		}
   2907 	    }
   2908 
   2909 	  ilen = 2;
   2910 
   2911 	  switch (dcode)
   2912 	    {
   2913 	    case 1:
   2914 	      ilen += 1;
   2915 	      break;
   2916 	    case 2:
   2917 	      ilen += 2;
   2918 	      break;
   2919 	    }
   2920 
   2921 	  switch (icode)
   2922 	    {
   2923 	    case 1:
   2924 	      ilen += 1;
   2925 	      break;
   2926 	    case 2:
   2927 	      ilen += 2;
   2928 	      break;
   2929 	    case 3:
   2930 	      ilen += 3;
   2931 	      break;
   2932 	    case 4:
   2933 	      ilen += 4;
   2934 	      break;
   2935 	    }
   2936 
   2937 	  /* The shortcut happens when the immediate is 0..255,
   2938 	     register r0 to r7, and displacement (scaled) 0..31.  */
   2939 
   2940 	  if (0 <= imm_val && imm_val <= 255
   2941 	      && 0 <= reg && reg <= 7
   2942 	      && disp_val / dscale <= 31)
   2943 	    {
   2944 	      insn[0] = 0x3c | (insn[1] & 0x03);
   2945 	      insn[1] = (((disp_val / dscale) << 3) & 0x80) | (reg << 4) | ((disp_val/dscale) & 0x0f);
   2946 	      insn[2] = imm_val;
   2947 
   2948 	      if (disp_rel)
   2949 		{
   2950 		  int newrel = R_RX_NONE;
   2951 
   2952 		  switch (dscale)
   2953 		    {
   2954 		    case 1:
   2955 		      newrel = R_RX_RH_ABS5p8B;
   2956 		      break;
   2957 		    case 2:
   2958 		      newrel = R_RX_RH_ABS5p8W;
   2959 		      break;
   2960 		    case 4:
   2961 		      newrel = R_RX_RH_ABS5p8L;
   2962 		      break;
   2963 		    }
   2964 		  disp_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (disp_rel->r_info), newrel);
   2965 		  move_reloc (irel, disp_rel, -1);
   2966 		}
   2967 	      if (imm_rel)
   2968 		{
   2969 		  imm_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (imm_rel->r_info), R_RX_DIR8U);
   2970 		  move_reloc (disp_rel ? disp_rel : irel,
   2971 			      imm_rel,
   2972 			      irel->r_offset - imm_rel->r_offset + 2);
   2973 		}
   2974 
   2975 	      SNIPNR (3, ilen - 3);
   2976 	      *again = TRUE;
   2977 
   2978 	      /* We can't relax this new opcode.  */
   2979 	      irel->r_addend = 0;
   2980 	    }
   2981 	}
   2982     }
   2983 
   2984   /* We can't reliably relax branches to DIR3U_PCREL unless we know
   2985      whatever they're branching over won't shrink any more.  If we're
   2986      basically done here, do one more pass just for branches - but
   2987      don't request a pass after that one!  */
   2988   if (!*again && !allow_pcrel3)
   2989     {
   2990       bfd_boolean ignored;
   2991 
   2992       elf32_rx_relax_section (abfd, sec, link_info, &ignored, TRUE);
   2993     }
   2994 
   2995   return TRUE;
   2996 
   2997  error_return:
   2998   if (free_contents != NULL)
   2999     free (free_contents);
   3000 
   3001   if (shndx_buf != NULL)
   3002     {
   3003       shndx_hdr->contents = NULL;
   3004       free (shndx_buf);
   3005     }
   3006 
   3007   if (free_intsyms != NULL)
   3008     free (free_intsyms);
   3009 
   3010   return FALSE;
   3011 }
   3012 
   3013 static bfd_boolean
   3014 elf32_rx_relax_section_wrapper (bfd *                  abfd,
   3015 				asection *             sec,
   3016 				struct bfd_link_info * link_info,
   3017 				bfd_boolean *          again)
   3018 {
   3019   return elf32_rx_relax_section (abfd, sec, link_info, again, FALSE);
   3020 }
   3021 
   3022 /* Function to set the ELF flag bits.  */
   3024 
   3025 static bfd_boolean
   3026 rx_elf_set_private_flags (bfd * abfd, flagword flags)
   3027 {
   3028   elf_elfheader (abfd)->e_flags = flags;
   3029   elf_flags_init (abfd) = TRUE;
   3030   return TRUE;
   3031 }
   3032 
   3033 static bfd_boolean no_warn_mismatch = FALSE;
   3034 static bfd_boolean ignore_lma = TRUE;
   3035 
   3036 void bfd_elf32_rx_set_target_flags (bfd_boolean, bfd_boolean);
   3037 
   3038 void
   3039 bfd_elf32_rx_set_target_flags (bfd_boolean user_no_warn_mismatch,
   3040 			       bfd_boolean user_ignore_lma)
   3041 {
   3042   no_warn_mismatch = user_no_warn_mismatch;
   3043   ignore_lma = user_ignore_lma;
   3044 }
   3045 
   3046 /* Converts FLAGS into a descriptive string.
   3047    Returns a static pointer.  */
   3048 
   3049 static const char *
   3050 describe_flags (flagword flags)
   3051 {
   3052   static char buf [128];
   3053 
   3054   buf[0] = 0;
   3055 
   3056   if (flags & E_FLAG_RX_64BIT_DOUBLES)
   3057     strcat (buf, "64-bit doubles");
   3058   else
   3059     strcat (buf, "32-bit doubles");
   3060 
   3061   if (flags & E_FLAG_RX_DSP)
   3062     strcat (buf, ", dsp");
   3063   else
   3064     strcat (buf, ", no dsp");
   3065 
   3066   if (flags & E_FLAG_RX_PID)
   3067     strcat (buf, ", pid");
   3068   else
   3069     strcat (buf, ", no pid");
   3070 
   3071   if (flags & E_FLAG_RX_ABI)
   3072     strcat (buf, ", RX ABI");
   3073   else
   3074     strcat (buf, ", GCC ABI");
   3075 
   3076   if (flags & E_FLAG_RX_SINSNS_SET)
   3077     strcat (buf, flags & E_FLAG_RX_SINSNS_YES ? ", uses String instructions" : ", bans String instructions");
   3078 
   3079   return buf;
   3080 }
   3081 
   3082 /* Merge backend specific data from an object file to the output
   3083    object file when linking.  */
   3084 
   3085 static bfd_boolean
   3086 rx_elf_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
   3087 {
   3088   flagword old_flags;
   3089   flagword new_flags;
   3090   bfd_boolean error = FALSE;
   3091 
   3092   new_flags = elf_elfheader (ibfd)->e_flags;
   3093   old_flags = elf_elfheader (obfd)->e_flags;
   3094 
   3095   if (!elf_flags_init (obfd))
   3096     {
   3097       /* First call, no flags set.  */
   3098       elf_flags_init (obfd) = TRUE;
   3099       elf_elfheader (obfd)->e_flags = new_flags;
   3100     }
   3101   else if (old_flags != new_flags)
   3102     {
   3103       flagword known_flags;
   3104 
   3105       if (old_flags & E_FLAG_RX_SINSNS_SET)
   3106 	{
   3107 	  if ((new_flags & E_FLAG_RX_SINSNS_SET) == 0)
   3108 	    {
   3109 	      new_flags &= ~ E_FLAG_RX_SINSNS_MASK;
   3110 	      new_flags |= (old_flags & E_FLAG_RX_SINSNS_MASK);
   3111 	    }
   3112 	}
   3113       else if (new_flags & E_FLAG_RX_SINSNS_SET)
   3114 	{
   3115 	  old_flags &= ~ E_FLAG_RX_SINSNS_MASK;
   3116 	  old_flags |= (new_flags & E_FLAG_RX_SINSNS_MASK);
   3117 	}
   3118 
   3119       known_flags = E_FLAG_RX_ABI | E_FLAG_RX_64BIT_DOUBLES
   3120 	| E_FLAG_RX_DSP | E_FLAG_RX_PID | E_FLAG_RX_SINSNS_MASK;
   3121 
   3122       if ((old_flags ^ new_flags) & known_flags)
   3123 	{
   3124 	  /* Only complain if flag bits we care about do not match.
   3125 	     Other bits may be set, since older binaries did use some
   3126 	     deprecated flags.  */
   3127 	  if (no_warn_mismatch)
   3128 	    {
   3129 	      elf_elfheader (obfd)->e_flags = (new_flags | old_flags) & known_flags;
   3130 	    }
   3131 	  else
   3132 	    {
   3133 	      _bfd_error_handler ("There is a conflict merging the ELF header flags from %s",
   3134 				  bfd_get_filename (ibfd));
   3135 	      _bfd_error_handler ("  the input  file's flags: %s",
   3136 				  describe_flags (new_flags));
   3137 	      _bfd_error_handler ("  the output file's flags: %s",
   3138 				  describe_flags (old_flags));
   3139 	      error = TRUE;
   3140 	    }
   3141 	}
   3142       else
   3143 	elf_elfheader (obfd)->e_flags = new_flags & known_flags;
   3144     }
   3145 
   3146   if (error)
   3147     bfd_set_error (bfd_error_bad_value);
   3148 
   3149   return !error;
   3150 }
   3151 
   3152 static bfd_boolean
   3154 rx_elf_print_private_bfd_data (bfd * abfd, void * ptr)
   3155 {
   3156   FILE * file = (FILE *) ptr;
   3157   flagword flags;
   3158 
   3159   BFD_ASSERT (abfd != NULL && ptr != NULL);
   3160 
   3161   /* Print normal ELF private data.  */
   3162   _bfd_elf_print_private_bfd_data (abfd, ptr);
   3163 
   3164   flags = elf_elfheader (abfd)->e_flags;
   3165   fprintf (file, _("private flags = 0x%lx:"), (long) flags);
   3166 
   3167   fprintf (file, "%s", describe_flags (flags));
   3168   return TRUE;
   3169 }
   3170 
   3171 /* Return the MACH for an e_flags value.  */
   3172 
   3173 static int
   3174 elf32_rx_machine (bfd * abfd ATTRIBUTE_UNUSED)
   3175 {
   3176 #if 0 /* FIXME: EF_RX_CPU_MASK collides with E_FLAG_RX_...
   3177 	 Need to sort out how these flag bits are used.
   3178          For now we assume that the flags are OK.  */
   3179   if ((elf_elfheader (abfd)->e_flags & EF_RX_CPU_MASK) == EF_RX_CPU_RX)
   3180 #endif
   3181     return bfd_mach_rx;
   3182 
   3183   return 0;
   3184 }
   3185 
   3186 static bfd_boolean
   3187 rx_elf_object_p (bfd * abfd)
   3188 {
   3189   int i;
   3190   unsigned int u;
   3191   Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
   3192   int nphdrs = elf_elfheader (abfd)->e_phnum;
   3193   sec_ptr bsec;
   3194   static int saw_be = FALSE;
   3195 
   3196   /* We never want to automatically choose the non-swapping big-endian
   3197      target.  The user can only get that explicitly, such as with -I
   3198      and objcopy.  */
   3199   if (abfd->xvec == &rx_elf32_be_ns_vec
   3200       && abfd->target_defaulted)
   3201     return FALSE;
   3202 
   3203   /* BFD->target_defaulted is not set to TRUE when a target is chosen
   3204      as a fallback, so we check for "scanning" to know when to stop
   3205      using the non-swapping target.  */
   3206   if (abfd->xvec == &rx_elf32_be_ns_vec
   3207       && saw_be)
   3208     return FALSE;
   3209   if (abfd->xvec == &rx_elf32_be_vec)
   3210     saw_be = TRUE;
   3211 
   3212   bfd_default_set_arch_mach (abfd, bfd_arch_rx,
   3213 			     elf32_rx_machine (abfd));
   3214 
   3215   /* For each PHDR in the object, we must find some section that
   3216      corresponds (based on matching file offsets) and use its VMA
   3217      information to reconstruct the p_vaddr field we clobbered when we
   3218      wrote it out.  */
   3219   for (i=0; i<nphdrs; i++)
   3220     {
   3221       for (u=0; u<elf_tdata(abfd)->num_elf_sections; u++)
   3222 	{
   3223 	  Elf_Internal_Shdr *sec = elf_tdata(abfd)->elf_sect_ptr[u];
   3224 
   3225 	  if (phdr[i].p_filesz
   3226 	      && phdr[i].p_offset <= (bfd_vma) sec->sh_offset
   3227 	      && sec->sh_size > 0
   3228 	      && sec->sh_type != SHT_NOBITS
   3229 	      && (bfd_vma)sec->sh_offset <= phdr[i].p_offset + (phdr[i].p_filesz - 1))
   3230 	    {
   3231 	      /* Found one!  The difference between the two addresses,
   3232 		 plus the difference between the two file offsets, is
   3233 		 enough information to reconstruct the lma.  */
   3234 
   3235 	      /* Example where they aren't:
   3236 		 PHDR[1] = lma fffc0100 offset 00002010 size 00000100
   3237 		 SEC[6]  = vma 00000050 offset 00002050 size 00000040
   3238 
   3239 		 The correct LMA for the section is fffc0140 + (2050-2010).
   3240 	      */
   3241 
   3242 	      phdr[i].p_vaddr = sec->sh_addr + (sec->sh_offset - phdr[i].p_offset);
   3243 	      break;
   3244 	    }
   3245 	}
   3246 
   3247       /* We must update the bfd sections as well, so we don't stop
   3248 	 with one match.  */
   3249       bsec = abfd->sections;
   3250       while (bsec)
   3251 	{
   3252 	  if (phdr[i].p_filesz
   3253 	      && phdr[i].p_vaddr <= bsec->vma
   3254 	      && bsec->vma <= phdr[i].p_vaddr + (phdr[i].p_filesz - 1))
   3255 	    {
   3256 	      bsec->lma = phdr[i].p_paddr + (bsec->vma - phdr[i].p_vaddr);
   3257 	    }
   3258 	  bsec = bsec->next;
   3259 	}
   3260     }
   3261 
   3262   return TRUE;
   3263 }
   3264 
   3265 
   3267 #ifdef DEBUG
   3268 void
   3269 rx_dump_symtab (bfd * abfd, void * internal_syms, void * external_syms)
   3270 {
   3271   size_t locsymcount;
   3272   Elf_Internal_Sym * isymbuf;
   3273   Elf_Internal_Sym * isymend;
   3274   Elf_Internal_Sym * isym;
   3275   Elf_Internal_Shdr * symtab_hdr;
   3276   bfd_boolean free_internal = FALSE, free_external = FALSE;
   3277   char * st_info_str;
   3278   char * st_info_stb_str;
   3279   char * st_other_str;
   3280   char * st_shndx_str;
   3281 
   3282   if (! internal_syms)
   3283     {
   3284       internal_syms = bfd_malloc (1000);
   3285       free_internal = 1;
   3286     }
   3287   if (! external_syms)
   3288     {
   3289       external_syms = bfd_malloc (1000);
   3290       free_external = 1;
   3291     }
   3292 
   3293   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   3294   locsymcount = symtab_hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   3295   if (free_internal)
   3296     isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   3297 				    symtab_hdr->sh_info, 0,
   3298 				    internal_syms, external_syms, NULL);
   3299   else
   3300     isymbuf = internal_syms;
   3301   isymend = isymbuf + locsymcount;
   3302 
   3303   for (isym = isymbuf ; isym < isymend ; isym++)
   3304     {
   3305       switch (ELF_ST_TYPE (isym->st_info))
   3306 	{
   3307 	case STT_FUNC: st_info_str = "STT_FUNC"; break;
   3308 	case STT_SECTION: st_info_str = "STT_SECTION"; break;
   3309 	case STT_FILE: st_info_str = "STT_FILE"; break;
   3310 	case STT_OBJECT: st_info_str = "STT_OBJECT"; break;
   3311 	case STT_TLS: st_info_str = "STT_TLS"; break;
   3312 	default: st_info_str = "";
   3313 	}
   3314       switch (ELF_ST_BIND (isym->st_info))
   3315 	{
   3316 	case STB_LOCAL: st_info_stb_str = "STB_LOCAL"; break;
   3317 	case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL"; break;
   3318 	default: st_info_stb_str = "";
   3319 	}
   3320       switch (ELF_ST_VISIBILITY (isym->st_other))
   3321 	{
   3322 	case STV_DEFAULT: st_other_str = "STV_DEFAULT"; break;
   3323 	case STV_INTERNAL: st_other_str = "STV_INTERNAL"; break;
   3324 	case STV_PROTECTED: st_other_str = "STV_PROTECTED"; break;
   3325 	default: st_other_str = "";
   3326 	}
   3327       switch (isym->st_shndx)
   3328 	{
   3329 	case SHN_ABS: st_shndx_str = "SHN_ABS"; break;
   3330 	case SHN_COMMON: st_shndx_str = "SHN_COMMON"; break;
   3331 	case SHN_UNDEF: st_shndx_str = "SHN_UNDEF"; break;
   3332 	default: st_shndx_str = "";
   3333 	}
   3334 
   3335       printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
   3336 	      "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
   3337 	      isym,
   3338 	      (unsigned long) isym->st_value,
   3339 	      (unsigned long) isym->st_size,
   3340 	      isym->st_name,
   3341 	      bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
   3342 					       isym->st_name),
   3343 	      isym->st_info, st_info_str, st_info_stb_str,
   3344 	      isym->st_other, st_other_str,
   3345 	      isym->st_shndx, st_shndx_str);
   3346     }
   3347   if (free_internal)
   3348     free (internal_syms);
   3349   if (free_external)
   3350     free (external_syms);
   3351 }
   3352 
   3353 char *
   3354 rx_get_reloc (long reloc)
   3355 {
   3356   if (0 <= reloc && reloc < R_RX_max)
   3357     return rx_elf_howto_table[reloc].name;
   3358   return "";
   3359 }
   3360 #endif /* DEBUG */
   3361 
   3362 
   3363 /* We must take care to keep the on-disk copy of any code sections
   3365    that are fully linked swapped if the target is big endian, to match
   3366    the Renesas tools.  */
   3367 
   3368 /* The rule is: big endian object that are final-link executables,
   3369    have code sections stored with 32-bit words swapped relative to
   3370    what you'd get by default.  */
   3371 
   3372 static bfd_boolean
   3373 rx_get_section_contents (bfd *         abfd,
   3374 			 sec_ptr       section,
   3375 			 void *        location,
   3376 			 file_ptr      offset,
   3377 			 bfd_size_type count)
   3378 {
   3379   int exec = (abfd->flags & EXEC_P) ? 1 : 0;
   3380   int s_code = (section->flags & SEC_CODE) ? 1 : 0;
   3381   bfd_boolean rv;
   3382 
   3383 #ifdef DJDEBUG
   3384   fprintf (stderr, "dj: get %ld %ld from %s  %s e%d sc%d  %08lx:%08lx\n",
   3385 	   (long) offset, (long) count, section->name,
   3386 	   bfd_big_endian(abfd) ? "be" : "le",
   3387 	   exec, s_code, (long unsigned) section->filepos,
   3388 	   (long unsigned) offset);
   3389 #endif
   3390 
   3391   if (exec && s_code && bfd_big_endian (abfd))
   3392     {
   3393       char * cloc = (char *) location;
   3394       bfd_size_type cnt, end_cnt;
   3395 
   3396       rv = TRUE;
   3397 
   3398       /* Fetch and swap unaligned bytes at the beginning.  */
   3399       if (offset % 4)
   3400         {
   3401 	  char buf[4];
   3402 
   3403 	  rv = _bfd_generic_get_section_contents (abfd, section, buf,
   3404 	                                          (offset & -4), 4);
   3405 	  if (!rv)
   3406 	    return FALSE;
   3407 
   3408 	  bfd_putb32 (bfd_getl32 (buf), buf);
   3409 
   3410 	  cnt = 4 - (offset % 4);
   3411 	  if (cnt > count)
   3412 	    cnt = count;
   3413 
   3414 	  memcpy (location, buf + (offset % 4), cnt);
   3415 
   3416 	  count -= cnt;
   3417 	  offset += cnt;
   3418 	  cloc += count;
   3419 	}
   3420 
   3421       end_cnt = count % 4;
   3422 
   3423       /* Fetch and swap the middle bytes.  */
   3424       if (count >= 4)
   3425 	{
   3426 	  rv = _bfd_generic_get_section_contents (abfd, section, cloc, offset,
   3427 						  count - end_cnt);
   3428 	  if (!rv)
   3429 	    return FALSE;
   3430 
   3431 	  for (cnt = count; cnt >= 4; cnt -= 4, cloc += 4)
   3432 	    bfd_putb32 (bfd_getl32 (cloc), cloc);
   3433 	}
   3434 
   3435       /* Fetch and swap the end bytes.  */
   3436       if (end_cnt > 0)
   3437 	{
   3438 	  char buf[4];
   3439 
   3440 	  /* Fetch the end bytes.  */
   3441 	  rv = _bfd_generic_get_section_contents (abfd, section, buf,
   3442 	                                          offset + count - end_cnt, 4);
   3443 	  if (!rv)
   3444 	    return FALSE;
   3445 
   3446 	  bfd_putb32 (bfd_getl32 (buf), buf);
   3447 	  memcpy (cloc, buf, end_cnt);
   3448 	}
   3449     }
   3450   else
   3451     rv = _bfd_generic_get_section_contents (abfd, section, location, offset, count);
   3452 
   3453   return rv;
   3454 }
   3455 
   3456 #ifdef DJDEBUG
   3457 static bfd_boolean
   3458 rx2_set_section_contents (bfd *        abfd,
   3459 			 sec_ptr       section,
   3460 			 const void *  location,
   3461 			 file_ptr      offset,
   3462 			 bfd_size_type count)
   3463 {
   3464   bfd_size_type i;
   3465 
   3466   fprintf (stderr, "   set sec %s %08x loc %p offset %#x count %#x\n",
   3467 	   section->name, (unsigned) section->vma, location, (int) offset, (int) count);
   3468   for (i = 0; i < count; i++)
   3469     {
   3470       if (i % 16 == 0 && i > 0)
   3471 	fprintf (stderr, "\n");
   3472 
   3473       if (i % 16  && i % 4 == 0)
   3474 	fprintf (stderr, " ");
   3475 
   3476       if (i % 16 == 0)
   3477 	fprintf (stderr, "      %08x:", (int) (section->vma + offset + i));
   3478 
   3479       fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
   3480     }
   3481   fprintf (stderr, "\n");
   3482 
   3483   return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
   3484 }
   3485 #define _bfd_elf_set_section_contents rx2_set_section_contents
   3486 #endif
   3487 
   3488 static bfd_boolean
   3489 rx_set_section_contents (bfd *         abfd,
   3490 			 sec_ptr       section,
   3491 			 const void *  location,
   3492 			 file_ptr      offset,
   3493 			 bfd_size_type count)
   3494 {
   3495   bfd_boolean exec = (abfd->flags & EXEC_P) ? TRUE : FALSE;
   3496   bfd_boolean s_code = (section->flags & SEC_CODE) ? TRUE : FALSE;
   3497   bfd_boolean rv;
   3498   char * swapped_data = NULL;
   3499   bfd_size_type i;
   3500   bfd_vma caddr = section->vma + offset;
   3501   file_ptr faddr = 0;
   3502   bfd_size_type scount;
   3503 
   3504 #ifdef DJDEBUG
   3505   bfd_size_type i;
   3506 
   3507   fprintf (stderr, "\ndj: set %ld %ld to %s  %s e%d sc%d\n",
   3508 	   (long) offset, (long) count, section->name,
   3509 	   bfd_big_endian (abfd) ? "be" : "le",
   3510 	   exec, s_code);
   3511 
   3512   for (i = 0; i < count; i++)
   3513     {
   3514       int a = section->vma + offset + i;
   3515 
   3516       if (a % 16 == 0 && a > 0)
   3517 	fprintf (stderr, "\n");
   3518 
   3519       if (a % 16  && a % 4 == 0)
   3520 	fprintf (stderr, " ");
   3521 
   3522       if (a % 16 == 0 || i == 0)
   3523 	fprintf (stderr, "      %08x:", (int) (section->vma + offset + i));
   3524 
   3525       fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
   3526     }
   3527 
   3528   fprintf (stderr, "\n");
   3529 #endif
   3530 
   3531   if (! exec || ! s_code || ! bfd_big_endian (abfd))
   3532     return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
   3533 
   3534   while (count > 0 && caddr > 0 && caddr % 4)
   3535     {
   3536       switch (caddr % 4)
   3537 	{
   3538 	case 0: faddr = offset + 3; break;
   3539 	case 1: faddr = offset + 1; break;
   3540 	case 2: faddr = offset - 1; break;
   3541 	case 3: faddr = offset - 3; break;
   3542 	}
   3543 
   3544       rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
   3545       if (! rv)
   3546 	return rv;
   3547 
   3548       location = (bfd_byte *) location + 1;
   3549       offset ++;
   3550       count --;
   3551       caddr ++;
   3552     }
   3553 
   3554   scount = (int)(count / 4) * 4;
   3555   if (scount > 0)
   3556     {
   3557       char * cloc = (char *) location;
   3558 
   3559       swapped_data = (char *) bfd_alloc (abfd, count);
   3560 
   3561       for (i = 0; i < count; i += 4)
   3562 	{
   3563 	  bfd_vma v = bfd_getl32 (cloc + i);
   3564 	  bfd_putb32 (v, swapped_data + i);
   3565 	}
   3566 
   3567       rv = _bfd_elf_set_section_contents (abfd, section, swapped_data, offset, scount);
   3568 
   3569       if (!rv)
   3570 	return rv;
   3571     }
   3572 
   3573   count -= scount;
   3574   location = (bfd_byte *) location + scount;
   3575   offset += scount;
   3576 
   3577   if (count > 0)
   3578     {
   3579       caddr = section->vma + offset;
   3580       while (count > 0)
   3581 	{
   3582 	  switch (caddr % 4)
   3583 	    {
   3584 	    case 0: faddr = offset + 3; break;
   3585 	    case 1: faddr = offset + 1; break;
   3586 	    case 2: faddr = offset - 1; break;
   3587 	    case 3: faddr = offset - 3; break;
   3588 	    }
   3589 	  rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
   3590 	  if (! rv)
   3591 	    return rv;
   3592 
   3593 	  location = (bfd_byte *) location + 1;
   3594 	  offset ++;
   3595 	  count --;
   3596 	  caddr ++;
   3597 	}
   3598     }
   3599 
   3600   return TRUE;
   3601 }
   3602 
   3603 static bfd_boolean
   3604 rx_final_link (bfd * abfd, struct bfd_link_info * info)
   3605 {
   3606   asection * o;
   3607 
   3608   for (o = abfd->sections; o != NULL; o = o->next)
   3609     {
   3610 #ifdef DJDEBUG
   3611       fprintf (stderr, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
   3612 	       o->name, o->flags, o->vma, o->lma, o->size, o->rawsize);
   3613 #endif
   3614       if (o->flags & SEC_CODE
   3615 	  && bfd_big_endian (abfd)
   3616 	  && o->size % 4)
   3617 	{
   3618 #ifdef DJDEBUG
   3619 	  fprintf (stderr, "adjusting...\n");
   3620 #endif
   3621 	  o->size += 4 - (o->size % 4);
   3622 	}
   3623     }
   3624 
   3625   return bfd_elf_final_link (abfd, info);
   3626 }
   3627 
   3628 static bfd_boolean
   3629 elf32_rx_modify_program_headers (bfd * abfd ATTRIBUTE_UNUSED,
   3630 				 struct bfd_link_info * info ATTRIBUTE_UNUSED)
   3631 {
   3632   const struct elf_backend_data * bed;
   3633   struct elf_obj_tdata * tdata;
   3634   Elf_Internal_Phdr * phdr;
   3635   unsigned int count;
   3636   unsigned int i;
   3637 
   3638   bed = get_elf_backend_data (abfd);
   3639   tdata = elf_tdata (abfd);
   3640   phdr = tdata->phdr;
   3641   count = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   3642 
   3643   if (ignore_lma)
   3644     for (i = count; i-- != 0;)
   3645       if (phdr[i].p_type == PT_LOAD)
   3646 	{
   3647 	  /* The Renesas tools expect p_paddr to be zero.  However,
   3648 	     there is no other way to store the writable data in ROM for
   3649 	     startup initialization.  So, we let the linker *think*
   3650 	     we're using paddr and vaddr the "usual" way, but at the
   3651 	     last minute we move the paddr into the vaddr (which is what
   3652 	     the simulator uses) and zero out paddr.  Note that this
   3653 	     does not affect the section headers, just the program
   3654 	     headers.  We hope.  */
   3655 	  phdr[i].p_vaddr = phdr[i].p_paddr;
   3656 #if 0	  /* If we zero out p_paddr, then the LMA in the section table
   3657 	     becomes wrong.  */
   3658 	  phdr[i].p_paddr = 0;
   3659 #endif
   3660 	}
   3661 
   3662   return TRUE;
   3663 }
   3664 
   3665 /* The default literal sections should always be marked as "code" (i.e.,
   3666    SHF_EXECINSTR).  This is particularly important for big-endian mode
   3667    when we do not want their contents byte reversed.  */
   3668 static const struct bfd_elf_special_section elf32_rx_special_sections[] =
   3669 {
   3670   { STRING_COMMA_LEN (".init_array"),    0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
   3671   { STRING_COMMA_LEN (".fini_array"),    0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
   3672   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
   3673   { NULL,                        0,      0, 0,            0 }
   3674 };
   3675 
   3676 typedef struct {
   3678   bfd *abfd;
   3679   struct bfd_link_info *info;
   3680   bfd_vma table_start;
   3681   int table_size;
   3682   bfd_vma *table_handlers;
   3683   bfd_vma table_default_handler;
   3684   struct bfd_link_hash_entry **table_entries;
   3685   struct bfd_link_hash_entry *table_default_entry;
   3686   FILE *mapfile;
   3687 } RX_Table_Info;
   3688 
   3689 static bfd_boolean
   3690 rx_table_find (struct bfd_hash_entry *vent, void *vinfo)
   3691 {
   3692   RX_Table_Info *info = (RX_Table_Info *)vinfo;
   3693   struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
   3694   const char *name; /* of the symbol we've found */
   3695   asection *sec;
   3696   struct bfd *abfd;
   3697   int idx;
   3698   const char *tname; /* name of the table */
   3699   bfd_vma start_addr, end_addr;
   3700   char *buf;
   3701   struct bfd_link_hash_entry * h;
   3702 
   3703   /* We're looking for globally defined symbols of the form
   3704      $tablestart$<NAME>.  */
   3705   if (ent->type != bfd_link_hash_defined
   3706       && ent->type != bfd_link_hash_defweak)
   3707     return TRUE;
   3708 
   3709   name = ent->root.string;
   3710   sec = ent->u.def.section;
   3711   abfd = sec->owner;
   3712 
   3713   if (strncmp (name, "$tablestart$", 12))
   3714     return TRUE;
   3715 
   3716   sec->flags |= SEC_KEEP;
   3717 
   3718   tname = name + 12;
   3719 
   3720   start_addr = ent->u.def.value;
   3721 
   3722   /* At this point, we can't build the table but we can (and must)
   3723      find all the related symbols and mark their sections as SEC_KEEP
   3724      so we don't garbage collect them.  */
   3725 
   3726   buf = (char *) malloc (12 + 10 + strlen (tname));
   3727 
   3728   sprintf (buf, "$tableend$%s", tname);
   3729   h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
   3730   if (!h || (h->type != bfd_link_hash_defined
   3731 	     && h->type != bfd_link_hash_defweak))
   3732     {
   3733       _bfd_error_handler (_("%B:%A: table %s missing corresponding %s"),
   3734 			  abfd, sec, name, buf);
   3735       return TRUE;
   3736     }
   3737 
   3738   if (h->u.def.section != ent->u.def.section)
   3739     {
   3740       _bfd_error_handler (_("%B:%A: %s and %s must be in the same input section"),
   3741 			  h->u.def.section->owner, h->u.def.section,
   3742 			  name, buf);
   3743       return TRUE;
   3744     }
   3745 
   3746   end_addr = h->u.def.value;
   3747 
   3748   sprintf (buf, "$tableentry$default$%s", tname);
   3749   h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
   3750   if (h && (h->type == bfd_link_hash_defined
   3751 	    || h->type == bfd_link_hash_defweak))
   3752     {
   3753       h->u.def.section->flags |= SEC_KEEP;
   3754     }
   3755 
   3756   for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
   3757     {
   3758       sprintf (buf, "$tableentry$%d$%s", idx, tname);
   3759       h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
   3760       if (h && (h->type == bfd_link_hash_defined
   3761 		|| h->type == bfd_link_hash_defweak))
   3762 	{
   3763 	  h->u.def.section->flags |= SEC_KEEP;
   3764 	}
   3765     }
   3766 
   3767   /* Return TRUE to keep scanning, FALSE to end the traversal.  */
   3768   return TRUE;
   3769 }
   3770 
   3771 /* We need to check for table entry symbols and build the tables, and
   3772    we need to do it before the linker does garbage collection.  This function is
   3773    called once per input object file.  */
   3774 static bfd_boolean
   3775 rx_check_directives
   3776     (bfd *                     abfd ATTRIBUTE_UNUSED,
   3777      struct bfd_link_info *    info ATTRIBUTE_UNUSED)
   3778 {
   3779   RX_Table_Info stuff;
   3780 
   3781   stuff.abfd = abfd;
   3782   stuff.info = info;
   3783   bfd_hash_traverse (&(info->hash->table), rx_table_find, &stuff);
   3784 
   3785   return TRUE;
   3786 }
   3787 
   3788 
   3789 static bfd_boolean
   3791 rx_table_map_2 (struct bfd_hash_entry *vent, void *vinfo)
   3792 {
   3793   RX_Table_Info *info = (RX_Table_Info *)vinfo;
   3794   struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
   3795   int idx;
   3796   const char *name;
   3797   bfd_vma addr;
   3798 
   3799   /* See if the symbol ENT has an address listed in the table, and
   3800      isn't a debug/special symbol.  If so, put it in the table.  */
   3801 
   3802   if (ent->type != bfd_link_hash_defined
   3803       && ent->type != bfd_link_hash_defweak)
   3804     return TRUE;
   3805 
   3806   name = ent->root.string;
   3807 
   3808   if (name[0] == '$' || name[0] == '.' || name[0] < ' ')
   3809     return TRUE;
   3810 
   3811   addr = (ent->u.def.value
   3812 	  + ent->u.def.section->output_section->vma
   3813 	  + ent->u.def.section->output_offset);
   3814 
   3815   for (idx = 0; idx < info->table_size; idx ++)
   3816     if (addr == info->table_handlers[idx])
   3817       info->table_entries[idx] = ent;
   3818 
   3819   if (addr == info->table_default_handler)
   3820     info->table_default_entry = ent;
   3821 
   3822   return TRUE;
   3823 }
   3824 
   3825 static bfd_boolean
   3826 rx_table_map (struct bfd_hash_entry *vent, void *vinfo)
   3827 {
   3828   RX_Table_Info *info = (RX_Table_Info *)vinfo;
   3829   struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
   3830   const char *name; /* of the symbol we've found */
   3831   int idx;
   3832   const char *tname; /* name of the table */
   3833   bfd_vma start_addr, end_addr;
   3834   char *buf;
   3835   struct bfd_link_hash_entry * h;
   3836   int need_elipses;
   3837 
   3838   /* We're looking for globally defined symbols of the form
   3839      $tablestart$<NAME>.  */
   3840   if (ent->type != bfd_link_hash_defined
   3841       && ent->type != bfd_link_hash_defweak)
   3842     return TRUE;
   3843 
   3844   name = ent->root.string;
   3845 
   3846   if (strncmp (name, "$tablestart$", 12))
   3847     return TRUE;
   3848 
   3849   tname = name + 12;
   3850   start_addr = (ent->u.def.value
   3851 		+ ent->u.def.section->output_section->vma
   3852 		+ ent->u.def.section->output_offset);
   3853 
   3854   buf = (char *) malloc (12 + 10 + strlen (tname));
   3855 
   3856   sprintf (buf, "$tableend$%s", tname);
   3857   end_addr = get_symbol_value_maybe (buf, info->info);
   3858 
   3859   sprintf (buf, "$tableentry$default$%s", tname);
   3860   h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
   3861   if (h)
   3862     {
   3863       info->table_default_handler = (h->u.def.value
   3864 				     + h->u.def.section->output_section->vma
   3865 				     + h->u.def.section->output_offset);
   3866     }
   3867   else
   3868     /* Zero is a valid handler address!  */
   3869     info->table_default_handler = (bfd_vma) (-1);
   3870   info->table_default_entry = NULL;
   3871 
   3872   info->table_start = start_addr;
   3873   info->table_size = (int) (end_addr - start_addr) / 4;
   3874   info->table_handlers = (bfd_vma *) malloc (info->table_size * sizeof (bfd_vma));
   3875   info->table_entries = (struct bfd_link_hash_entry **) malloc (info->table_size * sizeof (struct bfd_link_hash_entry));
   3876 
   3877   for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
   3878     {
   3879       sprintf (buf, "$tableentry$%d$%s", idx, tname);
   3880       h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
   3881       if (h && (h->type == bfd_link_hash_defined
   3882 		|| h->type == bfd_link_hash_defweak))
   3883 	{
   3884 	  info->table_handlers[idx] = (h->u.def.value
   3885 				       + h->u.def.section->output_section->vma
   3886 				       + h->u.def.section->output_offset);
   3887 	}
   3888       else
   3889 	info->table_handlers[idx] = info->table_default_handler;
   3890       info->table_entries[idx] = NULL;
   3891     }
   3892 
   3893   free (buf);
   3894 
   3895   bfd_hash_traverse (&(info->info->hash->table), rx_table_map_2, info);
   3896 
   3897   fprintf (info->mapfile, "\nRX Vector Table: %s has %d entries at 0x%08" BFD_VMA_FMT "x\n\n",
   3898 	   tname, info->table_size, start_addr);
   3899 
   3900   if (info->table_default_entry)
   3901     fprintf (info->mapfile, "  default handler is: %s at 0x%08" BFD_VMA_FMT "x\n",
   3902 	     info->table_default_entry->root.string,
   3903 	     info->table_default_handler);
   3904   else if (info->table_default_handler != (bfd_vma)(-1))
   3905     fprintf (info->mapfile, "  default handler is at 0x%08" BFD_VMA_FMT "x\n",
   3906 	     info->table_default_handler);
   3907   else
   3908     fprintf (info->mapfile, "  no default handler\n");
   3909 
   3910   need_elipses = 1;
   3911   for (idx = 0; idx < info->table_size; idx ++)
   3912     {
   3913       if (info->table_handlers[idx] == info->table_default_handler)
   3914 	{
   3915 	  if (need_elipses)
   3916 	    fprintf (info->mapfile, "  . . .\n");
   3917 	  need_elipses = 0;
   3918 	  continue;
   3919 	}
   3920       need_elipses = 1;
   3921 
   3922       fprintf (info->mapfile, "  0x%08" BFD_VMA_FMT "x [%3d] ", start_addr + 4 * idx, idx);
   3923 
   3924       if (info->table_handlers[idx] == (bfd_vma) (-1))
   3925 	fprintf (info->mapfile, "(no handler found)\n");
   3926 
   3927       else if (info->table_handlers[idx] == info->table_default_handler)
   3928 	{
   3929 	  if (info->table_default_entry)
   3930 	    fprintf (info->mapfile, "(default)\n");
   3931 	  else
   3932 	    fprintf (info->mapfile, "(default)\n");
   3933 	}
   3934 
   3935       else if (info->table_entries[idx])
   3936 	{
   3937 	  fprintf (info->mapfile, "0x%08" BFD_VMA_FMT "x %s\n", info->table_handlers[idx], info->table_entries[idx]->root.string);
   3938 	}
   3939 
   3940       else
   3941 	{
   3942 	  fprintf (info->mapfile, "0x%08" BFD_VMA_FMT "x ???\n", info->table_handlers[idx]);
   3943 	}
   3944     }
   3945   if (need_elipses)
   3946     fprintf (info->mapfile, "  . . .\n");
   3947 
   3948   return TRUE;
   3949 }
   3950 
   3951 void
   3952 rx_additional_link_map_text (bfd *obfd, struct bfd_link_info *info, FILE *mapfile)
   3953 {
   3954   /* We scan the symbol table looking for $tableentry$'s, and for
   3955      each, try to deduce which handlers go with which entries.  */
   3956 
   3957   RX_Table_Info stuff;
   3958 
   3959   stuff.abfd = obfd;
   3960   stuff.info = info;
   3961   stuff.mapfile = mapfile;
   3962   bfd_hash_traverse (&(info->hash->table), rx_table_map, &stuff);
   3963 }
   3964 
   3965 
   3966 #define ELF_ARCH		bfd_arch_rx
   3968 #define ELF_MACHINE_CODE	EM_RX
   3969 #define ELF_MAXPAGESIZE		0x1000
   3970 
   3971 #define TARGET_BIG_SYM		rx_elf32_be_vec
   3972 #define TARGET_BIG_NAME		"elf32-rx-be"
   3973 
   3974 #define TARGET_LITTLE_SYM	rx_elf32_le_vec
   3975 #define TARGET_LITTLE_NAME	"elf32-rx-le"
   3976 
   3977 #define elf_info_to_howto_rel			NULL
   3978 #define elf_info_to_howto			rx_info_to_howto_rela
   3979 #define elf_backend_object_p			rx_elf_object_p
   3980 #define elf_backend_relocate_section		rx_elf_relocate_section
   3981 #define elf_symbol_leading_char                 ('_')
   3982 #define elf_backend_can_gc_sections		1
   3983 #define elf_backend_modify_program_headers	elf32_rx_modify_program_headers
   3984 
   3985 #define bfd_elf32_bfd_reloc_type_lookup		rx_reloc_type_lookup
   3986 #define bfd_elf32_bfd_reloc_name_lookup		rx_reloc_name_lookup
   3987 #define bfd_elf32_bfd_set_private_flags		rx_elf_set_private_flags
   3988 #define bfd_elf32_bfd_merge_private_bfd_data	rx_elf_merge_private_bfd_data
   3989 #define bfd_elf32_bfd_print_private_bfd_data	rx_elf_print_private_bfd_data
   3990 #define bfd_elf32_get_section_contents		rx_get_section_contents
   3991 #define bfd_elf32_set_section_contents		rx_set_section_contents
   3992 #define bfd_elf32_bfd_final_link		rx_final_link
   3993 #define bfd_elf32_bfd_relax_section		elf32_rx_relax_section_wrapper
   3994 #define elf_backend_special_sections	        elf32_rx_special_sections
   3995 #define elf_backend_check_directives		rx_check_directives
   3996 
   3997 #include "elf32-target.h"
   3998 
   3999 /* We define a second big-endian target that doesn't have the custom
   4000    section get/set hooks, for times when we want to preserve the
   4001    pre-swapped .text sections (like objcopy).  */
   4002 
   4003 #undef  TARGET_BIG_SYM
   4004 #define TARGET_BIG_SYM		rx_elf32_be_ns_vec
   4005 #undef  TARGET_BIG_NAME
   4006 #define TARGET_BIG_NAME		"elf32-rx-be-ns"
   4007 #undef  TARGET_LITTLE_SYM
   4008 
   4009 #undef bfd_elf32_get_section_contents
   4010 #undef bfd_elf32_set_section_contents
   4011 
   4012 #undef	elf32_bed
   4013 #define elf32_bed				elf32_rx_be_ns_bed
   4014 
   4015 #include "elf32-target.h"
   4016