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