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