1 /* Renesas RX specific support for 32-bit ELF.
2 Copyright (C) 2008-2014 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "bfd_stdint.h"
26 #include "libiberty.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
34 const bfd_target rx_elf32_be_ns_vec;
35 const bfd_target rx_elf32_be_vec;
38 char * rx_get_reloc (long);
39 void rx_dump_symtab (bfd *, void *, void *);
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, 0, 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),
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),
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)
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. */
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)
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;
287 static reloc_howto_type *
288 rx_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, const char * r_name)
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;
300 /* Set the howto pointer for an RX ELF reloc. */
303 rx_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
305 Elf_Internal_Rela * dst)
309 r_type = ELF32_R_TYPE (dst->r_info);
310 if (r_type >= (unsigned int) R_RX_max)
312 _bfd_error_handler (_("%A: invalid RX reloc number: %d"), abfd, r_type);
315 cache_ptr->howto = rx_elf_howto_table + r_type;
319 get_symbol_value (const char * name,
320 bfd_reloc_status_type * status,
321 struct bfd_link_info * info,
323 asection * input_section,
327 struct bfd_link_hash_entry * h;
329 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
332 || (h->type != bfd_link_hash_defined
333 && h->type != bfd_link_hash_defweak))
334 * status = info->callbacks->undefined_symbol
335 (info, name, input_bfd, input_section, offset, TRUE);
337 value = (h->u.def.value
338 + h->u.def.section->output_section->vma
339 + h->u.def.section->output_offset);
344 get_symbol_value_maybe (const char * name,
345 struct bfd_link_info * info)
348 struct bfd_link_hash_entry * h;
350 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
353 || (h->type != bfd_link_hash_defined
354 && h->type != bfd_link_hash_defweak))
357 value = (h->u.def.value
358 + h->u.def.section->output_section->vma
359 + h->u.def.section->output_offset);
365 get_gp (bfd_reloc_status_type * status,
366 struct bfd_link_info * info,
371 static bfd_boolean cached = FALSE;
372 static bfd_vma cached_value = 0;
376 cached_value = get_symbol_value ("__gp", status, info, abfd, sec, offset);
383 get_romstart (bfd_reloc_status_type * status,
384 struct bfd_link_info * info,
389 static bfd_boolean cached = FALSE;
390 static bfd_vma cached_value = 0;
394 cached_value = get_symbol_value ("_start", status, info, abfd, sec, offset);
401 get_ramstart (bfd_reloc_status_type * status,
402 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", status, 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)
556 bfd_reloc_status_type tstat = 0;
558 if (table_default_cache != name)
561 /* All relocs for a given table should be to the same
562 (weak) default symbol) so we can use it to detect a
563 cache miss. We use the offset into the table to find
564 the "real" symbol. Calculate and store the table's
567 table_default_cache = name;
569 /* We have already done error checking in rx_table_find(). */
571 buf = (char *) malloc (13 + strlen (name + 20));
573 sprintf (buf, "$tablestart$%s", name + 20);
575 table_start_cache = get_symbol_value (buf,
582 sprintf (buf, "$tableend$%s", name + 20);
584 table_end_cache = get_symbol_value (buf,
594 entry_vma = (input_section->output_section->vma
595 + input_section->output_offset
598 if (table_end_cache <= entry_vma || entry_vma < table_start_cache)
600 _bfd_error_handler (_("%B:%A: table entry %s outside table"),
601 input_bfd, input_section,
604 else if ((int) (entry_vma - table_start_cache) % 4)
606 _bfd_error_handler (_("%B:%A: table entry %s not word-aligned within table"),
607 input_bfd, input_section,
612 idx = (int) (entry_vma - table_start_cache) / 4;
614 /* This will look like $tableentry$<N>$<name> */
615 buf = (char *) malloc (12 + 20 + strlen (name + 20));
616 sprintf (buf, "$tableentry$%d$%s", idx, name + 20);
618 h = (struct elf_link_hash_entry *) bfd_link_hash_lookup (info->hash, buf, FALSE, FALSE, TRUE);
622 relocation = (h->root.u.def.value
623 + h->root.u.def.section->output_section->vma
624 + h->root.u.def.section->output_offset);;
631 if (sec != NULL && discarded_section (sec))
632 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
633 rel, 1, relend, howto, 0, contents);
635 if (info->relocatable)
637 /* This is a relocatable link. We don't have to change
638 anything, unless the reloc is against a section symbol,
639 in which case we have to adjust according to where the
640 section symbol winds up in the output section. */
641 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
642 rel->r_addend += sec->output_offset;
646 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
647 /* If the symbol is undefined and weak
648 then the relocation resolves to zero. */
652 if (howto->pc_relative)
654 relocation -= (input_section->output_section->vma
655 + input_section->output_offset
657 if (r_type != R_RX_RH_3_PCREL
658 && r_type != R_RX_DIR3U_PCREL)
662 relocation += rel->r_addend;
667 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
668 #define ALIGN(m) if (relocation & m) r = bfd_reloc_other;
669 #define OP(i) (contents[rel->r_offset + (i)])
670 #define WARN_REDHAT(type) \
671 _bfd_error_handler (_("%B:%A: Warning: deprecated Red Hat reloc " type " detected against: %s."), \
672 input_bfd, input_section, name)
674 /* Check for unsafe relocs in PID mode. These are any relocs where
675 an absolute address is being computed. There are special cases
676 for relocs against symbols that are known to be referenced in
677 crt0.o before the PID base address register has been initialised. */
678 #define UNSAFE_FOR_PID \
683 && sec->flags & SEC_READONLY \
684 && !(input_section->flags & SEC_DEBUGGING) \
685 && strcmp (name, "__pid_base") != 0 \
686 && strcmp (name, "__gp") != 0 \
687 && strcmp (name, "__romdatastart") != 0 \
689 _bfd_error_handler (_("%B(%A): unsafe PID relocation %s at 0x%08lx (against %s in %s)"), \
690 input_bfd, input_section, howto->name, \
691 input_section->output_section->vma + input_section->output_offset + rel->r_offset, \
696 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
705 case R_RX_RH_3_PCREL:
706 WARN_REDHAT ("RX_RH_3_PCREL");
709 OP (0) |= relocation & 0x07;
713 WARN_REDHAT ("RX_RH_8_NEG");
714 relocation = - relocation;
715 case R_RX_DIR8S_PCREL:
734 WARN_REDHAT ("RX_RH_16_NEG");
735 relocation = - relocation;
736 case R_RX_DIR16S_PCREL:
738 RANGE (-32768, 32767);
739 #if RX_OPCODE_BIG_ENDIAN
742 OP (1) = relocation >> 8;
747 WARN_REDHAT ("RX_RH_16_OP");
749 RANGE (-32768, 32767);
750 #if RX_OPCODE_BIG_ENDIAN
752 OP (0) = relocation >> 8;
755 OP (1) = relocation >> 8;
761 RANGE (-32768, 65535);
762 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
765 OP (0) = relocation >> 8;
770 OP (1) = relocation >> 8;
777 #if RX_OPCODE_BIG_ENDIAN
779 OP (0) = relocation >> 8;
782 OP (1) = relocation >> 8;
788 RANGE (-32768, 65536);
789 #if RX_OPCODE_BIG_ENDIAN
791 OP (0) = relocation >> 8;
794 OP (1) = relocation >> 8;
800 RANGE (-32768, 65536);
801 #if RX_OPCODE_BIG_ENDIAN
803 OP (1) = relocation >> 8;
806 OP (0) = relocation >> 8;
810 case R_RX_DIR3U_PCREL:
813 OP (0) |= relocation & 0x07;
818 WARN_REDHAT ("RX_RH_24_NEG");
819 relocation = - relocation;
820 case R_RX_DIR24S_PCREL:
821 RANGE (-0x800000, 0x7fffff);
822 #if RX_OPCODE_BIG_ENDIAN
824 OP (1) = relocation >> 8;
825 OP (0) = relocation >> 16;
828 OP (1) = relocation >> 8;
829 OP (2) = relocation >> 16;
835 WARN_REDHAT ("RX_RH_24_OP");
836 RANGE (-0x800000, 0x7fffff);
837 #if RX_OPCODE_BIG_ENDIAN
839 OP (1) = relocation >> 8;
840 OP (0) = relocation >> 16;
843 OP (1) = relocation >> 8;
844 OP (2) = relocation >> 16;
850 RANGE (-0x800000, 0x7fffff);
851 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
854 OP (1) = relocation >> 8;
855 OP (0) = relocation >> 16;
860 OP (1) = relocation >> 8;
861 OP (2) = relocation >> 16;
867 WARN_REDHAT ("RX_RH_24_UNS");
869 #if RX_OPCODE_BIG_ENDIAN
871 OP (1) = relocation >> 8;
872 OP (0) = relocation >> 16;
875 OP (1) = relocation >> 8;
876 OP (2) = relocation >> 16;
882 WARN_REDHAT ("RX_RH_32_NEG");
883 relocation = - relocation;
884 #if RX_OPCODE_BIG_ENDIAN
886 OP (2) = relocation >> 8;
887 OP (1) = relocation >> 16;
888 OP (0) = relocation >> 24;
891 OP (1) = relocation >> 8;
892 OP (2) = relocation >> 16;
893 OP (3) = relocation >> 24;
899 WARN_REDHAT ("RX_RH_32_OP");
900 #if RX_OPCODE_BIG_ENDIAN
902 OP (2) = relocation >> 8;
903 OP (1) = relocation >> 16;
904 OP (0) = relocation >> 24;
907 OP (1) = relocation >> 8;
908 OP (2) = relocation >> 16;
909 OP (3) = relocation >> 24;
914 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
917 OP (2) = relocation >> 8;
918 OP (1) = relocation >> 16;
919 OP (0) = relocation >> 24;
924 OP (1) = relocation >> 8;
925 OP (2) = relocation >> 16;
926 OP (3) = relocation >> 24;
931 if (BIGE (output_bfd))
934 OP (1) = relocation >> 8;
935 OP (2) = relocation >> 16;
936 OP (3) = relocation >> 24;
941 OP (2) = relocation >> 8;
942 OP (1) = relocation >> 16;
943 OP (0) = relocation >> 24;
950 WARN_REDHAT ("RX_RH_DIFF");
951 val = bfd_get_32 (output_bfd, & OP (0));
953 bfd_put_32 (output_bfd, val, & OP (0));
958 WARN_REDHAT ("RX_RH_GPRELB");
959 relocation -= get_gp (&r, info, input_bfd, input_section, rel->r_offset);
961 #if RX_OPCODE_BIG_ENDIAN
963 OP (0) = relocation >> 8;
966 OP (1) = relocation >> 8;
971 WARN_REDHAT ("RX_RH_GPRELW");
972 relocation -= get_gp (&r, info, input_bfd, input_section, rel->r_offset);
976 #if RX_OPCODE_BIG_ENDIAN
978 OP (0) = relocation >> 8;
981 OP (1) = relocation >> 8;
986 WARN_REDHAT ("RX_RH_GPRELL");
987 relocation -= get_gp (&r, info, input_bfd, input_section, rel->r_offset);
991 #if RX_OPCODE_BIG_ENDIAN
993 OP (0) = relocation >> 8;
996 OP (1) = relocation >> 8;
1000 /* Internal relocations just for relaxation: */
1001 case R_RX_RH_ABS5p5B:
1002 RX_STACK_POP (relocation);
1005 OP (0) |= relocation >> 2;
1007 OP (1) |= (relocation << 6) & 0x80;
1008 OP (1) |= (relocation << 3) & 0x08;
1011 case R_RX_RH_ABS5p5W:
1012 RX_STACK_POP (relocation);
1017 OP (0) |= relocation >> 2;
1019 OP (1) |= (relocation << 6) & 0x80;
1020 OP (1) |= (relocation << 3) & 0x08;
1023 case R_RX_RH_ABS5p5L:
1024 RX_STACK_POP (relocation);
1029 OP (0) |= relocation >> 2;
1031 OP (1) |= (relocation << 6) & 0x80;
1032 OP (1) |= (relocation << 3) & 0x08;
1035 case R_RX_RH_ABS5p8B:
1036 RX_STACK_POP (relocation);
1039 OP (0) |= (relocation << 3) & 0x80;
1040 OP (0) |= relocation & 0x0f;
1043 case R_RX_RH_ABS5p8W:
1044 RX_STACK_POP (relocation);
1049 OP (0) |= (relocation << 3) & 0x80;
1050 OP (0) |= relocation & 0x0f;
1053 case R_RX_RH_ABS5p8L:
1054 RX_STACK_POP (relocation);
1059 OP (0) |= (relocation << 3) & 0x80;
1060 OP (0) |= relocation & 0x0f;
1063 case R_RX_RH_UIMM4p8:
1066 OP (0) |= relocation << 4;
1069 case R_RX_RH_UNEG4p8:
1072 OP (0) |= (-relocation) << 4;
1075 /* Complex reloc handling: */
1079 RX_STACK_POP (relocation);
1080 #if RX_OPCODE_BIG_ENDIAN
1081 OP (3) = relocation;
1082 OP (2) = relocation >> 8;
1083 OP (1) = relocation >> 16;
1084 OP (0) = relocation >> 24;
1086 OP (0) = relocation;
1087 OP (1) = relocation >> 8;
1088 OP (2) = relocation >> 16;
1089 OP (3) = relocation >> 24;
1093 case R_RX_ABS32_REV:
1095 RX_STACK_POP (relocation);
1096 #if RX_OPCODE_BIG_ENDIAN
1097 OP (0) = relocation;
1098 OP (1) = relocation >> 8;
1099 OP (2) = relocation >> 16;
1100 OP (3) = relocation >> 24;
1102 OP (3) = relocation;
1103 OP (2) = relocation >> 8;
1104 OP (1) = relocation >> 16;
1105 OP (0) = relocation >> 24;
1109 case R_RX_ABS24S_PCREL:
1112 RX_STACK_POP (relocation);
1113 RANGE (-0x800000, 0x7fffff);
1114 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
1116 OP (2) = relocation;
1117 OP (1) = relocation >> 8;
1118 OP (0) = relocation >> 16;
1122 OP (0) = relocation;
1123 OP (1) = relocation >> 8;
1124 OP (2) = relocation >> 16;
1130 RX_STACK_POP (relocation);
1131 RANGE (-32768, 65535);
1132 #if RX_OPCODE_BIG_ENDIAN
1133 OP (1) = relocation;
1134 OP (0) = relocation >> 8;
1136 OP (0) = relocation;
1137 OP (1) = relocation >> 8;
1141 case R_RX_ABS16_REV:
1143 RX_STACK_POP (relocation);
1144 RANGE (-32768, 65535);
1145 #if RX_OPCODE_BIG_ENDIAN
1146 OP (0) = relocation;
1147 OP (1) = relocation >> 8;
1149 OP (1) = relocation;
1150 OP (0) = relocation >> 8;
1154 case R_RX_ABS16S_PCREL:
1156 RX_STACK_POP (relocation);
1157 RANGE (-32768, 32767);
1158 if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
1160 OP (1) = relocation;
1161 OP (0) = relocation >> 8;
1165 OP (0) = relocation;
1166 OP (1) = relocation >> 8;
1172 RX_STACK_POP (relocation);
1174 #if RX_OPCODE_BIG_ENDIAN
1175 OP (1) = relocation;
1176 OP (0) = relocation >> 8;
1178 OP (0) = relocation;
1179 OP (1) = relocation >> 8;
1185 RX_STACK_POP (relocation);
1188 #if RX_OPCODE_BIG_ENDIAN
1189 OP (1) = relocation;
1190 OP (0) = relocation >> 8;
1192 OP (0) = relocation;
1193 OP (1) = relocation >> 8;
1199 RX_STACK_POP (relocation);
1202 #if RX_OPCODE_BIG_ENDIAN
1203 OP (1) = relocation;
1204 OP (0) = relocation >> 8;
1206 OP (0) = relocation;
1207 OP (1) = relocation >> 8;
1213 RX_STACK_POP (relocation);
1215 OP (0) = relocation;
1220 RX_STACK_POP (relocation);
1222 OP (0) = relocation;
1227 RX_STACK_POP (relocation);
1230 OP (0) = relocation;
1235 RX_STACK_POP (relocation);
1238 OP (0) = relocation;
1243 case R_RX_ABS8S_PCREL:
1244 RX_STACK_POP (relocation);
1246 OP (0) = relocation;
1250 if (r_symndx < symtab_hdr->sh_info)
1251 RX_STACK_PUSH (sec->output_section->vma
1252 + sec->output_offset
1258 && (h->root.type == bfd_link_hash_defined
1259 || h->root.type == bfd_link_hash_defweak))
1260 RX_STACK_PUSH (h->root.u.def.value
1261 + sec->output_section->vma
1262 + sec->output_offset
1265 _bfd_error_handler (_("Warning: RX_SYM reloc with an unknown symbol"));
1273 saw_subtract = TRUE;
1276 RX_STACK_PUSH (tmp);
1284 RX_STACK_POP (tmp1);
1285 RX_STACK_POP (tmp2);
1287 RX_STACK_PUSH (tmp1);
1295 saw_subtract = TRUE;
1296 RX_STACK_POP (tmp1);
1297 RX_STACK_POP (tmp2);
1299 RX_STACK_PUSH (tmp2);
1307 RX_STACK_POP (tmp1);
1308 RX_STACK_POP (tmp2);
1310 RX_STACK_PUSH (tmp1);
1318 RX_STACK_POP (tmp1);
1319 RX_STACK_POP (tmp2);
1321 RX_STACK_PUSH (tmp1);
1329 RX_STACK_POP (tmp1);
1330 RX_STACK_POP (tmp2);
1332 RX_STACK_PUSH (tmp1);
1340 RX_STACK_POP (tmp1);
1341 RX_STACK_POP (tmp2);
1343 RX_STACK_PUSH (tmp1);
1347 case R_RX_OPsctsize:
1348 RX_STACK_PUSH (input_section->size);
1352 RX_STACK_PUSH (input_section->output_section->vma);
1359 RX_STACK_POP (tmp1);
1360 RX_STACK_POP (tmp2);
1362 RX_STACK_PUSH (tmp1);
1370 RX_STACK_POP (tmp1);
1371 RX_STACK_POP (tmp2);
1373 RX_STACK_PUSH (tmp1);
1381 RX_STACK_POP (tmp1);
1382 RX_STACK_POP (tmp2);
1384 RX_STACK_PUSH (tmp1);
1394 RX_STACK_PUSH (tmp);
1402 RX_STACK_POP (tmp1);
1403 RX_STACK_POP (tmp2);
1405 RX_STACK_PUSH (tmp1);
1410 RX_STACK_PUSH (get_romstart (&r, info, input_bfd, input_section, rel->r_offset));
1414 RX_STACK_PUSH (get_ramstart (&r, info, input_bfd, input_section, rel->r_offset));
1418 r = bfd_reloc_notsupported;
1422 if (r != bfd_reloc_ok)
1424 const char * msg = NULL;
1428 case bfd_reloc_overflow:
1429 /* Catch the case of a missing function declaration
1430 and emit a more helpful error message. */
1431 if (r_type == R_RX_DIR24S_PCREL)
1432 msg = _("%B(%A): error: call to undefined function '%s'");
1434 r = info->callbacks->reloc_overflow
1435 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
1436 input_bfd, input_section, rel->r_offset);
1439 case bfd_reloc_undefined:
1440 r = info->callbacks->undefined_symbol
1441 (info, name, input_bfd, input_section, rel->r_offset,
1445 case bfd_reloc_other:
1446 msg = _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
1449 case bfd_reloc_outofrange:
1450 msg = _("%B(%A): internal error: out of range error");
1453 case bfd_reloc_notsupported:
1454 msg = _("%B(%A): internal error: unsupported relocation error");
1457 case bfd_reloc_dangerous:
1458 msg = _("%B(%A): internal error: dangerous relocation");
1462 msg = _("%B(%A): internal error: unknown error");
1467 _bfd_error_handler (msg, input_bfd, input_section, name);
1477 /* Relaxation Support. */
1479 /* Progression of relocations from largest operand size to smallest
1483 next_smaller_reloc (int r)
1487 case R_RX_DIR32: return R_RX_DIR24S;
1488 case R_RX_DIR24S: return R_RX_DIR16S;
1489 case R_RX_DIR16S: return R_RX_DIR8S;
1490 case R_RX_DIR8S: return R_RX_NONE;
1492 case R_RX_DIR16: return R_RX_DIR8;
1493 case R_RX_DIR8: return R_RX_NONE;
1495 case R_RX_DIR16U: return R_RX_DIR8U;
1496 case R_RX_DIR8U: return R_RX_NONE;
1498 case R_RX_DIR24S_PCREL: return R_RX_DIR16S_PCREL;
1499 case R_RX_DIR16S_PCREL: return R_RX_DIR8S_PCREL;
1500 case R_RX_DIR8S_PCREL: return R_RX_DIR3U_PCREL;
1502 case R_RX_DIR16UL: return R_RX_DIR8UL;
1503 case R_RX_DIR8UL: return R_RX_NONE;
1504 case R_RX_DIR16UW: return R_RX_DIR8UW;
1505 case R_RX_DIR8UW: return R_RX_NONE;
1507 case R_RX_RH_32_OP: return R_RX_RH_24_OP;
1508 case R_RX_RH_24_OP: return R_RX_RH_16_OP;
1509 case R_RX_RH_16_OP: return R_RX_DIR8;
1511 case R_RX_ABS32: return R_RX_ABS24S;
1512 case R_RX_ABS24S: return R_RX_ABS16S;
1513 case R_RX_ABS16: return R_RX_ABS8;
1514 case R_RX_ABS16U: return R_RX_ABS8U;
1515 case R_RX_ABS16S: return R_RX_ABS8S;
1516 case R_RX_ABS8: return R_RX_NONE;
1517 case R_RX_ABS8U: return R_RX_NONE;
1518 case R_RX_ABS8S: return R_RX_NONE;
1519 case R_RX_ABS24S_PCREL: return R_RX_ABS16S_PCREL;
1520 case R_RX_ABS16S_PCREL: return R_RX_ABS8S_PCREL;
1521 case R_RX_ABS8S_PCREL: return R_RX_NONE;
1522 case R_RX_ABS16UL: return R_RX_ABS8UL;
1523 case R_RX_ABS16UW: return R_RX_ABS8UW;
1524 case R_RX_ABS8UL: return R_RX_NONE;
1525 case R_RX_ABS8UW: return R_RX_NONE;
1530 /* Delete some bytes from a section while relaxing. */
1533 elf32_rx_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
1534 Elf_Internal_Rela *alignment_rel, int force_snip)
1536 Elf_Internal_Shdr * symtab_hdr;
1537 unsigned int sec_shndx;
1538 bfd_byte * contents;
1539 Elf_Internal_Rela * irel;
1540 Elf_Internal_Rela * irelend;
1541 Elf_Internal_Sym * isym;
1542 Elf_Internal_Sym * isymend;
1544 unsigned int symcount;
1545 struct elf_link_hash_entry ** sym_hashes;
1546 struct elf_link_hash_entry ** end_hashes;
1551 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1553 contents = elf_section_data (sec)->this_hdr.contents;
1555 /* The deletion must stop at the next alignment boundary, if
1556 ALIGNMENT_REL is non-NULL. */
1559 toaddr = alignment_rel->r_offset;
1561 irel = elf_section_data (sec)->relocs;
1562 irelend = irel + sec->reloc_count;
1564 /* Actually delete the bytes. */
1565 memmove (contents + addr, contents + addr + count,
1566 (size_t) (toaddr - addr - count));
1568 /* If we don't have an alignment marker to worry about, we can just
1569 shrink the section. Otherwise, we have to fill in the newly
1570 created gap with NOP insns (0x03). */
1574 memset (contents + toaddr - count, 0x03, count);
1576 /* Adjust all the relocs. */
1577 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1579 /* Get the new reloc address. */
1580 if (irel->r_offset > addr
1581 && (irel->r_offset < toaddr
1582 || (force_snip && irel->r_offset == toaddr)))
1583 irel->r_offset -= count;
1585 /* If we see an ALIGN marker at the end of the gap, we move it
1586 to the beginning of the gap, since marking these gaps is what
1588 if (irel->r_offset == toaddr
1589 && ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
1590 && irel->r_addend & RX_RELAXA_ALIGN)
1591 irel->r_offset -= count;
1594 /* Adjust the local symbols defined in this section. */
1595 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1596 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1597 isymend = isym + symtab_hdr->sh_info;
1599 for (; isym < isymend; isym++)
1601 /* If the symbol is in the range of memory we just moved, we
1602 have to adjust its value. */
1603 if (isym->st_shndx == sec_shndx
1604 && isym->st_value > addr
1605 && isym->st_value < toaddr)
1606 isym->st_value -= count;
1608 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1609 *end* is in the moved bytes but it's *start* isn't), then we
1610 must adjust its size. */
1611 if (isym->st_shndx == sec_shndx
1612 && isym->st_value < addr
1613 && isym->st_value + isym->st_size > addr
1614 && isym->st_value + isym->st_size < toaddr)
1615 isym->st_size -= count;
1618 /* Now adjust the global symbols defined in this section. */
1619 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1620 - symtab_hdr->sh_info);
1621 sym_hashes = elf_sym_hashes (abfd);
1622 end_hashes = sym_hashes + symcount;
1624 for (; sym_hashes < end_hashes; sym_hashes++)
1626 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1628 if ((sym_hash->root.type == bfd_link_hash_defined
1629 || sym_hash->root.type == bfd_link_hash_defweak)
1630 && sym_hash->root.u.def.section == sec)
1632 /* As above, adjust the value if needed. */
1633 if (sym_hash->root.u.def.value > addr
1634 && sym_hash->root.u.def.value < toaddr)
1635 sym_hash->root.u.def.value -= count;
1637 /* As above, adjust the size if needed. */
1638 if (sym_hash->root.u.def.value < addr
1639 && sym_hash->root.u.def.value + sym_hash->size > addr
1640 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1641 sym_hash->size -= count;
1648 /* Used to sort relocs by address. If relocs have the same address,
1649 we maintain their relative order, except that R_RX_RH_RELAX
1650 alignment relocs must be the first reloc for any given address. */
1653 reloc_bubblesort (Elf_Internal_Rela * r, int count)
1657 bfd_boolean swappit;
1659 /* This is almost a classic bubblesort. It's the slowest sort, but
1660 we're taking advantage of the fact that the relocations are
1661 mostly in order already (the assembler emits them that way) and
1662 we need relocs with the same address to remain in the same
1668 for (i = 0; i < count - 1; i ++)
1670 if (r[i].r_offset > r[i + 1].r_offset)
1672 else if (r[i].r_offset < r[i + 1].r_offset)
1674 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
1675 && (r[i + 1].r_addend & RX_RELAXA_ALIGN))
1677 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
1678 && (r[i + 1].r_addend & RX_RELAXA_ELIGN)
1679 && !(ELF32_R_TYPE (r[i].r_info) == R_RX_RH_RELAX
1680 && (r[i].r_addend & RX_RELAXA_ALIGN)))
1687 Elf_Internal_Rela tmp;
1692 /* If we do move a reloc back, re-scan to see if it
1693 needs to be moved even further back. This avoids
1694 most of the O(n^2) behavior for our cases. */
1704 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1705 rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1706 lrel, abfd, sec, link_info, scale)
1709 rx_offset_for_reloc (bfd * abfd,
1710 Elf_Internal_Rela * rel,
1711 Elf_Internal_Shdr * symtab_hdr,
1712 Elf_External_Sym_Shndx * shndx_buf ATTRIBUTE_UNUSED,
1713 Elf_Internal_Sym * intsyms,
1714 Elf_Internal_Rela ** lrel,
1716 asection * input_section,
1717 struct bfd_link_info * info,
1721 bfd_reloc_status_type r;
1725 /* REL is the first of 1..N relocations. We compute the symbol
1726 value for each relocation, then combine them if needed. LREL
1727 gets a pointer to the last relocation used. */
1732 /* Get the value of the symbol referred to by the reloc. */
1733 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1735 /* A local symbol. */
1736 Elf_Internal_Sym *isym;
1739 isym = intsyms + ELF32_R_SYM (rel->r_info);
1741 if (isym->st_shndx == SHN_UNDEF)
1742 ssec = bfd_und_section_ptr;
1743 else if (isym->st_shndx == SHN_ABS)
1744 ssec = bfd_abs_section_ptr;
1745 else if (isym->st_shndx == SHN_COMMON)
1746 ssec = bfd_com_section_ptr;
1748 ssec = bfd_section_from_elf_index (abfd,
1751 /* Initial symbol value. */
1752 symval = isym->st_value;
1754 /* GAS may have made this symbol relative to a section, in
1755 which case, we have to add the addend to find the
1757 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
1758 symval += rel->r_addend;
1762 if ((ssec->flags & SEC_MERGE)
1763 && ssec->sec_info_type == SEC_INFO_TYPE_MERGE)
1764 symval = _bfd_merged_section_offset (abfd, & ssec,
1765 elf_section_data (ssec)->sec_info,
1769 /* Now make the offset relative to where the linker is putting it. */
1772 ssec->output_section->vma + ssec->output_offset;
1774 symval += rel->r_addend;
1779 struct elf_link_hash_entry * h;
1781 /* An external symbol. */
1782 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1783 h = elf_sym_hashes (abfd)[indx];
1784 BFD_ASSERT (h != NULL);
1786 if (h->root.type != bfd_link_hash_defined
1787 && h->root.type != bfd_link_hash_defweak)
1789 /* This appears to be a reference to an undefined
1790 symbol. Just ignore it--it will be caught by the
1791 regular reloc processing. */
1797 symval = (h->root.u.def.value
1798 + h->root.u.def.section->output_section->vma
1799 + h->root.u.def.section->output_offset);
1801 symval += rel->r_addend;
1804 switch (ELF32_R_TYPE (rel->r_info))
1807 RX_STACK_PUSH (symval);
1811 RX_STACK_POP (tmp1);
1813 RX_STACK_PUSH (tmp1);
1817 RX_STACK_POP (tmp1);
1818 RX_STACK_POP (tmp2);
1820 RX_STACK_PUSH (tmp1);
1824 RX_STACK_POP (tmp1);
1825 RX_STACK_POP (tmp2);
1827 RX_STACK_PUSH (tmp2);
1831 RX_STACK_POP (tmp1);
1832 RX_STACK_POP (tmp2);
1834 RX_STACK_PUSH (tmp1);
1838 RX_STACK_POP (tmp1);
1839 RX_STACK_POP (tmp2);
1841 RX_STACK_PUSH (tmp1);
1845 RX_STACK_POP (tmp1);
1846 RX_STACK_POP (tmp2);
1848 RX_STACK_PUSH (tmp1);
1852 RX_STACK_POP (tmp1);
1853 RX_STACK_POP (tmp2);
1855 RX_STACK_PUSH (tmp1);
1858 case R_RX_OPsctsize:
1859 RX_STACK_PUSH (input_section->size);
1863 RX_STACK_PUSH (input_section->output_section->vma);
1867 RX_STACK_POP (tmp1);
1868 RX_STACK_POP (tmp2);
1870 RX_STACK_PUSH (tmp1);
1874 RX_STACK_POP (tmp1);
1875 RX_STACK_POP (tmp2);
1877 RX_STACK_PUSH (tmp1);
1881 RX_STACK_POP (tmp1);
1882 RX_STACK_POP (tmp2);
1884 RX_STACK_PUSH (tmp1);
1888 RX_STACK_POP (tmp1);
1890 RX_STACK_PUSH (tmp1);
1894 RX_STACK_POP (tmp1);
1895 RX_STACK_POP (tmp2);
1897 RX_STACK_PUSH (tmp1);
1901 RX_STACK_PUSH (get_romstart (&r, info, input_bfd, input_section, rel->r_offset));
1905 RX_STACK_PUSH (get_ramstart (&r, info, input_bfd, input_section, rel->r_offset));
1913 RX_STACK_POP (symval);
1924 RX_STACK_POP (symval);
1932 RX_STACK_POP (symval);
1943 move_reloc (Elf_Internal_Rela * irel, Elf_Internal_Rela * srel, int delta)
1945 bfd_vma old_offset = srel->r_offset;
1948 while (irel <= srel)
1950 if (irel->r_offset == old_offset)
1951 irel->r_offset += delta;
1956 /* Relax one section. */
1959 elf32_rx_relax_section (bfd * abfd,
1961 struct bfd_link_info * link_info,
1962 bfd_boolean * again,
1963 bfd_boolean allow_pcrel3)
1965 Elf_Internal_Shdr * symtab_hdr;
1966 Elf_Internal_Shdr * shndx_hdr;
1967 Elf_Internal_Rela * internal_relocs;
1968 Elf_Internal_Rela * free_relocs = NULL;
1969 Elf_Internal_Rela * irel;
1970 Elf_Internal_Rela * srel;
1971 Elf_Internal_Rela * irelend;
1972 Elf_Internal_Rela * next_alignment;
1973 Elf_Internal_Rela * prev_alignment;
1974 bfd_byte * contents = NULL;
1975 bfd_byte * free_contents = NULL;
1976 Elf_Internal_Sym * intsyms = NULL;
1977 Elf_Internal_Sym * free_intsyms = NULL;
1978 Elf_External_Sym_Shndx * shndx_buf = NULL;
1984 int section_alignment_glue;
1985 /* how much to scale the relocation by - 1, 2, or 4. */
1988 /* Assume nothing changes. */
1991 /* We don't have to do anything for a relocatable link, if
1992 this section does not have relocs, or if this is not a
1994 if (link_info->relocatable
1995 || (sec->flags & SEC_RELOC) == 0
1996 || sec->reloc_count == 0
1997 || (sec->flags & SEC_CODE) == 0)
2000 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2001 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2003 sec_start = sec->output_section->vma + sec->output_offset;
2005 /* Get the section contents. */
2006 if (elf_section_data (sec)->this_hdr.contents != NULL)
2007 contents = elf_section_data (sec)->this_hdr.contents;
2008 /* Go get them off disk. */
2011 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2013 elf_section_data (sec)->this_hdr.contents = contents;
2016 /* Read this BFD's symbols. */
2017 /* Get cached copy if it exists. */
2018 if (symtab_hdr->contents != NULL)
2019 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2022 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
2023 symtab_hdr->contents = (bfd_byte *) intsyms;
2026 if (shndx_hdr->sh_size != 0)
2030 amt = symtab_hdr->sh_info;
2031 amt *= sizeof (Elf_External_Sym_Shndx);
2032 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2033 if (shndx_buf == NULL)
2035 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2036 || bfd_bread (shndx_buf, amt, abfd) != amt)
2038 shndx_hdr->contents = (bfd_byte *) shndx_buf;
2041 /* Get a copy of the native relocations. */
2042 internal_relocs = (_bfd_elf_link_read_relocs
2043 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2044 link_info->keep_memory));
2045 if (internal_relocs == NULL)
2047 if (! link_info->keep_memory)
2048 free_relocs = internal_relocs;
2050 /* The RL_ relocs must be just before the operand relocs they go
2051 with, so we must sort them to guarantee this. We use bubblesort
2052 instead of qsort so we can guarantee that relocs with the same
2053 address remain in the same relative order. */
2054 reloc_bubblesort (internal_relocs, sec->reloc_count);
2056 /* Walk through them looking for relaxing opportunities. */
2057 irelend = internal_relocs + sec->reloc_count;
2059 /* This will either be NULL or a pointer to the next alignment
2061 next_alignment = internal_relocs;
2062 /* This will be the previous alignment, although at first it points
2063 to the first real relocation. */
2064 prev_alignment = internal_relocs;
2066 /* We calculate worst case shrinkage caused by alignment directives.
2067 No fool-proof, but better than either ignoring the problem or
2068 doing heavy duty analysis of all the alignment markers in all
2070 section_alignment_glue = 0;
2071 for (irel = internal_relocs; irel < irelend; irel++)
2072 if (ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
2073 && irel->r_addend & RX_RELAXA_ALIGN)
2075 int this_glue = 1 << (irel->r_addend & RX_RELAXA_ANUM);
2077 if (section_alignment_glue < this_glue)
2078 section_alignment_glue = this_glue;
2080 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2082 section_alignment_glue *= 2;
2084 for (irel = internal_relocs; irel < irelend; irel++)
2086 unsigned char *insn;
2089 /* The insns we care about are all marked with one of these. */
2090 if (ELF32_R_TYPE (irel->r_info) != R_RX_RH_RELAX)
2093 if (irel->r_addend & RX_RELAXA_ALIGN
2094 || next_alignment == internal_relocs)
2096 /* When we delete bytes, we need to maintain all the alignments
2097 indicated. In addition, we need to be careful about relaxing
2098 jumps across alignment boundaries - these displacements
2099 *grow* when we delete bytes. For now, don't shrink
2100 displacements across an alignment boundary, just in case.
2101 Note that this only affects relocations to the same
2103 prev_alignment = next_alignment;
2104 next_alignment += 2;
2105 while (next_alignment < irelend
2106 && (ELF32_R_TYPE (next_alignment->r_info) != R_RX_RH_RELAX
2107 || !(next_alignment->r_addend & RX_RELAXA_ELIGN)))
2109 if (next_alignment >= irelend || next_alignment->r_offset == 0)
2110 next_alignment = NULL;
2113 /* When we hit alignment markers, see if we've shrunk enough
2114 before them to reduce the gap without violating the alignment
2116 if (irel->r_addend & RX_RELAXA_ALIGN)
2118 /* At this point, the next relocation *should* be the ELIGN
2120 Elf_Internal_Rela *erel = irel + 1;
2121 unsigned int alignment, nbytes;
2123 if (ELF32_R_TYPE (erel->r_info) != R_RX_RH_RELAX)
2125 if (!(erel->r_addend & RX_RELAXA_ELIGN))
2128 alignment = 1 << (irel->r_addend & RX_RELAXA_ANUM);
2130 if (erel->r_offset - irel->r_offset < alignment)
2133 nbytes = erel->r_offset - irel->r_offset;
2134 nbytes /= alignment;
2135 nbytes *= alignment;
2137 elf32_rx_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment,
2138 erel->r_offset == sec->size);
2144 if (irel->r_addend & RX_RELAXA_ELIGN)
2147 insn = contents + irel->r_offset;
2149 nrelocs = irel->r_addend & RX_RELAXA_RNUM;
2151 /* At this point, we have an insn that is a candidate for linker
2152 relaxation. There are NRELOCS relocs following that may be
2153 relaxed, although each reloc may be made of more than one
2154 reloc entry (such as gp-rel symbols). */
2156 /* Get the value of the symbol referred to by the reloc. Just
2157 in case this is the last reloc in the list, use the RL's
2158 addend to choose between this reloc (no addend) or the next
2159 (yes addend, which means at least one following reloc). */
2161 /* srel points to the "current" reloction for this insn -
2162 actually the last reloc for a given operand, which is the one
2163 we need to update. We check the relaxations in the same
2164 order that the relocations happen, so we'll just push it
2168 pc = sec->output_section->vma + sec->output_offset
2172 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2173 pcrel = symval - pc + srel->r_addend; \
2176 #define SNIPNR(offset, nbytes) \
2177 elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0);
2178 #define SNIP(offset, nbytes, newtype) \
2179 SNIPNR (offset, nbytes); \
2180 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2182 /* The order of these bit tests must match the order that the
2183 relocs appear in. Since we sorted those by offset, we can
2186 /* Note that the numbers in, say, DSP6 are the bit offsets of
2187 the code fields that describe the operand. Bits number 0 for
2188 the MSB of insn[0]. */
2195 if (irel->r_addend & RX_RELAXA_DSP6)
2200 if (code == 2 && symval/scale <= 255)
2202 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2205 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2206 if (newrel != ELF32_R_TYPE (srel->r_info))
2208 SNIP (3, 1, newrel);
2213 else if (code == 1 && symval == 0)
2216 SNIP (2, 1, R_RX_NONE);
2220 /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst. */
2221 else if (code == 1 && symval/scale <= 31
2222 /* Decodable bits. */
2223 && (insn[0] & 0xcc) == 0xcc
2225 && (insn[0] & 0x30) != 0x30
2226 /* Register MSBs. */
2227 && (insn[1] & 0x88) == 0x00)
2231 insn[0] = 0x88 | (insn[0] & 0x30);
2232 /* The register fields are in the right place already. */
2234 /* We can't relax this new opcode. */
2237 switch ((insn[0] & 0x30) >> 4)
2240 newrel = R_RX_RH_ABS5p5B;
2243 newrel = R_RX_RH_ABS5p5W;
2246 newrel = R_RX_RH_ABS5p5L;
2250 move_reloc (irel, srel, -2);
2251 SNIP (2, 1, newrel);
2254 /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst. */
2255 else if (code == 1 && symval/scale <= 31
2256 /* Decodable bits. */
2257 && (insn[0] & 0xf8) == 0x58
2258 /* Register MSBs. */
2259 && (insn[1] & 0x88) == 0x00)
2263 insn[0] = 0xb0 | ((insn[0] & 0x04) << 1);
2264 /* The register fields are in the right place already. */
2266 /* We can't relax this new opcode. */
2269 switch ((insn[0] & 0x08) >> 3)
2272 newrel = R_RX_RH_ABS5p5B;
2275 newrel = R_RX_RH_ABS5p5W;
2279 move_reloc (irel, srel, -2);
2280 SNIP (2, 1, newrel);
2284 /* A DSP4 operand always follows a DSP6 operand, even if there's
2285 no relocation for it. We have to read the code out of the
2286 opcode to calculate the offset of the operand. */
2287 if (irel->r_addend & RX_RELAXA_DSP4)
2289 int code6, offset = 0;
2293 code6 = insn[0] & 0x03;
2296 case 0: offset = 2; break;
2297 case 1: offset = 3; break;
2298 case 2: offset = 4; break;
2299 case 3: offset = 2; break;
2302 code = (insn[0] & 0x0c) >> 2;
2304 if (code == 2 && symval / scale <= 255)
2306 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2310 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2311 if (newrel != ELF32_R_TYPE (srel->r_info))
2313 SNIP (offset+1, 1, newrel);
2318 else if (code == 1 && symval == 0)
2321 SNIP (offset, 1, R_RX_NONE);
2324 /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2325 else if (code == 1 && symval/scale <= 31
2326 /* Decodable bits. */
2327 && (insn[0] & 0xc3) == 0xc3
2329 && (insn[0] & 0x30) != 0x30
2330 /* Register MSBs. */
2331 && (insn[1] & 0x88) == 0x00)
2335 insn[0] = 0x80 | (insn[0] & 0x30);
2336 /* The register fields are in the right place already. */
2338 /* We can't relax this new opcode. */
2341 switch ((insn[0] & 0x30) >> 4)
2344 newrel = R_RX_RH_ABS5p5B;
2347 newrel = R_RX_RH_ABS5p5W;
2350 newrel = R_RX_RH_ABS5p5L;
2354 move_reloc (irel, srel, -2);
2355 SNIP (2, 1, newrel);
2359 /* These always occur alone, but the offset depends on whether
2360 it's a MEMEX opcode (0x06) or not. */
2361 if (irel->r_addend & RX_RELAXA_DSP14)
2366 if (insn[0] == 0x06)
2373 if (code == 2 && symval / scale <= 255)
2375 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2379 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2380 if (newrel != ELF32_R_TYPE (srel->r_info))
2382 SNIP (offset, 1, newrel);
2386 else if (code == 1 && symval == 0)
2389 SNIP (offset, 1, R_RX_NONE);
2400 /* These always occur alone. */
2401 if (irel->r_addend & RX_RELAXA_IMM6)
2407 /* These relocations sign-extend, so we must do signed compares. */
2408 ssymval = (long) symval;
2410 code = insn[0] & 0x03;
2412 if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
2414 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2418 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2419 if (newrel != ELF32_R_TYPE (srel->r_info))
2421 SNIP (2, 1, newrel);
2426 else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
2428 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2432 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2433 if (newrel != ELF32_R_TYPE (srel->r_info))
2435 SNIP (2, 1, newrel);
2440 /* Special case UIMM8 format: CMP #uimm8,Rdst. */
2441 else if (code == 2 && ssymval <= 255 && ssymval >= 16
2442 /* Decodable bits. */
2443 && (insn[0] & 0xfc) == 0x74
2444 /* Decodable bits. */
2445 && ((insn[1] & 0xf0) == 0x00))
2450 insn[1] = 0x50 | (insn[1] & 0x0f);
2452 /* We can't relax this new opcode. */
2455 if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
2456 newrel = R_RX_ABS8U;
2458 newrel = R_RX_DIR8U;
2460 SNIP (2, 1, newrel);
2464 else if (code == 2 && ssymval <= 127 && ssymval >= -128)
2466 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2470 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2471 if (newrel != ELF32_R_TYPE (srel->r_info))
2473 SNIP (2, 1, newrel);
2478 /* Special case UIMM4 format: CMP, MUL, AND, OR. */
2479 else if (code == 1 && ssymval <= 15 && ssymval >= 0
2480 /* Decodable bits and immediate type. */
2482 /* Decodable bits. */
2483 && (insn[1] & 0xc0) == 0x00)
2485 static const int newop[4] = { 1, 3, 4, 5 };
2487 insn[0] = 0x60 | newop[insn[1] >> 4];
2488 /* The register number doesn't move. */
2490 /* We can't relax this new opcode. */
2493 move_reloc (irel, srel, -1);
2495 SNIP (2, 1, R_RX_RH_UIMM4p8);
2499 /* Special case UIMM4 format: ADD -> ADD/SUB. */
2500 else if (code == 1 && ssymval <= 15 && ssymval >= -15
2501 /* Decodable bits and immediate type. */
2503 /* Same register for source and destination. */
2504 && ((insn[1] >> 4) == (insn[1] & 0x0f)))
2508 /* Note that we can't turn "add $0,Rs" into a NOP
2509 because the flags need to be set right. */
2513 insn[0] = 0x60; /* Subtract. */
2514 newrel = R_RX_RH_UNEG4p8;
2518 insn[0] = 0x62; /* Add. */
2519 newrel = R_RX_RH_UIMM4p8;
2522 /* The register number is in the right place. */
2524 /* We can't relax this new opcode. */
2527 move_reloc (irel, srel, -1);
2529 SNIP (2, 1, newrel);
2534 /* These are either matched with a DSP6 (2-byte base) or an id24
2536 if (irel->r_addend & RX_RELAXA_IMM12)
2538 int dspcode, offset = 0;
2543 if ((insn[0] & 0xfc) == 0xfc)
2544 dspcode = 1; /* Just something with one byte operand. */
2546 dspcode = insn[0] & 3;
2549 case 0: offset = 2; break;
2550 case 1: offset = 3; break;
2551 case 2: offset = 4; break;
2552 case 3: offset = 2; break;
2555 /* These relocations sign-extend, so we must do signed compares. */
2556 ssymval = (long) symval;
2558 code = (insn[1] >> 2) & 3;
2559 if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
2561 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2565 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2566 if (newrel != ELF32_R_TYPE (srel->r_info))
2568 SNIP (offset, 1, newrel);
2573 else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
2575 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2579 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2580 if (newrel != ELF32_R_TYPE (srel->r_info))
2582 SNIP (offset, 1, newrel);
2587 /* Special case UIMM8 format: MOV #uimm8,Rdst. */
2588 else if (code == 2 && ssymval <= 255 && ssymval >= 16
2589 /* Decodable bits. */
2591 /* Decodable bits. */
2592 && ((insn[1] & 0x03) == 0x02))
2597 insn[1] = 0x40 | (insn[1] >> 4);
2599 /* We can't relax this new opcode. */
2602 if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
2603 newrel = R_RX_ABS8U;
2605 newrel = R_RX_DIR8U;
2607 SNIP (2, 1, newrel);
2611 else if (code == 2 && ssymval <= 127 && ssymval >= -128)
2613 unsigned int newrel = ELF32_R_TYPE(srel->r_info);
2617 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2618 if (newrel != ELF32_R_TYPE(srel->r_info))
2620 SNIP (offset, 1, newrel);
2625 /* Special case UIMM4 format: MOV #uimm4,Rdst. */
2626 else if (code == 1 && ssymval <= 15 && ssymval >= 0
2627 /* Decodable bits. */
2629 /* Decodable bits. */
2630 && ((insn[1] & 0x03) == 0x02))
2633 insn[1] = insn[1] >> 4;
2635 /* We can't relax this new opcode. */
2638 move_reloc (irel, srel, -1);
2640 SNIP (2, 1, R_RX_RH_UIMM4p8);
2645 if (irel->r_addend & RX_RELAXA_BRA)
2647 unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2649 int alignment_glue = 0;
2653 /* Branches over alignment chunks are problematic, as
2654 deleting bytes here makes the branch *further* away. We
2655 can be agressive with branches within this alignment
2656 block, but not branches outside it. */
2657 if ((prev_alignment == NULL
2658 || symval < (bfd_vma)(sec_start + prev_alignment->r_offset))
2659 && (next_alignment == NULL
2660 || symval > (bfd_vma)(sec_start + next_alignment->r_offset)))
2661 alignment_glue = section_alignment_glue;
2663 if (ELF32_R_TYPE(srel[1].r_info) == R_RX_RH_RELAX
2664 && srel[1].r_addend & RX_RELAXA_BRA
2665 && srel[1].r_offset < irel->r_offset + pcrel)
2668 newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2670 /* The values we compare PCREL with are not what you'd
2671 expect; they're off by a little to compensate for (1)
2672 where the reloc is relative to the insn, and (2) how much
2673 the insn is going to change when we relax it. */
2675 /* These we have to decode. */
2678 case 0x04: /* BRA pcdsp:24 */
2679 if (-32768 + alignment_glue <= pcrel
2680 && pcrel <= 32765 - alignment_glue)
2683 SNIP (3, 1, newrel);
2688 case 0x38: /* BRA pcdsp:16 */
2689 if (-128 + alignment_glue <= pcrel
2690 && pcrel <= 127 - alignment_glue)
2693 SNIP (2, 1, newrel);
2698 case 0x2e: /* BRA pcdsp:8 */
2699 /* Note that there's a risk here of shortening things so
2700 much that we no longer fit this reloc; it *should*
2701 only happen when you branch across a branch, and that
2702 branch also devolves into BRA.S. "Real" code should
2704 if (max_pcrel3 + alignment_glue <= pcrel
2705 && pcrel <= 10 - alignment_glue
2709 SNIP (1, 1, newrel);
2710 move_reloc (irel, srel, -1);
2715 case 0x05: /* BSR pcdsp:24 */
2716 if (-32768 + alignment_glue <= pcrel
2717 && pcrel <= 32765 - alignment_glue)
2720 SNIP (1, 1, newrel);
2725 case 0x3a: /* BEQ.W pcdsp:16 */
2726 case 0x3b: /* BNE.W pcdsp:16 */
2727 if (-128 + alignment_glue <= pcrel
2728 && pcrel <= 127 - alignment_glue)
2730 insn[0] = 0x20 | (insn[0] & 1);
2731 SNIP (1, 1, newrel);
2736 case 0x20: /* BEQ.B pcdsp:8 */
2737 case 0x21: /* BNE.B pcdsp:8 */
2738 if (max_pcrel3 + alignment_glue <= pcrel
2739 && pcrel - alignment_glue <= 10
2742 insn[0] = 0x10 | ((insn[0] & 1) << 3);
2743 SNIP (1, 1, newrel);
2744 move_reloc (irel, srel, -1);
2749 case 0x16: /* synthetic BNE dsp24 */
2750 case 0x1e: /* synthetic BEQ dsp24 */
2751 if (-32767 + alignment_glue <= pcrel
2752 && pcrel <= 32766 - alignment_glue
2755 if (insn[0] == 0x16)
2759 /* We snip out the bytes at the end else the reloc
2760 will get moved too, and too much. */
2761 SNIP (3, 2, newrel);
2762 move_reloc (irel, srel, -1);
2768 /* Special case - synthetic conditional branches, pcrel24.
2769 Note that EQ and NE have been handled above. */
2770 if ((insn[0] & 0xf0) == 0x20
2773 && srel->r_offset != irel->r_offset + 1
2774 && -32767 + alignment_glue <= pcrel
2775 && pcrel <= 32766 - alignment_glue)
2779 SNIP (5, 1, newrel);
2783 /* Special case - synthetic conditional branches, pcrel16 */
2784 if ((insn[0] & 0xf0) == 0x20
2787 && srel->r_offset != irel->r_offset + 1
2788 && -127 + alignment_glue <= pcrel
2789 && pcrel <= 126 - alignment_glue)
2791 int cond = (insn[0] & 0x0f) ^ 0x01;
2793 insn[0] = 0x20 | cond;
2794 /* By moving the reloc first, we avoid having
2795 delete_bytes move it also. */
2796 move_reloc (irel, srel, -2);
2797 SNIP (2, 3, newrel);
2802 BFD_ASSERT (nrelocs == 0);
2804 /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2805 use MOV.bwl #uimm:8, dsp:5[r7] format. This is tricky
2806 because it may have one or two relocations. */
2807 if ((insn[0] & 0xfc) == 0xf8
2808 && (insn[1] & 0x80) == 0x00
2809 && (insn[0] & 0x03) != 0x03)
2811 int dcode, icode, reg, ioff, dscale, ilen;
2812 bfd_vma disp_val = 0;
2814 Elf_Internal_Rela * disp_rel = 0;
2815 Elf_Internal_Rela * imm_rel = 0;
2820 dcode = insn[0] & 0x03;
2821 icode = (insn[1] >> 2) & 0x03;
2822 reg = (insn[1] >> 4) & 0x0f;
2824 ioff = dcode == 1 ? 3 : dcode == 2 ? 4 : 2;
2826 /* Figure out what the dispacement is. */
2827 if (dcode == 1 || dcode == 2)
2829 /* There's a displacement. See if there's a reloc for it. */
2830 if (srel[1].r_offset == irel->r_offset + 2)
2842 #if RX_OPCODE_BIG_ENDIAN
2843 disp_val = insn[2] * 256 + insn[3];
2845 disp_val = insn[2] + insn[3] * 256;
2848 switch (insn[1] & 3)
2864 /* Figure out what the immediate is. */
2865 if (srel[1].r_offset == irel->r_offset + ioff)
2868 imm_val = (long) symval;
2873 unsigned char * ip = insn + ioff;
2878 /* For byte writes, we don't sign extend. Makes the math easier later. */
2882 imm_val = (char) ip[0];
2885 #if RX_OPCODE_BIG_ENDIAN
2886 imm_val = ((char) ip[0] << 8) | ip[1];
2888 imm_val = ((char) ip[1] << 8) | ip[0];
2892 #if RX_OPCODE_BIG_ENDIAN
2893 imm_val = ((char) ip[0] << 16) | (ip[1] << 8) | ip[2];
2895 imm_val = ((char) ip[2] << 16) | (ip[1] << 8) | ip[0];
2899 #if RX_OPCODE_BIG_ENDIAN
2900 imm_val = (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];
2902 imm_val = (ip[3] << 24) | (ip[2] << 16) | (ip[1] << 8) | ip[0];
2936 /* The shortcut happens when the immediate is 0..255,
2937 register r0 to r7, and displacement (scaled) 0..31. */
2939 if (0 <= imm_val && imm_val <= 255
2940 && 0 <= reg && reg <= 7
2941 && disp_val / dscale <= 31)
2943 insn[0] = 0x3c | (insn[1] & 0x03);
2944 insn[1] = (((disp_val / dscale) << 3) & 0x80) | (reg << 4) | ((disp_val/dscale) & 0x0f);
2949 int newrel = R_RX_NONE;
2954 newrel = R_RX_RH_ABS5p8B;
2957 newrel = R_RX_RH_ABS5p8W;
2960 newrel = R_RX_RH_ABS5p8L;
2963 disp_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (disp_rel->r_info), newrel);
2964 move_reloc (irel, disp_rel, -1);
2968 imm_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (imm_rel->r_info), R_RX_DIR8U);
2969 move_reloc (disp_rel ? disp_rel : irel,
2971 irel->r_offset - imm_rel->r_offset + 2);
2974 SNIPNR (3, ilen - 3);
2977 /* We can't relax this new opcode. */
2983 /* We can't reliably relax branches to DIR3U_PCREL unless we know
2984 whatever they're branching over won't shrink any more. If we're
2985 basically done here, do one more pass just for branches - but
2986 don't request a pass after that one! */
2987 if (!*again && !allow_pcrel3)
2989 bfd_boolean ignored;
2991 elf32_rx_relax_section (abfd, sec, link_info, &ignored, TRUE);
2997 if (free_relocs != NULL)
3000 if (free_contents != NULL)
3001 free (free_contents);
3003 if (shndx_buf != NULL)
3005 shndx_hdr->contents = NULL;
3009 if (free_intsyms != NULL)
3010 free (free_intsyms);
3016 elf32_rx_relax_section_wrapper (bfd * abfd,
3018 struct bfd_link_info * link_info,
3019 bfd_boolean * again)
3021 return elf32_rx_relax_section (abfd, sec, link_info, again, FALSE);
3024 /* Function to set the ELF flag bits. */
3027 rx_elf_set_private_flags (bfd * abfd, flagword flags)
3029 elf_elfheader (abfd)->e_flags = flags;
3030 elf_flags_init (abfd) = TRUE;
3034 static bfd_boolean no_warn_mismatch = FALSE;
3035 static bfd_boolean ignore_lma = TRUE;
3037 void bfd_elf32_rx_set_target_flags (bfd_boolean, bfd_boolean);
3040 bfd_elf32_rx_set_target_flags (bfd_boolean user_no_warn_mismatch,
3041 bfd_boolean user_ignore_lma)
3043 no_warn_mismatch = user_no_warn_mismatch;
3044 ignore_lma = user_ignore_lma;
3047 /* Converts FLAGS into a descriptive string.
3048 Returns a static pointer. */
3051 describe_flags (flagword flags)
3053 static char buf [128];
3057 if (flags & E_FLAG_RX_64BIT_DOUBLES)
3058 strcat (buf, "64-bit doubles");
3060 strcat (buf, "32-bit doubles");
3062 if (flags & E_FLAG_RX_DSP)
3063 strcat (buf, ", dsp");
3065 strcat (buf, ", no dsp");
3067 if (flags & E_FLAG_RX_PID)
3068 strcat (buf, ", pid");
3070 strcat (buf, ", no pid");
3072 if (flags & E_FLAG_RX_ABI)
3073 strcat (buf, ", RX ABI");
3075 strcat (buf, ", GCC ABI");
3080 /* Merge backend specific data from an object file to the output
3081 object file when linking. */
3084 rx_elf_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
3088 bfd_boolean error = FALSE;
3090 new_flags = elf_elfheader (ibfd)->e_flags;
3091 old_flags = elf_elfheader (obfd)->e_flags;
3093 if (!elf_flags_init (obfd))
3095 /* First call, no flags set. */
3096 elf_flags_init (obfd) = TRUE;
3097 elf_elfheader (obfd)->e_flags = new_flags;
3099 else if (old_flags != new_flags)
3101 flagword known_flags;
3103 known_flags = E_FLAG_RX_ABI | E_FLAG_RX_64BIT_DOUBLES
3104 | E_FLAG_RX_DSP | E_FLAG_RX_PID;
3106 if ((old_flags ^ new_flags) & known_flags)
3108 /* Only complain if flag bits we care about do not match.
3109 Other bits may be set, since older binaries did use some
3110 deprecated flags. */
3111 if (no_warn_mismatch)
3113 elf_elfheader (obfd)->e_flags = (new_flags | old_flags) & known_flags;
3117 _bfd_error_handler ("There is a conflict merging the ELF header flags from %s",
3118 bfd_get_filename (ibfd));
3119 _bfd_error_handler (" the input file's flags: %s",
3120 describe_flags (new_flags));
3121 _bfd_error_handler (" the output file's flags: %s",
3122 describe_flags (old_flags));
3127 elf_elfheader (obfd)->e_flags = new_flags & known_flags;
3131 bfd_set_error (bfd_error_bad_value);
3137 rx_elf_print_private_bfd_data (bfd * abfd, void * ptr)
3139 FILE * file = (FILE *) ptr;
3142 BFD_ASSERT (abfd != NULL && ptr != NULL);
3144 /* Print normal ELF private data. */
3145 _bfd_elf_print_private_bfd_data (abfd, ptr);
3147 flags = elf_elfheader (abfd)->e_flags;
3148 fprintf (file, _("private flags = 0x%lx:"), (long) flags);
3150 fprintf (file, "%s", describe_flags (flags));
3154 /* Return the MACH for an e_flags value. */
3157 elf32_rx_machine (bfd * abfd ATTRIBUTE_UNUSED)
3159 #if 0 /* FIXME: EF_RX_CPU_MASK collides with E_FLAG_RX_...
3160 Need to sort out how these flag bits are used.
3161 For now we assume that the flags are OK. */
3162 if ((elf_elfheader (abfd)->e_flags & EF_RX_CPU_MASK) == EF_RX_CPU_RX)
3170 rx_elf_object_p (bfd * abfd)
3174 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
3175 int nphdrs = elf_elfheader (abfd)->e_phnum;
3177 static int saw_be = FALSE;
3179 /* We never want to automatically choose the non-swapping big-endian
3180 target. The user can only get that explicitly, such as with -I
3182 if (abfd->xvec == &rx_elf32_be_ns_vec
3183 && abfd->target_defaulted)
3186 /* BFD->target_defaulted is not set to TRUE when a target is chosen
3187 as a fallback, so we check for "scanning" to know when to stop
3188 using the non-swapping target. */
3189 if (abfd->xvec == &rx_elf32_be_ns_vec
3192 if (abfd->xvec == &rx_elf32_be_vec)
3195 bfd_default_set_arch_mach (abfd, bfd_arch_rx,
3196 elf32_rx_machine (abfd));
3198 /* For each PHDR in the object, we must find some section that
3199 corresponds (based on matching file offsets) and use its VMA
3200 information to reconstruct the p_vaddr field we clobbered when we
3202 for (i=0; i<nphdrs; i++)
3204 for (u=0; u<elf_tdata(abfd)->num_elf_sections; u++)
3206 Elf_Internal_Shdr *sec = elf_tdata(abfd)->elf_sect_ptr[u];
3208 if (phdr[i].p_filesz
3209 && phdr[i].p_offset <= (bfd_vma) sec->sh_offset
3210 && (bfd_vma)sec->sh_offset <= phdr[i].p_offset + (phdr[i].p_filesz - 1))
3212 /* Found one! The difference between the two addresses,
3213 plus the difference between the two file offsets, is
3214 enough information to reconstruct the lma. */
3216 /* Example where they aren't:
3217 PHDR[1] = lma fffc0100 offset 00002010 size 00000100
3218 SEC[6] = vma 00000050 offset 00002050 size 00000040
3220 The correct LMA for the section is fffc0140 + (2050-2010).
3223 phdr[i].p_vaddr = sec->sh_addr + (sec->sh_offset - phdr[i].p_offset);
3228 /* We must update the bfd sections as well, so we don't stop
3230 bsec = abfd->sections;
3233 if (phdr[i].p_filesz
3234 && phdr[i].p_vaddr <= bsec->vma
3235 && bsec->vma <= phdr[i].p_vaddr + (phdr[i].p_filesz - 1))
3237 bsec->lma = phdr[i].p_paddr + (bsec->vma - phdr[i].p_vaddr);
3249 rx_dump_symtab (bfd * abfd, void * internal_syms, void * external_syms)
3252 Elf_Internal_Sym * isymbuf;
3253 Elf_Internal_Sym * isymend;
3254 Elf_Internal_Sym * isym;
3255 Elf_Internal_Shdr * symtab_hdr;
3256 bfd_boolean free_internal = FALSE, free_external = FALSE;
3258 char * st_info_stb_str;
3259 char * st_other_str;
3260 char * st_shndx_str;
3262 if (! internal_syms)
3264 internal_syms = bfd_malloc (1000);
3267 if (! external_syms)
3269 external_syms = bfd_malloc (1000);
3273 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3274 locsymcount = symtab_hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3276 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3277 symtab_hdr->sh_info, 0,
3278 internal_syms, external_syms, NULL);
3280 isymbuf = internal_syms;
3281 isymend = isymbuf + locsymcount;
3283 for (isym = isymbuf ; isym < isymend ; isym++)
3285 switch (ELF_ST_TYPE (isym->st_info))
3287 case STT_FUNC: st_info_str = "STT_FUNC"; break;
3288 case STT_SECTION: st_info_str = "STT_SECTION"; break;
3289 case STT_FILE: st_info_str = "STT_FILE"; break;
3290 case STT_OBJECT: st_info_str = "STT_OBJECT"; break;
3291 case STT_TLS: st_info_str = "STT_TLS"; break;
3292 default: st_info_str = "";
3294 switch (ELF_ST_BIND (isym->st_info))
3296 case STB_LOCAL: st_info_stb_str = "STB_LOCAL"; break;
3297 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL"; break;
3298 default: st_info_stb_str = "";
3300 switch (ELF_ST_VISIBILITY (isym->st_other))
3302 case STV_DEFAULT: st_other_str = "STV_DEFAULT"; break;
3303 case STV_INTERNAL: st_other_str = "STV_INTERNAL"; break;
3304 case STV_PROTECTED: st_other_str = "STV_PROTECTED"; break;
3305 default: st_other_str = "";
3307 switch (isym->st_shndx)
3309 case SHN_ABS: st_shndx_str = "SHN_ABS"; break;
3310 case SHN_COMMON: st_shndx_str = "SHN_COMMON"; break;
3311 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF"; break;
3312 default: st_shndx_str = "";
3315 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3316 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3318 (unsigned long) isym->st_value,
3319 (unsigned long) isym->st_size,
3321 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
3323 isym->st_info, st_info_str, st_info_stb_str,
3324 isym->st_other, st_other_str,
3325 isym->st_shndx, st_shndx_str);
3328 free (internal_syms);
3330 free (external_syms);
3334 rx_get_reloc (long reloc)
3336 if (0 <= reloc && reloc < R_RX_max)
3337 return rx_elf_howto_table[reloc].name;
3343 /* We must take care to keep the on-disk copy of any code sections
3344 that are fully linked swapped if the target is big endian, to match
3345 the Renesas tools. */
3347 /* The rule is: big endian object that are final-link executables,
3348 have code sections stored with 32-bit words swapped relative to
3349 what you'd get by default. */
3352 rx_get_section_contents (bfd * abfd,
3356 bfd_size_type count)
3358 int exec = (abfd->flags & EXEC_P) ? 1 : 0;
3359 int s_code = (section->flags & SEC_CODE) ? 1 : 0;
3363 fprintf (stderr, "dj: get %ld %ld from %s %s e%d sc%d %08lx:%08lx\n",
3364 (long) offset, (long) count, section->name,
3365 bfd_big_endian(abfd) ? "be" : "le",
3366 exec, s_code, (long unsigned) section->filepos,
3367 (long unsigned) offset);
3370 if (exec && s_code && bfd_big_endian (abfd))
3372 char * cloc = (char *) location;
3373 bfd_size_type cnt, end_cnt;
3377 /* Fetch and swap unaligned bytes at the beginning. */
3382 rv = _bfd_generic_get_section_contents (abfd, section, buf,
3387 bfd_putb32 (bfd_getl32 (buf), buf);
3389 cnt = 4 - (offset % 4);
3393 memcpy (location, buf + (offset % 4), cnt);
3400 end_cnt = count % 4;
3402 /* Fetch and swap the middle bytes. */
3405 rv = _bfd_generic_get_section_contents (abfd, section, cloc, offset,
3410 for (cnt = count; cnt >= 4; cnt -= 4, cloc += 4)
3411 bfd_putb32 (bfd_getl32 (cloc), cloc);
3414 /* Fetch and swap the end bytes. */
3419 /* Fetch the end bytes. */
3420 rv = _bfd_generic_get_section_contents (abfd, section, buf,
3421 offset + count - end_cnt, 4);
3425 bfd_putb32 (bfd_getl32 (buf), buf);
3426 memcpy (cloc, buf, end_cnt);
3430 rv = _bfd_generic_get_section_contents (abfd, section, location, offset, count);
3437 rx2_set_section_contents (bfd * abfd,
3439 const void * location,
3441 bfd_size_type count)
3445 fprintf (stderr, " set sec %s %08x loc %p offset %#x count %#x\n",
3446 section->name, (unsigned) section->vma, location, (int) offset, (int) count);
3447 for (i = 0; i < count; i++)
3449 if (i % 16 == 0 && i > 0)
3450 fprintf (stderr, "\n");
3452 if (i % 16 && i % 4 == 0)
3453 fprintf (stderr, " ");
3456 fprintf (stderr, " %08x:", (int) (section->vma + offset + i));
3458 fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
3460 fprintf (stderr, "\n");
3462 return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
3464 #define _bfd_elf_set_section_contents rx2_set_section_contents
3468 rx_set_section_contents (bfd * abfd,
3470 const void * location,
3472 bfd_size_type count)
3474 bfd_boolean exec = (abfd->flags & EXEC_P) ? TRUE : FALSE;
3475 bfd_boolean s_code = (section->flags & SEC_CODE) ? TRUE : FALSE;
3477 char * swapped_data = NULL;
3479 bfd_vma caddr = section->vma + offset;
3481 bfd_size_type scount;
3486 fprintf (stderr, "\ndj: set %ld %ld to %s %s e%d sc%d\n",
3487 (long) offset, (long) count, section->name,
3488 bfd_big_endian (abfd) ? "be" : "le",
3491 for (i = 0; i < count; i++)
3493 int a = section->vma + offset + i;
3495 if (a % 16 == 0 && a > 0)
3496 fprintf (stderr, "\n");
3498 if (a % 16 && a % 4 == 0)
3499 fprintf (stderr, " ");
3501 if (a % 16 == 0 || i == 0)
3502 fprintf (stderr, " %08x:", (int) (section->vma + offset + i));
3504 fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
3507 fprintf (stderr, "\n");
3510 if (! exec || ! s_code || ! bfd_big_endian (abfd))
3511 return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
3513 while (count > 0 && caddr > 0 && caddr % 4)
3517 case 0: faddr = offset + 3; break;
3518 case 1: faddr = offset + 1; break;
3519 case 2: faddr = offset - 1; break;
3520 case 3: faddr = offset - 3; break;
3523 rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
3533 scount = (int)(count / 4) * 4;
3536 char * cloc = (char *) location;
3538 swapped_data = (char *) bfd_alloc (abfd, count);
3540 for (i = 0; i < count; i += 4)
3542 bfd_vma v = bfd_getl32 (cloc + i);
3543 bfd_putb32 (v, swapped_data + i);
3546 rv = _bfd_elf_set_section_contents (abfd, section, swapped_data, offset, scount);
3558 caddr = section->vma + offset;
3563 case 0: faddr = offset + 3; break;
3564 case 1: faddr = offset + 1; break;
3565 case 2: faddr = offset - 1; break;
3566 case 3: faddr = offset - 3; break;
3568 rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
3583 rx_final_link (bfd * abfd, struct bfd_link_info * info)
3587 for (o = abfd->sections; o != NULL; o = o->next)
3590 fprintf (stderr, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3591 o->name, o->flags, o->vma, o->lma, o->size, o->rawsize);
3593 if (o->flags & SEC_CODE
3594 && bfd_big_endian (abfd)
3598 fprintf (stderr, "adjusting...\n");
3600 o->size += 4 - (o->size % 4);
3604 return bfd_elf_final_link (abfd, info);
3608 elf32_rx_modify_program_headers (bfd * abfd ATTRIBUTE_UNUSED,
3609 struct bfd_link_info * info ATTRIBUTE_UNUSED)
3611 const struct elf_backend_data * bed;
3612 struct elf_obj_tdata * tdata;
3613 Elf_Internal_Phdr * phdr;
3617 bed = get_elf_backend_data (abfd);
3618 tdata = elf_tdata (abfd);
3620 count = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
3623 for (i = count; i-- != 0;)
3624 if (phdr[i].p_type == PT_LOAD)
3626 /* The Renesas tools expect p_paddr to be zero. However,
3627 there is no other way to store the writable data in ROM for
3628 startup initialization. So, we let the linker *think*
3629 we're using paddr and vaddr the "usual" way, but at the
3630 last minute we move the paddr into the vaddr (which is what
3631 the simulator uses) and zero out paddr. Note that this
3632 does not affect the section headers, just the program
3633 headers. We hope. */
3634 phdr[i].p_vaddr = phdr[i].p_paddr;
3635 #if 0 /* If we zero out p_paddr, then the LMA in the section table
3637 phdr[i].p_paddr = 0;
3644 /* The default literal sections should always be marked as "code" (i.e.,
3645 SHF_EXECINSTR). This is particularly important for big-endian mode
3646 when we do not want their contents byte reversed. */
3647 static const struct bfd_elf_special_section elf32_rx_special_sections[] =
3649 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3650 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3651 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3652 { NULL, 0, 0, 0, 0 }
3657 struct bfd_link_info *info;
3658 bfd_vma table_start;
3660 bfd_vma *table_handlers;
3661 bfd_vma table_default_handler;
3662 struct bfd_link_hash_entry **table_entries;
3663 struct bfd_link_hash_entry *table_default_entry;
3668 rx_table_find (struct bfd_hash_entry *vent, void *vinfo)
3670 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3671 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3672 const char *name; /* of the symbol we've found */
3676 const char *tname; /* name of the table */
3677 bfd_vma start_addr, end_addr;
3679 struct bfd_link_hash_entry * h;
3681 /* We're looking for globally defined symbols of the form
3682 $tablestart$<NAME>. */
3683 if (ent->type != bfd_link_hash_defined
3684 && ent->type != bfd_link_hash_defweak)
3687 name = ent->root.string;
3688 sec = ent->u.def.section;
3691 if (strncmp (name, "$tablestart$", 12))
3694 sec->flags |= SEC_KEEP;
3698 start_addr = ent->u.def.value;
3700 /* At this point, we can't build the table but we can (and must)
3701 find all the related symbols and mark their sections as SEC_KEEP
3702 so we don't garbage collect them. */
3704 buf = (char *) malloc (12 + 10 + strlen (tname));
3706 sprintf (buf, "$tableend$%s", tname);
3707 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3708 if (!h || (h->type != bfd_link_hash_defined
3709 && h->type != bfd_link_hash_defweak))
3711 _bfd_error_handler (_("%B:%A: table %s missing corresponding %s"),
3712 abfd, sec, name, buf);
3716 if (h->u.def.section != ent->u.def.section)
3718 _bfd_error_handler (_("%B:%A: %s and %s must be in the same input section"),
3719 h->u.def.section->owner, h->u.def.section,
3724 end_addr = h->u.def.value;
3726 sprintf (buf, "$tableentry$default$%s", tname);
3727 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3728 if (h && (h->type == bfd_link_hash_defined
3729 || h->type == bfd_link_hash_defweak))
3731 h->u.def.section->flags |= SEC_KEEP;
3734 for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
3736 sprintf (buf, "$tableentry$%d$%s", idx, tname);
3737 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3738 if (h && (h->type == bfd_link_hash_defined
3739 || h->type == bfd_link_hash_defweak))
3741 h->u.def.section->flags |= SEC_KEEP;
3745 /* Return TRUE to keep scanning, FALSE to end the traversal. */
3749 /* We need to check for table entry symbols and build the tables, and
3750 we need to do it before the linker does garbage collection. This function is
3751 called once per input object file. */
3754 (bfd * abfd ATTRIBUTE_UNUSED,
3755 struct bfd_link_info * info ATTRIBUTE_UNUSED)
3757 RX_Table_Info stuff;
3761 bfd_hash_traverse (&(info->hash->table), rx_table_find, &stuff);
3768 rx_table_map_2 (struct bfd_hash_entry *vent, void *vinfo)
3770 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3771 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3776 /* See if the symbol ENT has an address listed in the table, and
3777 isn't a debug/special symbol. If so, put it in the table. */
3779 if (ent->type != bfd_link_hash_defined
3780 && ent->type != bfd_link_hash_defweak)
3783 name = ent->root.string;
3785 if (name[0] == '$' || name[0] == '.' || name[0] < ' ')
3788 addr = (ent->u.def.value
3789 + ent->u.def.section->output_section->vma
3790 + ent->u.def.section->output_offset);
3792 for (idx = 0; idx < info->table_size; idx ++)
3793 if (addr == info->table_handlers[idx])
3794 info->table_entries[idx] = ent;
3796 if (addr == info->table_default_handler)
3797 info->table_default_entry = ent;
3803 rx_table_map (struct bfd_hash_entry *vent, void *vinfo)
3805 RX_Table_Info *info = (RX_Table_Info *)vinfo;
3806 struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3807 const char *name; /* of the symbol we've found */
3809 const char *tname; /* name of the table */
3810 bfd_vma start_addr, end_addr;
3812 struct bfd_link_hash_entry * h;
3815 /* We're looking for globally defined symbols of the form
3816 $tablestart$<NAME>. */
3817 if (ent->type != bfd_link_hash_defined
3818 && ent->type != bfd_link_hash_defweak)
3821 name = ent->root.string;
3823 if (strncmp (name, "$tablestart$", 12))
3827 start_addr = (ent->u.def.value
3828 + ent->u.def.section->output_section->vma
3829 + ent->u.def.section->output_offset);
3831 buf = (char *) malloc (12 + 10 + strlen (tname));
3833 sprintf (buf, "$tableend$%s", tname);
3834 end_addr = get_symbol_value_maybe (buf, info->info);
3836 sprintf (buf, "$tableentry$default$%s", tname);
3837 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3840 info->table_default_handler = (h->u.def.value
3841 + h->u.def.section->output_section->vma
3842 + h->u.def.section->output_offset);
3845 /* Zero is a valid handler address! */
3846 info->table_default_handler = (bfd_vma) (-1);
3847 info->table_default_entry = NULL;
3849 info->table_start = start_addr;
3850 info->table_size = (int) (end_addr - start_addr) / 4;
3851 info->table_handlers = (bfd_vma *) malloc (info->table_size * sizeof (bfd_vma));
3852 info->table_entries = (struct bfd_link_hash_entry **) malloc (info->table_size * sizeof (struct bfd_link_hash_entry));
3854 for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
3856 sprintf (buf, "$tableentry$%d$%s", idx, tname);
3857 h = bfd_link_hash_lookup (info->info->hash, buf, FALSE, FALSE, TRUE);
3858 if (h && (h->type == bfd_link_hash_defined
3859 || h->type == bfd_link_hash_defweak))
3861 info->table_handlers[idx] = (h->u.def.value
3862 + h->u.def.section->output_section->vma
3863 + h->u.def.section->output_offset);
3866 info->table_handlers[idx] = info->table_default_handler;
3867 info->table_entries[idx] = NULL;
3872 bfd_hash_traverse (&(info->info->hash->table), rx_table_map_2, info);
3874 fprintf (info->mapfile, "\nRX Vector Table: %s has %d entries at 0x%08" BFD_VMA_FMT "x\n\n",
3875 tname, info->table_size, start_addr);
3877 if (info->table_default_entry)
3878 fprintf (info->mapfile, " default handler is: %s at 0x%08" BFD_VMA_FMT "x\n",
3879 info->table_default_entry->root.string,
3880 info->table_default_handler);
3881 else if (info->table_default_handler != (bfd_vma)(-1))
3882 fprintf (info->mapfile, " default handler is at 0x%08" BFD_VMA_FMT "x\n",
3883 info->table_default_handler);
3885 fprintf (info->mapfile, " no default handler\n");
3888 for (idx = 0; idx < info->table_size; idx ++)
3890 if (info->table_handlers[idx] == info->table_default_handler)
3893 fprintf (info->mapfile, " . . .\n");
3899 fprintf (info->mapfile, " 0x%08" BFD_VMA_FMT "x [%3d] ", start_addr + 4 * idx, idx);
3901 if (info->table_handlers[idx] == (bfd_vma) (-1))
3902 fprintf (info->mapfile, "(no handler found)\n");
3904 else if (info->table_handlers[idx] == info->table_default_handler)
3906 if (info->table_default_entry)
3907 fprintf (info->mapfile, "(default)\n");
3909 fprintf (info->mapfile, "(default)\n");
3912 else if (info->table_entries[idx])
3914 fprintf (info->mapfile, "0x%08" BFD_VMA_FMT "x %s\n", info->table_handlers[idx], info->table_entries[idx]->root.string);
3919 fprintf (info->mapfile, "0x%08" BFD_VMA_FMT "x ???\n", info->table_handlers[idx]);
3923 fprintf (info->mapfile, " . . .\n");
3929 rx_additional_link_map_text (bfd *obfd, struct bfd_link_info *info, FILE *mapfile)
3931 /* We scan the symbol table looking for $tableentry$'s, and for
3932 each, try to deduce which handlers go with which entries. */
3934 RX_Table_Info stuff;
3938 stuff.mapfile = mapfile;
3939 bfd_hash_traverse (&(info->hash->table), rx_table_map, &stuff);
3943 #define ELF_ARCH bfd_arch_rx
3944 #define ELF_MACHINE_CODE EM_RX
3945 #define ELF_MAXPAGESIZE 0x1000
3947 #define TARGET_BIG_SYM rx_elf32_be_vec
3948 #define TARGET_BIG_NAME "elf32-rx-be"
3950 #define TARGET_LITTLE_SYM rx_elf32_le_vec
3951 #define TARGET_LITTLE_NAME "elf32-rx-le"
3953 #define elf_info_to_howto_rel NULL
3954 #define elf_info_to_howto rx_info_to_howto_rela
3955 #define elf_backend_object_p rx_elf_object_p
3956 #define elf_backend_relocate_section rx_elf_relocate_section
3957 #define elf_symbol_leading_char ('_')
3958 #define elf_backend_can_gc_sections 1
3959 #define elf_backend_modify_program_headers elf32_rx_modify_program_headers
3961 #define bfd_elf32_bfd_reloc_type_lookup rx_reloc_type_lookup
3962 #define bfd_elf32_bfd_reloc_name_lookup rx_reloc_name_lookup
3963 #define bfd_elf32_bfd_set_private_flags rx_elf_set_private_flags
3964 #define bfd_elf32_bfd_merge_private_bfd_data rx_elf_merge_private_bfd_data
3965 #define bfd_elf32_bfd_print_private_bfd_data rx_elf_print_private_bfd_data
3966 #define bfd_elf32_get_section_contents rx_get_section_contents
3967 #define bfd_elf32_set_section_contents rx_set_section_contents
3968 #define bfd_elf32_bfd_final_link rx_final_link
3969 #define bfd_elf32_bfd_relax_section elf32_rx_relax_section_wrapper
3970 #define elf_backend_special_sections elf32_rx_special_sections
3971 #define elf_backend_check_directives rx_check_directives
3973 #include "elf32-target.h"
3975 /* We define a second big-endian target that doesn't have the custom
3976 section get/set hooks, for times when we want to preserve the
3977 pre-swapped .text sections (like objcopy). */
3979 #undef TARGET_BIG_SYM
3980 #define TARGET_BIG_SYM rx_elf32_be_ns_vec
3981 #undef TARGET_BIG_NAME
3982 #define TARGET_BIG_NAME "elf32-rx-be-ns"
3983 #undef TARGET_LITTLE_SYM
3985 #undef bfd_elf32_get_section_contents
3986 #undef bfd_elf32_set_section_contents
3989 #define elf32_bed elf32_rx_be_ns_bed
3991 #include "elf32-target.h"