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