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