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