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