Document the GDB 10.2 release in gdb/ChangeLog
[binutils-gdb.git] / bfd / elf32-rx.c
blob771863590d5a39f2923df7a0292eefa82bdd0325
1 /* Renesas RX specific support for 32-bit ELF.
2 Copyright (C) 2008-2021 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/rx.h"
26 #include "libiberty.h"
27 #include "elf32-rx.h"
29 #define RX_OPCODE_BIG_ENDIAN 0
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;
37 #ifdef DEBUG
38 char * rx_get_reloc (long);
39 void rx_dump_symtab (bfd *, void *, void *);
40 #endif
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)
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. */
50 static reloc_howto_type rx_elf_howto_table [] =
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),
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),
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),
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),
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),
139 #define STACK_REL_P(x) ((x) <= R_RX_ABS16_REV && (x) >= R_RX_ABS32)
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),
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. */
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)
224 /* Map BFD reloc types to RX ELF reloc types. */
226 struct rx_reloc_map
228 bfd_reloc_code_real_type bfd_reloc_val;
229 unsigned int rx_reloc_val;
232 static const struct rx_reloc_map rx_reloc_map [] =
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 }
269 #define BIGE(abfd) ((abfd)->xvec->byteorder == BFD_ENDIAN_BIG)
271 static reloc_howto_type *
272 rx_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
273 bfd_reloc_code_real_type code)
275 unsigned int i;
277 if (code == BFD_RELOC_RX_32_OP)
278 return rx_elf_howto_table + R_RX_DIR32;
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;
284 return NULL;
287 static reloc_howto_type *
288 rx_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, const char * r_name)
290 unsigned int i;
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;
297 return NULL;
300 /* Set the howto pointer for an RX ELF reloc. */
302 static bool
303 rx_info_to_howto_rela (bfd * abfd,
304 arelent * cache_ptr,
305 Elf_Internal_Rela * dst)
307 unsigned int r_type;
309 r_type = ELF32_R_TYPE (dst->r_info);
310 if (r_type >= (unsigned int) R_RX_max)
312 /* xgettext:c-format */
313 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
314 abfd, r_type);
315 bfd_set_error (bfd_error_bad_value);
316 return false;
318 cache_ptr->howto = rx_elf_howto_table + r_type;
319 if (cache_ptr->howto->name == NULL)
321 /* xgettext:c-format */
322 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
323 abfd, r_type);
324 bfd_set_error (bfd_error_bad_value);
325 return false;
327 return true;
330 static bfd_vma
331 get_symbol_value (const char * name,
332 struct bfd_link_info * info,
333 bfd * input_bfd,
334 asection * input_section,
335 int offset)
337 bfd_vma value = 0;
338 struct bfd_link_hash_entry * h;
340 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
342 if (h == NULL
343 || (h->type != bfd_link_hash_defined
344 && h->type != bfd_link_hash_defweak))
345 (*info->callbacks->undefined_symbol)
346 (info, name, input_bfd, input_section, offset, true);
347 else
348 value = (h->u.def.value
349 + h->u.def.section->output_section->vma
350 + h->u.def.section->output_offset);
352 return value;
355 static bfd_vma
356 get_symbol_value_maybe (const char * name,
357 struct bfd_link_info * info)
359 bfd_vma value = 0;
360 struct bfd_link_hash_entry * h;
362 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
364 if (h == NULL
365 || (h->type != bfd_link_hash_defined
366 && h->type != bfd_link_hash_defweak))
367 return 0;
368 else
369 value = (h->u.def.value
370 + h->u.def.section->output_section->vma
371 + h->u.def.section->output_offset);
373 return value;
376 static bfd_vma
377 get_gp (struct bfd_link_info * info,
378 bfd * abfd,
379 asection * sec,
380 int offset)
382 static bool cached = false;
383 static bfd_vma cached_value = 0;
385 if (!cached)
387 cached_value = get_symbol_value ("__gp", info, abfd, sec, offset);
388 cached = true;
390 return cached_value;
393 static bfd_vma
394 get_romstart (struct bfd_link_info * info,
395 bfd * abfd,
396 asection * sec,
397 int offset)
399 static bool cached = false;
400 static bfd_vma cached_value = 0;
402 if (!cached)
404 cached_value = get_symbol_value ("_start", info, abfd, sec, offset);
405 cached = true;
407 return cached_value;
410 static bfd_vma
411 get_ramstart (struct bfd_link_info * info,
412 bfd * abfd,
413 asection * sec,
414 int offset)
416 static bool cached = false;
417 static bfd_vma cached_value = 0;
419 if (!cached)
421 cached_value = get_symbol_value ("__datastart", info, abfd, sec, offset);
422 cached = true;
424 return cached_value;
427 #define NUM_STACK_ENTRIES 16
428 static int32_t rx_stack [ NUM_STACK_ENTRIES ];
429 static unsigned int rx_stack_top;
431 #define RX_STACK_PUSH(val) \
432 do \
434 if (rx_stack_top < NUM_STACK_ENTRIES) \
435 rx_stack [rx_stack_top ++] = (val); \
436 else \
437 r = bfd_reloc_dangerous; \
439 while (0)
441 #define RX_STACK_POP(dest) \
442 do \
444 if (rx_stack_top > 0) \
445 (dest) = rx_stack [-- rx_stack_top]; \
446 else \
447 (dest) = 0, r = bfd_reloc_dangerous; \
449 while (0)
451 /* Relocate an RX ELF section.
452 There is some attempt to make this function usable for many architectures,
453 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
454 if only to serve as a learning tool.
456 The RELOCATE_SECTION function is called by the new ELF backend linker
457 to handle the relocations for a section.
459 The relocs are always passed as Rela structures; if the section
460 actually uses Rel structures, the r_addend field will always be
461 zero.
463 This function is responsible for adjusting the section contents as
464 necessary, and (if using Rela relocs and generating a relocatable
465 output file) adjusting the reloc addend as necessary.
467 This function does not have to worry about setting the reloc
468 address or the reloc symbol index.
470 LOCAL_SYMS is a pointer to the swapped in local symbols.
472 LOCAL_SECTIONS is an array giving the section in the input file
473 corresponding to the st_shndx field of each local symbol.
475 The global hash table entry for the global symbols can be found
476 via elf_sym_hashes (input_bfd).
478 When generating relocatable output, this function must handle
479 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
480 going to be the section symbol corresponding to the output
481 section, which means that the addend must be adjusted
482 accordingly. */
484 static int
485 rx_elf_relocate_section
486 (bfd * output_bfd,
487 struct bfd_link_info * info,
488 bfd * input_bfd,
489 asection * input_section,
490 bfd_byte * contents,
491 Elf_Internal_Rela * relocs,
492 Elf_Internal_Sym * local_syms,
493 asection ** local_sections)
495 Elf_Internal_Shdr *symtab_hdr;
496 struct elf_link_hash_entry **sym_hashes;
497 Elf_Internal_Rela *rel;
498 Elf_Internal_Rela *relend;
499 bool pid_mode;
500 bool saw_subtract = false;
501 const char *table_default_cache = NULL;
502 bfd_vma table_start_cache = 0;
503 bfd_vma table_end_cache = 0;
505 if (elf_elfheader (output_bfd)->e_flags & E_FLAG_RX_PID)
506 pid_mode = true;
507 else
508 pid_mode = false;
510 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
511 sym_hashes = elf_sym_hashes (input_bfd);
512 relend = relocs + input_section->reloc_count;
513 for (rel = relocs; rel < relend; rel ++)
515 reloc_howto_type *howto;
516 unsigned long r_symndx;
517 Elf_Internal_Sym *sym;
518 asection *sec;
519 struct elf_link_hash_entry *h;
520 bfd_vma relocation;
521 bfd_reloc_status_type r;
522 const char * name = NULL;
523 bool unresolved_reloc = true;
524 int r_type;
526 r_type = ELF32_R_TYPE (rel->r_info);
527 r_symndx = ELF32_R_SYM (rel->r_info);
529 howto = rx_elf_howto_table + ELF32_R_TYPE (rel->r_info);
530 h = NULL;
531 sym = NULL;
532 sec = NULL;
533 relocation = 0;
535 if (rx_stack_top == 0)
536 saw_subtract = false;
538 if (r_symndx < symtab_hdr->sh_info)
540 sym = local_syms + r_symndx;
541 sec = local_sections [r_symndx];
542 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel);
544 name = bfd_elf_string_from_elf_section
545 (input_bfd, symtab_hdr->sh_link, sym->st_name);
546 name = sym->st_name == 0 ? bfd_section_name (sec) : name;
548 else
550 bool warned, ignored;
552 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
553 r_symndx, symtab_hdr, sym_hashes, h,
554 sec, relocation, unresolved_reloc,
555 warned, ignored);
557 name = h->root.root.string;
560 if (startswith (name, "$tableentry$default$"))
562 bfd_vma entry_vma;
563 int idx;
564 char *buf;
566 if (table_default_cache != name)
569 /* All relocs for a given table should be to the same
570 (weak) default symbol) so we can use it to detect a
571 cache miss. We use the offset into the table to find
572 the "real" symbol. Calculate and store the table's
573 offset here. */
575 table_default_cache = name;
577 /* We have already done error checking in rx_table_find(). */
579 buf = (char *) bfd_malloc (13 + strlen (name + 20));
580 if (buf == NULL)
581 return false;
583 sprintf (buf, "$tablestart$%s", name + 20);
584 table_start_cache = get_symbol_value (buf,
585 info,
586 input_bfd,
587 input_section,
588 rel->r_offset);
590 sprintf (buf, "$tableend$%s", name + 20);
591 table_end_cache = get_symbol_value (buf,
592 info,
593 input_bfd,
594 input_section,
595 rel->r_offset);
597 free (buf);
600 entry_vma = (input_section->output_section->vma
601 + input_section->output_offset
602 + rel->r_offset);
604 if (table_end_cache <= entry_vma || entry_vma < table_start_cache)
606 /* xgettext:c-format */
607 _bfd_error_handler (_("%pB:%pA: table entry %s outside table"),
608 input_bfd, input_section,
609 name);
611 else if ((int) (entry_vma - table_start_cache) % 4)
613 /* xgettext:c-format */
614 _bfd_error_handler (_("%pB:%pA: table entry %s not word-aligned within table"),
615 input_bfd, input_section,
616 name);
618 else
620 idx = (int) (entry_vma - table_start_cache) / 4;
622 /* This will look like $tableentry$<N>$<name> */
623 buf = (char *) bfd_malloc (12 + 20 + strlen (name + 20));
624 if (buf == NULL)
625 return false;
627 sprintf (buf, "$tableentry$%d$%s", idx, name + 20);
629 h = (struct elf_link_hash_entry *) bfd_link_hash_lookup (info->hash, buf, false, false, true);
631 if (h)
633 relocation = (h->root.u.def.value
634 + h->root.u.def.section->output_section->vma
635 + h->root.u.def.section->output_offset);;
638 free (buf);
642 if (sec != NULL && discarded_section (sec))
643 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
644 rel, 1, relend, howto, 0, contents);
646 if (bfd_link_relocatable (info))
648 /* This is a relocatable link. We don't have to change
649 anything, unless the reloc is against a section symbol,
650 in which case we have to adjust according to where the
651 section symbol winds up in the output section. */
652 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
653 rel->r_addend += sec->output_offset;
654 continue;
657 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
658 /* If the symbol is undefined and weak
659 then the relocation resolves to zero. */
660 relocation = 0;
661 else
663 if (howto->pc_relative)
665 relocation -= (input_section->output_section->vma
666 + input_section->output_offset
667 + rel->r_offset);
668 if (r_type != R_RX_RH_3_PCREL
669 && r_type != R_RX_DIR3U_PCREL)
670 relocation ++;
673 relocation += rel->r_addend;
676 r = bfd_reloc_ok;
678 #define RANGE(a,b) \
679 if (a > (long) relocation || (long) relocation > b) \
680 r = bfd_reloc_overflow
681 #define ALIGN(m) \
682 if (relocation & m) \
683 r = bfd_reloc_other
684 #define OP(i) \
685 (contents[rel->r_offset + (i)])
686 #define WARN_REDHAT(type) \
687 /* xgettext:c-format */ \
688 _bfd_error_handler \
689 (_("%pB:%pA: warning: deprecated Red Hat reloc " \
690 "%s detected against: %s"), \
691 input_bfd, input_section, #type, name)
693 /* Check for unsafe relocs in PID mode. These are any relocs where
694 an absolute address is being computed. There are special cases
695 for relocs against symbols that are known to be referenced in
696 crt0.o before the PID base address register has been initialised. */
697 #define UNSAFE_FOR_PID \
698 do \
700 if (pid_mode \
701 && sec != NULL \
702 && sec->flags & SEC_READONLY \
703 && !(input_section->flags & SEC_DEBUGGING) \
704 && strcmp (name, "__pid_base") != 0 \
705 && strcmp (name, "__gp") != 0 \
706 && strcmp (name, "__romdatastart") != 0 \
707 && !saw_subtract) \
708 /* xgettext:c-format */ \
709 _bfd_error_handler (_("%pB(%pA): unsafe PID relocation %s " \
710 "at %#" PRIx64 " (against %s in %s)"), \
711 input_bfd, input_section, howto->name, \
712 (uint64_t) (input_section->output_section->vma \
713 + input_section->output_offset \
714 + rel->r_offset), \
715 name, sec->name); \
717 while (0)
719 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
720 switch (r_type)
722 case R_RX_NONE:
723 break;
725 case R_RX_RH_RELAX:
726 break;
728 case R_RX_RH_3_PCREL:
729 WARN_REDHAT ("RX_RH_3_PCREL");
730 RANGE (3, 10);
731 OP (0) &= 0xf8;
732 OP (0) |= relocation & 0x07;
733 break;
735 case R_RX_RH_8_NEG:
736 WARN_REDHAT ("RX_RH_8_NEG");
737 relocation = - relocation;
738 /* Fall through. */
739 case R_RX_DIR8S_PCREL:
740 UNSAFE_FOR_PID;
741 RANGE (-128, 127);
742 OP (0) = relocation;
743 break;
745 case R_RX_DIR8S:
746 UNSAFE_FOR_PID;
747 RANGE (-128, 255);
748 OP (0) = relocation;
749 break;
751 case R_RX_DIR8U:
752 UNSAFE_FOR_PID;
753 RANGE (0, 255);
754 OP (0) = relocation;
755 break;
757 case R_RX_RH_16_NEG:
758 WARN_REDHAT ("RX_RH_16_NEG");
759 relocation = - relocation;
760 /* Fall through. */
761 case R_RX_DIR16S_PCREL:
762 UNSAFE_FOR_PID;
763 RANGE (-32768, 32767);
764 #if RX_OPCODE_BIG_ENDIAN
765 #else
766 OP (0) = relocation;
767 OP (1) = relocation >> 8;
768 #endif
769 break;
771 case R_RX_RH_16_OP:
772 WARN_REDHAT ("RX_RH_16_OP");
773 UNSAFE_FOR_PID;
774 RANGE (-32768, 32767);
775 #if RX_OPCODE_BIG_ENDIAN
776 OP (1) = relocation;
777 OP (0) = relocation >> 8;
778 #else
779 OP (0) = relocation;
780 OP (1) = relocation >> 8;
781 #endif
782 break;
784 case R_RX_DIR16S:
785 UNSAFE_FOR_PID;
786 RANGE (-32768, 65535);
787 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
789 OP (1) = relocation;
790 OP (0) = relocation >> 8;
792 else
794 OP (0) = relocation;
795 OP (1) = relocation >> 8;
797 break;
799 case R_RX_DIR16U:
800 UNSAFE_FOR_PID;
801 RANGE (0, 65536);
802 #if RX_OPCODE_BIG_ENDIAN
803 OP (1) = relocation;
804 OP (0) = relocation >> 8;
805 #else
806 OP (0) = relocation;
807 OP (1) = relocation >> 8;
808 #endif
809 break;
811 case R_RX_DIR16:
812 UNSAFE_FOR_PID;
813 RANGE (-32768, 65536);
814 #if RX_OPCODE_BIG_ENDIAN
815 OP (1) = relocation;
816 OP (0) = relocation >> 8;
817 #else
818 OP (0) = relocation;
819 OP (1) = relocation >> 8;
820 #endif
821 break;
823 case R_RX_DIR16_REV:
824 UNSAFE_FOR_PID;
825 RANGE (-32768, 65536);
826 #if RX_OPCODE_BIG_ENDIAN
827 OP (0) = relocation;
828 OP (1) = relocation >> 8;
829 #else
830 OP (1) = relocation;
831 OP (0) = relocation >> 8;
832 #endif
833 break;
835 case R_RX_DIR3U_PCREL:
836 RANGE (3, 10);
837 OP (0) &= 0xf8;
838 OP (0) |= relocation & 0x07;
839 break;
841 case R_RX_RH_24_NEG:
842 UNSAFE_FOR_PID;
843 WARN_REDHAT ("RX_RH_24_NEG");
844 relocation = - relocation;
845 /* Fall through. */
846 case R_RX_DIR24S_PCREL:
847 RANGE (-0x800000, 0x7fffff);
848 #if RX_OPCODE_BIG_ENDIAN
849 OP (2) = relocation;
850 OP (1) = relocation >> 8;
851 OP (0) = relocation >> 16;
852 #else
853 OP (0) = relocation;
854 OP (1) = relocation >> 8;
855 OP (2) = relocation >> 16;
856 #endif
857 break;
859 case R_RX_RH_24_OP:
860 UNSAFE_FOR_PID;
861 WARN_REDHAT ("RX_RH_24_OP");
862 RANGE (-0x800000, 0x7fffff);
863 #if RX_OPCODE_BIG_ENDIAN
864 OP (2) = relocation;
865 OP (1) = relocation >> 8;
866 OP (0) = relocation >> 16;
867 #else
868 OP (0) = relocation;
869 OP (1) = relocation >> 8;
870 OP (2) = relocation >> 16;
871 #endif
872 break;
874 case R_RX_DIR24S:
875 UNSAFE_FOR_PID;
876 RANGE (-0x800000, 0x7fffff);
877 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
879 OP (2) = relocation;
880 OP (1) = relocation >> 8;
881 OP (0) = relocation >> 16;
883 else
885 OP (0) = relocation;
886 OP (1) = relocation >> 8;
887 OP (2) = relocation >> 16;
889 break;
891 case R_RX_RH_24_UNS:
892 UNSAFE_FOR_PID;
893 WARN_REDHAT ("RX_RH_24_UNS");
894 RANGE (0, 0xffffff);
895 #if RX_OPCODE_BIG_ENDIAN
896 OP (2) = relocation;
897 OP (1) = relocation >> 8;
898 OP (0) = relocation >> 16;
899 #else
900 OP (0) = relocation;
901 OP (1) = relocation >> 8;
902 OP (2) = relocation >> 16;
903 #endif
904 break;
906 case R_RX_RH_32_NEG:
907 UNSAFE_FOR_PID;
908 WARN_REDHAT ("RX_RH_32_NEG");
909 relocation = - relocation;
910 #if RX_OPCODE_BIG_ENDIAN
911 OP (3) = relocation;
912 OP (2) = relocation >> 8;
913 OP (1) = relocation >> 16;
914 OP (0) = relocation >> 24;
915 #else
916 OP (0) = relocation;
917 OP (1) = relocation >> 8;
918 OP (2) = relocation >> 16;
919 OP (3) = relocation >> 24;
920 #endif
921 break;
923 case R_RX_RH_32_OP:
924 UNSAFE_FOR_PID;
925 WARN_REDHAT ("RX_RH_32_OP");
926 #if RX_OPCODE_BIG_ENDIAN
927 OP (3) = relocation;
928 OP (2) = relocation >> 8;
929 OP (1) = relocation >> 16;
930 OP (0) = relocation >> 24;
931 #else
932 OP (0) = relocation;
933 OP (1) = relocation >> 8;
934 OP (2) = relocation >> 16;
935 OP (3) = relocation >> 24;
936 #endif
937 break;
939 case R_RX_DIR32:
940 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
942 OP (3) = relocation;
943 OP (2) = relocation >> 8;
944 OP (1) = relocation >> 16;
945 OP (0) = relocation >> 24;
947 else
949 OP (0) = relocation;
950 OP (1) = relocation >> 8;
951 OP (2) = relocation >> 16;
952 OP (3) = relocation >> 24;
954 break;
956 case R_RX_DIR32_REV:
957 if (BIGE (output_bfd))
959 OP (0) = relocation;
960 OP (1) = relocation >> 8;
961 OP (2) = relocation >> 16;
962 OP (3) = relocation >> 24;
964 else
966 OP (3) = relocation;
967 OP (2) = relocation >> 8;
968 OP (1) = relocation >> 16;
969 OP (0) = relocation >> 24;
971 break;
973 case R_RX_RH_DIFF:
975 bfd_vma val;
976 WARN_REDHAT ("RX_RH_DIFF");
977 val = bfd_get_32 (output_bfd, & OP (0));
978 val -= relocation;
979 bfd_put_32 (output_bfd, val, & OP (0));
981 break;
983 case R_RX_RH_GPRELB:
984 WARN_REDHAT ("RX_RH_GPRELB");
985 relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
986 RANGE (0, 65535);
987 #if RX_OPCODE_BIG_ENDIAN
988 OP (1) = relocation;
989 OP (0) = relocation >> 8;
990 #else
991 OP (0) = relocation;
992 OP (1) = relocation >> 8;
993 #endif
994 break;
996 case R_RX_RH_GPRELW:
997 WARN_REDHAT ("RX_RH_GPRELW");
998 relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
999 ALIGN (1);
1000 relocation >>= 1;
1001 RANGE (0, 65535);
1002 #if RX_OPCODE_BIG_ENDIAN
1003 OP (1) = relocation;
1004 OP (0) = relocation >> 8;
1005 #else
1006 OP (0) = relocation;
1007 OP (1) = relocation >> 8;
1008 #endif
1009 break;
1011 case R_RX_RH_GPRELL:
1012 WARN_REDHAT ("RX_RH_GPRELL");
1013 relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
1014 ALIGN (3);
1015 relocation >>= 2;
1016 RANGE (0, 65535);
1017 #if RX_OPCODE_BIG_ENDIAN
1018 OP (1) = relocation;
1019 OP (0) = relocation >> 8;
1020 #else
1021 OP (0) = relocation;
1022 OP (1) = relocation >> 8;
1023 #endif
1024 break;
1026 /* Internal relocations just for relaxation: */
1027 case R_RX_RH_ABS5p5B:
1028 RX_STACK_POP (relocation);
1029 RANGE (0, 31);
1030 OP (0) &= 0xf8;
1031 OP (0) |= relocation >> 2;
1032 OP (1) &= 0x77;
1033 OP (1) |= (relocation << 6) & 0x80;
1034 OP (1) |= (relocation << 3) & 0x08;
1035 break;
1037 case R_RX_RH_ABS5p5W:
1038 RX_STACK_POP (relocation);
1039 RANGE (0, 62);
1040 ALIGN (1);
1041 relocation >>= 1;
1042 OP (0) &= 0xf8;
1043 OP (0) |= relocation >> 2;
1044 OP (1) &= 0x77;
1045 OP (1) |= (relocation << 6) & 0x80;
1046 OP (1) |= (relocation << 3) & 0x08;
1047 break;
1049 case R_RX_RH_ABS5p5L:
1050 RX_STACK_POP (relocation);
1051 RANGE (0, 124);
1052 ALIGN (3);
1053 relocation >>= 2;
1054 OP (0) &= 0xf8;
1055 OP (0) |= relocation >> 2;
1056 OP (1) &= 0x77;
1057 OP (1) |= (relocation << 6) & 0x80;
1058 OP (1) |= (relocation << 3) & 0x08;
1059 break;
1061 case R_RX_RH_ABS5p8B:
1062 RX_STACK_POP (relocation);
1063 RANGE (0, 31);
1064 OP (0) &= 0x70;
1065 OP (0) |= (relocation << 3) & 0x80;
1066 OP (0) |= relocation & 0x0f;
1067 break;
1069 case R_RX_RH_ABS5p8W:
1070 RX_STACK_POP (relocation);
1071 RANGE (0, 62);
1072 ALIGN (1);
1073 relocation >>= 1;
1074 OP (0) &= 0x70;
1075 OP (0) |= (relocation << 3) & 0x80;
1076 OP (0) |= relocation & 0x0f;
1077 break;
1079 case R_RX_RH_ABS5p8L:
1080 RX_STACK_POP (relocation);
1081 RANGE (0, 124);
1082 ALIGN (3);
1083 relocation >>= 2;
1084 OP (0) &= 0x70;
1085 OP (0) |= (relocation << 3) & 0x80;
1086 OP (0) |= relocation & 0x0f;
1087 break;
1089 case R_RX_RH_UIMM4p8:
1090 RANGE (0, 15);
1091 OP (0) &= 0x0f;
1092 OP (0) |= relocation << 4;
1093 break;
1095 case R_RX_RH_UNEG4p8:
1096 RANGE (-15, 0);
1097 OP (0) &= 0x0f;
1098 OP (0) |= (-relocation) << 4;
1099 break;
1101 /* Complex reloc handling: */
1103 case R_RX_ABS32:
1104 UNSAFE_FOR_PID;
1105 RX_STACK_POP (relocation);
1106 #if RX_OPCODE_BIG_ENDIAN
1107 OP (3) = relocation;
1108 OP (2) = relocation >> 8;
1109 OP (1) = relocation >> 16;
1110 OP (0) = relocation >> 24;
1111 #else
1112 OP (0) = relocation;
1113 OP (1) = relocation >> 8;
1114 OP (2) = relocation >> 16;
1115 OP (3) = relocation >> 24;
1116 #endif
1117 break;
1119 case R_RX_ABS32_REV:
1120 UNSAFE_FOR_PID;
1121 RX_STACK_POP (relocation);
1122 #if RX_OPCODE_BIG_ENDIAN
1123 OP (0) = relocation;
1124 OP (1) = relocation >> 8;
1125 OP (2) = relocation >> 16;
1126 OP (3) = relocation >> 24;
1127 #else
1128 OP (3) = relocation;
1129 OP (2) = relocation >> 8;
1130 OP (1) = relocation >> 16;
1131 OP (0) = relocation >> 24;
1132 #endif
1133 break;
1135 case R_RX_ABS24S_PCREL:
1136 case R_RX_ABS24S:
1137 UNSAFE_FOR_PID;
1138 RX_STACK_POP (relocation);
1139 RANGE (-0x800000, 0x7fffff);
1140 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
1142 OP (2) = relocation;
1143 OP (1) = relocation >> 8;
1144 OP (0) = relocation >> 16;
1146 else
1148 OP (0) = relocation;
1149 OP (1) = relocation >> 8;
1150 OP (2) = relocation >> 16;
1152 break;
1154 case R_RX_ABS16:
1155 UNSAFE_FOR_PID;
1156 RX_STACK_POP (relocation);
1157 RANGE (-32768, 65535);
1158 #if RX_OPCODE_BIG_ENDIAN
1159 OP (1) = relocation;
1160 OP (0) = relocation >> 8;
1161 #else
1162 OP (0) = relocation;
1163 OP (1) = relocation >> 8;
1164 #endif
1165 break;
1167 case R_RX_ABS16_REV:
1168 UNSAFE_FOR_PID;
1169 RX_STACK_POP (relocation);
1170 RANGE (-32768, 65535);
1171 #if RX_OPCODE_BIG_ENDIAN
1172 OP (0) = relocation;
1173 OP (1) = relocation >> 8;
1174 #else
1175 OP (1) = relocation;
1176 OP (0) = relocation >> 8;
1177 #endif
1178 break;
1180 case R_RX_ABS16S_PCREL:
1181 case R_RX_ABS16S:
1182 RX_STACK_POP (relocation);
1183 RANGE (-32768, 32767);
1184 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
1186 OP (1) = relocation;
1187 OP (0) = relocation >> 8;
1189 else
1191 OP (0) = relocation;
1192 OP (1) = relocation >> 8;
1194 break;
1196 case R_RX_ABS16U:
1197 UNSAFE_FOR_PID;
1198 RX_STACK_POP (relocation);
1199 RANGE (0, 65536);
1200 #if RX_OPCODE_BIG_ENDIAN
1201 OP (1) = relocation;
1202 OP (0) = relocation >> 8;
1203 #else
1204 OP (0) = relocation;
1205 OP (1) = relocation >> 8;
1206 #endif
1207 break;
1209 case R_RX_ABS16UL:
1210 UNSAFE_FOR_PID;
1211 RX_STACK_POP (relocation);
1212 relocation >>= 2;
1213 RANGE (0, 65536);
1214 #if RX_OPCODE_BIG_ENDIAN
1215 OP (1) = relocation;
1216 OP (0) = relocation >> 8;
1217 #else
1218 OP (0) = relocation;
1219 OP (1) = relocation >> 8;
1220 #endif
1221 break;
1223 case R_RX_ABS16UW:
1224 UNSAFE_FOR_PID;
1225 RX_STACK_POP (relocation);
1226 relocation >>= 1;
1227 RANGE (0, 65536);
1228 #if RX_OPCODE_BIG_ENDIAN
1229 OP (1) = relocation;
1230 OP (0) = relocation >> 8;
1231 #else
1232 OP (0) = relocation;
1233 OP (1) = relocation >> 8;
1234 #endif
1235 break;
1237 case R_RX_ABS8:
1238 UNSAFE_FOR_PID;
1239 RX_STACK_POP (relocation);
1240 RANGE (-128, 255);
1241 OP (0) = relocation;
1242 break;
1244 case R_RX_ABS8U:
1245 UNSAFE_FOR_PID;
1246 RX_STACK_POP (relocation);
1247 RANGE (0, 255);
1248 OP (0) = relocation;
1249 break;
1251 case R_RX_ABS8UL:
1252 UNSAFE_FOR_PID;
1253 RX_STACK_POP (relocation);
1254 relocation >>= 2;
1255 RANGE (0, 255);
1256 OP (0) = relocation;
1257 break;
1259 case R_RX_ABS8UW:
1260 UNSAFE_FOR_PID;
1261 RX_STACK_POP (relocation);
1262 relocation >>= 1;
1263 RANGE (0, 255);
1264 OP (0) = relocation;
1265 break;
1267 case R_RX_ABS8S:
1268 UNSAFE_FOR_PID;
1269 /* Fall through. */
1270 case R_RX_ABS8S_PCREL:
1271 RX_STACK_POP (relocation);
1272 RANGE (-128, 127);
1273 OP (0) = relocation;
1274 break;
1276 case R_RX_SYM:
1277 if (r_symndx < symtab_hdr->sh_info)
1278 RX_STACK_PUSH (sec->output_section->vma
1279 + sec->output_offset
1280 + sym->st_value
1281 + rel->r_addend);
1282 else
1284 if (h != NULL
1285 && (h->root.type == bfd_link_hash_defined
1286 || h->root.type == bfd_link_hash_defweak))
1287 RX_STACK_PUSH (h->root.u.def.value
1288 + sec->output_section->vma
1289 + sec->output_offset
1290 + rel->r_addend);
1291 else
1292 _bfd_error_handler
1293 (_("warning: RX_SYM reloc with an unknown symbol"));
1295 break;
1297 case R_RX_OPneg:
1299 int32_t tmp;
1301 saw_subtract = true;
1302 RX_STACK_POP (tmp);
1303 tmp = - tmp;
1304 RX_STACK_PUSH (tmp);
1306 break;
1308 case R_RX_OPadd:
1310 int32_t tmp1, tmp2;
1312 RX_STACK_POP (tmp1);
1313 RX_STACK_POP (tmp2);
1314 tmp1 += tmp2;
1315 RX_STACK_PUSH (tmp1);
1317 break;
1319 case R_RX_OPsub:
1321 int32_t tmp1, tmp2;
1323 saw_subtract = true;
1324 RX_STACK_POP (tmp1);
1325 RX_STACK_POP (tmp2);
1326 tmp2 -= tmp1;
1327 RX_STACK_PUSH (tmp2);
1329 break;
1331 case R_RX_OPmul:
1333 int32_t tmp1, tmp2;
1335 RX_STACK_POP (tmp1);
1336 RX_STACK_POP (tmp2);
1337 tmp1 *= tmp2;
1338 RX_STACK_PUSH (tmp1);
1340 break;
1342 case R_RX_OPdiv:
1344 int32_t tmp1, tmp2;
1346 RX_STACK_POP (tmp1);
1347 RX_STACK_POP (tmp2);
1348 tmp1 /= tmp2;
1349 RX_STACK_PUSH (tmp1);
1351 break;
1353 case R_RX_OPshla:
1355 int32_t tmp1, tmp2;
1357 RX_STACK_POP (tmp1);
1358 RX_STACK_POP (tmp2);
1359 tmp1 <<= tmp2;
1360 RX_STACK_PUSH (tmp1);
1362 break;
1364 case R_RX_OPshra:
1366 int32_t tmp1, tmp2;
1368 RX_STACK_POP (tmp1);
1369 RX_STACK_POP (tmp2);
1370 tmp1 >>= tmp2;
1371 RX_STACK_PUSH (tmp1);
1373 break;
1375 case R_RX_OPsctsize:
1376 RX_STACK_PUSH (input_section->size);
1377 break;
1379 case R_RX_OPscttop:
1380 RX_STACK_PUSH (input_section->output_section->vma);
1381 break;
1383 case R_RX_OPand:
1385 int32_t tmp1, tmp2;
1387 RX_STACK_POP (tmp1);
1388 RX_STACK_POP (tmp2);
1389 tmp1 &= tmp2;
1390 RX_STACK_PUSH (tmp1);
1392 break;
1394 case R_RX_OPor:
1396 int32_t tmp1, tmp2;
1398 RX_STACK_POP (tmp1);
1399 RX_STACK_POP (tmp2);
1400 tmp1 |= tmp2;
1401 RX_STACK_PUSH (tmp1);
1403 break;
1405 case R_RX_OPxor:
1407 int32_t tmp1, tmp2;
1409 RX_STACK_POP (tmp1);
1410 RX_STACK_POP (tmp2);
1411 tmp1 ^= tmp2;
1412 RX_STACK_PUSH (tmp1);
1414 break;
1416 case R_RX_OPnot:
1418 int32_t tmp;
1420 RX_STACK_POP (tmp);
1421 tmp = ~ tmp;
1422 RX_STACK_PUSH (tmp);
1424 break;
1426 case R_RX_OPmod:
1428 int32_t tmp1, tmp2;
1430 RX_STACK_POP (tmp1);
1431 RX_STACK_POP (tmp2);
1432 tmp1 %= tmp2;
1433 RX_STACK_PUSH (tmp1);
1435 break;
1437 case R_RX_OPromtop:
1438 RX_STACK_PUSH (get_romstart (info, input_bfd, input_section, rel->r_offset));
1439 break;
1441 case R_RX_OPramtop:
1442 RX_STACK_PUSH (get_ramstart (info, input_bfd, input_section, rel->r_offset));
1443 break;
1445 default:
1446 r = bfd_reloc_notsupported;
1447 break;
1450 if (r != bfd_reloc_ok)
1452 const char * msg = NULL;
1454 switch (r)
1456 case bfd_reloc_overflow:
1457 /* Catch the case of a missing function declaration
1458 and emit a more helpful error message. */
1459 if (r_type == R_RX_DIR24S_PCREL)
1460 /* xgettext:c-format */
1461 msg = _("%pB(%pA): error: call to undefined function '%s'");
1462 else
1463 (*info->callbacks->reloc_overflow)
1464 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
1465 input_bfd, input_section, rel->r_offset);
1466 break;
1468 case bfd_reloc_undefined:
1469 (*info->callbacks->undefined_symbol)
1470 (info, name, input_bfd, input_section, rel->r_offset, true);
1471 break;
1473 case bfd_reloc_other:
1474 /* xgettext:c-format */
1475 msg = _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
1476 break;
1478 case bfd_reloc_outofrange:
1479 /* xgettext:c-format */
1480 msg = _("%pB(%pA): internal error: out of range error");
1481 break;
1483 case bfd_reloc_notsupported:
1484 /* xgettext:c-format */
1485 msg = _("%pB(%pA): internal error: unsupported relocation error");
1486 break;
1488 case bfd_reloc_dangerous:
1489 /* xgettext:c-format */
1490 msg = _("%pB(%pA): internal error: dangerous relocation");
1491 break;
1493 default:
1494 /* xgettext:c-format */
1495 msg = _("%pB(%pA): internal error: unknown error");
1496 break;
1499 if (msg)
1500 _bfd_error_handler (msg, input_bfd, input_section, name);
1504 return true;
1507 /* Relaxation Support. */
1509 /* Progression of relocations from largest operand size to smallest
1510 operand size. */
1512 static int
1513 next_smaller_reloc (int r)
1515 switch (r)
1517 case R_RX_DIR32: return R_RX_DIR24S;
1518 case R_RX_DIR24S: return R_RX_DIR16S;
1519 case R_RX_DIR16S: return R_RX_DIR8S;
1520 case R_RX_DIR8S: return R_RX_NONE;
1522 case R_RX_DIR16: return R_RX_DIR8;
1523 case R_RX_DIR8: return R_RX_NONE;
1525 case R_RX_DIR16U: return R_RX_DIR8U;
1526 case R_RX_DIR8U: return R_RX_NONE;
1528 case R_RX_DIR24S_PCREL: return R_RX_DIR16S_PCREL;
1529 case R_RX_DIR16S_PCREL: return R_RX_DIR8S_PCREL;
1530 case R_RX_DIR8S_PCREL: return R_RX_DIR3U_PCREL;
1532 case R_RX_DIR16UL: return R_RX_DIR8UL;
1533 case R_RX_DIR8UL: return R_RX_NONE;
1534 case R_RX_DIR16UW: return R_RX_DIR8UW;
1535 case R_RX_DIR8UW: return R_RX_NONE;
1537 case R_RX_RH_32_OP: return R_RX_RH_24_OP;
1538 case R_RX_RH_24_OP: return R_RX_RH_16_OP;
1539 case R_RX_RH_16_OP: return R_RX_DIR8;
1541 case R_RX_ABS32: return R_RX_ABS24S;
1542 case R_RX_ABS24S: return R_RX_ABS16S;
1543 case R_RX_ABS16: return R_RX_ABS8;
1544 case R_RX_ABS16U: return R_RX_ABS8U;
1545 case R_RX_ABS16S: return R_RX_ABS8S;
1546 case R_RX_ABS8: return R_RX_NONE;
1547 case R_RX_ABS8U: return R_RX_NONE;
1548 case R_RX_ABS8S: return R_RX_NONE;
1549 case R_RX_ABS24S_PCREL: return R_RX_ABS16S_PCREL;
1550 case R_RX_ABS16S_PCREL: return R_RX_ABS8S_PCREL;
1551 case R_RX_ABS8S_PCREL: return R_RX_NONE;
1552 case R_RX_ABS16UL: return R_RX_ABS8UL;
1553 case R_RX_ABS16UW: return R_RX_ABS8UW;
1554 case R_RX_ABS8UL: return R_RX_NONE;
1555 case R_RX_ABS8UW: return R_RX_NONE;
1557 return r;
1560 /* Delete some bytes from a section while relaxing. */
1562 static bool
1563 elf32_rx_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
1564 Elf_Internal_Rela *alignment_rel, int force_snip,
1565 Elf_Internal_Rela *irelstart)
1567 Elf_Internal_Shdr * symtab_hdr;
1568 unsigned int sec_shndx;
1569 bfd_byte * contents;
1570 Elf_Internal_Rela * irel;
1571 Elf_Internal_Rela * irelend;
1572 Elf_Internal_Sym * isym;
1573 Elf_Internal_Sym * isymend;
1574 bfd_vma toaddr;
1575 unsigned int symcount;
1576 struct elf_link_hash_entry ** sym_hashes;
1577 struct elf_link_hash_entry ** end_hashes;
1579 if (!alignment_rel)
1580 force_snip = 1;
1582 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1584 contents = elf_section_data (sec)->this_hdr.contents;
1586 /* The deletion must stop at the next alignment boundary, if
1587 ALIGNMENT_REL is non-NULL. */
1588 toaddr = sec->size;
1589 if (alignment_rel)
1590 toaddr = alignment_rel->r_offset;
1592 BFD_ASSERT (toaddr > addr);
1594 /* Actually delete the bytes. */
1595 memmove (contents + addr, contents + addr + count,
1596 (size_t) (toaddr - addr - count));
1598 /* If we don't have an alignment marker to worry about, we can just
1599 shrink the section. Otherwise, we have to fill in the newly
1600 created gap with NOP insns (0x03). */
1601 if (force_snip)
1602 sec->size -= count;
1603 else
1604 memset (contents + toaddr - count, 0x03, count);
1606 irel = irelstart;
1607 BFD_ASSERT (irel != NULL || sec->reloc_count == 0);
1608 irelend = irel + sec->reloc_count;
1610 /* Adjust all the relocs. */
1611 for (; irel < irelend; irel++)
1613 /* Get the new reloc address. */
1614 if (irel->r_offset > addr
1615 && (irel->r_offset < toaddr
1616 || (force_snip && irel->r_offset == toaddr)))
1617 irel->r_offset -= count;
1619 /* If we see an ALIGN marker at the end of the gap, we move it
1620 to the beginning of the gap, since marking these gaps is what
1621 they're for. */
1622 if (irel->r_offset == toaddr
1623 && ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
1624 && irel->r_addend & RX_RELAXA_ALIGN)
1625 irel->r_offset -= count;
1628 /* Adjust the local symbols defined in this section. */
1629 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1630 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1631 isymend = isym + symtab_hdr->sh_info;
1633 for (; isym < isymend; isym++)
1635 /* If the symbol is in the range of memory we just moved, we
1636 have to adjust its value. */
1637 if (isym->st_shndx == sec_shndx
1638 && isym->st_value > addr
1639 && isym->st_value < toaddr)
1640 isym->st_value -= count;
1642 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1643 *end* is in the moved bytes but it's *start* isn't), then we
1644 must adjust its size. */
1645 if (isym->st_shndx == sec_shndx
1646 && isym->st_value < addr
1647 && isym->st_value + isym->st_size > addr
1648 && isym->st_value + isym->st_size < toaddr)
1649 isym->st_size -= count;
1652 /* Now adjust the global symbols defined in this section. */
1653 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1654 - symtab_hdr->sh_info);
1655 sym_hashes = elf_sym_hashes (abfd);
1656 end_hashes = sym_hashes + symcount;
1658 for (; sym_hashes < end_hashes; sym_hashes++)
1660 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1662 if ((sym_hash->root.type == bfd_link_hash_defined
1663 || sym_hash->root.type == bfd_link_hash_defweak)
1664 && sym_hash->root.u.def.section == sec)
1666 /* As above, adjust the value if needed. */
1667 if (sym_hash->root.u.def.value > addr
1668 && sym_hash->root.u.def.value < toaddr)
1669 sym_hash->root.u.def.value -= count;
1671 /* As above, adjust the size if needed. */
1672 if (sym_hash->root.u.def.value < addr
1673 && sym_hash->root.u.def.value + sym_hash->size > addr
1674 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1675 sym_hash->size -= count;
1679 return true;
1682 /* Used to sort relocs by address. If relocs have the same address,
1683 we maintain their relative order, except that R_RX_RH_RELAX
1684 alignment relocs must be the first reloc for any given address. */
1686 static void
1687 reloc_bubblesort (Elf_Internal_Rela * r, int count)
1689 int i;
1690 bool again;
1691 bool swappit;
1693 /* This is almost a classic bubblesort. It's the slowest sort, but
1694 we're taking advantage of the fact that the relocations are
1695 mostly in order already (the assembler emits them that way) and
1696 we need relocs with the same address to remain in the same
1697 relative order. */
1698 again = true;
1699 while (again)
1701 again = false;
1702 for (i = 0; i < count - 1; i ++)
1704 if (r[i].r_offset > r[i + 1].r_offset)
1705 swappit = true;
1706 else if (r[i].r_offset < r[i + 1].r_offset)
1707 swappit = false;
1708 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
1709 && (r[i + 1].r_addend & RX_RELAXA_ALIGN))
1710 swappit = true;
1711 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
1712 && (r[i + 1].r_addend & RX_RELAXA_ELIGN)
1713 && !(ELF32_R_TYPE (r[i].r_info) == R_RX_RH_RELAX
1714 && (r[i].r_addend & RX_RELAXA_ALIGN)))
1715 swappit = true;
1716 else
1717 swappit = false;
1719 if (swappit)
1721 Elf_Internal_Rela tmp;
1723 tmp = r[i];
1724 r[i] = r[i + 1];
1725 r[i + 1] = tmp;
1726 /* If we do move a reloc back, re-scan to see if it
1727 needs to be moved even further back. This avoids
1728 most of the O(n^2) behavior for our cases. */
1729 if (i > 0)
1730 i -= 2;
1731 again = true;
1738 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1739 rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1740 lrel, abfd, sec, link_info, scale)
1742 static bfd_vma
1743 rx_offset_for_reloc (bfd * abfd,
1744 Elf_Internal_Rela * rel,
1745 Elf_Internal_Shdr * symtab_hdr,
1746 bfd_byte * shndx_buf ATTRIBUTE_UNUSED,
1747 Elf_Internal_Sym * intsyms,
1748 Elf_Internal_Rela ** lrel,
1749 bfd * input_bfd,
1750 asection * input_section,
1751 struct bfd_link_info * info,
1752 int * scale)
1754 bfd_vma symval;
1755 bfd_reloc_status_type r;
1757 *scale = 1;
1759 /* REL is the first of 1..N relocations. We compute the symbol
1760 value for each relocation, then combine them if needed. LREL
1761 gets a pointer to the last relocation used. */
1762 while (1)
1764 int32_t tmp1, tmp2;
1766 /* Get the value of the symbol referred to by the reloc. */
1767 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1769 /* A local symbol. */
1770 Elf_Internal_Sym *isym;
1771 asection *ssec;
1773 isym = intsyms + ELF32_R_SYM (rel->r_info);
1775 if (isym->st_shndx == SHN_UNDEF)
1776 ssec = bfd_und_section_ptr;
1777 else if (isym->st_shndx == SHN_ABS)
1778 ssec = bfd_abs_section_ptr;
1779 else if (isym->st_shndx == SHN_COMMON)
1780 ssec = bfd_com_section_ptr;
1781 else
1782 ssec = bfd_section_from_elf_index (abfd,
1783 isym->st_shndx);
1785 /* Initial symbol value. */
1786 symval = isym->st_value;
1788 /* GAS may have made this symbol relative to a section, in
1789 which case, we have to add the addend to find the
1790 symbol. */
1791 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
1792 symval += rel->r_addend;
1794 if (ssec)
1796 if ((ssec->flags & SEC_MERGE)
1797 && ssec->sec_info_type == SEC_INFO_TYPE_MERGE)
1798 symval = _bfd_merged_section_offset (abfd, & ssec,
1799 elf_section_data (ssec)->sec_info,
1800 symval);
1803 /* Now make the offset relative to where the linker is putting it. */
1804 if (ssec)
1805 symval +=
1806 ssec->output_section->vma + ssec->output_offset;
1808 symval += rel->r_addend;
1810 else
1812 unsigned long indx;
1813 struct elf_link_hash_entry * h;
1815 /* An external symbol. */
1816 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1817 h = elf_sym_hashes (abfd)[indx];
1818 BFD_ASSERT (h != NULL);
1820 if (h->root.type != bfd_link_hash_defined
1821 && h->root.type != bfd_link_hash_defweak)
1823 /* This appears to be a reference to an undefined
1824 symbol. Just ignore it--it will be caught by the
1825 regular reloc processing. */
1826 if (lrel)
1827 *lrel = rel;
1828 return 0;
1831 symval = (h->root.u.def.value
1832 + h->root.u.def.section->output_section->vma
1833 + h->root.u.def.section->output_offset);
1835 symval += rel->r_addend;
1838 switch (ELF32_R_TYPE (rel->r_info))
1840 case R_RX_SYM:
1841 RX_STACK_PUSH (symval);
1842 break;
1844 case R_RX_OPneg:
1845 RX_STACK_POP (tmp1);
1846 tmp1 = - tmp1;
1847 RX_STACK_PUSH (tmp1);
1848 break;
1850 case R_RX_OPadd:
1851 RX_STACK_POP (tmp1);
1852 RX_STACK_POP (tmp2);
1853 tmp1 += tmp2;
1854 RX_STACK_PUSH (tmp1);
1855 break;
1857 case R_RX_OPsub:
1858 RX_STACK_POP (tmp1);
1859 RX_STACK_POP (tmp2);
1860 tmp2 -= tmp1;
1861 RX_STACK_PUSH (tmp2);
1862 break;
1864 case R_RX_OPmul:
1865 RX_STACK_POP (tmp1);
1866 RX_STACK_POP (tmp2);
1867 tmp1 *= tmp2;
1868 RX_STACK_PUSH (tmp1);
1869 break;
1871 case R_RX_OPdiv:
1872 RX_STACK_POP (tmp1);
1873 RX_STACK_POP (tmp2);
1874 tmp1 /= tmp2;
1875 RX_STACK_PUSH (tmp1);
1876 break;
1878 case R_RX_OPshla:
1879 RX_STACK_POP (tmp1);
1880 RX_STACK_POP (tmp2);
1881 tmp1 <<= tmp2;
1882 RX_STACK_PUSH (tmp1);
1883 break;
1885 case R_RX_OPshra:
1886 RX_STACK_POP (tmp1);
1887 RX_STACK_POP (tmp2);
1888 tmp1 >>= tmp2;
1889 RX_STACK_PUSH (tmp1);
1890 break;
1892 case R_RX_OPsctsize:
1893 RX_STACK_PUSH (input_section->size);
1894 break;
1896 case R_RX_OPscttop:
1897 RX_STACK_PUSH (input_section->output_section->vma);
1898 break;
1900 case R_RX_OPand:
1901 RX_STACK_POP (tmp1);
1902 RX_STACK_POP (tmp2);
1903 tmp1 &= tmp2;
1904 RX_STACK_PUSH (tmp1);
1905 break;
1907 case R_RX_OPor:
1908 RX_STACK_POP (tmp1);
1909 RX_STACK_POP (tmp2);
1910 tmp1 |= tmp2;
1911 RX_STACK_PUSH (tmp1);
1912 break;
1914 case R_RX_OPxor:
1915 RX_STACK_POP (tmp1);
1916 RX_STACK_POP (tmp2);
1917 tmp1 ^= tmp2;
1918 RX_STACK_PUSH (tmp1);
1919 break;
1921 case R_RX_OPnot:
1922 RX_STACK_POP (tmp1);
1923 tmp1 = ~ tmp1;
1924 RX_STACK_PUSH (tmp1);
1925 break;
1927 case R_RX_OPmod:
1928 RX_STACK_POP (tmp1);
1929 RX_STACK_POP (tmp2);
1930 tmp1 %= tmp2;
1931 RX_STACK_PUSH (tmp1);
1932 break;
1934 case R_RX_OPromtop:
1935 RX_STACK_PUSH (get_romstart (info, input_bfd, input_section, rel->r_offset));
1936 break;
1938 case R_RX_OPramtop:
1939 RX_STACK_PUSH (get_ramstart (info, input_bfd, input_section, rel->r_offset));
1940 break;
1942 case R_RX_DIR16UL:
1943 case R_RX_DIR8UL:
1944 case R_RX_ABS16UL:
1945 case R_RX_ABS8UL:
1946 if (rx_stack_top)
1947 RX_STACK_POP (symval);
1948 if (lrel)
1949 *lrel = rel;
1950 *scale = 4;
1951 return symval;
1953 case R_RX_DIR16UW:
1954 case R_RX_DIR8UW:
1955 case R_RX_ABS16UW:
1956 case R_RX_ABS8UW:
1957 if (rx_stack_top)
1958 RX_STACK_POP (symval);
1959 if (lrel)
1960 *lrel = rel;
1961 *scale = 2;
1962 return symval;
1964 default:
1965 if (rx_stack_top)
1966 RX_STACK_POP (symval);
1967 if (lrel)
1968 *lrel = rel;
1969 return symval;
1972 rel ++;
1974 /* FIXME. */
1975 (void) r;
1978 static void
1979 move_reloc (Elf_Internal_Rela * irel, Elf_Internal_Rela * srel, int delta)
1981 bfd_vma old_offset = srel->r_offset;
1983 irel ++;
1984 while (irel <= srel)
1986 if (irel->r_offset == old_offset)
1987 irel->r_offset += delta;
1988 irel ++;
1992 /* Relax one section. */
1994 static bool
1995 elf32_rx_relax_section (bfd *abfd,
1996 asection *sec,
1997 struct bfd_link_info *link_info,
1998 bool *again,
1999 bool allow_pcrel3)
2001 Elf_Internal_Shdr *symtab_hdr;
2002 Elf_Internal_Shdr *shndx_hdr;
2003 Elf_Internal_Rela *internal_relocs;
2004 Elf_Internal_Rela *irel;
2005 Elf_Internal_Rela *srel;
2006 Elf_Internal_Rela *irelend;
2007 Elf_Internal_Rela *next_alignment;
2008 Elf_Internal_Rela *prev_alignment;
2009 bfd_byte *contents = NULL;
2010 bfd_byte *free_contents = NULL;
2011 Elf_Internal_Sym *intsyms = NULL;
2012 Elf_Internal_Sym *free_intsyms = NULL;
2013 bfd_byte *shndx_buf = NULL;
2014 bfd_vma pc;
2015 bfd_vma sec_start;
2016 bfd_vma symval = 0;
2017 int pcrel = 0;
2018 int code = 0;
2019 int section_alignment_glue;
2020 /* how much to scale the relocation by - 1, 2, or 4. */
2021 int scale;
2023 /* Assume nothing changes. */
2024 *again = false;
2026 /* We don't have to do anything for a relocatable link, if
2027 this section does not have relocs, or if this is not a
2028 code section. */
2029 if (bfd_link_relocatable (link_info)
2030 || (sec->flags & SEC_RELOC) == 0
2031 || sec->reloc_count == 0
2032 || (sec->flags & SEC_CODE) == 0)
2033 return true;
2035 symtab_hdr = & elf_symtab_hdr (abfd);
2036 if (elf_symtab_shndx_list (abfd))
2037 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
2038 else
2039 shndx_hdr = NULL;
2041 sec_start = sec->output_section->vma + sec->output_offset;
2043 /* Get the section contents. */
2044 if (elf_section_data (sec)->this_hdr.contents != NULL)
2045 contents = elf_section_data (sec)->this_hdr.contents;
2046 /* Go get them off disk. */
2047 else
2049 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2050 goto error_return;
2051 elf_section_data (sec)->this_hdr.contents = contents;
2054 /* Read this BFD's symbols. */
2055 /* Get cached copy if it exists. */
2056 if (symtab_hdr->contents != NULL)
2057 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2058 else
2060 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
2061 symtab_hdr->contents = (bfd_byte *) intsyms;
2064 if (shndx_hdr && shndx_hdr->sh_size != 0)
2066 size_t amt;
2068 if (_bfd_mul_overflow (symtab_hdr->sh_info,
2069 sizeof (Elf_External_Sym_Shndx), &amt))
2071 bfd_set_error (bfd_error_file_too_big);
2072 goto error_return;
2074 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0)
2075 goto error_return;
2076 shndx_buf = _bfd_malloc_and_read (abfd, amt, amt);
2077 if (shndx_buf == NULL)
2078 goto error_return;
2079 shndx_hdr->contents = shndx_buf;
2082 /* Get a copy of the native relocations. */
2083 /* Note - we ignore the setting of link_info->keep_memory when reading
2084 in these relocs. We have to maintain a permanent copy of the relocs
2085 because we are going to walk over them multiple times, adjusting them
2086 as bytes are deleted from the section, and with this relaxation
2087 function itself being called multiple times on the same section... */
2088 internal_relocs = _bfd_elf_link_read_relocs
2089 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, true);
2090 if (internal_relocs == NULL)
2091 goto error_return;
2093 /* The RL_ relocs must be just before the operand relocs they go
2094 with, so we must sort them to guarantee this. We use bubblesort
2095 instead of qsort so we can guarantee that relocs with the same
2096 address remain in the same relative order. */
2097 reloc_bubblesort (internal_relocs, sec->reloc_count);
2099 /* Walk through them looking for relaxing opportunities. */
2100 irelend = internal_relocs + sec->reloc_count;
2102 /* This will either be NULL or a pointer to the next alignment
2103 relocation. */
2104 next_alignment = internal_relocs;
2105 /* This will be the previous alignment, although at first it points
2106 to the first real relocation. */
2107 prev_alignment = internal_relocs;
2109 /* We calculate worst case shrinkage caused by alignment directives.
2110 No fool-proof, but better than either ignoring the problem or
2111 doing heavy duty analysis of all the alignment markers in all
2112 input sections. */
2113 section_alignment_glue = 0;
2114 for (irel = internal_relocs; irel < irelend; irel++)
2115 if (ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
2116 && irel->r_addend & RX_RELAXA_ALIGN)
2118 int this_glue = 1 << (irel->r_addend & RX_RELAXA_ANUM);
2120 if (section_alignment_glue < this_glue)
2121 section_alignment_glue = this_glue;
2123 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2124 shrinkage. */
2125 section_alignment_glue *= 2;
2127 for (irel = internal_relocs; irel < irelend; irel++)
2129 unsigned char *insn;
2130 int nrelocs;
2132 /* The insns we care about are all marked with one of these. */
2133 if (ELF32_R_TYPE (irel->r_info) != R_RX_RH_RELAX)
2134 continue;
2136 if (irel->r_addend & RX_RELAXA_ALIGN
2137 || next_alignment == internal_relocs)
2139 /* When we delete bytes, we need to maintain all the alignments
2140 indicated. In addition, we need to be careful about relaxing
2141 jumps across alignment boundaries - these displacements
2142 *grow* when we delete bytes. For now, don't shrink
2143 displacements across an alignment boundary, just in case.
2144 Note that this only affects relocations to the same
2145 section. */
2146 prev_alignment = next_alignment;
2147 next_alignment += 2;
2148 while (next_alignment < irelend
2149 && (ELF32_R_TYPE (next_alignment->r_info) != R_RX_RH_RELAX
2150 || !(next_alignment->r_addend & RX_RELAXA_ELIGN)))
2151 next_alignment ++;
2152 if (next_alignment >= irelend || next_alignment->r_offset == 0)
2153 next_alignment = NULL;
2156 /* When we hit alignment markers, see if we've shrunk enough
2157 before them to reduce the gap without violating the alignment
2158 requirements. */
2159 if (irel->r_addend & RX_RELAXA_ALIGN)
2161 /* At this point, the next relocation *should* be the ELIGN
2162 end marker. */
2163 Elf_Internal_Rela *erel = irel + 1;
2164 unsigned int alignment, nbytes;
2166 if (ELF32_R_TYPE (erel->r_info) != R_RX_RH_RELAX)
2167 continue;
2168 if (!(erel->r_addend & RX_RELAXA_ELIGN))
2169 continue;
2171 alignment = 1 << (irel->r_addend & RX_RELAXA_ANUM);
2173 if (erel->r_offset - irel->r_offset < alignment)
2174 continue;
2176 nbytes = erel->r_offset - irel->r_offset;
2177 nbytes /= alignment;
2178 nbytes *= alignment;
2180 elf32_rx_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment,
2181 erel->r_offset == sec->size, internal_relocs);
2182 *again = true;
2184 continue;
2187 if (irel->r_addend & RX_RELAXA_ELIGN)
2188 continue;
2190 insn = contents + irel->r_offset;
2192 nrelocs = irel->r_addend & RX_RELAXA_RNUM;
2194 /* At this point, we have an insn that is a candidate for linker
2195 relaxation. There are NRELOCS relocs following that may be
2196 relaxed, although each reloc may be made of more than one
2197 reloc entry (such as gp-rel symbols). */
2199 /* Get the value of the symbol referred to by the reloc. Just
2200 in case this is the last reloc in the list, use the RL's
2201 addend to choose between this reloc (no addend) or the next
2202 (yes addend, which means at least one following reloc). */
2204 /* srel points to the "current" reloction for this insn -
2205 actually the last reloc for a given operand, which is the one
2206 we need to update. We check the relaxations in the same
2207 order that the relocations happen, so we'll just push it
2208 along as we go. */
2209 srel = irel;
2211 pc = sec->output_section->vma + sec->output_offset
2212 + srel->r_offset;
2214 #define GET_RELOC \
2215 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2216 pcrel = symval - pc + srel->r_addend; \
2217 nrelocs --;
2219 #define SNIPNR(offset, nbytes) \
2220 elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0, internal_relocs);
2221 #define SNIP(offset, nbytes, newtype) \
2222 SNIPNR (offset, nbytes); \
2223 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2225 /* The order of these bit tests must match the order that the
2226 relocs appear in. Since we sorted those by offset, we can
2227 predict them. */
2229 /* Note that the numbers in, say, DSP6 are the bit offsets of
2230 the code fields that describe the operand. Bits number 0 for
2231 the MSB of insn[0]. */
2233 /* DSP* codes:
2234 0 00 [reg]
2235 1 01 dsp:8[reg]
2236 2 10 dsp:16[reg]
2237 3 11 reg */
2238 if (irel->r_addend & RX_RELAXA_DSP6)
2240 GET_RELOC;
2242 code = insn[0] & 3;
2243 if (code == 2 && symval/scale <= 255)
2245 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2246 insn[0] &= 0xfc;
2247 insn[0] |= 0x01;
2248 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2249 if (newrel != ELF32_R_TYPE (srel->r_info))
2251 SNIP (3, 1, newrel);
2252 *again = true;
2256 else if (code == 1 && symval == 0)
2258 insn[0] &= 0xfc;
2259 SNIP (2, 1, R_RX_NONE);
2260 *again = true;
2263 /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst. */
2264 else if (code == 1 && symval/scale <= 31
2265 /* Decodable bits. */
2266 && (insn[0] & 0xcc) == 0xcc
2267 /* Width. */
2268 && (insn[0] & 0x30) != 0x30
2269 /* Register MSBs. */
2270 && (insn[1] & 0x88) == 0x00)
2272 int newrel = 0;
2274 insn[0] = 0x88 | (insn[0] & 0x30);
2275 /* The register fields are in the right place already. */
2277 /* We can't relax this new opcode. */
2278 irel->r_addend = 0;
2280 switch ((insn[0] & 0x30) >> 4)
2282 case 0:
2283 newrel = R_RX_RH_ABS5p5B;
2284 break;
2285 case 1:
2286 newrel = R_RX_RH_ABS5p5W;
2287 break;
2288 case 2:
2289 newrel = R_RX_RH_ABS5p5L;
2290 break;
2293 move_reloc (irel, srel, -2);
2294 SNIP (2, 1, newrel);
2297 /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst. */
2298 else if (code == 1 && symval/scale <= 31
2299 /* Decodable bits. */
2300 && (insn[0] & 0xf8) == 0x58
2301 /* Register MSBs. */
2302 && (insn[1] & 0x88) == 0x00)
2304 int newrel = 0;
2306 insn[0] = 0xb0 | ((insn[0] & 0x04) << 1);
2307 /* The register fields are in the right place already. */
2309 /* We can't relax this new opcode. */
2310 irel->r_addend = 0;
2312 switch ((insn[0] & 0x08) >> 3)
2314 case 0:
2315 newrel = R_RX_RH_ABS5p5B;
2316 break;
2317 case 1:
2318 newrel = R_RX_RH_ABS5p5W;
2319 break;
2322 move_reloc (irel, srel, -2);
2323 SNIP (2, 1, newrel);
2327 /* A DSP4 operand always follows a DSP6 operand, even if there's
2328 no relocation for it. We have to read the code out of the
2329 opcode to calculate the offset of the operand. */
2330 if (irel->r_addend & RX_RELAXA_DSP4)
2332 int code6, offset = 0;
2334 GET_RELOC;
2336 code6 = insn[0] & 0x03;
2337 switch (code6)
2339 case 0: offset = 2; break;
2340 case 1: offset = 3; break;
2341 case 2: offset = 4; break;
2342 case 3: offset = 2; break;
2345 code = (insn[0] & 0x0c) >> 2;
2347 if (code == 2 && symval / scale <= 255)
2349 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2351 insn[0] &= 0xf3;
2352 insn[0] |= 0x04;
2353 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2354 if (newrel != ELF32_R_TYPE (srel->r_info))
2356 SNIP (offset+1, 1, newrel);
2357 *again = true;
2361 else if (code == 1 && symval == 0)
2363 insn[0] &= 0xf3;
2364 SNIP (offset, 1, R_RX_NONE);
2365 *again = true;
2367 /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2368 else if (code == 1 && symval/scale <= 31
2369 /* Decodable bits. */
2370 && (insn[0] & 0xc3) == 0xc3
2371 /* Width. */
2372 && (insn[0] & 0x30) != 0x30
2373 /* Register MSBs. */
2374 && (insn[1] & 0x88) == 0x00)
2376 int newrel = 0;
2378 insn[0] = 0x80 | (insn[0] & 0x30);
2379 /* The register fields are in the right place already. */
2381 /* We can't relax this new opcode. */
2382 irel->r_addend = 0;
2384 switch ((insn[0] & 0x30) >> 4)
2386 case 0:
2387 newrel = R_RX_RH_ABS5p5B;
2388 break;
2389 case 1:
2390 newrel = R_RX_RH_ABS5p5W;
2391 break;
2392 case 2:
2393 newrel = R_RX_RH_ABS5p5L;
2394 break;
2397 move_reloc (irel, srel, -2);
2398 SNIP (2, 1, newrel);
2402 /* These always occur alone, but the offset depends on whether
2403 it's a MEMEX opcode (0x06) or not. */
2404 if (irel->r_addend & RX_RELAXA_DSP14)
2406 int offset;
2407 GET_RELOC;
2409 if (insn[0] == 0x06)
2410 offset = 3;
2411 else
2412 offset = 4;
2414 code = insn[1] & 3;
2416 if (code == 2 && symval / scale <= 255)
2418 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2420 insn[1] &= 0xfc;
2421 insn[1] |= 0x01;
2422 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2423 if (newrel != ELF32_R_TYPE (srel->r_info))
2425 SNIP (offset, 1, newrel);
2426 *again = true;
2429 else if (code == 1 && symval == 0)
2431 insn[1] &= 0xfc;
2432 SNIP (offset, 1, R_RX_NONE);
2433 *again = true;
2437 /* IMM* codes:
2438 0 00 imm:32
2439 1 01 simm:8
2440 2 10 simm:16
2441 3 11 simm:24. */
2443 /* These always occur alone. */
2444 if (irel->r_addend & RX_RELAXA_IMM6)
2446 long ssymval;
2448 GET_RELOC;
2450 /* These relocations sign-extend, so we must do signed compares. */
2451 ssymval = (long) symval;
2453 code = insn[0] & 0x03;
2455 if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
2457 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2459 insn[0] &= 0xfc;
2460 insn[0] |= 0x03;
2461 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2462 if (newrel != ELF32_R_TYPE (srel->r_info))
2464 SNIP (2, 1, newrel);
2465 *again = true;
2469 else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
2471 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2473 insn[0] &= 0xfc;
2474 insn[0] |= 0x02;
2475 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2476 if (newrel != ELF32_R_TYPE (srel->r_info))
2478 SNIP (2, 1, newrel);
2479 *again = true;
2483 /* Special case UIMM8 format: CMP #uimm8,Rdst. */
2484 else if (code == 2 && ssymval <= 255 && ssymval >= 16
2485 /* Decodable bits. */
2486 && (insn[0] & 0xfc) == 0x74
2487 /* Decodable bits. */
2488 && ((insn[1] & 0xf0) == 0x00))
2490 int newrel;
2492 insn[0] = 0x75;
2493 insn[1] = 0x50 | (insn[1] & 0x0f);
2495 /* We can't relax this new opcode. */
2496 irel->r_addend = 0;
2498 if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
2499 newrel = R_RX_ABS8U;
2500 else
2501 newrel = R_RX_DIR8U;
2503 SNIP (2, 1, newrel);
2504 *again = true;
2507 else if (code == 2 && ssymval <= 127 && ssymval >= -128)
2509 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2511 insn[0] &= 0xfc;
2512 insn[0] |= 0x01;
2513 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2514 if (newrel != ELF32_R_TYPE (srel->r_info))
2516 SNIP (2, 1, newrel);
2517 *again = true;
2521 /* Special case UIMM4 format: CMP, MUL, AND, OR. */
2522 else if (code == 1 && ssymval <= 15 && ssymval >= 0
2523 /* Decodable bits and immediate type. */
2524 && insn[0] == 0x75
2525 /* Decodable bits. */
2526 && (insn[1] & 0xc0) == 0x00)
2528 static const int newop[4] = { 1, 3, 4, 5 };
2530 insn[0] = 0x60 | newop[insn[1] >> 4];
2531 /* The register number doesn't move. */
2533 /* We can't relax this new opcode. */
2534 irel->r_addend = 0;
2536 move_reloc (irel, srel, -1);
2538 SNIP (2, 1, R_RX_RH_UIMM4p8);
2539 *again = true;
2542 /* Special case UIMM4 format: ADD -> ADD/SUB. */
2543 else if (code == 1 && ssymval <= 15 && ssymval >= -15
2544 /* Decodable bits and immediate type. */
2545 && insn[0] == 0x71
2546 /* Same register for source and destination. */
2547 && ((insn[1] >> 4) == (insn[1] & 0x0f)))
2549 int newrel;
2551 /* Note that we can't turn "add $0,Rs" into a NOP
2552 because the flags need to be set right. */
2554 if (ssymval < 0)
2556 insn[0] = 0x60; /* Subtract. */
2557 newrel = R_RX_RH_UNEG4p8;
2559 else
2561 insn[0] = 0x62; /* Add. */
2562 newrel = R_RX_RH_UIMM4p8;
2565 /* The register number is in the right place. */
2567 /* We can't relax this new opcode. */
2568 irel->r_addend = 0;
2570 move_reloc (irel, srel, -1);
2572 SNIP (2, 1, newrel);
2573 *again = true;
2577 /* These are either matched with a DSP6 (2-byte base) or an id24
2578 (3-byte base). */
2579 if (irel->r_addend & RX_RELAXA_IMM12)
2581 int dspcode, offset = 0;
2582 long ssymval;
2584 GET_RELOC;
2586 if ((insn[0] & 0xfc) == 0xfc)
2587 dspcode = 1; /* Just something with one byte operand. */
2588 else
2589 dspcode = insn[0] & 3;
2590 switch (dspcode)
2592 case 0: offset = 2; break;
2593 case 1: offset = 3; break;
2594 case 2: offset = 4; break;
2595 case 3: offset = 2; break;
2598 /* These relocations sign-extend, so we must do signed compares. */
2599 ssymval = (long) symval;
2601 code = (insn[1] >> 2) & 3;
2602 if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
2604 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2606 insn[1] &= 0xf3;
2607 insn[1] |= 0x0c;
2608 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2609 if (newrel != ELF32_R_TYPE (srel->r_info))
2611 SNIP (offset, 1, newrel);
2612 *again = true;
2616 else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
2618 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2620 insn[1] &= 0xf3;
2621 insn[1] |= 0x08;
2622 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2623 if (newrel != ELF32_R_TYPE (srel->r_info))
2625 SNIP (offset, 1, newrel);
2626 *again = true;
2630 /* Special case UIMM8 format: MOV #uimm8,Rdst. */
2631 else if (code == 2 && ssymval <= 255 && ssymval >= 16
2632 /* Decodable bits. */
2633 && insn[0] == 0xfb
2634 /* Decodable bits. */
2635 && ((insn[1] & 0x03) == 0x02))
2637 int newrel;
2639 insn[0] = 0x75;
2640 insn[1] = 0x40 | (insn[1] >> 4);
2642 /* We can't relax this new opcode. */
2643 irel->r_addend = 0;
2645 if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
2646 newrel = R_RX_ABS8U;
2647 else
2648 newrel = R_RX_DIR8U;
2650 SNIP (2, 1, newrel);
2651 *again = true;
2654 else if (code == 2 && ssymval <= 127 && ssymval >= -128)
2656 unsigned int newrel = ELF32_R_TYPE(srel->r_info);
2658 insn[1] &= 0xf3;
2659 insn[1] |= 0x04;
2660 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2661 if (newrel != ELF32_R_TYPE(srel->r_info))
2663 SNIP (offset, 1, newrel);
2664 *again = true;
2668 /* Special case UIMM4 format: MOV #uimm4,Rdst. */
2669 else if (code == 1 && ssymval <= 15 && ssymval >= 0
2670 /* Decodable bits. */
2671 && insn[0] == 0xfb
2672 /* Decodable bits. */
2673 && ((insn[1] & 0x03) == 0x02))
2675 insn[0] = 0x66;
2676 insn[1] = insn[1] >> 4;
2678 /* We can't relax this new opcode. */
2679 irel->r_addend = 0;
2681 move_reloc (irel, srel, -1);
2683 SNIP (2, 1, R_RX_RH_UIMM4p8);
2684 *again = true;
2688 if (irel->r_addend & RX_RELAXA_BRA)
2690 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2691 int max_pcrel3 = 4;
2692 int alignment_glue = 0;
2694 GET_RELOC;
2696 /* Branches over alignment chunks are problematic, as
2697 deleting bytes here makes the branch *further* away. We
2698 can be agressive with branches within this alignment
2699 block, but not branches outside it. */
2700 if ((prev_alignment == NULL
2701 || symval < (bfd_vma)(sec_start + prev_alignment->r_offset))
2702 && (next_alignment == NULL
2703 || symval > (bfd_vma)(sec_start + next_alignment->r_offset)))
2704 alignment_glue = section_alignment_glue;
2706 if (ELF32_R_TYPE(srel[1].r_info) == R_RX_RH_RELAX
2707 && srel[1].r_addend & RX_RELAXA_BRA
2708 && srel[1].r_offset < irel->r_offset + pcrel)
2709 max_pcrel3 ++;
2711 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2713 /* The values we compare PCREL with are not what you'd
2714 expect; they're off by a little to compensate for (1)
2715 where the reloc is relative to the insn, and (2) how much
2716 the insn is going to change when we relax it. */
2718 /* These we have to decode. */
2719 switch (insn[0])
2721 case 0x04: /* BRA pcdsp:24 */
2722 if (-32768 + alignment_glue <= pcrel
2723 && pcrel <= 32765 - alignment_glue)
2725 insn[0] = 0x38;
2726 SNIP (3, 1, newrel);
2727 *again = true;
2729 break;
2731 case 0x38: /* BRA pcdsp:16 */
2732 if (-128 + alignment_glue <= pcrel
2733 && pcrel <= 127 - alignment_glue)
2735 insn[0] = 0x2e;
2736 SNIP (2, 1, newrel);
2737 *again = true;
2739 break;
2741 case 0x2e: /* BRA pcdsp:8 */
2742 /* Note that there's a risk here of shortening things so
2743 much that we no longer fit this reloc; it *should*
2744 only happen when you branch across a branch, and that
2745 branch also devolves into BRA.S. "Real" code should
2746 be OK. */
2747 if (max_pcrel3 + alignment_glue <= pcrel
2748 && pcrel <= 10 - alignment_glue
2749 && allow_pcrel3)
2751 insn[0] = 0x08;
2752 SNIP (1, 1, newrel);
2753 move_reloc (irel, srel, -1);
2754 *again = true;
2756 break;
2758 case 0x05: /* BSR pcdsp:24 */
2759 if (-32768 + alignment_glue <= pcrel
2760 && pcrel <= 32765 - alignment_glue)
2762 insn[0] = 0x39;
2763 SNIP (1, 1, newrel);
2764 *again = true;
2766 break;
2768 case 0x3a: /* BEQ.W pcdsp:16 */
2769 case 0x3b: /* BNE.W pcdsp:16 */
2770 if (-128 + alignment_glue <= pcrel
2771 && pcrel <= 127 - alignment_glue)
2773 insn[0] = 0x20 | (insn[0] & 1);
2774 SNIP (1, 1, newrel);
2775 *again = true;
2777 break;
2779 case 0x20: /* BEQ.B pcdsp:8 */
2780 case 0x21: /* BNE.B pcdsp:8 */
2781 if (max_pcrel3 + alignment_glue <= pcrel
2782 && pcrel - alignment_glue <= 10
2783 && allow_pcrel3)
2785 insn[0] = 0x10 | ((insn[0] & 1) << 3);
2786 SNIP (1, 1, newrel);
2787 move_reloc (irel, srel, -1);
2788 *again = true;
2790 break;
2792 case 0x16: /* synthetic BNE dsp24 */
2793 case 0x1e: /* synthetic BEQ dsp24 */
2794 if (-32767 + alignment_glue <= pcrel
2795 && pcrel <= 32766 - alignment_glue
2796 && insn[1] == 0x04)
2798 if (insn[0] == 0x16)
2799 insn[0] = 0x3b;
2800 else
2801 insn[0] = 0x3a;
2802 /* We snip out the bytes at the end else the reloc
2803 will get moved too, and too much. */
2804 SNIP (3, 2, newrel);
2805 move_reloc (irel, srel, -1);
2806 *again = true;
2808 break;
2811 /* Special case - synthetic conditional branches, pcrel24.
2812 Note that EQ and NE have been handled above. */
2813 if ((insn[0] & 0xf0) == 0x20
2814 && insn[1] == 0x06
2815 && insn[2] == 0x04
2816 && srel->r_offset != irel->r_offset + 1
2817 && -32767 + alignment_glue <= pcrel
2818 && pcrel <= 32766 - alignment_glue)
2820 insn[1] = 0x05;
2821 insn[2] = 0x38;
2822 SNIP (5, 1, newrel);
2823 *again = true;
2826 /* Special case - synthetic conditional branches, pcrel16 */
2827 if ((insn[0] & 0xf0) == 0x20
2828 && insn[1] == 0x05
2829 && insn[2] == 0x38
2830 && srel->r_offset != irel->r_offset + 1
2831 && -127 + alignment_glue <= pcrel
2832 && pcrel <= 126 - alignment_glue)
2834 int cond = (insn[0] & 0x0f) ^ 0x01;
2836 insn[0] = 0x20 | cond;
2837 /* By moving the reloc first, we avoid having
2838 delete_bytes move it also. */
2839 move_reloc (irel, srel, -2);
2840 SNIP (2, 3, newrel);
2841 *again = true;
2845 BFD_ASSERT (nrelocs == 0);
2847 /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2848 use MOV.bwl #uimm:8, dsp:5[r7] format. This is tricky
2849 because it may have one or two relocations. */
2850 if ((insn[0] & 0xfc) == 0xf8
2851 && (insn[1] & 0x80) == 0x00
2852 && (insn[0] & 0x03) != 0x03)
2854 int dcode, icode, reg, ioff, dscale, ilen;
2855 bfd_vma disp_val = 0;
2856 long imm_val = 0;
2857 Elf_Internal_Rela * disp_rel = 0;
2858 Elf_Internal_Rela * imm_rel = 0;
2860 /* Reset this. */
2861 srel = irel;
2863 dcode = insn[0] & 0x03;
2864 icode = (insn[1] >> 2) & 0x03;
2865 reg = (insn[1] >> 4) & 0x0f;
2867 ioff = dcode == 1 ? 3 : dcode == 2 ? 4 : 2;
2869 /* Figure out what the dispacement is. */
2870 if (dcode == 1 || dcode == 2)
2872 /* There's a displacement. See if there's a reloc for it. */
2873 if (srel[1].r_offset == irel->r_offset + 2)
2875 GET_RELOC;
2876 disp_val = symval;
2877 disp_rel = srel;
2879 else
2881 if (dcode == 1)
2882 disp_val = insn[2];
2883 else
2885 #if RX_OPCODE_BIG_ENDIAN
2886 disp_val = insn[2] * 256 + insn[3];
2887 #else
2888 disp_val = insn[2] + insn[3] * 256;
2889 #endif
2891 switch (insn[1] & 3)
2893 case 1:
2894 disp_val *= 2;
2895 scale = 2;
2896 break;
2897 case 2:
2898 disp_val *= 4;
2899 scale = 4;
2900 break;
2905 dscale = scale;
2907 /* Figure out what the immediate is. */
2908 if (srel[1].r_offset == irel->r_offset + ioff)
2910 GET_RELOC;
2911 imm_val = (long) symval;
2912 imm_rel = srel;
2914 else
2916 unsigned char * ip = insn + ioff;
2918 switch (icode)
2920 case 1:
2921 /* For byte writes, we don't sign extend. Makes the math easier later. */
2922 if (scale == 1)
2923 imm_val = ip[0];
2924 else
2925 imm_val = (char) ip[0];
2926 break;
2927 case 2:
2928 #if RX_OPCODE_BIG_ENDIAN
2929 imm_val = ((char) ip[0] << 8) | ip[1];
2930 #else
2931 imm_val = ((char) ip[1] << 8) | ip[0];
2932 #endif
2933 break;
2934 case 3:
2935 #if RX_OPCODE_BIG_ENDIAN
2936 imm_val = ((char) ip[0] << 16) | (ip[1] << 8) | ip[2];
2937 #else
2938 imm_val = ((char) ip[2] << 16) | (ip[1] << 8) | ip[0];
2939 #endif
2940 break;
2941 case 0:
2942 #if RX_OPCODE_BIG_ENDIAN
2943 imm_val = ((unsigned) ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];
2944 #else
2945 imm_val = ((unsigned) ip[3] << 24) | (ip[2] << 16) | (ip[1] << 8) | ip[0];
2946 #endif
2947 break;
2951 ilen = 2;
2953 switch (dcode)
2955 case 1:
2956 ilen += 1;
2957 break;
2958 case 2:
2959 ilen += 2;
2960 break;
2963 switch (icode)
2965 case 1:
2966 ilen += 1;
2967 break;
2968 case 2:
2969 ilen += 2;
2970 break;
2971 case 3:
2972 ilen += 3;
2973 break;
2974 case 4:
2975 ilen += 4;
2976 break;
2979 /* The shortcut happens when the immediate is 0..255,
2980 register r0 to r7, and displacement (scaled) 0..31. */
2982 if (0 <= imm_val && imm_val <= 255
2983 && 0 <= reg && reg <= 7
2984 && disp_val / dscale <= 31)
2986 insn[0] = 0x3c | (insn[1] & 0x03);
2987 insn[1] = (((disp_val / dscale) << 3) & 0x80) | (reg << 4) | ((disp_val/dscale) & 0x0f);
2988 insn[2] = imm_val;
2990 if (disp_rel)
2992 int newrel = R_RX_NONE;
2994 switch (dscale)
2996 case 1:
2997 newrel = R_RX_RH_ABS5p8B;
2998 break;
2999 case 2:
3000 newrel = R_RX_RH_ABS5p8W;
3001 break;
3002 case 4:
3003 newrel = R_RX_RH_ABS5p8L;
3004 break;
3006 disp_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (disp_rel->r_info), newrel);
3007 move_reloc (irel, disp_rel, -1);
3009 if (imm_rel)
3011 imm_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (imm_rel->r_info), R_RX_DIR8U);
3012 move_reloc (disp_rel ? disp_rel : irel,
3013 imm_rel,
3014 irel->r_offset - imm_rel->r_offset + 2);
3017 SNIPNR (3, ilen - 3);
3018 *again = true;
3020 /* We can't relax this new opcode. */
3021 irel->r_addend = 0;
3026 /* We can't reliably relax branches to DIR3U_PCREL unless we know
3027 whatever they're branching over won't shrink any more. If we're
3028 basically done here, do one more pass just for branches - but
3029 don't request a pass after that one! */
3030 if (!*again && !allow_pcrel3)
3032 bool ignored;
3034 elf32_rx_relax_section (abfd, sec, link_info, &ignored, true);
3037 return true;
3039 error_return:
3040 free (free_contents);
3042 if (shndx_buf != NULL)
3044 shndx_hdr->contents = NULL;
3045 free (shndx_buf);
3048 free (free_intsyms);
3050 return false;
3053 static bool
3054 elf32_rx_relax_section_wrapper (bfd *abfd,
3055 asection *sec,
3056 struct bfd_link_info *link_info,
3057 bool *again)
3059 return elf32_rx_relax_section (abfd, sec, link_info, again, false);
3062 /* Function to set the ELF flag bits. */
3064 static bool
3065 rx_elf_set_private_flags (bfd * abfd, flagword flags)
3067 elf_elfheader (abfd)->e_flags = flags;
3068 elf_flags_init (abfd) = true;
3069 return true;
3072 static bool no_warn_mismatch = false;
3073 static bool ignore_lma = true;
3075 void bfd_elf32_rx_set_target_flags (bool, bool);
3077 void
3078 bfd_elf32_rx_set_target_flags (bool user_no_warn_mismatch,
3079 bool user_ignore_lma)
3081 no_warn_mismatch = user_no_warn_mismatch;
3082 ignore_lma = user_ignore_lma;
3085 /* Converts FLAGS into a descriptive string.
3086 Returns a static pointer. */
3088 static const char *
3089 describe_flags (flagword flags, char *buf)
3091 buf[0] = 0;
3093 if (flags & E_FLAG_RX_64BIT_DOUBLES)
3094 strcat (buf, "64-bit doubles");
3095 else
3096 strcat (buf, "32-bit doubles");
3098 if (flags & E_FLAG_RX_DSP)
3099 strcat (buf, ", dsp");
3100 else
3101 strcat (buf, ", no dsp");
3103 if (flags & E_FLAG_RX_PID)
3104 strcat (buf, ", pid");
3105 else
3106 strcat (buf, ", no pid");
3108 if (flags & E_FLAG_RX_ABI)
3109 strcat (buf, ", RX ABI");
3110 else
3111 strcat (buf, ", GCC ABI");
3113 if (flags & E_FLAG_RX_SINSNS_SET)
3114 strcat (buf, flags & E_FLAG_RX_SINSNS_YES ? ", uses String instructions" : ", bans String instructions");
3116 return buf;
3119 /* Merge backend specific data from an object file to the output
3120 object file when linking. */
3122 static bool
3123 rx_elf_merge_private_bfd_data (bfd * ibfd, struct bfd_link_info *info)
3125 bfd *obfd = info->output_bfd;
3126 flagword old_flags;
3127 flagword new_flags;
3128 bool error = false;
3130 new_flags = elf_elfheader (ibfd)->e_flags;
3131 old_flags = elf_elfheader (obfd)->e_flags;
3133 if (!elf_flags_init (obfd))
3135 /* First call, no flags set. */
3136 elf_flags_init (obfd) = true;
3137 elf_elfheader (obfd)->e_flags = new_flags;
3139 else if (old_flags != new_flags)
3141 flagword known_flags;
3143 if (old_flags & E_FLAG_RX_SINSNS_SET)
3145 if ((new_flags & E_FLAG_RX_SINSNS_SET) == 0)
3147 new_flags &= ~ E_FLAG_RX_SINSNS_MASK;
3148 new_flags |= (old_flags & E_FLAG_RX_SINSNS_MASK);
3151 else if (new_flags & E_FLAG_RX_SINSNS_SET)
3153 old_flags &= ~ E_FLAG_RX_SINSNS_MASK;
3154 old_flags |= (new_flags & E_FLAG_RX_SINSNS_MASK);
3157 known_flags = E_FLAG_RX_ABI | E_FLAG_RX_64BIT_DOUBLES
3158 | E_FLAG_RX_DSP | E_FLAG_RX_PID | E_FLAG_RX_SINSNS_MASK;
3160 if ((old_flags ^ new_flags) & known_flags)
3162 /* Only complain if flag bits we care about do not match.
3163 Other bits may be set, since older binaries did use some
3164 deprecated flags. */
3165 if (no_warn_mismatch)
3167 elf_elfheader (obfd)->e_flags = (new_flags | old_flags) & known_flags;
3169 else
3171 char buf[128];
3173 _bfd_error_handler (_("there is a conflict merging the"
3174 " ELF header flags from %pB"),
3175 ibfd);
3176 _bfd_error_handler (_(" the input file's flags: %s"),
3177 describe_flags (new_flags, buf));
3178 _bfd_error_handler (_(" the output file's flags: %s"),
3179 describe_flags (old_flags, buf));
3180 error = true;
3183 else
3184 elf_elfheader (obfd)->e_flags = new_flags & known_flags;
3187 if (error)
3188 bfd_set_error (bfd_error_bad_value);
3190 return !error;
3193 static bool
3194 rx_elf_print_private_bfd_data (bfd * abfd, void * ptr)
3196 FILE * file = (FILE *) ptr;
3197 flagword flags;
3198 char buf[128];
3200 BFD_ASSERT (abfd != NULL && ptr != NULL);
3202 /* Print normal ELF private data. */
3203 _bfd_elf_print_private_bfd_data (abfd, ptr);
3205 flags = elf_elfheader (abfd)->e_flags;
3206 fprintf (file, _("private flags = 0x%lx:"), (long) flags);
3208 fprintf (file, "%s", describe_flags (flags, buf));
3209 return true;
3212 /* Return the MACH for an e_flags value. */
3214 static int
3215 elf32_rx_machine (bfd * abfd ATTRIBUTE_UNUSED)
3217 #if 0 /* FIXME: EF_RX_CPU_MASK collides with E_FLAG_RX_...
3218 Need to sort out how these flag bits are used.
3219 For now we assume that the flags are OK. */
3220 if ((elf_elfheader (abfd)->e_flags & EF_RX_CPU_MASK) == EF_RX_CPU_RX)
3221 #endif
3222 if ((elf_elfheader (abfd)->e_flags & E_FLAG_RX_V2))
3223 return bfd_mach_rx_v2;
3224 else if ((elf_elfheader (abfd)->e_flags & E_FLAG_RX_V3))
3225 return bfd_mach_rx_v3;
3226 else
3227 return bfd_mach_rx;
3229 return 0;
3232 static bool
3233 rx_elf_object_p (bfd * abfd)
3235 int i;
3236 unsigned int u;
3237 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
3238 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
3239 int nphdrs = ehdr->e_phnum;
3240 sec_ptr bsec;
3241 static int saw_be = false;
3242 bfd_vma end_phdroff;
3244 /* We never want to automatically choose the non-swapping big-endian
3245 target. The user can only get that explicitly, such as with -I
3246 and objcopy. */
3247 if (abfd->xvec == &rx_elf32_be_ns_vec
3248 && abfd->target_defaulted)
3249 return false;
3251 /* BFD->target_defaulted is not set to TRUE when a target is chosen
3252 as a fallback, so we check for "scanning" to know when to stop
3253 using the non-swapping target. */
3254 if (abfd->xvec == &rx_elf32_be_ns_vec
3255 && saw_be)
3256 return false;
3257 if (abfd->xvec == &rx_elf32_be_vec)
3258 saw_be = true;
3260 bfd_default_set_arch_mach (abfd, bfd_arch_rx,
3261 elf32_rx_machine (abfd));
3263 /* For each PHDR in the object, we must find some section that
3264 corresponds (based on matching file offsets) and use its VMA
3265 information to reconstruct the p_vaddr field we clobbered when we
3266 wrote it out. */
3267 /* If PT_LOAD headers include the ELF file header or program headers
3268 then the PT_LOAD header does not start with some section contents.
3269 Making adjustments based on the difference between sh_offset and
3270 p_offset is nonsense in such cases. Exclude them. Note that
3271 since standard linker scripts for RX do not use SIZEOF_HEADERS,
3272 the linker won't normally create PT_LOAD segments covering the
3273 headers so this is mainly for passing the ld testsuite.
3274 FIXME. Why are we looking at non-PT_LOAD headers here? */
3275 end_phdroff = ehdr->e_ehsize;
3276 if (ehdr->e_phoff != 0)
3277 end_phdroff = ehdr->e_phoff + nphdrs * ehdr->e_phentsize;
3278 for (i=0; i<nphdrs; i++)
3280 for (u=0; u<elf_tdata(abfd)->num_elf_sections; u++)
3282 Elf_Internal_Shdr *sec = elf_tdata(abfd)->elf_sect_ptr[u];
3284 if (phdr[i].p_filesz
3285 && phdr[i].p_offset >= end_phdroff
3286 && phdr[i].p_offset <= (bfd_vma) sec->sh_offset
3287 && sec->sh_size > 0
3288 && sec->sh_type != SHT_NOBITS
3289 && (bfd_vma)sec->sh_offset <= phdr[i].p_offset + (phdr[i].p_filesz - 1))
3291 /* Found one! The difference between the two addresses,
3292 plus the difference between the two file offsets, is
3293 enough information to reconstruct the lma. */
3295 /* Example where they aren't:
3296 PHDR[1] = lma fffc0100 offset 00002010 size 00000100
3297 SEC[6] = vma 00000050 offset 00002050 size 00000040
3299 The correct LMA for the section is fffc0140 + (2050-2010).
3302 phdr[i].p_vaddr = sec->sh_addr + (sec->sh_offset - phdr[i].p_offset);
3303 break;
3307 /* We must update the bfd sections as well, so we don't stop
3308 with one match. */
3309 bsec = abfd->sections;
3310 while (bsec)
3312 if (phdr[i].p_filesz
3313 && phdr[i].p_vaddr <= bsec->vma
3314 && bsec->vma <= phdr[i].p_vaddr + (phdr[i].p_filesz - 1))
3316 bsec->lma = phdr[i].p_paddr + (bsec->vma - phdr[i].p_vaddr);
3318 bsec = bsec->next;
3322 return true;
3325 static bool
3326 rx_linux_object_p (bfd * abfd)
3328 bfd_default_set_arch_mach (abfd, bfd_arch_rx, elf32_rx_machine (abfd));
3329 return true;
3333 #ifdef DEBUG
3334 void
3335 rx_dump_symtab (bfd * abfd, void * internal_syms, void * external_syms)
3337 size_t locsymcount;
3338 Elf_Internal_Sym * isymbuf;
3339 Elf_Internal_Sym * isymend;
3340 Elf_Internal_Sym * isym;
3341 Elf_Internal_Shdr * symtab_hdr;
3342 char * st_info_str;
3343 char * st_info_stb_str;
3344 char * st_other_str;
3345 char * st_shndx_str;
3347 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3348 locsymcount = symtab_hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3349 if (!internal_syms)
3350 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3351 symtab_hdr->sh_info, 0,
3352 internal_syms, external_syms, NULL);
3353 else
3354 isymbuf = internal_syms;
3355 isymend = isymbuf + locsymcount;
3357 for (isym = isymbuf ; isym < isymend ; isym++)
3359 switch (ELF_ST_TYPE (isym->st_info))
3361 case STT_FUNC: st_info_str = "STT_FUNC"; break;
3362 case STT_SECTION: st_info_str = "STT_SECTION"; break;
3363 case STT_FILE: st_info_str = "STT_FILE"; break;
3364 case STT_OBJECT: st_info_str = "STT_OBJECT"; break;
3365 case STT_TLS: st_info_str = "STT_TLS"; break;
3366 default: st_info_str = "";
3368 switch (ELF_ST_BIND (isym->st_info))
3370 case STB_LOCAL: st_info_stb_str = "STB_LOCAL"; break;
3371 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL"; break;
3372 default: st_info_stb_str = "";
3374 switch (ELF_ST_VISIBILITY (isym->st_other))
3376 case STV_DEFAULT: st_other_str = "STV_DEFAULT"; break;
3377 case STV_INTERNAL: st_other_str = "STV_INTERNAL"; break;
3378 case STV_PROTECTED: st_other_str = "STV_PROTECTED"; break;
3379 default: st_other_str = "";
3381 switch (isym->st_shndx)
3383 case SHN_ABS: st_shndx_str = "SHN_ABS"; break;
3384 case SHN_COMMON: st_shndx_str = "SHN_COMMON"; break;
3385 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF"; break;
3386 default: st_shndx_str = "";
3389 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3390 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3391 isym,
3392 (unsigned long) isym->st_value,
3393 (unsigned long) isym->st_size,
3394 isym->st_name,
3395 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
3396 isym->st_name),
3397 isym->st_info, st_info_str, st_info_stb_str,
3398 isym->st_other, st_other_str,
3399 isym->st_shndx, st_shndx_str);
3403 char *
3404 rx_get_reloc (long reloc)
3406 if (0 <= reloc && reloc < R_RX_max)
3407 return rx_elf_howto_table[reloc].name;
3408 return "";
3410 #endif /* DEBUG */
3413 /* We must take care to keep the on-disk copy of any code sections
3414 that are fully linked swapped if the target is big endian, to match
3415 the Renesas tools. */
3417 /* The rule is: big endian object that are final-link executables,
3418 have code sections stored with 32-bit words swapped relative to
3419 what you'd get by default. */
3421 static bool
3422 rx_get_section_contents (bfd * abfd,
3423 sec_ptr section,
3424 void * location,
3425 file_ptr offset,
3426 bfd_size_type count)
3428 int exec = (abfd->flags & EXEC_P) ? 1 : 0;
3429 int s_code = (section->flags & SEC_CODE) ? 1 : 0;
3430 bool rv;
3432 #ifdef DJDEBUG
3433 fprintf (stderr, "dj: get %ld %ld from %s %s e%d sc%d %08lx:%08lx\n",
3434 (long) offset, (long) count, section->name,
3435 bfd_big_endian(abfd) ? "be" : "le",
3436 exec, s_code, (long unsigned) section->filepos,
3437 (long unsigned) offset);
3438 #endif
3440 if (exec && s_code && bfd_big_endian (abfd))
3442 char * cloc = (char *) location;
3443 bfd_size_type cnt, end_cnt;
3445 rv = true;
3447 /* Fetch and swap unaligned bytes at the beginning. */
3448 if (offset % 4)
3450 char buf[4];
3452 rv = _bfd_generic_get_section_contents (abfd, section, buf,
3453 (offset & -4), 4);
3454 if (!rv)
3455 return false;
3457 bfd_putb32 (bfd_getl32 (buf), buf);
3459 cnt = 4 - (offset % 4);
3460 if (cnt > count)
3461 cnt = count;
3463 memcpy (location, buf + (offset % 4), cnt);
3465 count -= cnt;
3466 offset += cnt;
3467 cloc += count;
3470 end_cnt = count % 4;
3472 /* Fetch and swap the middle bytes. */
3473 if (count >= 4)
3475 rv = _bfd_generic_get_section_contents (abfd, section, cloc, offset,
3476 count - end_cnt);
3477 if (!rv)
3478 return false;
3480 for (cnt = count; cnt >= 4; cnt -= 4, cloc += 4)
3481 bfd_putb32 (bfd_getl32 (cloc), cloc);
3484 /* Fetch and swap the end bytes. */
3485 if (end_cnt > 0)
3487 char buf[4];
3489 /* Fetch the end bytes. */
3490 rv = _bfd_generic_get_section_contents (abfd, section, buf,
3491 offset + count - end_cnt, 4);
3492 if (!rv)
3493 return false;
3495 bfd_putb32 (bfd_getl32 (buf), buf);
3496 memcpy (cloc, buf, end_cnt);
3499 else
3500 rv = _bfd_generic_get_section_contents (abfd, section, location, offset, count);
3502 return rv;
3505 #ifdef DJDEBUG
3506 static bool
3507 rx2_set_section_contents (bfd * abfd,
3508 sec_ptr section,
3509 const void * location,
3510 file_ptr offset,
3511 bfd_size_type count)
3513 bfd_size_type i;
3515 fprintf (stderr, " set sec %s %08x loc %p offset %#x count %#x\n",
3516 section->name, (unsigned) section->vma, location, (int) offset, (int) count);
3517 for (i = 0; i < count; i++)
3519 if (i % 16 == 0 && i > 0)
3520 fprintf (stderr, "\n");
3522 if (i % 16 && i % 4 == 0)
3523 fprintf (stderr, " ");
3525 if (i % 16 == 0)
3526 fprintf (stderr, " %08x:", (int) (section->vma + offset + i));
3528 fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
3530 fprintf (stderr, "\n");
3532 return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
3534 #define _bfd_elf_set_section_contents rx2_set_section_contents
3535 #endif
3537 static bool
3538 rx_set_section_contents (bfd * abfd,
3539 sec_ptr section,
3540 const void * location,
3541 file_ptr offset,
3542 bfd_size_type count)
3544 bool exec = (abfd->flags & EXEC_P) != 0;
3545 bool s_code = (section->flags & SEC_CODE) != 0;
3546 bool rv;
3547 char * swapped_data = NULL;
3548 bfd_size_type i;
3549 bfd_vma caddr = section->vma + offset;
3550 file_ptr faddr = 0;
3551 bfd_size_type scount;
3553 #ifdef DJDEBUG
3554 bfd_size_type i;
3556 fprintf (stderr, "\ndj: set %ld %ld to %s %s e%d sc%d\n",
3557 (long) offset, (long) count, section->name,
3558 bfd_big_endian (abfd) ? "be" : "le",
3559 exec, s_code);
3561 for (i = 0; i < count; i++)
3563 int a = section->vma + offset + i;
3565 if (a % 16 == 0 && a > 0)
3566 fprintf (stderr, "\n");
3568 if (a % 16 && a % 4 == 0)
3569 fprintf (stderr, " ");
3571 if (a % 16 == 0 || i == 0)
3572 fprintf (stderr, " %08x:", (int) (section->vma + offset + i));
3574 fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
3577 fprintf (stderr, "\n");
3578 #endif
3580 if (! exec || ! s_code || ! bfd_big_endian (abfd))
3581 return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
3583 while (count > 0 && caddr > 0 && caddr % 4)
3585 switch (caddr % 4)
3587 case 0: faddr = offset + 3; break;
3588 case 1: faddr = offset + 1; break;
3589 case 2: faddr = offset - 1; break;
3590 case 3: faddr = offset - 3; break;
3593 rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
3594 if (! rv)
3595 return rv;
3597 location = (bfd_byte *) location + 1;
3598 offset ++;
3599 count --;
3600 caddr ++;
3603 scount = (int)(count / 4) * 4;
3604 if (scount > 0)
3606 char * cloc = (char *) location;
3608 swapped_data = (char *) bfd_alloc (abfd, count);
3609 if (swapped_data == NULL)
3610 return false;
3612 for (i = 0; i < count; i += 4)
3614 bfd_vma v = bfd_getl32 (cloc + i);
3615 bfd_putb32 (v, swapped_data + i);
3618 rv = _bfd_elf_set_section_contents (abfd, section, swapped_data, offset, scount);
3620 if (!rv)
3621 return rv;
3624 count -= scount;
3625 location = (bfd_byte *) location + scount;
3626 offset += scount;
3628 if (count > 0)
3630 caddr = section->vma + offset;
3631 while (count > 0)
3633 switch (caddr % 4)
3635 case 0: faddr = offset + 3; break;
3636 case 1: faddr = offset + 1; break;
3637 case 2: faddr = offset - 1; break;
3638 case 3: faddr = offset - 3; break;
3640 rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
3641 if (! rv)
3642 return rv;
3644 location = (bfd_byte *) location + 1;
3645 offset ++;
3646 count --;
3647 caddr ++;
3651 return true;
3654 static bool
3655 rx_final_link (bfd * abfd, struct bfd_link_info * info)
3657 asection * o;
3659 for (o = abfd->sections; o != NULL; o = o->next)
3661 #ifdef DJDEBUG
3662 fprintf (stderr, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3663 o->name, o->flags, o->vma, o->lma, o->size, o->rawsize);
3664 #endif
3665 if (o->flags & SEC_CODE
3666 && bfd_big_endian (abfd)
3667 && o->size % 4)
3669 #ifdef DJDEBUG
3670 fprintf (stderr, "adjusting...\n");
3671 #endif
3672 o->size += 4 - (o->size % 4);
3676 return bfd_elf_final_link (abfd, info);
3679 static bool
3680 elf32_rx_modify_headers (bfd *abfd, struct bfd_link_info *info)
3682 const struct elf_backend_data * bed;
3683 struct elf_obj_tdata * tdata;
3684 Elf_Internal_Phdr * phdr;
3685 unsigned int count;
3686 unsigned int i;
3688 bed = get_elf_backend_data (abfd);
3689 tdata = elf_tdata (abfd);
3690 phdr = tdata->phdr;
3691 count = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
3693 if (ignore_lma)
3694 for (i = count; i-- != 0;)
3695 if (phdr[i].p_type == PT_LOAD)
3697 /* The Renesas tools expect p_paddr to be zero. However,
3698 there is no other way to store the writable data in ROM for
3699 startup initialization. So, we let the linker *think*
3700 we're using paddr and vaddr the "usual" way, but at the
3701 last minute we move the paddr into the vaddr (which is what
3702 the simulator uses) and zero out paddr. Note that this
3703 does not affect the section headers, just the program
3704 headers. We hope. */
3705 phdr[i].p_vaddr = phdr[i].p_paddr;
3706 #if 0 /* If we zero out p_paddr, then the LMA in the section table
3707 becomes wrong. */
3708 phdr[i].p_paddr = 0;
3709 #endif
3712 return _bfd_elf_modify_headers (abfd, info);
3715 /* The default literal sections should always be marked as "code" (i.e.,
3716 SHF_EXECINSTR). This is particularly important for big-endian mode
3717 when we do not want their contents byte reversed. */
3718 static const struct bfd_elf_special_section elf32_rx_special_sections[] =
3720 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3721 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3722 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3723 { NULL, 0, 0, 0, 0 }
3726 typedef struct {
3727 bfd *abfd;
3728 struct bfd_link_info *info;
3729 bfd_vma table_start;
3730 int table_size;
3731 bfd_vma *table_handlers;
3732 bfd_vma table_default_handler;
3733 struct bfd_link_hash_entry **table_entries;
3734 struct bfd_link_hash_entry *table_default_entry;
3735 FILE *mapfile;
3736 } RX_Table_Info;
3738 static bool
3739 rx_table_find (struct bfd_hash_entry *vent, void *vinfo)
3741 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3742 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3743 const char *name; /* of the symbol we've found */
3744 asection *sec;
3745 struct bfd *abfd;
3746 int idx;
3747 const char *tname; /* name of the table */
3748 bfd_vma start_addr, end_addr;
3749 char *buf;
3750 struct bfd_link_hash_entry * h;
3752 /* We're looking for globally defined symbols of the form
3753 $tablestart$<NAME>. */
3754 if (ent->type != bfd_link_hash_defined
3755 && ent->type != bfd_link_hash_defweak)
3756 return true;
3758 name = ent->root.string;
3759 sec = ent->u.def.section;
3760 abfd = sec->owner;
3762 if (!startswith (name, "$tablestart$"))
3763 return true;
3765 sec->flags |= SEC_KEEP;
3767 tname = name + 12;
3769 start_addr = ent->u.def.value;
3771 /* At this point, we can't build the table but we can (and must)
3772 find all the related symbols and mark their sections as SEC_KEEP
3773 so we don't garbage collect them. */
3775 buf = (char *) bfd_malloc (12 + 10 + strlen (tname));
3776 if (buf == NULL)
3777 return false;
3779 sprintf (buf, "$tableend$%s", tname);
3780 h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3781 if (!h || (h->type != bfd_link_hash_defined
3782 && h->type != bfd_link_hash_defweak))
3784 /* xgettext:c-format */
3785 _bfd_error_handler (_("%pB:%pA: table %s missing corresponding %s"),
3786 abfd, sec, name, buf);
3787 return true;
3790 if (h->u.def.section != ent->u.def.section)
3792 /* xgettext:c-format */
3793 _bfd_error_handler (_("%pB:%pA: %s and %s must be in the same input section"),
3794 h->u.def.section->owner, h->u.def.section,
3795 name, buf);
3796 return true;
3799 end_addr = h->u.def.value;
3801 sprintf (buf, "$tableentry$default$%s", tname);
3802 h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3803 if (h && (h->type == bfd_link_hash_defined
3804 || h->type == bfd_link_hash_defweak))
3806 h->u.def.section->flags |= SEC_KEEP;
3809 for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
3811 sprintf (buf, "$tableentry$%d$%s", idx, tname);
3812 h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3813 if (h && (h->type == bfd_link_hash_defined
3814 || h->type == bfd_link_hash_defweak))
3816 h->u.def.section->flags |= SEC_KEEP;
3820 /* Return TRUE to keep scanning, FALSE to end the traversal. */
3821 return true;
3824 /* We need to check for table entry symbols and build the tables, and
3825 we need to do it before the linker does garbage collection. This function is
3826 called once per input object file. */
3827 static bool
3828 rx_check_directives
3829 (bfd * abfd ATTRIBUTE_UNUSED,
3830 struct bfd_link_info * info ATTRIBUTE_UNUSED)
3832 RX_Table_Info stuff;
3834 stuff.abfd = abfd;
3835 stuff.info = info;
3836 bfd_hash_traverse (&(info->hash->table), rx_table_find, &stuff);
3838 return true;
3842 static bool
3843 rx_table_map_2 (struct bfd_hash_entry *vent, void *vinfo)
3845 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3846 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3847 int idx;
3848 const char *name;
3849 bfd_vma addr;
3851 /* See if the symbol ENT has an address listed in the table, and
3852 isn't a debug/special symbol. If so, put it in the table. */
3854 if (ent->type != bfd_link_hash_defined
3855 && ent->type != bfd_link_hash_defweak)
3856 return true;
3858 name = ent->root.string;
3860 if (name[0] == '$' || name[0] == '.' || name[0] < ' ')
3861 return true;
3863 addr = (ent->u.def.value
3864 + ent->u.def.section->output_section->vma
3865 + ent->u.def.section->output_offset);
3867 for (idx = 0; idx < info->table_size; idx ++)
3868 if (addr == info->table_handlers[idx])
3869 info->table_entries[idx] = ent;
3871 if (addr == info->table_default_handler)
3872 info->table_default_entry = ent;
3874 return true;
3877 static bool
3878 rx_table_map (struct bfd_hash_entry *vent, void *vinfo)
3880 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3881 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3882 const char *name; /* of the symbol we've found */
3883 int idx;
3884 const char *tname; /* name of the table */
3885 bfd_vma start_addr, end_addr;
3886 char *buf;
3887 struct bfd_link_hash_entry * h;
3888 int need_elipses;
3890 /* We're looking for globally defined symbols of the form
3891 $tablestart$<NAME>. */
3892 if (ent->type != bfd_link_hash_defined
3893 && ent->type != bfd_link_hash_defweak)
3894 return true;
3896 name = ent->root.string;
3898 if (!startswith (name, "$tablestart$"))
3899 return true;
3901 tname = name + 12;
3902 start_addr = (ent->u.def.value
3903 + ent->u.def.section->output_section->vma
3904 + ent->u.def.section->output_offset);
3906 buf = (char *) bfd_malloc (12 + 10 + strlen (tname));
3907 if (buf == NULL)
3908 return false;
3910 sprintf (buf, "$tableend$%s", tname);
3911 end_addr = get_symbol_value_maybe (buf, info->info);
3913 sprintf (buf, "$tableentry$default$%s", tname);
3914 h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3915 if (h)
3917 info->table_default_handler = (h->u.def.value
3918 + h->u.def.section->output_section->vma
3919 + h->u.def.section->output_offset);
3921 else
3922 /* Zero is a valid handler address! */
3923 info->table_default_handler = (bfd_vma) (-1);
3924 info->table_default_entry = NULL;
3926 info->table_start = start_addr;
3927 info->table_size = (int) (end_addr - start_addr) / 4;
3928 info->table_handlers = (bfd_vma *)
3929 bfd_malloc (info->table_size * sizeof (bfd_vma));
3930 if (info->table_handlers == NULL)
3932 free (buf);
3933 return false;
3935 info->table_entries = (struct bfd_link_hash_entry **)
3936 bfd_malloc (info->table_size * sizeof (struct bfd_link_hash_entry));
3937 if (info->table_entries == NULL)
3939 free (info->table_handlers);
3940 free (buf);
3941 return false;
3944 for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
3946 sprintf (buf, "$tableentry$%d$%s", idx, tname);
3947 h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3948 if (h && (h->type == bfd_link_hash_defined
3949 || h->type == bfd_link_hash_defweak))
3951 info->table_handlers[idx] = (h->u.def.value
3952 + h->u.def.section->output_section->vma
3953 + h->u.def.section->output_offset);
3955 else
3956 info->table_handlers[idx] = info->table_default_handler;
3957 info->table_entries[idx] = NULL;
3960 free (buf);
3962 bfd_hash_traverse (&(info->info->hash->table), rx_table_map_2, info);
3964 fprintf (info->mapfile, "\nRX Vector Table: %s has %d entries at 0x%08" BFD_VMA_FMT "x\n\n",
3965 tname, info->table_size, start_addr);
3967 if (info->table_default_entry)
3968 fprintf (info->mapfile, " default handler is: %s at 0x%08" BFD_VMA_FMT "x\n",
3969 info->table_default_entry->root.string,
3970 info->table_default_handler);
3971 else if (info->table_default_handler != (bfd_vma)(-1))
3972 fprintf (info->mapfile, " default handler is at 0x%08" BFD_VMA_FMT "x\n",
3973 info->table_default_handler);
3974 else
3975 fprintf (info->mapfile, " no default handler\n");
3977 need_elipses = 1;
3978 for (idx = 0; idx < info->table_size; idx ++)
3980 if (info->table_handlers[idx] == info->table_default_handler)
3982 if (need_elipses)
3983 fprintf (info->mapfile, " . . .\n");
3984 need_elipses = 0;
3985 continue;
3987 need_elipses = 1;
3989 fprintf (info->mapfile, " 0x%08" BFD_VMA_FMT "x [%3d] ", start_addr + 4 * idx, idx);
3991 if (info->table_handlers[idx] == (bfd_vma) (-1))
3992 fprintf (info->mapfile, "(no handler found)\n");
3994 else if (info->table_handlers[idx] == info->table_default_handler)
3996 if (info->table_default_entry)
3997 fprintf (info->mapfile, "(default)\n");
3998 else
3999 fprintf (info->mapfile, "(default)\n");
4002 else if (info->table_entries[idx])
4004 fprintf (info->mapfile, "0x%08" BFD_VMA_FMT "x %s\n", info->table_handlers[idx], info->table_entries[idx]->root.string);
4007 else
4009 fprintf (info->mapfile, "0x%08" BFD_VMA_FMT "x ???\n", info->table_handlers[idx]);
4012 if (need_elipses)
4013 fprintf (info->mapfile, " . . .\n");
4015 return true;
4018 void
4019 rx_additional_link_map_text (bfd *obfd, struct bfd_link_info *info, FILE *mapfile)
4021 /* We scan the symbol table looking for $tableentry$'s, and for
4022 each, try to deduce which handlers go with which entries. */
4024 RX_Table_Info stuff;
4026 stuff.abfd = obfd;
4027 stuff.info = info;
4028 stuff.mapfile = mapfile;
4029 bfd_hash_traverse (&(info->hash->table), rx_table_map, &stuff);
4033 #define ELF_ARCH bfd_arch_rx
4034 #define ELF_MACHINE_CODE EM_RX
4035 #define ELF_MAXPAGESIZE 0x1000
4037 #define TARGET_BIG_SYM rx_elf32_be_vec
4038 #define TARGET_BIG_NAME "elf32-rx-be"
4040 #define TARGET_LITTLE_SYM rx_elf32_le_vec
4041 #define TARGET_LITTLE_NAME "elf32-rx-le"
4043 #define elf_info_to_howto_rel NULL
4044 #define elf_info_to_howto rx_info_to_howto_rela
4045 #define elf_backend_object_p rx_elf_object_p
4046 #define elf_backend_relocate_section rx_elf_relocate_section
4047 #define elf_symbol_leading_char ('_')
4048 #define elf_backend_can_gc_sections 1
4049 #define elf_backend_modify_headers elf32_rx_modify_headers
4051 #define bfd_elf32_bfd_reloc_type_lookup rx_reloc_type_lookup
4052 #define bfd_elf32_bfd_reloc_name_lookup rx_reloc_name_lookup
4053 #define bfd_elf32_bfd_set_private_flags rx_elf_set_private_flags
4054 #define bfd_elf32_bfd_merge_private_bfd_data rx_elf_merge_private_bfd_data
4055 #define bfd_elf32_bfd_print_private_bfd_data rx_elf_print_private_bfd_data
4056 #define bfd_elf32_get_section_contents rx_get_section_contents
4057 #define bfd_elf32_set_section_contents rx_set_section_contents
4058 #define bfd_elf32_bfd_final_link rx_final_link
4059 #define bfd_elf32_bfd_relax_section elf32_rx_relax_section_wrapper
4060 #define elf_backend_special_sections elf32_rx_special_sections
4061 #define elf_backend_check_directives rx_check_directives
4063 #include "elf32-target.h"
4065 /* We define a second big-endian target that doesn't have the custom
4066 section get/set hooks, for times when we want to preserve the
4067 pre-swapped .text sections (like objcopy). */
4069 #undef TARGET_BIG_SYM
4070 #define TARGET_BIG_SYM rx_elf32_be_ns_vec
4071 #undef TARGET_BIG_NAME
4072 #define TARGET_BIG_NAME "elf32-rx-be-ns"
4073 #undef TARGET_LITTLE_SYM
4075 #undef bfd_elf32_get_section_contents
4076 #undef bfd_elf32_set_section_contents
4078 #undef elf32_bed
4079 #define elf32_bed elf32_rx_be_ns_bed
4081 #include "elf32-target.h"
4083 #undef TARGET_LITTLE_SYM
4084 #define TARGET_LITTLE_SYM rx_elf32_linux_le_vec
4085 #undef TARGET_LITTLE_NAME
4086 #define TARGET_LITTLE_NAME "elf32-rx-linux"
4087 #undef TARGET_BIG_SYM
4088 #undef TARGET_BIG_NAME
4090 #undef elf_backend_object_p
4091 #define elf_backend_object_p rx_linux_object_p
4092 #undef elf_symbol_leading_char
4093 #undef elf32_bed
4094 #define elf32_bed elf32_rx_le_linux_bed
4096 #include "elf32-target.h"