1 /* Renesas RX specific support for 32-bit ELF.
2 Copyright (C) 2008-2018 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. */
23 #include "bfd_stdint.h"
27 #include "libiberty.h"
30 #define RX_OPCODE_BIG_ENDIAN 0
32 /* This is a meta-target that's used only with objcopy, to avoid the
33 endian-swap we would otherwise get. We check for this in
35 const bfd_target rx_elf32_be_ns_vec;
36 const bfd_target rx_elf32_be_vec;
39 char * rx_get_reloc (long);
40 void rx_dump_symtab (bfd *, void *, void *);
43 #define RXREL(n,sz,bit,shift,complain,pcrel) \
44 HOWTO (R_RX_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
45 bfd_elf_generic_reloc, "R_RX_" #n, FALSE, 0, ~0, FALSE)
47 /* Note that the relocations around 0x7f are internal to this file;
48 feel free to move them as needed to avoid conflicts with published
49 relocation numbers. */
51 static reloc_howto_type rx_elf_howto_table [] =
53 RXREL (NONE, 3, 0, 0, dont, FALSE),
54 RXREL (DIR32, 2, 32, 0, signed, FALSE),
55 RXREL (DIR24S, 2, 24, 0, signed, FALSE),
56 RXREL (DIR16, 1, 16, 0, dont, FALSE),
57 RXREL (DIR16U, 1, 16, 0, unsigned, FALSE),
58 RXREL (DIR16S, 1, 16, 0, signed, FALSE),
59 RXREL (DIR8, 0, 8, 0, dont, FALSE),
60 RXREL (DIR8U, 0, 8, 0, unsigned, FALSE),
61 RXREL (DIR8S, 0, 8, 0, signed, FALSE),
62 RXREL (DIR24S_PCREL, 2, 24, 0, signed, TRUE),
63 RXREL (DIR16S_PCREL, 1, 16, 0, signed, TRUE),
64 RXREL (DIR8S_PCREL, 0, 8, 0, signed, TRUE),
65 RXREL (DIR16UL, 1, 16, 2, unsigned, FALSE),
66 RXREL (DIR16UW, 1, 16, 1, unsigned, FALSE),
67 RXREL (DIR8UL, 0, 8, 2, unsigned, FALSE),
68 RXREL (DIR8UW, 0, 8, 1, unsigned, FALSE),
69 RXREL (DIR32_REV, 1, 16, 0, dont, FALSE),
70 RXREL (DIR16_REV, 1, 16, 0, dont, FALSE),
71 RXREL (DIR3U_PCREL, 0, 3, 0, dont, TRUE),
87 RXREL (RH_3_PCREL, 0, 3, 0, signed, TRUE),
88 RXREL (RH_16_OP, 1, 16, 0, signed, FALSE),
89 RXREL (RH_24_OP, 2, 24, 0, signed, FALSE),
90 RXREL (RH_32_OP, 2, 32, 0, signed, FALSE),
91 RXREL (RH_24_UNS, 2, 24, 0, unsigned, FALSE),
92 RXREL (RH_8_NEG, 0, 8, 0, signed, FALSE),
93 RXREL (RH_16_NEG, 1, 16, 0, signed, FALSE),
94 RXREL (RH_24_NEG, 2, 24, 0, signed, FALSE),
95 RXREL (RH_32_NEG, 2, 32, 0, signed, FALSE),
96 RXREL (RH_DIFF, 2, 32, 0, signed, FALSE),
97 RXREL (RH_GPRELB, 1, 16, 0, unsigned, FALSE),
98 RXREL (RH_GPRELW, 1, 16, 0, unsigned, FALSE),
99 RXREL (RH_GPRELL, 1, 16, 0, unsigned, FALSE),
100 RXREL (RH_RELAX, 0, 0, 0, dont, FALSE),
122 RXREL (ABS32, 2, 32, 0, dont, FALSE),
123 RXREL (ABS24S, 2, 24, 0, signed, FALSE),
124 RXREL (ABS16, 1, 16, 0, dont, FALSE),
125 RXREL (ABS16U, 1, 16, 0, unsigned, FALSE),
126 RXREL (ABS16S, 1, 16, 0, signed, FALSE),
127 RXREL (ABS8, 0, 8, 0, dont, FALSE),
128 RXREL (ABS8U, 0, 8, 0, unsigned, FALSE),
129 RXREL (ABS8S, 0, 8, 0, signed, FALSE),
130 RXREL (ABS24S_PCREL, 2, 24, 0, signed, TRUE),
131 RXREL (ABS16S_PCREL, 1, 16, 0, signed, TRUE),
132 RXREL (ABS8S_PCREL, 0, 8, 0, signed, TRUE),
133 RXREL (ABS16UL, 1, 16, 0, unsigned, FALSE),
134 RXREL (ABS16UW, 1, 16, 0, unsigned, FALSE),
135 RXREL (ABS8UL, 0, 8, 0, unsigned, FALSE),
136 RXREL (ABS8UW, 0, 8, 0, unsigned, FALSE),
137 RXREL (ABS32_REV, 2, 32, 0, dont, FALSE),
138 RXREL (ABS16_REV, 1, 16, 0, dont, FALSE),
140 #define STACK_REL_P(x) ((x) <= R_RX_ABS16_REV && (x) >= R_RX_ABS32)
181 /* These are internal. */
182 /* A 5-bit unsigned displacement to a B/W/L address, at bit position 8/12. */
183 /* ---- ---- 4--- 3210. */
184 #define R_RX_RH_ABS5p8B 0x78
185 RXREL (RH_ABS5p8B, 0, 0, 0, dont, FALSE),
186 #define R_RX_RH_ABS5p8W 0x79
187 RXREL (RH_ABS5p8W, 0, 0, 0, dont, FALSE),
188 #define R_RX_RH_ABS5p8L 0x7a
189 RXREL (RH_ABS5p8L, 0, 0, 0, dont, FALSE),
190 /* A 5-bit unsigned displacement to a B/W/L address, at bit position 5/12. */
191 /* ---- -432 1--- 0---. */
192 #define R_RX_RH_ABS5p5B 0x7b
193 RXREL (RH_ABS5p5B, 0, 0, 0, dont, FALSE),
194 #define R_RX_RH_ABS5p5W 0x7c
195 RXREL (RH_ABS5p5W, 0, 0, 0, dont, FALSE),
196 #define R_RX_RH_ABS5p5L 0x7d
197 RXREL (RH_ABS5p5L, 0, 0, 0, dont, FALSE),
198 /* A 4-bit unsigned immediate at bit position 8. */
199 #define R_RX_RH_UIMM4p8 0x7e
200 RXREL (RH_UIMM4p8, 0, 0, 0, dont, FALSE),
201 /* A 4-bit negative unsigned immediate at bit position 8. */
202 #define R_RX_RH_UNEG4p8 0x7f
203 RXREL (RH_UNEG4p8, 0, 0, 0, dont, FALSE),
204 /* End of internal relocs. */
206 RXREL (SYM, 2, 32, 0, dont, FALSE),
207 RXREL (OPneg, 2, 32, 0, dont, FALSE),
208 RXREL (OPadd, 2, 32, 0, dont, FALSE),
209 RXREL (OPsub, 2, 32, 0, dont, FALSE),
210 RXREL (OPmul, 2, 32, 0, dont, FALSE),
211 RXREL (OPdiv, 2, 32, 0, dont, FALSE),
212 RXREL (OPshla, 2, 32, 0, dont, FALSE),
213 RXREL (OPshra, 2, 32, 0, dont, FALSE),
214 RXREL (OPsctsize, 2, 32, 0, dont, FALSE),
215 RXREL (OPscttop, 2, 32, 0, dont, FALSE),
216 RXREL (OPand, 2, 32, 0, dont, FALSE),
217 RXREL (OPor, 2, 32, 0, dont, FALSE),
218 RXREL (OPxor, 2, 32, 0, dont, FALSE),
219 RXREL (OPnot, 2, 32, 0, dont, FALSE),
220 RXREL (OPmod, 2, 32, 0, dont, FALSE),
221 RXREL (OPromtop, 2, 32, 0, dont, FALSE),
222 RXREL (OPramtop, 2, 32, 0, dont, FALSE)
225 /* Map BFD reloc types to RX ELF reloc types. */
229 bfd_reloc_code_real_type bfd_reloc_val;
230 unsigned int rx_reloc_val;
233 static const struct rx_reloc_map rx_reloc_map [] =
235 { BFD_RELOC_NONE, R_RX_NONE },
236 { BFD_RELOC_8, R_RX_DIR8S },
237 { BFD_RELOC_16, R_RX_DIR16S },
238 { BFD_RELOC_24, R_RX_DIR24S },
239 { BFD_RELOC_32, R_RX_DIR32 },
240 { BFD_RELOC_RX_16_OP, R_RX_DIR16 },
241 { BFD_RELOC_RX_DIR3U_PCREL, R_RX_DIR3U_PCREL },
242 { BFD_RELOC_8_PCREL, R_RX_DIR8S_PCREL },
243 { BFD_RELOC_16_PCREL, R_RX_DIR16S_PCREL },
244 { BFD_RELOC_24_PCREL, R_RX_DIR24S_PCREL },
245 { BFD_RELOC_RX_8U, R_RX_DIR8U },
246 { BFD_RELOC_RX_16U, R_RX_DIR16U },
247 { BFD_RELOC_RX_24U, R_RX_RH_24_UNS },
248 { BFD_RELOC_RX_NEG8, R_RX_RH_8_NEG },
249 { BFD_RELOC_RX_NEG16, R_RX_RH_16_NEG },
250 { BFD_RELOC_RX_NEG24, R_RX_RH_24_NEG },
251 { BFD_RELOC_RX_NEG32, R_RX_RH_32_NEG },
252 { BFD_RELOC_RX_DIFF, R_RX_RH_DIFF },
253 { BFD_RELOC_RX_GPRELB, R_RX_RH_GPRELB },
254 { BFD_RELOC_RX_GPRELW, R_RX_RH_GPRELW },
255 { BFD_RELOC_RX_GPRELL, R_RX_RH_GPRELL },
256 { BFD_RELOC_RX_RELAX, R_RX_RH_RELAX },
257 { BFD_RELOC_RX_SYM, R_RX_SYM },
258 { BFD_RELOC_RX_OP_SUBTRACT, R_RX_OPsub },
259 { BFD_RELOC_RX_OP_NEG, R_RX_OPneg },
260 { BFD_RELOC_RX_ABS8, R_RX_ABS8 },
261 { BFD_RELOC_RX_ABS16, R_RX_ABS16 },
262 { BFD_RELOC_RX_ABS16_REV, R_RX_ABS16_REV },
263 { BFD_RELOC_RX_ABS32, R_RX_ABS32 },
264 { BFD_RELOC_RX_ABS32_REV, R_RX_ABS32_REV },
265 { BFD_RELOC_RX_ABS16UL, R_RX_ABS16UL },
266 { BFD_RELOC_RX_ABS16UW, R_RX_ABS16UW },
267 { BFD_RELOC_RX_ABS16U, R_RX_ABS16U }
270 #define BIGE(abfd) ((abfd)->xvec->byteorder == BFD_ENDIAN_BIG)
272 static reloc_howto_type *
273 rx_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
274 bfd_reloc_code_real_type code)
278 if (code == BFD_RELOC_RX_32_OP)
279 return rx_elf_howto_table + R_RX_DIR32;
281 for (i = ARRAY_SIZE (rx_reloc_map); i--;)
282 if (rx_reloc_map [i].bfd_reloc_val == code)
283 return rx_elf_howto_table + rx_reloc_map[i].rx_reloc_val;
288 static reloc_howto_type *
289 rx_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, const char * r_name)
293 for (i = 0; i < ARRAY_SIZE (rx_elf_howto_table); i++)
294 if (rx_elf_howto_table[i].name != NULL
295 && strcasecmp (rx_elf_howto_table[i].name, r_name) == 0)
296 return rx_elf_howto_table + i;
301 /* Set the howto pointer for an RX ELF reloc. */
304 rx_info_to_howto_rela (bfd *abfd,
306 Elf_Internal_Rela *dst)
310 r_type = ELF32_R_TYPE (dst->r_info);
311 if (r_type >= (unsigned int) R_RX_max)
313 /* xgettext:c-format */
314 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
318 cache_ptr->howto = rx_elf_howto_table + r_type;
322 get_symbol_value (const char * name,
323 struct bfd_link_info * info,
325 asection * input_section,
329 struct bfd_link_hash_entry * h;
331 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
334 || (h->type != bfd_link_hash_defined
335 && h->type != bfd_link_hash_defweak))
336 (*info->callbacks->undefined_symbol)
337 (info, name, input_bfd, input_section, offset, TRUE);
339 value = (h->u.def.value
340 + h->u.def.section->output_section->vma
341 + h->u.def.section->output_offset);
347 get_symbol_value_maybe (const char * name,
348 struct bfd_link_info * info)
351 struct bfd_link_hash_entry * h;
353 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
356 || (h->type != bfd_link_hash_defined
357 && h->type != bfd_link_hash_defweak))
360 value = (h->u.def.value
361 + h->u.def.section->output_section->vma
362 + h->u.def.section->output_offset);
368 get_gp (struct bfd_link_info * info,
373 static bfd_boolean cached = FALSE;
374 static bfd_vma cached_value = 0;
378 cached_value = get_symbol_value ("__gp", info, abfd, sec, offset);
385 get_romstart (struct bfd_link_info * info,
390 static bfd_boolean cached = FALSE;
391 static bfd_vma cached_value = 0;
395 cached_value = get_symbol_value ("_start", info, abfd, sec, offset);
402 get_ramstart (struct bfd_link_info * info,
407 static bfd_boolean cached = FALSE;
408 static bfd_vma cached_value = 0;
412 cached_value = get_symbol_value ("__datastart", info, abfd, sec, offset);
418 #define NUM_STACK_ENTRIES 16
419 static int32_t rx_stack [ NUM_STACK_ENTRIES ];
420 static unsigned int rx_stack_top;
422 #define RX_STACK_PUSH(val) \
425 if (rx_stack_top < NUM_STACK_ENTRIES) \
426 rx_stack [rx_stack_top ++] = (val); \
428 r = bfd_reloc_dangerous; \
432 #define RX_STACK_POP(dest) \
435 if (rx_stack_top > 0) \
436 (dest) = rx_stack [-- rx_stack_top]; \
438 (dest) = 0, r = bfd_reloc_dangerous; \
442 /* Relocate an RX ELF section.
443 There is some attempt to make this function usable for many architectures,
444 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
445 if only to serve as a learning tool.
447 The RELOCATE_SECTION function is called by the new ELF backend linker
448 to handle the relocations for a section.
450 The relocs are always passed as Rela structures; if the section
451 actually uses Rel structures, the r_addend field will always be
454 This function is responsible for adjusting the section contents as
455 necessary, and (if using Rela relocs and generating a relocatable
456 output file) adjusting the reloc addend as necessary.
458 This function does not have to worry about setting the reloc
459 address or the reloc symbol index.
461 LOCAL_SYMS is a pointer to the swapped in local symbols.
463 LOCAL_SECTIONS is an array giving the section in the input file
464 corresponding to the st_shndx field of each local symbol.
466 The global hash table entry for the global symbols can be found
467 via elf_sym_hashes (input_bfd).
469 When generating relocatable output, this function must handle
470 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
471 going to be the section symbol corresponding to the output
472 section, which means that the addend must be adjusted
476 rx_elf_relocate_section
478 struct bfd_link_info * info,
480 asection * input_section,
482 Elf_Internal_Rela * relocs,
483 Elf_Internal_Sym * local_syms,
484 asection ** local_sections)
486 Elf_Internal_Shdr * symtab_hdr;
487 struct elf_link_hash_entry ** sym_hashes;
488 Elf_Internal_Rela * rel;
489 Elf_Internal_Rela * relend;
490 bfd_boolean pid_mode;
491 bfd_boolean saw_subtract = FALSE;
492 const char * table_default_cache = NULL;
493 bfd_vma table_start_cache = 0;
494 bfd_vma table_end_cache = 0;
496 if (elf_elfheader (output_bfd)->e_flags & E_FLAG_RX_PID)
501 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
502 sym_hashes = elf_sym_hashes (input_bfd);
503 relend = relocs + input_section->reloc_count;
504 for (rel = relocs; rel < relend; rel ++)
506 reloc_howto_type * howto;
507 unsigned long r_symndx;
508 Elf_Internal_Sym * sym;
510 struct elf_link_hash_entry * h;
512 bfd_reloc_status_type r;
513 const char * name = NULL;
514 bfd_boolean unresolved_reloc = TRUE;
517 r_type = ELF32_R_TYPE (rel->r_info);
518 r_symndx = ELF32_R_SYM (rel->r_info);
520 howto = rx_elf_howto_table + ELF32_R_TYPE (rel->r_info);
526 if (rx_stack_top == 0)
527 saw_subtract = FALSE;
529 if (r_symndx < symtab_hdr->sh_info)
531 sym = local_syms + r_symndx;
532 sec = local_sections [r_symndx];
533 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel);
535 name = bfd_elf_string_from_elf_section
536 (input_bfd, symtab_hdr->sh_link, sym->st_name);
537 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
541 bfd_boolean warned, ignored;
543 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
544 r_symndx, symtab_hdr, sym_hashes, h,
545 sec, relocation, unresolved_reloc,
548 name = h->root.root.string;
551 if (strncmp (name, "$tableentry$default$", 20) == 0)
557 if (table_default_cache != name)
560 /* All relocs for a given table should be to the same
561 (weak) default symbol) so we can use it to detect a
562 cache miss. We use the offset into the table to find
563 the "real" symbol. Calculate and store the table's
566 table_default_cache = name;
568 /* We have already done error checking in rx_table_find(). */
570 buf = (char *) malloc (13 + strlen (name + 20));
572 sprintf (buf, "$tablestart$%s", name + 20);
573 table_start_cache = get_symbol_value (buf,
579 sprintf (buf, "$tableend$%s", name + 20);
580 table_end_cache = get_symbol_value (buf,
589 entry_vma = (input_section->output_section->vma
590 + input_section->output_offset
593 if (table_end_cache <= entry_vma || entry_vma < table_start_cache)
595 /* xgettext:c-format */
596 _bfd_error_handler (_("%pB:%pA: table entry %s outside table"),
597 input_bfd, input_section,
600 else if ((int) (entry_vma - table_start_cache) % 4)
602 /* xgettext:c-format */
603 _bfd_error_handler (_("%pB:%pA: table entry %s not word-aligned within table"),
604 input_bfd, input_section,
609 idx = (int) (entry_vma - table_start_cache) / 4;
611 /* This will look like $tableentry$<N>$<name> */
612 buf = (char *) malloc (12 + 20 + strlen (name + 20));
613 sprintf (buf, "$tableentry$%d$%s", idx, name + 20);
615 h = (struct elf_link_hash_entry *) bfd_link_hash_lookup (info->hash, buf, FALSE, FALSE, TRUE);
619 relocation = (h->root.u.def.value
620 + h->root.u.def.section->output_section->vma
621 + h->root.u.def.section->output_offset);;
628 if (sec != NULL && discarded_section (sec))
629 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
630 rel, 1, relend, howto, 0, contents);
632 if (bfd_link_relocatable (info))
634 /* This is a relocatable link. We don't have to change
635 anything, unless the reloc is against a section symbol,
636 in which case we have to adjust according to where the
637 section symbol winds up in the output section. */
638 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
639 rel->r_addend += sec->output_offset;
643 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
644 /* If the symbol is undefined and weak
645 then the relocation resolves to zero. */
649 if (howto->pc_relative)
651 relocation -= (input_section->output_section->vma
652 + input_section->output_offset
654 if (r_type != R_RX_RH_3_PCREL
655 && r_type != R_RX_DIR3U_PCREL)
659 relocation += rel->r_addend;
664 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
665 #define ALIGN(m) if (relocation & m) r = bfd_reloc_other;
666 #define OP(i) (contents[rel->r_offset + (i)])
667 #define WARN_REDHAT(type) \
668 /* xgettext:c-format */ \
669 _bfd_error_handler (_("%pB:%pA: Warning: deprecated Red Hat reloc " type " detected against: %s."), \
670 input_bfd, input_section, name)
672 /* Check for unsafe relocs in PID mode. These are any relocs where
673 an absolute address is being computed. There are special cases
674 for relocs against symbols that are known to be referenced in
675 crt0.o before the PID base address register has been initialised. */
676 #define UNSAFE_FOR_PID \
681 && sec->flags & SEC_READONLY \
682 && !(input_section->flags & SEC_DEBUGGING) \
683 && strcmp (name, "__pid_base") != 0 \
684 && strcmp (name, "__gp") != 0 \
685 && strcmp (name, "__romdatastart") != 0 \
687 /* xgettext:c-format */ \
688 _bfd_error_handler (_("%pB(%pA): unsafe PID relocation %s " \
689 "at %#" PRIx64 " (against %s in %s)"), \
690 input_bfd, input_section, howto->name, \
691 (uint64_t) (input_section->output_section->vma \
692 + input_section->output_offset \
698 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
707 case R_RX_RH_3_PCREL:
708 WARN_REDHAT ("RX_RH_3_PCREL");
711 OP (0) |= relocation & 0x07;
715 WARN_REDHAT ("RX_RH_8_NEG");
716 relocation = - relocation;
718 case R_RX_DIR8S_PCREL:
737 WARN_REDHAT ("RX_RH_16_NEG");
738 relocation = - relocation;
740 case R_RX_DIR16S_PCREL:
742 RANGE (-32768, 32767);
743 #if RX_OPCODE_BIG_ENDIAN
746 OP (1) = relocation >> 8;
751 WARN_REDHAT ("RX_RH_16_OP");
753 RANGE (-32768, 32767);
754 #if RX_OPCODE_BIG_ENDIAN
756 OP (0) = relocation >> 8;
759 OP (1) = relocation >> 8;
765 RANGE (-32768, 65535);
766 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
769 OP (0) = relocation >> 8;
774 OP (1) = relocation >> 8;
781 #if RX_OPCODE_BIG_ENDIAN
783 OP (0) = relocation >> 8;
786 OP (1) = relocation >> 8;
792 RANGE (-32768, 65536);
793 #if RX_OPCODE_BIG_ENDIAN
795 OP (0) = relocation >> 8;
798 OP (1) = relocation >> 8;
804 RANGE (-32768, 65536);
805 #if RX_OPCODE_BIG_ENDIAN
807 OP (1) = relocation >> 8;
810 OP (0) = relocation >> 8;
814 case R_RX_DIR3U_PCREL:
817 OP (0) |= relocation & 0x07;
822 WARN_REDHAT ("RX_RH_24_NEG");
823 relocation = - relocation;
825 case R_RX_DIR24S_PCREL:
826 RANGE (-0x800000, 0x7fffff);
827 #if RX_OPCODE_BIG_ENDIAN
829 OP (1) = relocation >> 8;
830 OP (0) = relocation >> 16;
833 OP (1) = relocation >> 8;
834 OP (2) = relocation >> 16;
840 WARN_REDHAT ("RX_RH_24_OP");
841 RANGE (-0x800000, 0x7fffff);
842 #if RX_OPCODE_BIG_ENDIAN
844 OP (1) = relocation >> 8;
845 OP (0) = relocation >> 16;
848 OP (1) = relocation >> 8;
849 OP (2) = relocation >> 16;
855 RANGE (-0x800000, 0x7fffff);
856 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
859 OP (1) = relocation >> 8;
860 OP (0) = relocation >> 16;
865 OP (1) = relocation >> 8;
866 OP (2) = relocation >> 16;
872 WARN_REDHAT ("RX_RH_24_UNS");
874 #if RX_OPCODE_BIG_ENDIAN
876 OP (1) = relocation >> 8;
877 OP (0) = relocation >> 16;
880 OP (1) = relocation >> 8;
881 OP (2) = relocation >> 16;
887 WARN_REDHAT ("RX_RH_32_NEG");
888 relocation = - relocation;
889 #if RX_OPCODE_BIG_ENDIAN
891 OP (2) = relocation >> 8;
892 OP (1) = relocation >> 16;
893 OP (0) = relocation >> 24;
896 OP (1) = relocation >> 8;
897 OP (2) = relocation >> 16;
898 OP (3) = relocation >> 24;
904 WARN_REDHAT ("RX_RH_32_OP");
905 #if RX_OPCODE_BIG_ENDIAN
907 OP (2) = relocation >> 8;
908 OP (1) = relocation >> 16;
909 OP (0) = relocation >> 24;
912 OP (1) = relocation >> 8;
913 OP (2) = relocation >> 16;
914 OP (3) = relocation >> 24;
919 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
922 OP (2) = relocation >> 8;
923 OP (1) = relocation >> 16;
924 OP (0) = relocation >> 24;
929 OP (1) = relocation >> 8;
930 OP (2) = relocation >> 16;
931 OP (3) = relocation >> 24;
936 if (BIGE (output_bfd))
939 OP (1) = relocation >> 8;
940 OP (2) = relocation >> 16;
941 OP (3) = relocation >> 24;
946 OP (2) = relocation >> 8;
947 OP (1) = relocation >> 16;
948 OP (0) = relocation >> 24;
955 WARN_REDHAT ("RX_RH_DIFF");
956 val = bfd_get_32 (output_bfd, & OP (0));
958 bfd_put_32 (output_bfd, val, & OP (0));
963 WARN_REDHAT ("RX_RH_GPRELB");
964 relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
966 #if RX_OPCODE_BIG_ENDIAN
968 OP (0) = relocation >> 8;
971 OP (1) = relocation >> 8;
976 WARN_REDHAT ("RX_RH_GPRELW");
977 relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
981 #if RX_OPCODE_BIG_ENDIAN
983 OP (0) = relocation >> 8;
986 OP (1) = relocation >> 8;
991 WARN_REDHAT ("RX_RH_GPRELL");
992 relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
996 #if RX_OPCODE_BIG_ENDIAN
998 OP (0) = relocation >> 8;
1000 OP (0) = relocation;
1001 OP (1) = relocation >> 8;
1005 /* Internal relocations just for relaxation: */
1006 case R_RX_RH_ABS5p5B:
1007 RX_STACK_POP (relocation);
1010 OP (0) |= relocation >> 2;
1012 OP (1) |= (relocation << 6) & 0x80;
1013 OP (1) |= (relocation << 3) & 0x08;
1016 case R_RX_RH_ABS5p5W:
1017 RX_STACK_POP (relocation);
1022 OP (0) |= relocation >> 2;
1024 OP (1) |= (relocation << 6) & 0x80;
1025 OP (1) |= (relocation << 3) & 0x08;
1028 case R_RX_RH_ABS5p5L:
1029 RX_STACK_POP (relocation);
1034 OP (0) |= relocation >> 2;
1036 OP (1) |= (relocation << 6) & 0x80;
1037 OP (1) |= (relocation << 3) & 0x08;
1040 case R_RX_RH_ABS5p8B:
1041 RX_STACK_POP (relocation);
1044 OP (0) |= (relocation << 3) & 0x80;
1045 OP (0) |= relocation & 0x0f;
1048 case R_RX_RH_ABS5p8W:
1049 RX_STACK_POP (relocation);
1054 OP (0) |= (relocation << 3) & 0x80;
1055 OP (0) |= relocation & 0x0f;
1058 case R_RX_RH_ABS5p8L:
1059 RX_STACK_POP (relocation);
1064 OP (0) |= (relocation << 3) & 0x80;
1065 OP (0) |= relocation & 0x0f;
1068 case R_RX_RH_UIMM4p8:
1071 OP (0) |= relocation << 4;
1074 case R_RX_RH_UNEG4p8:
1077 OP (0) |= (-relocation) << 4;
1080 /* Complex reloc handling: */
1084 RX_STACK_POP (relocation);
1085 #if RX_OPCODE_BIG_ENDIAN
1086 OP (3) = relocation;
1087 OP (2) = relocation >> 8;
1088 OP (1) = relocation >> 16;
1089 OP (0) = relocation >> 24;
1091 OP (0) = relocation;
1092 OP (1) = relocation >> 8;
1093 OP (2) = relocation >> 16;
1094 OP (3) = relocation >> 24;
1098 case R_RX_ABS32_REV:
1100 RX_STACK_POP (relocation);
1101 #if RX_OPCODE_BIG_ENDIAN
1102 OP (0) = relocation;
1103 OP (1) = relocation >> 8;
1104 OP (2) = relocation >> 16;
1105 OP (3) = relocation >> 24;
1107 OP (3) = relocation;
1108 OP (2) = relocation >> 8;
1109 OP (1) = relocation >> 16;
1110 OP (0) = relocation >> 24;
1114 case R_RX_ABS24S_PCREL:
1117 RX_STACK_POP (relocation);
1118 RANGE (-0x800000, 0x7fffff);
1119 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
1121 OP (2) = relocation;
1122 OP (1) = relocation >> 8;
1123 OP (0) = relocation >> 16;
1127 OP (0) = relocation;
1128 OP (1) = relocation >> 8;
1129 OP (2) = relocation >> 16;
1135 RX_STACK_POP (relocation);
1136 RANGE (-32768, 65535);
1137 #if RX_OPCODE_BIG_ENDIAN
1138 OP (1) = relocation;
1139 OP (0) = relocation >> 8;
1141 OP (0) = relocation;
1142 OP (1) = relocation >> 8;
1146 case R_RX_ABS16_REV:
1148 RX_STACK_POP (relocation);
1149 RANGE (-32768, 65535);
1150 #if RX_OPCODE_BIG_ENDIAN
1151 OP (0) = relocation;
1152 OP (1) = relocation >> 8;
1154 OP (1) = relocation;
1155 OP (0) = relocation >> 8;
1159 case R_RX_ABS16S_PCREL:
1161 RX_STACK_POP (relocation);
1162 RANGE (-32768, 32767);
1163 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
1165 OP (1) = relocation;
1166 OP (0) = relocation >> 8;
1170 OP (0) = relocation;
1171 OP (1) = relocation >> 8;
1177 RX_STACK_POP (relocation);
1179 #if RX_OPCODE_BIG_ENDIAN
1180 OP (1) = relocation;
1181 OP (0) = relocation >> 8;
1183 OP (0) = relocation;
1184 OP (1) = relocation >> 8;
1190 RX_STACK_POP (relocation);
1193 #if RX_OPCODE_BIG_ENDIAN
1194 OP (1) = relocation;
1195 OP (0) = relocation >> 8;
1197 OP (0) = relocation;
1198 OP (1) = relocation >> 8;
1204 RX_STACK_POP (relocation);
1207 #if RX_OPCODE_BIG_ENDIAN
1208 OP (1) = relocation;
1209 OP (0) = relocation >> 8;
1211 OP (0) = relocation;
1212 OP (1) = relocation >> 8;
1218 RX_STACK_POP (relocation);
1220 OP (0) = relocation;
1225 RX_STACK_POP (relocation);
1227 OP (0) = relocation;
1232 RX_STACK_POP (relocation);
1235 OP (0) = relocation;
1240 RX_STACK_POP (relocation);
1243 OP (0) = relocation;
1249 case R_RX_ABS8S_PCREL:
1250 RX_STACK_POP (relocation);
1252 OP (0) = relocation;
1256 if (r_symndx < symtab_hdr->sh_info)
1257 RX_STACK_PUSH (sec->output_section->vma
1258 + sec->output_offset
1264 && (h->root.type == bfd_link_hash_defined
1265 || h->root.type == bfd_link_hash_defweak))
1266 RX_STACK_PUSH (h->root.u.def.value
1267 + sec->output_section->vma
1268 + sec->output_offset
1271 _bfd_error_handler (_("Warning: RX_SYM reloc with an unknown symbol"));
1279 saw_subtract = TRUE;
1282 RX_STACK_PUSH (tmp);
1290 RX_STACK_POP (tmp1);
1291 RX_STACK_POP (tmp2);
1293 RX_STACK_PUSH (tmp1);
1301 saw_subtract = TRUE;
1302 RX_STACK_POP (tmp1);
1303 RX_STACK_POP (tmp2);
1305 RX_STACK_PUSH (tmp2);
1313 RX_STACK_POP (tmp1);
1314 RX_STACK_POP (tmp2);
1316 RX_STACK_PUSH (tmp1);
1324 RX_STACK_POP (tmp1);
1325 RX_STACK_POP (tmp2);
1327 RX_STACK_PUSH (tmp1);
1335 RX_STACK_POP (tmp1);
1336 RX_STACK_POP (tmp2);
1338 RX_STACK_PUSH (tmp1);
1346 RX_STACK_POP (tmp1);
1347 RX_STACK_POP (tmp2);
1349 RX_STACK_PUSH (tmp1);
1353 case R_RX_OPsctsize:
1354 RX_STACK_PUSH (input_section->size);
1358 RX_STACK_PUSH (input_section->output_section->vma);
1365 RX_STACK_POP (tmp1);
1366 RX_STACK_POP (tmp2);
1368 RX_STACK_PUSH (tmp1);
1376 RX_STACK_POP (tmp1);
1377 RX_STACK_POP (tmp2);
1379 RX_STACK_PUSH (tmp1);
1387 RX_STACK_POP (tmp1);
1388 RX_STACK_POP (tmp2);
1390 RX_STACK_PUSH (tmp1);
1400 RX_STACK_PUSH (tmp);
1408 RX_STACK_POP (tmp1);
1409 RX_STACK_POP (tmp2);
1411 RX_STACK_PUSH (tmp1);
1416 RX_STACK_PUSH (get_romstart (info, input_bfd, input_section, rel->r_offset));
1420 RX_STACK_PUSH (get_ramstart (info, input_bfd, input_section, rel->r_offset));
1424 r = bfd_reloc_notsupported;
1428 if (r != bfd_reloc_ok)
1430 const char * msg = NULL;
1434 case bfd_reloc_overflow:
1435 /* Catch the case of a missing function declaration
1436 and emit a more helpful error message. */
1437 if (r_type == R_RX_DIR24S_PCREL)
1438 /* xgettext:c-format */
1439 msg = _("%pB(%pA): error: call to undefined function '%s'");
1441 (*info->callbacks->reloc_overflow)
1442 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
1443 input_bfd, input_section, rel->r_offset);
1446 case bfd_reloc_undefined:
1447 (*info->callbacks->undefined_symbol)
1448 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1451 case bfd_reloc_other:
1452 /* xgettext:c-format */
1453 msg = _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
1456 case bfd_reloc_outofrange:
1457 /* xgettext:c-format */
1458 msg = _("%pB(%pA): internal error: out of range error");
1461 case bfd_reloc_notsupported:
1462 /* xgettext:c-format */
1463 msg = _("%pB(%pA): internal error: unsupported relocation error");
1466 case bfd_reloc_dangerous:
1467 /* xgettext:c-format */
1468 msg = _("%pB(%pA): internal error: dangerous relocation");
1472 /* xgettext:c-format */
1473 msg = _("%pB(%pA): internal error: unknown error");
1478 _bfd_error_handler (msg, input_bfd, input_section, name);
1485 /* Relaxation Support. */
1487 /* Progression of relocations from largest operand size to smallest
1491 next_smaller_reloc (int r)
1495 case R_RX_DIR32: return R_RX_DIR24S;
1496 case R_RX_DIR24S: return R_RX_DIR16S;
1497 case R_RX_DIR16S: return R_RX_DIR8S;
1498 case R_RX_DIR8S: return R_RX_NONE;
1500 case R_RX_DIR16: return R_RX_DIR8;
1501 case R_RX_DIR8: return R_RX_NONE;
1503 case R_RX_DIR16U: return R_RX_DIR8U;
1504 case R_RX_DIR8U: return R_RX_NONE;
1506 case R_RX_DIR24S_PCREL: return R_RX_DIR16S_PCREL;
1507 case R_RX_DIR16S_PCREL: return R_RX_DIR8S_PCREL;
1508 case R_RX_DIR8S_PCREL: return R_RX_DIR3U_PCREL;
1510 case R_RX_DIR16UL: return R_RX_DIR8UL;
1511 case R_RX_DIR8UL: return R_RX_NONE;
1512 case R_RX_DIR16UW: return R_RX_DIR8UW;
1513 case R_RX_DIR8UW: return R_RX_NONE;
1515 case R_RX_RH_32_OP: return R_RX_RH_24_OP;
1516 case R_RX_RH_24_OP: return R_RX_RH_16_OP;
1517 case R_RX_RH_16_OP: return R_RX_DIR8;
1519 case R_RX_ABS32: return R_RX_ABS24S;
1520 case R_RX_ABS24S: return R_RX_ABS16S;
1521 case R_RX_ABS16: return R_RX_ABS8;
1522 case R_RX_ABS16U: return R_RX_ABS8U;
1523 case R_RX_ABS16S: return R_RX_ABS8S;
1524 case R_RX_ABS8: return R_RX_NONE;
1525 case R_RX_ABS8U: return R_RX_NONE;
1526 case R_RX_ABS8S: return R_RX_NONE;
1527 case R_RX_ABS24S_PCREL: return R_RX_ABS16S_PCREL;
1528 case R_RX_ABS16S_PCREL: return R_RX_ABS8S_PCREL;
1529 case R_RX_ABS8S_PCREL: return R_RX_NONE;
1530 case R_RX_ABS16UL: return R_RX_ABS8UL;
1531 case R_RX_ABS16UW: return R_RX_ABS8UW;
1532 case R_RX_ABS8UL: return R_RX_NONE;
1533 case R_RX_ABS8UW: return R_RX_NONE;
1538 /* Delete some bytes from a section while relaxing. */
1541 elf32_rx_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
1542 Elf_Internal_Rela *alignment_rel, int force_snip,
1543 Elf_Internal_Rela *irelstart)
1545 Elf_Internal_Shdr * symtab_hdr;
1546 unsigned int sec_shndx;
1547 bfd_byte * contents;
1548 Elf_Internal_Rela * irel;
1549 Elf_Internal_Rela * irelend;
1550 Elf_Internal_Sym * isym;
1551 Elf_Internal_Sym * isymend;
1553 unsigned int symcount;
1554 struct elf_link_hash_entry ** sym_hashes;
1555 struct elf_link_hash_entry ** end_hashes;
1560 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1562 contents = elf_section_data (sec)->this_hdr.contents;
1564 /* The deletion must stop at the next alignment boundary, if
1565 ALIGNMENT_REL is non-NULL. */
1568 toaddr = alignment_rel->r_offset;
1570 BFD_ASSERT (toaddr > addr);
1572 /* Actually delete the bytes. */
1573 memmove (contents + addr, contents + addr + count,
1574 (size_t) (toaddr - addr - count));
1576 /* If we don't have an alignment marker to worry about, we can just
1577 shrink the section. Otherwise, we have to fill in the newly
1578 created gap with NOP insns (0x03). */
1582 memset (contents + toaddr - count, 0x03, count);
1585 BFD_ASSERT (irel != NULL || sec->reloc_count == 0);
1586 irelend = irel + sec->reloc_count;
1588 /* Adjust all the relocs. */
1589 for (; irel < irelend; irel++)
1591 /* Get the new reloc address. */
1592 if (irel->r_offset > addr
1593 && (irel->r_offset < toaddr
1594 || (force_snip && irel->r_offset == toaddr)))
1595 irel->r_offset -= count;
1597 /* If we see an ALIGN marker at the end of the gap, we move it
1598 to the beginning of the gap, since marking these gaps is what
1600 if (irel->r_offset == toaddr
1601 && ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
1602 && irel->r_addend & RX_RELAXA_ALIGN)
1603 irel->r_offset -= count;
1606 /* Adjust the local symbols defined in this section. */
1607 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1608 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1609 isymend = isym + symtab_hdr->sh_info;
1611 for (; isym < isymend; isym++)
1613 /* If the symbol is in the range of memory we just moved, we
1614 have to adjust its value. */
1615 if (isym->st_shndx == sec_shndx
1616 && isym->st_value > addr
1617 && isym->st_value < toaddr)
1618 isym->st_value -= count;
1620 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1621 *end* is in the moved bytes but it's *start* isn't), then we
1622 must adjust its size. */
1623 if (isym->st_shndx == sec_shndx
1624 && isym->st_value < addr
1625 && isym->st_value + isym->st_size > addr
1626 && isym->st_value + isym->st_size < toaddr)
1627 isym->st_size -= count;
1630 /* Now adjust the global symbols defined in this section. */
1631 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1632 - symtab_hdr->sh_info);
1633 sym_hashes = elf_sym_hashes (abfd);
1634 end_hashes = sym_hashes + symcount;
1636 for (; sym_hashes < end_hashes; sym_hashes++)
1638 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1640 if ((sym_hash->root.type == bfd_link_hash_defined
1641 || sym_hash->root.type == bfd_link_hash_defweak)
1642 && sym_hash->root.u.def.section == sec)
1644 /* As above, adjust the value if needed. */
1645 if (sym_hash->root.u.def.value > addr
1646 && sym_hash->root.u.def.value < toaddr)
1647 sym_hash->root.u.def.value -= count;
1649 /* As above, adjust the size if needed. */
1650 if (sym_hash->root.u.def.value < addr
1651 && sym_hash->root.u.def.value + sym_hash->size > addr
1652 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1653 sym_hash->size -= count;
1660 /* Used to sort relocs by address. If relocs have the same address,
1661 we maintain their relative order, except that R_RX_RH_RELAX
1662 alignment relocs must be the first reloc for any given address. */
1665 reloc_bubblesort (Elf_Internal_Rela * r, int count)
1669 bfd_boolean swappit;
1671 /* This is almost a classic bubblesort. It's the slowest sort, but
1672 we're taking advantage of the fact that the relocations are
1673 mostly in order already (the assembler emits them that way) and
1674 we need relocs with the same address to remain in the same
1680 for (i = 0; i < count - 1; i ++)
1682 if (r[i].r_offset > r[i + 1].r_offset)
1684 else if (r[i].r_offset < r[i + 1].r_offset)
1686 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
1687 && (r[i + 1].r_addend & RX_RELAXA_ALIGN))
1689 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
1690 && (r[i + 1].r_addend & RX_RELAXA_ELIGN)
1691 && !(ELF32_R_TYPE (r[i].r_info) == R_RX_RH_RELAX
1692 && (r[i].r_addend & RX_RELAXA_ALIGN)))
1699 Elf_Internal_Rela tmp;
1704 /* If we do move a reloc back, re-scan to see if it
1705 needs to be moved even further back. This avoids
1706 most of the O(n^2) behavior for our cases. */
1716 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1717 rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1718 lrel, abfd, sec, link_info, scale)
1721 rx_offset_for_reloc (bfd * abfd,
1722 Elf_Internal_Rela * rel,
1723 Elf_Internal_Shdr * symtab_hdr,
1724 Elf_External_Sym_Shndx * shndx_buf ATTRIBUTE_UNUSED,
1725 Elf_Internal_Sym * intsyms,
1726 Elf_Internal_Rela ** lrel,
1728 asection * input_section,
1729 struct bfd_link_info * info,
1733 bfd_reloc_status_type r;
1737 /* REL is the first of 1..N relocations. We compute the symbol
1738 value for each relocation, then combine them if needed. LREL
1739 gets a pointer to the last relocation used. */
1744 /* Get the value of the symbol referred to by the reloc. */
1745 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1747 /* A local symbol. */
1748 Elf_Internal_Sym *isym;
1751 isym = intsyms + ELF32_R_SYM (rel->r_info);
1753 if (isym->st_shndx == SHN_UNDEF)
1754 ssec = bfd_und_section_ptr;
1755 else if (isym->st_shndx == SHN_ABS)
1756 ssec = bfd_abs_section_ptr;
1757 else if (isym->st_shndx == SHN_COMMON)
1758 ssec = bfd_com_section_ptr;
1760 ssec = bfd_section_from_elf_index (abfd,
1763 /* Initial symbol value. */
1764 symval = isym->st_value;
1766 /* GAS may have made this symbol relative to a section, in
1767 which case, we have to add the addend to find the
1769 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
1770 symval += rel->r_addend;
1774 if ((ssec->flags & SEC_MERGE)
1775 && ssec->sec_info_type == SEC_INFO_TYPE_MERGE)
1776 symval = _bfd_merged_section_offset (abfd, & ssec,
1777 elf_section_data (ssec)->sec_info,
1781 /* Now make the offset relative to where the linker is putting it. */
1784 ssec->output_section->vma + ssec->output_offset;
1786 symval += rel->r_addend;
1791 struct elf_link_hash_entry * h;
1793 /* An external symbol. */
1794 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1795 h = elf_sym_hashes (abfd)[indx];
1796 BFD_ASSERT (h != NULL);
1798 if (h->root.type != bfd_link_hash_defined
1799 && h->root.type != bfd_link_hash_defweak)
1801 /* This appears to be a reference to an undefined
1802 symbol. Just ignore it--it will be caught by the
1803 regular reloc processing. */
1809 symval = (h->root.u.def.value
1810 + h->root.u.def.section->output_section->vma
1811 + h->root.u.def.section->output_offset);
1813 symval += rel->r_addend;
1816 switch (ELF32_R_TYPE (rel->r_info))
1819 RX_STACK_PUSH (symval);
1823 RX_STACK_POP (tmp1);
1825 RX_STACK_PUSH (tmp1);
1829 RX_STACK_POP (tmp1);
1830 RX_STACK_POP (tmp2);
1832 RX_STACK_PUSH (tmp1);
1836 RX_STACK_POP (tmp1);
1837 RX_STACK_POP (tmp2);
1839 RX_STACK_PUSH (tmp2);
1843 RX_STACK_POP (tmp1);
1844 RX_STACK_POP (tmp2);
1846 RX_STACK_PUSH (tmp1);
1850 RX_STACK_POP (tmp1);
1851 RX_STACK_POP (tmp2);
1853 RX_STACK_PUSH (tmp1);
1857 RX_STACK_POP (tmp1);
1858 RX_STACK_POP (tmp2);
1860 RX_STACK_PUSH (tmp1);
1864 RX_STACK_POP (tmp1);
1865 RX_STACK_POP (tmp2);
1867 RX_STACK_PUSH (tmp1);
1870 case R_RX_OPsctsize:
1871 RX_STACK_PUSH (input_section->size);
1875 RX_STACK_PUSH (input_section->output_section->vma);
1879 RX_STACK_POP (tmp1);
1880 RX_STACK_POP (tmp2);
1882 RX_STACK_PUSH (tmp1);
1886 RX_STACK_POP (tmp1);
1887 RX_STACK_POP (tmp2);
1889 RX_STACK_PUSH (tmp1);
1893 RX_STACK_POP (tmp1);
1894 RX_STACK_POP (tmp2);
1896 RX_STACK_PUSH (tmp1);
1900 RX_STACK_POP (tmp1);
1902 RX_STACK_PUSH (tmp1);
1906 RX_STACK_POP (tmp1);
1907 RX_STACK_POP (tmp2);
1909 RX_STACK_PUSH (tmp1);
1913 RX_STACK_PUSH (get_romstart (info, input_bfd, input_section, rel->r_offset));
1917 RX_STACK_PUSH (get_ramstart (info, input_bfd, input_section, rel->r_offset));
1925 RX_STACK_POP (symval);
1936 RX_STACK_POP (symval);
1944 RX_STACK_POP (symval);
1957 move_reloc (Elf_Internal_Rela * irel, Elf_Internal_Rela * srel, int delta)
1959 bfd_vma old_offset = srel->r_offset;
1962 while (irel <= srel)
1964 if (irel->r_offset == old_offset)
1965 irel->r_offset += delta;
1970 /* Relax one section. */
1973 elf32_rx_relax_section (bfd * abfd,
1975 struct bfd_link_info * link_info,
1976 bfd_boolean * again,
1977 bfd_boolean allow_pcrel3)
1979 Elf_Internal_Shdr * symtab_hdr;
1980 Elf_Internal_Shdr * shndx_hdr;
1981 Elf_Internal_Rela * internal_relocs;
1982 Elf_Internal_Rela * irel;
1983 Elf_Internal_Rela * srel;
1984 Elf_Internal_Rela * irelend;
1985 Elf_Internal_Rela * next_alignment;
1986 Elf_Internal_Rela * prev_alignment;
1987 bfd_byte * contents = NULL;
1988 bfd_byte * free_contents = NULL;
1989 Elf_Internal_Sym * intsyms = NULL;
1990 Elf_Internal_Sym * free_intsyms = NULL;
1991 Elf_External_Sym_Shndx * shndx_buf = NULL;
1997 int section_alignment_glue;
1998 /* how much to scale the relocation by - 1, 2, or 4. */
2001 /* Assume nothing changes. */
2004 /* We don't have to do anything for a relocatable link, if
2005 this section does not have relocs, or if this is not a
2007 if (bfd_link_relocatable (link_info)
2008 || (sec->flags & SEC_RELOC) == 0
2009 || sec->reloc_count == 0
2010 || (sec->flags & SEC_CODE) == 0)
2013 symtab_hdr = & elf_symtab_hdr (abfd);
2014 if (elf_symtab_shndx_list (abfd))
2015 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
2019 sec_start = sec->output_section->vma + sec->output_offset;
2021 /* Get the section contents. */
2022 if (elf_section_data (sec)->this_hdr.contents != NULL)
2023 contents = elf_section_data (sec)->this_hdr.contents;
2024 /* Go get them off disk. */
2027 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2029 elf_section_data (sec)->this_hdr.contents = contents;
2032 /* Read this BFD's symbols. */
2033 /* Get cached copy if it exists. */
2034 if (symtab_hdr->contents != NULL)
2035 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2038 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
2039 symtab_hdr->contents = (bfd_byte *) intsyms;
2042 if (shndx_hdr && shndx_hdr->sh_size != 0)
2046 amt = symtab_hdr->sh_info;
2047 amt *= sizeof (Elf_External_Sym_Shndx);
2048 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2049 if (shndx_buf == NULL)
2051 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2052 || bfd_bread (shndx_buf, amt, abfd) != amt)
2054 shndx_hdr->contents = (bfd_byte *) shndx_buf;
2057 /* Get a copy of the native relocations. */
2058 /* Note - we ignore the setting of link_info->keep_memory when reading
2059 in these relocs. We have to maintain a permanent copy of the relocs
2060 because we are going to walk over them multiple times, adjusting them
2061 as bytes are deleted from the section, and with this relaxation
2062 function itself being called multiple times on the same section... */
2063 internal_relocs = _bfd_elf_link_read_relocs
2064 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, TRUE);
2065 if (internal_relocs == NULL)
2068 /* The RL_ relocs must be just before the operand relocs they go
2069 with, so we must sort them to guarantee this. We use bubblesort
2070 instead of qsort so we can guarantee that relocs with the same
2071 address remain in the same relative order. */
2072 reloc_bubblesort (internal_relocs, sec->reloc_count);
2074 /* Walk through them looking for relaxing opportunities. */
2075 irelend = internal_relocs + sec->reloc_count;
2077 /* This will either be NULL or a pointer to the next alignment
2079 next_alignment = internal_relocs;
2080 /* This will be the previous alignment, although at first it points
2081 to the first real relocation. */
2082 prev_alignment = internal_relocs;
2084 /* We calculate worst case shrinkage caused by alignment directives.
2085 No fool-proof, but better than either ignoring the problem or
2086 doing heavy duty analysis of all the alignment markers in all
2088 section_alignment_glue = 0;
2089 for (irel = internal_relocs; irel < irelend; irel++)
2090 if (ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
2091 && irel->r_addend & RX_RELAXA_ALIGN)
2093 int this_glue = 1 << (irel->r_addend & RX_RELAXA_ANUM);
2095 if (section_alignment_glue < this_glue)
2096 section_alignment_glue = this_glue;
2098 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2100 section_alignment_glue *= 2;
2102 for (irel = internal_relocs; irel < irelend; irel++)
2104 unsigned char *insn;
2107 /* The insns we care about are all marked with one of these. */
2108 if (ELF32_R_TYPE (irel->r_info) != R_RX_RH_RELAX)
2111 if (irel->r_addend & RX_RELAXA_ALIGN
2112 || next_alignment == internal_relocs)
2114 /* When we delete bytes, we need to maintain all the alignments
2115 indicated. In addition, we need to be careful about relaxing
2116 jumps across alignment boundaries - these displacements
2117 *grow* when we delete bytes. For now, don't shrink
2118 displacements across an alignment boundary, just in case.
2119 Note that this only affects relocations to the same
2121 prev_alignment = next_alignment;
2122 next_alignment += 2;
2123 while (next_alignment < irelend
2124 && (ELF32_R_TYPE (next_alignment->r_info) != R_RX_RH_RELAX
2125 || !(next_alignment->r_addend & RX_RELAXA_ELIGN)))
2127 if (next_alignment >= irelend || next_alignment->r_offset == 0)
2128 next_alignment = NULL;
2131 /* When we hit alignment markers, see if we've shrunk enough
2132 before them to reduce the gap without violating the alignment
2134 if (irel->r_addend & RX_RELAXA_ALIGN)
2136 /* At this point, the next relocation *should* be the ELIGN
2138 Elf_Internal_Rela *erel = irel + 1;
2139 unsigned int alignment, nbytes;
2141 if (ELF32_R_TYPE (erel->r_info) != R_RX_RH_RELAX)
2143 if (!(erel->r_addend & RX_RELAXA_ELIGN))
2146 alignment = 1 << (irel->r_addend & RX_RELAXA_ANUM);
2148 if (erel->r_offset - irel->r_offset < alignment)
2151 nbytes = erel->r_offset - irel->r_offset;
2152 nbytes /= alignment;
2153 nbytes *= alignment;
2155 elf32_rx_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment,
2156 erel->r_offset == sec->size, internal_relocs);
2162 if (irel->r_addend & RX_RELAXA_ELIGN)
2165 insn = contents + irel->r_offset;
2167 nrelocs = irel->r_addend & RX_RELAXA_RNUM;
2169 /* At this point, we have an insn that is a candidate for linker
2170 relaxation. There are NRELOCS relocs following that may be
2171 relaxed, although each reloc may be made of more than one
2172 reloc entry (such as gp-rel symbols). */
2174 /* Get the value of the symbol referred to by the reloc. Just
2175 in case this is the last reloc in the list, use the RL's
2176 addend to choose between this reloc (no addend) or the next
2177 (yes addend, which means at least one following reloc). */
2179 /* srel points to the "current" reloction for this insn -
2180 actually the last reloc for a given operand, which is the one
2181 we need to update. We check the relaxations in the same
2182 order that the relocations happen, so we'll just push it
2186 pc = sec->output_section->vma + sec->output_offset
2190 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2191 pcrel = symval - pc + srel->r_addend; \
2194 #define SNIPNR(offset, nbytes) \
2195 elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0, internal_relocs);
2196 #define SNIP(offset, nbytes, newtype) \
2197 SNIPNR (offset, nbytes); \
2198 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2200 /* The order of these bit tests must match the order that the
2201 relocs appear in. Since we sorted those by offset, we can
2204 /* Note that the numbers in, say, DSP6 are the bit offsets of
2205 the code fields that describe the operand. Bits number 0 for
2206 the MSB of insn[0]. */
2213 if (irel->r_addend & RX_RELAXA_DSP6)
2218 if (code == 2 && symval/scale <= 255)
2220 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2223 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2224 if (newrel != ELF32_R_TYPE (srel->r_info))
2226 SNIP (3, 1, newrel);
2231 else if (code == 1 && symval == 0)
2234 SNIP (2, 1, R_RX_NONE);
2238 /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst. */
2239 else if (code == 1 && symval/scale <= 31
2240 /* Decodable bits. */
2241 && (insn[0] & 0xcc) == 0xcc
2243 && (insn[0] & 0x30) != 0x30
2244 /* Register MSBs. */
2245 && (insn[1] & 0x88) == 0x00)
2249 insn[0] = 0x88 | (insn[0] & 0x30);
2250 /* The register fields are in the right place already. */
2252 /* We can't relax this new opcode. */
2255 switch ((insn[0] & 0x30) >> 4)
2258 newrel = R_RX_RH_ABS5p5B;
2261 newrel = R_RX_RH_ABS5p5W;
2264 newrel = R_RX_RH_ABS5p5L;
2268 move_reloc (irel, srel, -2);
2269 SNIP (2, 1, newrel);
2272 /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst. */
2273 else if (code == 1 && symval/scale <= 31
2274 /* Decodable bits. */
2275 && (insn[0] & 0xf8) == 0x58
2276 /* Register MSBs. */
2277 && (insn[1] & 0x88) == 0x00)
2281 insn[0] = 0xb0 | ((insn[0] & 0x04) << 1);
2282 /* The register fields are in the right place already. */
2284 /* We can't relax this new opcode. */
2287 switch ((insn[0] & 0x08) >> 3)
2290 newrel = R_RX_RH_ABS5p5B;
2293 newrel = R_RX_RH_ABS5p5W;
2297 move_reloc (irel, srel, -2);
2298 SNIP (2, 1, newrel);
2302 /* A DSP4 operand always follows a DSP6 operand, even if there's
2303 no relocation for it. We have to read the code out of the
2304 opcode to calculate the offset of the operand. */
2305 if (irel->r_addend & RX_RELAXA_DSP4)
2307 int code6, offset = 0;
2311 code6 = insn[0] & 0x03;
2314 case 0: offset = 2; break;
2315 case 1: offset = 3; break;
2316 case 2: offset = 4; break;
2317 case 3: offset = 2; break;
2320 code = (insn[0] & 0x0c) >> 2;
2322 if (code == 2 && symval / scale <= 255)
2324 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2328 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2329 if (newrel != ELF32_R_TYPE (srel->r_info))
2331 SNIP (offset+1, 1, newrel);
2336 else if (code == 1 && symval == 0)
2339 SNIP (offset, 1, R_RX_NONE);
2342 /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2343 else if (code == 1 && symval/scale <= 31
2344 /* Decodable bits. */
2345 && (insn[0] & 0xc3) == 0xc3
2347 && (insn[0] & 0x30) != 0x30
2348 /* Register MSBs. */
2349 && (insn[1] & 0x88) == 0x00)
2353 insn[0] = 0x80 | (insn[0] & 0x30);
2354 /* The register fields are in the right place already. */
2356 /* We can't relax this new opcode. */
2359 switch ((insn[0] & 0x30) >> 4)
2362 newrel = R_RX_RH_ABS5p5B;
2365 newrel = R_RX_RH_ABS5p5W;
2368 newrel = R_RX_RH_ABS5p5L;
2372 move_reloc (irel, srel, -2);
2373 SNIP (2, 1, newrel);
2377 /* These always occur alone, but the offset depends on whether
2378 it's a MEMEX opcode (0x06) or not. */
2379 if (irel->r_addend & RX_RELAXA_DSP14)
2384 if (insn[0] == 0x06)
2391 if (code == 2 && symval / scale <= 255)
2393 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2397 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2398 if (newrel != ELF32_R_TYPE (srel->r_info))
2400 SNIP (offset, 1, newrel);
2404 else if (code == 1 && symval == 0)
2407 SNIP (offset, 1, R_RX_NONE);
2418 /* These always occur alone. */
2419 if (irel->r_addend & RX_RELAXA_IMM6)
2425 /* These relocations sign-extend, so we must do signed compares. */
2426 ssymval = (long) symval;
2428 code = insn[0] & 0x03;
2430 if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
2432 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2436 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2437 if (newrel != ELF32_R_TYPE (srel->r_info))
2439 SNIP (2, 1, newrel);
2444 else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
2446 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2450 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2451 if (newrel != ELF32_R_TYPE (srel->r_info))
2453 SNIP (2, 1, newrel);
2458 /* Special case UIMM8 format: CMP #uimm8,Rdst. */
2459 else if (code == 2 && ssymval <= 255 && ssymval >= 16
2460 /* Decodable bits. */
2461 && (insn[0] & 0xfc) == 0x74
2462 /* Decodable bits. */
2463 && ((insn[1] & 0xf0) == 0x00))
2468 insn[1] = 0x50 | (insn[1] & 0x0f);
2470 /* We can't relax this new opcode. */
2473 if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
2474 newrel = R_RX_ABS8U;
2476 newrel = R_RX_DIR8U;
2478 SNIP (2, 1, newrel);
2482 else if (code == 2 && ssymval <= 127 && ssymval >= -128)
2484 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2488 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2489 if (newrel != ELF32_R_TYPE (srel->r_info))
2491 SNIP (2, 1, newrel);
2496 /* Special case UIMM4 format: CMP, MUL, AND, OR. */
2497 else if (code == 1 && ssymval <= 15 && ssymval >= 0
2498 /* Decodable bits and immediate type. */
2500 /* Decodable bits. */
2501 && (insn[1] & 0xc0) == 0x00)
2503 static const int newop[4] = { 1, 3, 4, 5 };
2505 insn[0] = 0x60 | newop[insn[1] >> 4];
2506 /* The register number doesn't move. */
2508 /* We can't relax this new opcode. */
2511 move_reloc (irel, srel, -1);
2513 SNIP (2, 1, R_RX_RH_UIMM4p8);
2517 /* Special case UIMM4 format: ADD -> ADD/SUB. */
2518 else if (code == 1 && ssymval <= 15 && ssymval >= -15
2519 /* Decodable bits and immediate type. */
2521 /* Same register for source and destination. */
2522 && ((insn[1] >> 4) == (insn[1] & 0x0f)))
2526 /* Note that we can't turn "add $0,Rs" into a NOP
2527 because the flags need to be set right. */
2531 insn[0] = 0x60; /* Subtract. */
2532 newrel = R_RX_RH_UNEG4p8;
2536 insn[0] = 0x62; /* Add. */
2537 newrel = R_RX_RH_UIMM4p8;
2540 /* The register number is in the right place. */
2542 /* We can't relax this new opcode. */
2545 move_reloc (irel, srel, -1);
2547 SNIP (2, 1, newrel);
2552 /* These are either matched with a DSP6 (2-byte base) or an id24
2554 if (irel->r_addend & RX_RELAXA_IMM12)
2556 int dspcode, offset = 0;
2561 if ((insn[0] & 0xfc) == 0xfc)
2562 dspcode = 1; /* Just something with one byte operand. */
2564 dspcode = insn[0] & 3;
2567 case 0: offset = 2; break;
2568 case 1: offset = 3; break;
2569 case 2: offset = 4; break;
2570 case 3: offset = 2; break;
2573 /* These relocations sign-extend, so we must do signed compares. */
2574 ssymval = (long) symval;
2576 code = (insn[1] >> 2) & 3;
2577 if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
2579 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2583 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2584 if (newrel != ELF32_R_TYPE (srel->r_info))
2586 SNIP (offset, 1, newrel);
2591 else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
2593 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2597 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2598 if (newrel != ELF32_R_TYPE (srel->r_info))
2600 SNIP (offset, 1, newrel);
2605 /* Special case UIMM8 format: MOV #uimm8,Rdst. */
2606 else if (code == 2 && ssymval <= 255 && ssymval >= 16
2607 /* Decodable bits. */
2609 /* Decodable bits. */
2610 && ((insn[1] & 0x03) == 0x02))
2615 insn[1] = 0x40 | (insn[1] >> 4);
2617 /* We can't relax this new opcode. */
2620 if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
2621 newrel = R_RX_ABS8U;
2623 newrel = R_RX_DIR8U;
2625 SNIP (2, 1, newrel);
2629 else if (code == 2 && ssymval <= 127 && ssymval >= -128)
2631 unsigned int newrel = ELF32_R_TYPE(srel->r_info);
2635 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2636 if (newrel != ELF32_R_TYPE(srel->r_info))
2638 SNIP (offset, 1, newrel);
2643 /* Special case UIMM4 format: MOV #uimm4,Rdst. */
2644 else if (code == 1 && ssymval <= 15 && ssymval >= 0
2645 /* Decodable bits. */
2647 /* Decodable bits. */
2648 && ((insn[1] & 0x03) == 0x02))
2651 insn[1] = insn[1] >> 4;
2653 /* We can't relax this new opcode. */
2656 move_reloc (irel, srel, -1);
2658 SNIP (2, 1, R_RX_RH_UIMM4p8);
2663 if (irel->r_addend & RX_RELAXA_BRA)
2665 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2667 int alignment_glue = 0;
2671 /* Branches over alignment chunks are problematic, as
2672 deleting bytes here makes the branch *further* away. We
2673 can be agressive with branches within this alignment
2674 block, but not branches outside it. */
2675 if ((prev_alignment == NULL
2676 || symval < (bfd_vma)(sec_start + prev_alignment->r_offset))
2677 && (next_alignment == NULL
2678 || symval > (bfd_vma)(sec_start + next_alignment->r_offset)))
2679 alignment_glue = section_alignment_glue;
2681 if (ELF32_R_TYPE(srel[1].r_info) == R_RX_RH_RELAX
2682 && srel[1].r_addend & RX_RELAXA_BRA
2683 && srel[1].r_offset < irel->r_offset + pcrel)
2686 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2688 /* The values we compare PCREL with are not what you'd
2689 expect; they're off by a little to compensate for (1)
2690 where the reloc is relative to the insn, and (2) how much
2691 the insn is going to change when we relax it. */
2693 /* These we have to decode. */
2696 case 0x04: /* BRA pcdsp:24 */
2697 if (-32768 + alignment_glue <= pcrel
2698 && pcrel <= 32765 - alignment_glue)
2701 SNIP (3, 1, newrel);
2706 case 0x38: /* BRA pcdsp:16 */
2707 if (-128 + alignment_glue <= pcrel
2708 && pcrel <= 127 - alignment_glue)
2711 SNIP (2, 1, newrel);
2716 case 0x2e: /* BRA pcdsp:8 */
2717 /* Note that there's a risk here of shortening things so
2718 much that we no longer fit this reloc; it *should*
2719 only happen when you branch across a branch, and that
2720 branch also devolves into BRA.S. "Real" code should
2722 if (max_pcrel3 + alignment_glue <= pcrel
2723 && pcrel <= 10 - alignment_glue
2727 SNIP (1, 1, newrel);
2728 move_reloc (irel, srel, -1);
2733 case 0x05: /* BSR pcdsp:24 */
2734 if (-32768 + alignment_glue <= pcrel
2735 && pcrel <= 32765 - alignment_glue)
2738 SNIP (1, 1, newrel);
2743 case 0x3a: /* BEQ.W pcdsp:16 */
2744 case 0x3b: /* BNE.W pcdsp:16 */
2745 if (-128 + alignment_glue <= pcrel
2746 && pcrel <= 127 - alignment_glue)
2748 insn[0] = 0x20 | (insn[0] & 1);
2749 SNIP (1, 1, newrel);
2754 case 0x20: /* BEQ.B pcdsp:8 */
2755 case 0x21: /* BNE.B pcdsp:8 */
2756 if (max_pcrel3 + alignment_glue <= pcrel
2757 && pcrel - alignment_glue <= 10
2760 insn[0] = 0x10 | ((insn[0] & 1) << 3);
2761 SNIP (1, 1, newrel);
2762 move_reloc (irel, srel, -1);
2767 case 0x16: /* synthetic BNE dsp24 */
2768 case 0x1e: /* synthetic BEQ dsp24 */
2769 if (-32767 + alignment_glue <= pcrel
2770 && pcrel <= 32766 - alignment_glue
2773 if (insn[0] == 0x16)
2777 /* We snip out the bytes at the end else the reloc
2778 will get moved too, and too much. */
2779 SNIP (3, 2, newrel);
2780 move_reloc (irel, srel, -1);
2786 /* Special case - synthetic conditional branches, pcrel24.
2787 Note that EQ and NE have been handled above. */
2788 if ((insn[0] & 0xf0) == 0x20
2791 && srel->r_offset != irel->r_offset + 1
2792 && -32767 + alignment_glue <= pcrel
2793 && pcrel <= 32766 - alignment_glue)
2797 SNIP (5, 1, newrel);
2801 /* Special case - synthetic conditional branches, pcrel16 */
2802 if ((insn[0] & 0xf0) == 0x20
2805 && srel->r_offset != irel->r_offset + 1
2806 && -127 + alignment_glue <= pcrel
2807 && pcrel <= 126 - alignment_glue)
2809 int cond = (insn[0] & 0x0f) ^ 0x01;
2811 insn[0] = 0x20 | cond;
2812 /* By moving the reloc first, we avoid having
2813 delete_bytes move it also. */
2814 move_reloc (irel, srel, -2);
2815 SNIP (2, 3, newrel);
2820 BFD_ASSERT (nrelocs == 0);
2822 /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2823 use MOV.bwl #uimm:8, dsp:5[r7] format. This is tricky
2824 because it may have one or two relocations. */
2825 if ((insn[0] & 0xfc) == 0xf8
2826 && (insn[1] & 0x80) == 0x00
2827 && (insn[0] & 0x03) != 0x03)
2829 int dcode, icode, reg, ioff, dscale, ilen;
2830 bfd_vma disp_val = 0;
2832 Elf_Internal_Rela * disp_rel = 0;
2833 Elf_Internal_Rela * imm_rel = 0;
2838 dcode = insn[0] & 0x03;
2839 icode = (insn[1] >> 2) & 0x03;
2840 reg = (insn[1] >> 4) & 0x0f;
2842 ioff = dcode == 1 ? 3 : dcode == 2 ? 4 : 2;
2844 /* Figure out what the dispacement is. */
2845 if (dcode == 1 || dcode == 2)
2847 /* There's a displacement. See if there's a reloc for it. */
2848 if (srel[1].r_offset == irel->r_offset + 2)
2860 #if RX_OPCODE_BIG_ENDIAN
2861 disp_val = insn[2] * 256 + insn[3];
2863 disp_val = insn[2] + insn[3] * 256;
2866 switch (insn[1] & 3)
2882 /* Figure out what the immediate is. */
2883 if (srel[1].r_offset == irel->r_offset + ioff)
2886 imm_val = (long) symval;
2891 unsigned char * ip = insn + ioff;
2896 /* For byte writes, we don't sign extend. Makes the math easier later. */
2900 imm_val = (char) ip[0];
2903 #if RX_OPCODE_BIG_ENDIAN
2904 imm_val = ((char) ip[0] << 8) | ip[1];
2906 imm_val = ((char) ip[1] << 8) | ip[0];
2910 #if RX_OPCODE_BIG_ENDIAN
2911 imm_val = ((char) ip[0] << 16) | (ip[1] << 8) | ip[2];
2913 imm_val = ((char) ip[2] << 16) | (ip[1] << 8) | ip[0];
2917 #if RX_OPCODE_BIG_ENDIAN
2918 imm_val = (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];
2920 imm_val = (ip[3] << 24) | (ip[2] << 16) | (ip[1] << 8) | ip[0];
2954 /* The shortcut happens when the immediate is 0..255,
2955 register r0 to r7, and displacement (scaled) 0..31. */
2957 if (0 <= imm_val && imm_val <= 255
2958 && 0 <= reg && reg <= 7
2959 && disp_val / dscale <= 31)
2961 insn[0] = 0x3c | (insn[1] & 0x03);
2962 insn[1] = (((disp_val / dscale) << 3) & 0x80) | (reg << 4) | ((disp_val/dscale) & 0x0f);
2967 int newrel = R_RX_NONE;
2972 newrel = R_RX_RH_ABS5p8B;
2975 newrel = R_RX_RH_ABS5p8W;
2978 newrel = R_RX_RH_ABS5p8L;
2981 disp_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (disp_rel->r_info), newrel);
2982 move_reloc (irel, disp_rel, -1);
2986 imm_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (imm_rel->r_info), R_RX_DIR8U);
2987 move_reloc (disp_rel ? disp_rel : irel,
2989 irel->r_offset - imm_rel->r_offset + 2);
2992 SNIPNR (3, ilen - 3);
2995 /* We can't relax this new opcode. */
3001 /* We can't reliably relax branches to DIR3U_PCREL unless we know
3002 whatever they're branching over won't shrink any more. If we're
3003 basically done here, do one more pass just for branches - but
3004 don't request a pass after that one! */
3005 if (!*again && !allow_pcrel3)
3007 bfd_boolean ignored;
3009 elf32_rx_relax_section (abfd, sec, link_info, &ignored, TRUE);
3015 if (free_contents != NULL)
3016 free (free_contents);
3018 if (shndx_buf != NULL)
3020 shndx_hdr->contents = NULL;
3024 if (free_intsyms != NULL)
3025 free (free_intsyms);
3031 elf32_rx_relax_section_wrapper (bfd * abfd,
3033 struct bfd_link_info * link_info,
3034 bfd_boolean * again)
3036 return elf32_rx_relax_section (abfd, sec, link_info, again, FALSE);
3039 /* Function to set the ELF flag bits. */
3042 rx_elf_set_private_flags (bfd * abfd, flagword flags)
3044 elf_elfheader (abfd)->e_flags = flags;
3045 elf_flags_init (abfd) = TRUE;
3049 static bfd_boolean no_warn_mismatch = FALSE;
3050 static bfd_boolean ignore_lma = TRUE;
3052 void bfd_elf32_rx_set_target_flags (bfd_boolean, bfd_boolean);
3055 bfd_elf32_rx_set_target_flags (bfd_boolean user_no_warn_mismatch,
3056 bfd_boolean user_ignore_lma)
3058 no_warn_mismatch = user_no_warn_mismatch;
3059 ignore_lma = user_ignore_lma;
3062 /* Converts FLAGS into a descriptive string.
3063 Returns a static pointer. */
3066 describe_flags (flagword flags)
3068 static char buf [128];
3072 if (flags & E_FLAG_RX_64BIT_DOUBLES)
3073 strcat (buf, "64-bit doubles");
3075 strcat (buf, "32-bit doubles");
3077 if (flags & E_FLAG_RX_DSP)
3078 strcat (buf, ", dsp");
3080 strcat (buf, ", no dsp");
3082 if (flags & E_FLAG_RX_PID)
3083 strcat (buf, ", pid");
3085 strcat (buf, ", no pid");
3087 if (flags & E_FLAG_RX_ABI)
3088 strcat (buf, ", RX ABI");
3090 strcat (buf, ", GCC ABI");
3092 if (flags & E_FLAG_RX_SINSNS_SET)
3093 strcat (buf, flags & E_FLAG_RX_SINSNS_YES ? ", uses String instructions" : ", bans String instructions");
3098 /* Merge backend specific data from an object file to the output
3099 object file when linking. */
3102 rx_elf_merge_private_bfd_data (bfd * ibfd, struct bfd_link_info *info)
3104 bfd *obfd = info->output_bfd;
3107 bfd_boolean error = FALSE;
3109 new_flags = elf_elfheader (ibfd)->e_flags;
3110 old_flags = elf_elfheader (obfd)->e_flags;
3112 if (!elf_flags_init (obfd))
3114 /* First call, no flags set. */
3115 elf_flags_init (obfd) = TRUE;
3116 elf_elfheader (obfd)->e_flags = new_flags;
3118 else if (old_flags != new_flags)
3120 flagword known_flags;
3122 if (old_flags & E_FLAG_RX_SINSNS_SET)
3124 if ((new_flags & E_FLAG_RX_SINSNS_SET) == 0)
3126 new_flags &= ~ E_FLAG_RX_SINSNS_MASK;
3127 new_flags |= (old_flags & E_FLAG_RX_SINSNS_MASK);
3130 else if (new_flags & E_FLAG_RX_SINSNS_SET)
3132 old_flags &= ~ E_FLAG_RX_SINSNS_MASK;
3133 old_flags |= (new_flags & E_FLAG_RX_SINSNS_MASK);
3136 known_flags = E_FLAG_RX_ABI | E_FLAG_RX_64BIT_DOUBLES
3137 | E_FLAG_RX_DSP | E_FLAG_RX_PID | E_FLAG_RX_SINSNS_MASK;
3139 if ((old_flags ^ new_flags) & known_flags)
3141 /* Only complain if flag bits we care about do not match.
3142 Other bits may be set, since older binaries did use some
3143 deprecated flags. */
3144 if (no_warn_mismatch)
3146 elf_elfheader (obfd)->e_flags = (new_flags | old_flags) & known_flags;
3150 _bfd_error_handler (_("There is a conflict merging the"
3151 " ELF header flags from %pB"),
3153 _bfd_error_handler (_(" the input file's flags: %s"),
3154 describe_flags (new_flags));
3155 _bfd_error_handler (_(" the output file's flags: %s"),
3156 describe_flags (old_flags));
3161 elf_elfheader (obfd)->e_flags = new_flags & known_flags;
3165 bfd_set_error (bfd_error_bad_value);
3171 rx_elf_print_private_bfd_data (bfd * abfd, void * ptr)
3173 FILE * file = (FILE *) ptr;
3176 BFD_ASSERT (abfd != NULL && ptr != NULL);
3178 /* Print normal ELF private data. */
3179 _bfd_elf_print_private_bfd_data (abfd, ptr);
3181 flags = elf_elfheader (abfd)->e_flags;
3182 fprintf (file, _("private flags = 0x%lx:"), (long) flags);
3184 fprintf (file, "%s", describe_flags (flags));
3188 /* Return the MACH for an e_flags value. */
3191 elf32_rx_machine (bfd * abfd ATTRIBUTE_UNUSED)
3193 #if 0 /* FIXME: EF_RX_CPU_MASK collides with E_FLAG_RX_...
3194 Need to sort out how these flag bits are used.
3195 For now we assume that the flags are OK. */
3196 if ((elf_elfheader (abfd)->e_flags & EF_RX_CPU_MASK) == EF_RX_CPU_RX)
3204 rx_elf_object_p (bfd * abfd)
3208 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
3209 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
3210 int nphdrs = ehdr->e_phnum;
3212 static int saw_be = FALSE;
3213 bfd_vma end_phdroff;
3215 /* We never want to automatically choose the non-swapping big-endian
3216 target. The user can only get that explicitly, such as with -I
3218 if (abfd->xvec == &rx_elf32_be_ns_vec
3219 && abfd->target_defaulted)
3222 /* BFD->target_defaulted is not set to TRUE when a target is chosen
3223 as a fallback, so we check for "scanning" to know when to stop
3224 using the non-swapping target. */
3225 if (abfd->xvec == &rx_elf32_be_ns_vec
3228 if (abfd->xvec == &rx_elf32_be_vec)
3231 bfd_default_set_arch_mach (abfd, bfd_arch_rx,
3232 elf32_rx_machine (abfd));
3234 /* For each PHDR in the object, we must find some section that
3235 corresponds (based on matching file offsets) and use its VMA
3236 information to reconstruct the p_vaddr field we clobbered when we
3238 /* If PT_LOAD headers include the ELF file header or program headers
3239 then the PT_LOAD header does not start with some section contents.
3240 Making adjustments based on the difference between sh_offset and
3241 p_offset is nonsense in such cases. Exclude them. Note that
3242 since standard linker scripts for RX do not use SIZEOF_HEADERS,
3243 the linker won't normally create PT_LOAD segments covering the
3244 headers so this is mainly for passing the ld testsuite.
3245 FIXME. Why are we looking at non-PT_LOAD headers here? */
3246 end_phdroff = ehdr->e_ehsize;
3247 if (ehdr->e_phoff != 0)
3248 end_phdroff = ehdr->e_phoff + nphdrs * ehdr->e_phentsize;
3249 for (i=0; i<nphdrs; i++)
3251 for (u=0; u<elf_tdata(abfd)->num_elf_sections; u++)
3253 Elf_Internal_Shdr *sec = elf_tdata(abfd)->elf_sect_ptr[u];
3255 if (phdr[i].p_filesz
3256 && phdr[i].p_offset >= end_phdroff
3257 && phdr[i].p_offset <= (bfd_vma) sec->sh_offset
3259 && sec->sh_type != SHT_NOBITS
3260 && (bfd_vma)sec->sh_offset <= phdr[i].p_offset + (phdr[i].p_filesz - 1))
3262 /* Found one! The difference between the two addresses,
3263 plus the difference between the two file offsets, is
3264 enough information to reconstruct the lma. */
3266 /* Example where they aren't:
3267 PHDR[1] = lma fffc0100 offset 00002010 size 00000100
3268 SEC[6] = vma 00000050 offset 00002050 size 00000040
3270 The correct LMA for the section is fffc0140 + (2050-2010).
3273 phdr[i].p_vaddr = sec->sh_addr + (sec->sh_offset - phdr[i].p_offset);
3278 /* We must update the bfd sections as well, so we don't stop
3280 bsec = abfd->sections;
3283 if (phdr[i].p_filesz
3284 && phdr[i].p_vaddr <= bsec->vma
3285 && bsec->vma <= phdr[i].p_vaddr + (phdr[i].p_filesz - 1))
3287 bsec->lma = phdr[i].p_paddr + (bsec->vma - phdr[i].p_vaddr);
3299 rx_dump_symtab (bfd * abfd, void * internal_syms, void * external_syms)
3302 Elf_Internal_Sym * isymbuf;
3303 Elf_Internal_Sym * isymend;
3304 Elf_Internal_Sym * isym;
3305 Elf_Internal_Shdr * symtab_hdr;
3306 bfd_boolean free_internal = FALSE, free_external = FALSE;
3308 char * st_info_stb_str;
3309 char * st_other_str;
3310 char * st_shndx_str;
3312 if (! internal_syms)
3314 internal_syms = bfd_malloc (1000);
3317 if (! external_syms)
3319 external_syms = bfd_malloc (1000);
3323 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3324 locsymcount = symtab_hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3326 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3327 symtab_hdr->sh_info, 0,
3328 internal_syms, external_syms, NULL);
3330 isymbuf = internal_syms;
3331 isymend = isymbuf + locsymcount;
3333 for (isym = isymbuf ; isym < isymend ; isym++)
3335 switch (ELF_ST_TYPE (isym->st_info))
3337 case STT_FUNC: st_info_str = "STT_FUNC"; break;
3338 case STT_SECTION: st_info_str = "STT_SECTION"; break;
3339 case STT_FILE: st_info_str = "STT_FILE"; break;
3340 case STT_OBJECT: st_info_str = "STT_OBJECT"; break;
3341 case STT_TLS: st_info_str = "STT_TLS"; break;
3342 default: st_info_str = "";
3344 switch (ELF_ST_BIND (isym->st_info))
3346 case STB_LOCAL: st_info_stb_str = "STB_LOCAL"; break;
3347 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL"; break;
3348 default: st_info_stb_str = "";
3350 switch (ELF_ST_VISIBILITY (isym->st_other))
3352 case STV_DEFAULT: st_other_str = "STV_DEFAULT"; break;
3353 case STV_INTERNAL: st_other_str = "STV_INTERNAL"; break;
3354 case STV_PROTECTED: st_other_str = "STV_PROTECTED"; break;
3355 default: st_other_str = "";
3357 switch (isym->st_shndx)
3359 case SHN_ABS: st_shndx_str = "SHN_ABS"; break;
3360 case SHN_COMMON: st_shndx_str = "SHN_COMMON"; break;
3361 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF"; break;
3362 default: st_shndx_str = "";
3365 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3366 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3368 (unsigned long) isym->st_value,
3369 (unsigned long) isym->st_size,
3371 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
3373 isym->st_info, st_info_str, st_info_stb_str,
3374 isym->st_other, st_other_str,
3375 isym->st_shndx, st_shndx_str);
3378 free (internal_syms);
3380 free (external_syms);
3384 rx_get_reloc (long reloc)
3386 if (0 <= reloc && reloc < R_RX_max)
3387 return rx_elf_howto_table[reloc].name;
3393 /* We must take care to keep the on-disk copy of any code sections
3394 that are fully linked swapped if the target is big endian, to match
3395 the Renesas tools. */
3397 /* The rule is: big endian object that are final-link executables,
3398 have code sections stored with 32-bit words swapped relative to
3399 what you'd get by default. */
3402 rx_get_section_contents (bfd * abfd,
3406 bfd_size_type count)
3408 int exec = (abfd->flags & EXEC_P) ? 1 : 0;
3409 int s_code = (section->flags & SEC_CODE) ? 1 : 0;
3413 fprintf (stderr, "dj: get %ld %ld from %s %s e%d sc%d %08lx:%08lx\n",
3414 (long) offset, (long) count, section->name,
3415 bfd_big_endian(abfd) ? "be" : "le",
3416 exec, s_code, (long unsigned) section->filepos,
3417 (long unsigned) offset);
3420 if (exec && s_code && bfd_big_endian (abfd))
3422 char * cloc = (char *) location;
3423 bfd_size_type cnt, end_cnt;
3427 /* Fetch and swap unaligned bytes at the beginning. */
3432 rv = _bfd_generic_get_section_contents (abfd, section, buf,
3437 bfd_putb32 (bfd_getl32 (buf), buf);
3439 cnt = 4 - (offset % 4);
3443 memcpy (location, buf + (offset % 4), cnt);
3450 end_cnt = count % 4;
3452 /* Fetch and swap the middle bytes. */
3455 rv = _bfd_generic_get_section_contents (abfd, section, cloc, offset,
3460 for (cnt = count; cnt >= 4; cnt -= 4, cloc += 4)
3461 bfd_putb32 (bfd_getl32 (cloc), cloc);
3464 /* Fetch and swap the end bytes. */
3469 /* Fetch the end bytes. */
3470 rv = _bfd_generic_get_section_contents (abfd, section, buf,
3471 offset + count - end_cnt, 4);
3475 bfd_putb32 (bfd_getl32 (buf), buf);
3476 memcpy (cloc, buf, end_cnt);
3480 rv = _bfd_generic_get_section_contents (abfd, section, location, offset, count);
3487 rx2_set_section_contents (bfd * abfd,
3489 const void * location,
3491 bfd_size_type count)
3495 fprintf (stderr, " set sec %s %08x loc %p offset %#x count %#x\n",
3496 section->name, (unsigned) section->vma, location, (int) offset, (int) count);
3497 for (i = 0; i < count; i++)
3499 if (i % 16 == 0 && i > 0)
3500 fprintf (stderr, "\n");
3502 if (i % 16 && i % 4 == 0)
3503 fprintf (stderr, " ");
3506 fprintf (stderr, " %08x:", (int) (section->vma + offset + i));
3508 fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
3510 fprintf (stderr, "\n");
3512 return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
3514 #define _bfd_elf_set_section_contents rx2_set_section_contents
3518 rx_set_section_contents (bfd * abfd,
3520 const void * location,
3522 bfd_size_type count)
3524 bfd_boolean exec = (abfd->flags & EXEC_P) ? TRUE : FALSE;
3525 bfd_boolean s_code = (section->flags & SEC_CODE) ? TRUE : FALSE;
3527 char * swapped_data = NULL;
3529 bfd_vma caddr = section->vma + offset;
3531 bfd_size_type scount;
3536 fprintf (stderr, "\ndj: set %ld %ld to %s %s e%d sc%d\n",
3537 (long) offset, (long) count, section->name,
3538 bfd_big_endian (abfd) ? "be" : "le",
3541 for (i = 0; i < count; i++)
3543 int a = section->vma + offset + i;
3545 if (a % 16 == 0 && a > 0)
3546 fprintf (stderr, "\n");
3548 if (a % 16 && a % 4 == 0)
3549 fprintf (stderr, " ");
3551 if (a % 16 == 0 || i == 0)
3552 fprintf (stderr, " %08x:", (int) (section->vma + offset + i));
3554 fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
3557 fprintf (stderr, "\n");
3560 if (! exec || ! s_code || ! bfd_big_endian (abfd))
3561 return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
3563 while (count > 0 && caddr > 0 && caddr % 4)
3567 case 0: faddr = offset + 3; break;
3568 case 1: faddr = offset + 1; break;
3569 case 2: faddr = offset - 1; break;
3570 case 3: faddr = offset - 3; break;
3573 rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
3577 location = (bfd_byte *) location + 1;
3583 scount = (int)(count / 4) * 4;
3586 char * cloc = (char *) location;
3588 swapped_data = (char *) bfd_alloc (abfd, count);
3590 for (i = 0; i < count; i += 4)
3592 bfd_vma v = bfd_getl32 (cloc + i);
3593 bfd_putb32 (v, swapped_data + i);
3596 rv = _bfd_elf_set_section_contents (abfd, section, swapped_data, offset, scount);
3603 location = (bfd_byte *) location + scount;
3608 caddr = section->vma + offset;
3613 case 0: faddr = offset + 3; break;
3614 case 1: faddr = offset + 1; break;
3615 case 2: faddr = offset - 1; break;
3616 case 3: faddr = offset - 3; break;
3618 rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
3622 location = (bfd_byte *) location + 1;
3633 rx_final_link (bfd * abfd, struct bfd_link_info * info)
3637 for (o = abfd->sections; o != NULL; o = o->next)
3640 fprintf (stderr, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3641 o->name, o->flags, o->vma, o->lma, o->size, o->rawsize);
3643 if (o->flags & SEC_CODE
3644 && bfd_big_endian (abfd)
3648 fprintf (stderr, "adjusting...\n");
3650 o->size += 4 - (o->size % 4);
3654 return bfd_elf_final_link (abfd, info);
3658 elf32_rx_modify_program_headers (bfd * abfd ATTRIBUTE_UNUSED,
3659 struct bfd_link_info * info ATTRIBUTE_UNUSED)
3661 const struct elf_backend_data * bed;
3662 struct elf_obj_tdata * tdata;
3663 Elf_Internal_Phdr * phdr;
3667 bed = get_elf_backend_data (abfd);
3668 tdata = elf_tdata (abfd);
3670 count = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
3673 for (i = count; i-- != 0;)
3674 if (phdr[i].p_type == PT_LOAD)
3676 /* The Renesas tools expect p_paddr to be zero. However,
3677 there is no other way to store the writable data in ROM for
3678 startup initialization. So, we let the linker *think*
3679 we're using paddr and vaddr the "usual" way, but at the
3680 last minute we move the paddr into the vaddr (which is what
3681 the simulator uses) and zero out paddr. Note that this
3682 does not affect the section headers, just the program
3683 headers. We hope. */
3684 phdr[i].p_vaddr = phdr[i].p_paddr;
3685 #if 0 /* If we zero out p_paddr, then the LMA in the section table
3687 phdr[i].p_paddr = 0;
3694 /* The default literal sections should always be marked as "code" (i.e.,
3695 SHF_EXECINSTR). This is particularly important for big-endian mode
3696 when we do not want their contents byte reversed. */
3697 static const struct bfd_elf_special_section elf32_rx_special_sections[] =
3699 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3700 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3701 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3702 { NULL, 0, 0, 0, 0 }
3707 struct bfd_link_info *info;
3708 bfd_vma table_start;
3710 bfd_vma *table_handlers;
3711 bfd_vma table_default_handler;
3712 struct bfd_link_hash_entry **table_entries;
3713 struct bfd_link_hash_entry *table_default_entry;
3718 rx_table_find (struct bfd_hash_entry *vent, void *vinfo)
3720 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3721 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3722 const char *name; /* of the symbol we've found */
3726 const char *tname; /* name of the table */
3727 bfd_vma start_addr, end_addr;
3729 struct bfd_link_hash_entry * h;
3731 /* We're looking for globally defined symbols of the form
3732 $tablestart$<NAME>. */
3733 if (ent->type != bfd_link_hash_defined
3734 && ent->type != bfd_link_hash_defweak)
3737 name = ent->root.string;
3738 sec = ent->u.def.section;
3741 if (strncmp (name, "$tablestart$", 12))
3744 sec->flags |= SEC_KEEP;
3748 start_addr = ent->u.def.value;
3750 /* At this point, we can't build the table but we can (and must)
3751 find all the related symbols and mark their sections as SEC_KEEP
3752 so we don't garbage collect them. */
3754 buf = (char *) malloc (12 + 10 + strlen (tname));
3756 sprintf (buf, "$tableend$%s", tname);
3757 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3758 if (!h || (h->type != bfd_link_hash_defined
3759 && h->type != bfd_link_hash_defweak))
3761 /* xgettext:c-format */
3762 _bfd_error_handler (_("%pB:%pA: table %s missing corresponding %s"),
3763 abfd, sec, name, buf);
3767 if (h->u.def.section != ent->u.def.section)
3769 /* xgettext:c-format */
3770 _bfd_error_handler (_("%pB:%pA: %s and %s must be in the same input section"),
3771 h->u.def.section->owner, h->u.def.section,
3776 end_addr = h->u.def.value;
3778 sprintf (buf, "$tableentry$default$%s", tname);
3779 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3780 if (h && (h->type == bfd_link_hash_defined
3781 || h->type == bfd_link_hash_defweak))
3783 h->u.def.section->flags |= SEC_KEEP;
3786 for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
3788 sprintf (buf, "$tableentry$%d$%s", idx, tname);
3789 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3790 if (h && (h->type == bfd_link_hash_defined
3791 || h->type == bfd_link_hash_defweak))
3793 h->u.def.section->flags |= SEC_KEEP;
3797 /* Return TRUE to keep scanning, FALSE to end the traversal. */
3801 /* We need to check for table entry symbols and build the tables, and
3802 we need to do it before the linker does garbage collection. This function is
3803 called once per input object file. */
3806 (bfd * abfd ATTRIBUTE_UNUSED,
3807 struct bfd_link_info * info ATTRIBUTE_UNUSED)
3809 RX_Table_Info stuff;
3813 bfd_hash_traverse (&(info->hash->table), rx_table_find, &stuff);
3820 rx_table_map_2 (struct bfd_hash_entry *vent, void *vinfo)
3822 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3823 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3828 /* See if the symbol ENT has an address listed in the table, and
3829 isn't a debug/special symbol. If so, put it in the table. */
3831 if (ent->type != bfd_link_hash_defined
3832 && ent->type != bfd_link_hash_defweak)
3835 name = ent->root.string;
3837 if (name[0] == '$' || name[0] == '.' || name[0] < ' ')
3840 addr = (ent->u.def.value
3841 + ent->u.def.section->output_section->vma
3842 + ent->u.def.section->output_offset);
3844 for (idx = 0; idx < info->table_size; idx ++)
3845 if (addr == info->table_handlers[idx])
3846 info->table_entries[idx] = ent;
3848 if (addr == info->table_default_handler)
3849 info->table_default_entry = ent;
3855 rx_table_map (struct bfd_hash_entry *vent, void *vinfo)
3857 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3858 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3859 const char *name; /* of the symbol we've found */
3861 const char *tname; /* name of the table */
3862 bfd_vma start_addr, end_addr;
3864 struct bfd_link_hash_entry * h;
3867 /* We're looking for globally defined symbols of the form
3868 $tablestart$<NAME>. */
3869 if (ent->type != bfd_link_hash_defined
3870 && ent->type != bfd_link_hash_defweak)
3873 name = ent->root.string;
3875 if (strncmp (name, "$tablestart$", 12))
3879 start_addr = (ent->u.def.value
3880 + ent->u.def.section->output_section->vma
3881 + ent->u.def.section->output_offset);
3883 buf = (char *) malloc (12 + 10 + strlen (tname));
3885 sprintf (buf, "$tableend$%s", tname);
3886 end_addr = get_symbol_value_maybe (buf, info->info);
3888 sprintf (buf, "$tableentry$default$%s", tname);
3889 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3892 info->table_default_handler = (h->u.def.value
3893 + h->u.def.section->output_section->vma
3894 + h->u.def.section->output_offset);
3897 /* Zero is a valid handler address! */
3898 info->table_default_handler = (bfd_vma) (-1);
3899 info->table_default_entry = NULL;
3901 info->table_start = start_addr;
3902 info->table_size = (int) (end_addr - start_addr) / 4;
3903 info->table_handlers = (bfd_vma *) malloc (info->table_size * sizeof (bfd_vma));
3904 info->table_entries = (struct bfd_link_hash_entry **) malloc (info->table_size * sizeof (struct bfd_link_hash_entry));
3906 for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
3908 sprintf (buf, "$tableentry$%d$%s", idx, tname);
3909 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3910 if (h && (h->type == bfd_link_hash_defined
3911 || h->type == bfd_link_hash_defweak))
3913 info->table_handlers[idx] = (h->u.def.value
3914 + h->u.def.section->output_section->vma
3915 + h->u.def.section->output_offset);
3918 info->table_handlers[idx] = info->table_default_handler;
3919 info->table_entries[idx] = NULL;
3924 bfd_hash_traverse (&(info->info->hash->table), rx_table_map_2, info);
3926 fprintf (info->mapfile, "\nRX Vector Table: %s has %d entries at 0x%08" BFD_VMA_FMT "x\n\n",
3927 tname, info->table_size, start_addr);
3929 if (info->table_default_entry)
3930 fprintf (info->mapfile, " default handler is: %s at 0x%08" BFD_VMA_FMT "x\n",
3931 info->table_default_entry->root.string,
3932 info->table_default_handler);
3933 else if (info->table_default_handler != (bfd_vma)(-1))
3934 fprintf (info->mapfile, " default handler is at 0x%08" BFD_VMA_FMT "x\n",
3935 info->table_default_handler);
3937 fprintf (info->mapfile, " no default handler\n");
3940 for (idx = 0; idx < info->table_size; idx ++)
3942 if (info->table_handlers[idx] == info->table_default_handler)
3945 fprintf (info->mapfile, " . . .\n");
3951 fprintf (info->mapfile, " 0x%08" BFD_VMA_FMT "x [%3d] ", start_addr + 4 * idx, idx);
3953 if (info->table_handlers[idx] == (bfd_vma) (-1))
3954 fprintf (info->mapfile, "(no handler found)\n");
3956 else if (info->table_handlers[idx] == info->table_default_handler)
3958 if (info->table_default_entry)
3959 fprintf (info->mapfile, "(default)\n");
3961 fprintf (info->mapfile, "(default)\n");
3964 else if (info->table_entries[idx])
3966 fprintf (info->mapfile, "0x%08" BFD_VMA_FMT "x %s\n", info->table_handlers[idx], info->table_entries[idx]->root.string);
3971 fprintf (info->mapfile, "0x%08" BFD_VMA_FMT "x ???\n", info->table_handlers[idx]);
3975 fprintf (info->mapfile, " . . .\n");
3981 rx_additional_link_map_text (bfd *obfd, struct bfd_link_info *info, FILE *mapfile)
3983 /* We scan the symbol table looking for $tableentry$'s, and for
3984 each, try to deduce which handlers go with which entries. */
3986 RX_Table_Info stuff;
3990 stuff.mapfile = mapfile;
3991 bfd_hash_traverse (&(info->hash->table), rx_table_map, &stuff);
3995 #define ELF_ARCH bfd_arch_rx
3996 #define ELF_MACHINE_CODE EM_RX
3997 #define ELF_MAXPAGESIZE 0x1000
3999 #define TARGET_BIG_SYM rx_elf32_be_vec
4000 #define TARGET_BIG_NAME "elf32-rx-be"
4002 #define TARGET_LITTLE_SYM rx_elf32_le_vec
4003 #define TARGET_LITTLE_NAME "elf32-rx-le"
4005 #define elf_info_to_howto_rel NULL
4006 #define elf_info_to_howto rx_info_to_howto_rela
4007 #define elf_backend_object_p rx_elf_object_p
4008 #define elf_backend_relocate_section rx_elf_relocate_section
4009 #define elf_symbol_leading_char ('_')
4010 #define elf_backend_can_gc_sections 1
4011 #define elf_backend_modify_program_headers elf32_rx_modify_program_headers
4013 #define bfd_elf32_bfd_reloc_type_lookup rx_reloc_type_lookup
4014 #define bfd_elf32_bfd_reloc_name_lookup rx_reloc_name_lookup
4015 #define bfd_elf32_bfd_set_private_flags rx_elf_set_private_flags
4016 #define bfd_elf32_bfd_merge_private_bfd_data rx_elf_merge_private_bfd_data
4017 #define bfd_elf32_bfd_print_private_bfd_data rx_elf_print_private_bfd_data
4018 #define bfd_elf32_get_section_contents rx_get_section_contents
4019 #define bfd_elf32_set_section_contents rx_set_section_contents
4020 #define bfd_elf32_bfd_final_link rx_final_link
4021 #define bfd_elf32_bfd_relax_section elf32_rx_relax_section_wrapper
4022 #define elf_backend_special_sections elf32_rx_special_sections
4023 #define elf_backend_check_directives rx_check_directives
4025 #include "elf32-target.h"
4027 /* We define a second big-endian target that doesn't have the custom
4028 section get/set hooks, for times when we want to preserve the
4029 pre-swapped .text sections (like objcopy). */
4031 #undef TARGET_BIG_SYM
4032 #define TARGET_BIG_SYM rx_elf32_be_ns_vec
4033 #undef TARGET_BIG_NAME
4034 #define TARGET_BIG_NAME "elf32-rx-be-ns"
4035 #undef TARGET_LITTLE_SYM
4037 #undef bfd_elf32_get_section_contents
4038 #undef bfd_elf32_set_section_contents
4041 #define elf32_bed elf32_rx_be_ns_bed
4043 #include "elf32-target.h"