1 /* Renesas RX specific support for 32-bit ELF.
2 Copyright (C) 2008-2021 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
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
, 3, 0, 0, dont
, false),
53 RXREL (DIR32
, 2, 32, 0, signed, false),
54 RXREL (DIR24S
, 2, 24, 0, signed, false),
55 RXREL (DIR16
, 1, 16, 0, dont
, false),
56 RXREL (DIR16U
, 1, 16, 0, unsigned, false),
57 RXREL (DIR16S
, 1, 16, 0, signed, false),
58 RXREL (DIR8
, 0, 8, 0, dont
, false),
59 RXREL (DIR8U
, 0, 8, 0, unsigned, false),
60 RXREL (DIR8S
, 0, 8, 0, signed, false),
61 RXREL (DIR24S_PCREL
, 2, 24, 0, signed, true),
62 RXREL (DIR16S_PCREL
, 1, 16, 0, signed, true),
63 RXREL (DIR8S_PCREL
, 0, 8, 0, signed, true),
64 RXREL (DIR16UL
, 1, 16, 2, unsigned, false),
65 RXREL (DIR16UW
, 1, 16, 1, unsigned, false),
66 RXREL (DIR8UL
, 0, 8, 2, unsigned, false),
67 RXREL (DIR8UW
, 0, 8, 1, unsigned, false),
68 RXREL (DIR32_REV
, 1, 16, 0, dont
, false),
69 RXREL (DIR16_REV
, 1, 16, 0, dont
, false),
70 RXREL (DIR3U_PCREL
, 0, 3, 0, dont
, true),
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
,
305 Elf_Internal_Rela
* dst
)
309 r_type
= ELF32_R_TYPE (dst
->r_info
);
310 if (r_type
>= (unsigned int) R_RX_max
)
312 /* xgettext:c-format */
313 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
315 bfd_set_error (bfd_error_bad_value
);
318 cache_ptr
->howto
= rx_elf_howto_table
+ r_type
;
319 if (cache_ptr
->howto
->name
== NULL
)
321 /* xgettext:c-format */
322 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
324 bfd_set_error (bfd_error_bad_value
);
331 get_symbol_value (const char * name
,
332 struct bfd_link_info
* info
,
334 asection
* input_section
,
338 struct bfd_link_hash_entry
* h
;
340 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
343 || (h
->type
!= bfd_link_hash_defined
344 && h
->type
!= bfd_link_hash_defweak
))
345 (*info
->callbacks
->undefined_symbol
)
346 (info
, name
, input_bfd
, input_section
, offset
, true);
348 value
= (h
->u
.def
.value
349 + h
->u
.def
.section
->output_section
->vma
350 + h
->u
.def
.section
->output_offset
);
356 get_symbol_value_maybe (const char * name
,
357 struct bfd_link_info
* info
)
360 struct bfd_link_hash_entry
* h
;
362 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
365 || (h
->type
!= bfd_link_hash_defined
366 && h
->type
!= bfd_link_hash_defweak
))
369 value
= (h
->u
.def
.value
370 + h
->u
.def
.section
->output_section
->vma
371 + h
->u
.def
.section
->output_offset
);
377 get_gp (struct bfd_link_info
* info
,
382 static bool cached
= false;
383 static bfd_vma cached_value
= 0;
387 cached_value
= get_symbol_value ("__gp", info
, abfd
, sec
, offset
);
394 get_romstart (struct bfd_link_info
* info
,
399 static bool cached
= false;
400 static bfd_vma cached_value
= 0;
404 cached_value
= get_symbol_value ("_start", info
, abfd
, sec
, offset
);
411 get_ramstart (struct bfd_link_info
* info
,
416 static bool cached
= false;
417 static bfd_vma cached_value
= 0;
421 cached_value
= get_symbol_value ("__datastart", info
, abfd
, sec
, offset
);
427 #define NUM_STACK_ENTRIES 16
428 static int32_t rx_stack
[ NUM_STACK_ENTRIES
];
429 static unsigned int rx_stack_top
;
431 #define RX_STACK_PUSH(val) \
434 if (rx_stack_top < NUM_STACK_ENTRIES) \
435 rx_stack [rx_stack_top ++] = (val); \
437 r = bfd_reloc_dangerous; \
441 #define RX_STACK_POP(dest) \
444 if (rx_stack_top > 0) \
445 (dest) = rx_stack [-- rx_stack_top]; \
447 (dest) = 0, r = bfd_reloc_dangerous; \
451 /* Relocate an RX ELF section.
452 There is some attempt to make this function usable for many architectures,
453 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
454 if only to serve as a learning tool.
456 The RELOCATE_SECTION function is called by the new ELF backend linker
457 to handle the relocations for a section.
459 The relocs are always passed as Rela structures; if the section
460 actually uses Rel structures, the r_addend field will always be
463 This function is responsible for adjusting the section contents as
464 necessary, and (if using Rela relocs and generating a relocatable
465 output file) adjusting the reloc addend as necessary.
467 This function does not have to worry about setting the reloc
468 address or the reloc symbol index.
470 LOCAL_SYMS is a pointer to the swapped in local symbols.
472 LOCAL_SECTIONS is an array giving the section in the input file
473 corresponding to the st_shndx field of each local symbol.
475 The global hash table entry for the global symbols can be found
476 via elf_sym_hashes (input_bfd).
478 When generating relocatable output, this function must handle
479 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
480 going to be the section symbol corresponding to the output
481 section, which means that the addend must be adjusted
485 rx_elf_relocate_section
487 struct bfd_link_info
* info
,
489 asection
* input_section
,
491 Elf_Internal_Rela
* relocs
,
492 Elf_Internal_Sym
* local_syms
,
493 asection
** local_sections
)
495 Elf_Internal_Shdr
*symtab_hdr
;
496 struct elf_link_hash_entry
**sym_hashes
;
497 Elf_Internal_Rela
*rel
;
498 Elf_Internal_Rela
*relend
;
500 bool saw_subtract
= false;
501 const char *table_default_cache
= NULL
;
502 bfd_vma table_start_cache
= 0;
503 bfd_vma table_end_cache
= 0;
505 if (elf_elfheader (output_bfd
)->e_flags
& E_FLAG_RX_PID
)
510 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
511 sym_hashes
= elf_sym_hashes (input_bfd
);
512 relend
= relocs
+ input_section
->reloc_count
;
513 for (rel
= relocs
; rel
< relend
; rel
++)
515 reloc_howto_type
*howto
;
516 unsigned long r_symndx
;
517 Elf_Internal_Sym
*sym
;
519 struct elf_link_hash_entry
*h
;
521 bfd_reloc_status_type r
;
522 const char * name
= NULL
;
523 bool unresolved_reloc
= true;
526 r_type
= ELF32_R_TYPE (rel
->r_info
);
527 r_symndx
= ELF32_R_SYM (rel
->r_info
);
529 howto
= rx_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
535 if (rx_stack_top
== 0)
536 saw_subtract
= false;
538 if (r_symndx
< symtab_hdr
->sh_info
)
540 sym
= local_syms
+ r_symndx
;
541 sec
= local_sections
[r_symndx
];
542 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, & sec
, rel
);
544 name
= bfd_elf_string_from_elf_section
545 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
546 name
= sym
->st_name
== 0 ? bfd_section_name (sec
) : name
;
550 bool warned
, ignored
;
552 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
553 r_symndx
, symtab_hdr
, sym_hashes
, h
,
554 sec
, relocation
, unresolved_reloc
,
557 name
= h
->root
.root
.string
;
560 if (startswith (name
, "$tableentry$default$"))
566 if (table_default_cache
!= name
)
569 /* All relocs for a given table should be to the same
570 (weak) default symbol) so we can use it to detect a
571 cache miss. We use the offset into the table to find
572 the "real" symbol. Calculate and store the table's
575 table_default_cache
= name
;
577 /* We have already done error checking in rx_table_find(). */
579 buf
= (char *) bfd_malloc (13 + strlen (name
+ 20));
583 sprintf (buf
, "$tablestart$%s", name
+ 20);
584 table_start_cache
= get_symbol_value (buf
,
590 sprintf (buf
, "$tableend$%s", name
+ 20);
591 table_end_cache
= get_symbol_value (buf
,
600 entry_vma
= (input_section
->output_section
->vma
601 + input_section
->output_offset
604 if (table_end_cache
<= entry_vma
|| entry_vma
< table_start_cache
)
606 /* xgettext:c-format */
607 _bfd_error_handler (_("%pB:%pA: table entry %s outside table"),
608 input_bfd
, input_section
,
611 else if ((int) (entry_vma
- table_start_cache
) % 4)
613 /* xgettext:c-format */
614 _bfd_error_handler (_("%pB:%pA: table entry %s not word-aligned within table"),
615 input_bfd
, input_section
,
620 idx
= (int) (entry_vma
- table_start_cache
) / 4;
622 /* This will look like $tableentry$<N>$<name> */
623 buf
= (char *) bfd_malloc (12 + 20 + strlen (name
+ 20));
627 sprintf (buf
, "$tableentry$%d$%s", idx
, name
+ 20);
629 h
= (struct elf_link_hash_entry
*) bfd_link_hash_lookup (info
->hash
, buf
, false, false, true);
633 relocation
= (h
->root
.u
.def
.value
634 + h
->root
.u
.def
.section
->output_section
->vma
635 + h
->root
.u
.def
.section
->output_offset
);;
642 if (sec
!= NULL
&& discarded_section (sec
))
643 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
644 rel
, 1, relend
, howto
, 0, contents
);
646 if (bfd_link_relocatable (info
))
648 /* This is a relocatable link. We don't have to change
649 anything, unless the reloc is against a section symbol,
650 in which case we have to adjust according to where the
651 section symbol winds up in the output section. */
652 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
653 rel
->r_addend
+= sec
->output_offset
;
657 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
658 /* If the symbol is undefined and weak
659 then the relocation resolves to zero. */
663 if (howto
->pc_relative
)
665 relocation
-= (input_section
->output_section
->vma
666 + input_section
->output_offset
668 if (r_type
!= R_RX_RH_3_PCREL
669 && r_type
!= R_RX_DIR3U_PCREL
)
673 relocation
+= rel
->r_addend
;
679 if (a > (long) relocation || (long) relocation > b) \
680 r = bfd_reloc_overflow
682 if (relocation & m) \
685 (contents[rel->r_offset + (i)])
686 #define WARN_REDHAT(type) \
687 /* xgettext:c-format */ \
689 (_("%pB:%pA: warning: deprecated Red Hat reloc " \
690 "%s detected against: %s"), \
691 input_bfd, input_section, #type, name)
693 /* Check for unsafe relocs in PID mode. These are any relocs where
694 an absolute address is being computed. There are special cases
695 for relocs against symbols that are known to be referenced in
696 crt0.o before the PID base address register has been initialised. */
697 #define UNSAFE_FOR_PID \
702 && sec->flags & SEC_READONLY \
703 && !(input_section->flags & SEC_DEBUGGING) \
704 && strcmp (name, "__pid_base") != 0 \
705 && strcmp (name, "__gp") != 0 \
706 && strcmp (name, "__romdatastart") != 0 \
708 /* xgettext:c-format */ \
709 _bfd_error_handler (_("%pB(%pA): unsafe PID relocation %s " \
710 "at %#" PRIx64 " (against %s in %s)"), \
711 input_bfd, input_section, howto->name, \
712 (uint64_t) (input_section->output_section->vma \
713 + input_section->output_offset \
719 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
728 case R_RX_RH_3_PCREL
:
729 WARN_REDHAT ("RX_RH_3_PCREL");
732 OP (0) |= relocation
& 0x07;
736 WARN_REDHAT ("RX_RH_8_NEG");
737 relocation
= - relocation
;
739 case R_RX_DIR8S_PCREL
:
758 WARN_REDHAT ("RX_RH_16_NEG");
759 relocation
= - relocation
;
761 case R_RX_DIR16S_PCREL
:
763 RANGE (-32768, 32767);
764 #if RX_OPCODE_BIG_ENDIAN
767 OP (1) = relocation
>> 8;
772 WARN_REDHAT ("RX_RH_16_OP");
774 RANGE (-32768, 32767);
775 #if RX_OPCODE_BIG_ENDIAN
777 OP (0) = relocation
>> 8;
780 OP (1) = relocation
>> 8;
786 RANGE (-32768, 65535);
787 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
790 OP (0) = relocation
>> 8;
795 OP (1) = relocation
>> 8;
802 #if RX_OPCODE_BIG_ENDIAN
804 OP (0) = relocation
>> 8;
807 OP (1) = relocation
>> 8;
813 RANGE (-32768, 65536);
814 #if RX_OPCODE_BIG_ENDIAN
816 OP (0) = relocation
>> 8;
819 OP (1) = relocation
>> 8;
825 RANGE (-32768, 65536);
826 #if RX_OPCODE_BIG_ENDIAN
828 OP (1) = relocation
>> 8;
831 OP (0) = relocation
>> 8;
835 case R_RX_DIR3U_PCREL
:
838 OP (0) |= relocation
& 0x07;
843 WARN_REDHAT ("RX_RH_24_NEG");
844 relocation
= - relocation
;
846 case R_RX_DIR24S_PCREL
:
847 RANGE (-0x800000, 0x7fffff);
848 #if RX_OPCODE_BIG_ENDIAN
850 OP (1) = relocation
>> 8;
851 OP (0) = relocation
>> 16;
854 OP (1) = relocation
>> 8;
855 OP (2) = relocation
>> 16;
861 WARN_REDHAT ("RX_RH_24_OP");
862 RANGE (-0x800000, 0x7fffff);
863 #if RX_OPCODE_BIG_ENDIAN
865 OP (1) = relocation
>> 8;
866 OP (0) = relocation
>> 16;
869 OP (1) = relocation
>> 8;
870 OP (2) = relocation
>> 16;
876 RANGE (-0x800000, 0x7fffff);
877 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
880 OP (1) = relocation
>> 8;
881 OP (0) = relocation
>> 16;
886 OP (1) = relocation
>> 8;
887 OP (2) = relocation
>> 16;
893 WARN_REDHAT ("RX_RH_24_UNS");
895 #if RX_OPCODE_BIG_ENDIAN
897 OP (1) = relocation
>> 8;
898 OP (0) = relocation
>> 16;
901 OP (1) = relocation
>> 8;
902 OP (2) = relocation
>> 16;
908 WARN_REDHAT ("RX_RH_32_NEG");
909 relocation
= - relocation
;
910 #if RX_OPCODE_BIG_ENDIAN
912 OP (2) = relocation
>> 8;
913 OP (1) = relocation
>> 16;
914 OP (0) = relocation
>> 24;
917 OP (1) = relocation
>> 8;
918 OP (2) = relocation
>> 16;
919 OP (3) = relocation
>> 24;
925 WARN_REDHAT ("RX_RH_32_OP");
926 #if RX_OPCODE_BIG_ENDIAN
928 OP (2) = relocation
>> 8;
929 OP (1) = relocation
>> 16;
930 OP (0) = relocation
>> 24;
933 OP (1) = relocation
>> 8;
934 OP (2) = relocation
>> 16;
935 OP (3) = relocation
>> 24;
940 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
943 OP (2) = relocation
>> 8;
944 OP (1) = relocation
>> 16;
945 OP (0) = relocation
>> 24;
950 OP (1) = relocation
>> 8;
951 OP (2) = relocation
>> 16;
952 OP (3) = relocation
>> 24;
957 if (BIGE (output_bfd
))
960 OP (1) = relocation
>> 8;
961 OP (2) = relocation
>> 16;
962 OP (3) = relocation
>> 24;
967 OP (2) = relocation
>> 8;
968 OP (1) = relocation
>> 16;
969 OP (0) = relocation
>> 24;
976 WARN_REDHAT ("RX_RH_DIFF");
977 val
= bfd_get_32 (output_bfd
, & OP (0));
979 bfd_put_32 (output_bfd
, val
, & OP (0));
984 WARN_REDHAT ("RX_RH_GPRELB");
985 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
987 #if RX_OPCODE_BIG_ENDIAN
989 OP (0) = relocation
>> 8;
992 OP (1) = relocation
>> 8;
997 WARN_REDHAT ("RX_RH_GPRELW");
998 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
1002 #if RX_OPCODE_BIG_ENDIAN
1003 OP (1) = relocation
;
1004 OP (0) = relocation
>> 8;
1006 OP (0) = relocation
;
1007 OP (1) = relocation
>> 8;
1011 case R_RX_RH_GPRELL
:
1012 WARN_REDHAT ("RX_RH_GPRELL");
1013 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
1017 #if RX_OPCODE_BIG_ENDIAN
1018 OP (1) = relocation
;
1019 OP (0) = relocation
>> 8;
1021 OP (0) = relocation
;
1022 OP (1) = relocation
>> 8;
1026 /* Internal relocations just for relaxation: */
1027 case R_RX_RH_ABS5p5B
:
1028 RX_STACK_POP (relocation
);
1031 OP (0) |= relocation
>> 2;
1033 OP (1) |= (relocation
<< 6) & 0x80;
1034 OP (1) |= (relocation
<< 3) & 0x08;
1037 case R_RX_RH_ABS5p5W
:
1038 RX_STACK_POP (relocation
);
1043 OP (0) |= relocation
>> 2;
1045 OP (1) |= (relocation
<< 6) & 0x80;
1046 OP (1) |= (relocation
<< 3) & 0x08;
1049 case R_RX_RH_ABS5p5L
:
1050 RX_STACK_POP (relocation
);
1055 OP (0) |= relocation
>> 2;
1057 OP (1) |= (relocation
<< 6) & 0x80;
1058 OP (1) |= (relocation
<< 3) & 0x08;
1061 case R_RX_RH_ABS5p8B
:
1062 RX_STACK_POP (relocation
);
1065 OP (0) |= (relocation
<< 3) & 0x80;
1066 OP (0) |= relocation
& 0x0f;
1069 case R_RX_RH_ABS5p8W
:
1070 RX_STACK_POP (relocation
);
1075 OP (0) |= (relocation
<< 3) & 0x80;
1076 OP (0) |= relocation
& 0x0f;
1079 case R_RX_RH_ABS5p8L
:
1080 RX_STACK_POP (relocation
);
1085 OP (0) |= (relocation
<< 3) & 0x80;
1086 OP (0) |= relocation
& 0x0f;
1089 case R_RX_RH_UIMM4p8
:
1092 OP (0) |= relocation
<< 4;
1095 case R_RX_RH_UNEG4p8
:
1098 OP (0) |= (-relocation
) << 4;
1101 /* Complex reloc handling: */
1105 RX_STACK_POP (relocation
);
1106 #if RX_OPCODE_BIG_ENDIAN
1107 OP (3) = relocation
;
1108 OP (2) = relocation
>> 8;
1109 OP (1) = relocation
>> 16;
1110 OP (0) = relocation
>> 24;
1112 OP (0) = relocation
;
1113 OP (1) = relocation
>> 8;
1114 OP (2) = relocation
>> 16;
1115 OP (3) = relocation
>> 24;
1119 case R_RX_ABS32_REV
:
1121 RX_STACK_POP (relocation
);
1122 #if RX_OPCODE_BIG_ENDIAN
1123 OP (0) = relocation
;
1124 OP (1) = relocation
>> 8;
1125 OP (2) = relocation
>> 16;
1126 OP (3) = relocation
>> 24;
1128 OP (3) = relocation
;
1129 OP (2) = relocation
>> 8;
1130 OP (1) = relocation
>> 16;
1131 OP (0) = relocation
>> 24;
1135 case R_RX_ABS24S_PCREL
:
1138 RX_STACK_POP (relocation
);
1139 RANGE (-0x800000, 0x7fffff);
1140 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
1142 OP (2) = relocation
;
1143 OP (1) = relocation
>> 8;
1144 OP (0) = relocation
>> 16;
1148 OP (0) = relocation
;
1149 OP (1) = relocation
>> 8;
1150 OP (2) = relocation
>> 16;
1156 RX_STACK_POP (relocation
);
1157 RANGE (-32768, 65535);
1158 #if RX_OPCODE_BIG_ENDIAN
1159 OP (1) = relocation
;
1160 OP (0) = relocation
>> 8;
1162 OP (0) = relocation
;
1163 OP (1) = relocation
>> 8;
1167 case R_RX_ABS16_REV
:
1169 RX_STACK_POP (relocation
);
1170 RANGE (-32768, 65535);
1171 #if RX_OPCODE_BIG_ENDIAN
1172 OP (0) = relocation
;
1173 OP (1) = relocation
>> 8;
1175 OP (1) = relocation
;
1176 OP (0) = relocation
>> 8;
1180 case R_RX_ABS16S_PCREL
:
1182 RX_STACK_POP (relocation
);
1183 RANGE (-32768, 32767);
1184 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
1186 OP (1) = relocation
;
1187 OP (0) = relocation
>> 8;
1191 OP (0) = relocation
;
1192 OP (1) = relocation
>> 8;
1198 RX_STACK_POP (relocation
);
1200 #if RX_OPCODE_BIG_ENDIAN
1201 OP (1) = relocation
;
1202 OP (0) = relocation
>> 8;
1204 OP (0) = relocation
;
1205 OP (1) = relocation
>> 8;
1211 RX_STACK_POP (relocation
);
1214 #if RX_OPCODE_BIG_ENDIAN
1215 OP (1) = relocation
;
1216 OP (0) = relocation
>> 8;
1218 OP (0) = relocation
;
1219 OP (1) = relocation
>> 8;
1225 RX_STACK_POP (relocation
);
1228 #if RX_OPCODE_BIG_ENDIAN
1229 OP (1) = relocation
;
1230 OP (0) = relocation
>> 8;
1232 OP (0) = relocation
;
1233 OP (1) = relocation
>> 8;
1239 RX_STACK_POP (relocation
);
1241 OP (0) = relocation
;
1246 RX_STACK_POP (relocation
);
1248 OP (0) = relocation
;
1253 RX_STACK_POP (relocation
);
1256 OP (0) = relocation
;
1261 RX_STACK_POP (relocation
);
1264 OP (0) = relocation
;
1270 case R_RX_ABS8S_PCREL
:
1271 RX_STACK_POP (relocation
);
1273 OP (0) = relocation
;
1277 if (r_symndx
< symtab_hdr
->sh_info
)
1278 RX_STACK_PUSH (sec
->output_section
->vma
1279 + sec
->output_offset
1285 && (h
->root
.type
== bfd_link_hash_defined
1286 || h
->root
.type
== bfd_link_hash_defweak
))
1287 RX_STACK_PUSH (h
->root
.u
.def
.value
1288 + sec
->output_section
->vma
1289 + sec
->output_offset
1293 (_("warning: RX_SYM reloc with an unknown symbol"));
1301 saw_subtract
= true;
1304 RX_STACK_PUSH (tmp
);
1312 RX_STACK_POP (tmp1
);
1313 RX_STACK_POP (tmp2
);
1315 RX_STACK_PUSH (tmp1
);
1323 saw_subtract
= true;
1324 RX_STACK_POP (tmp1
);
1325 RX_STACK_POP (tmp2
);
1327 RX_STACK_PUSH (tmp2
);
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
);
1357 RX_STACK_POP (tmp1
);
1358 RX_STACK_POP (tmp2
);
1360 RX_STACK_PUSH (tmp1
);
1368 RX_STACK_POP (tmp1
);
1369 RX_STACK_POP (tmp2
);
1371 RX_STACK_PUSH (tmp1
);
1375 case R_RX_OPsctsize
:
1376 RX_STACK_PUSH (input_section
->size
);
1380 RX_STACK_PUSH (input_section
->output_section
->vma
);
1387 RX_STACK_POP (tmp1
);
1388 RX_STACK_POP (tmp2
);
1390 RX_STACK_PUSH (tmp1
);
1398 RX_STACK_POP (tmp1
);
1399 RX_STACK_POP (tmp2
);
1401 RX_STACK_PUSH (tmp1
);
1409 RX_STACK_POP (tmp1
);
1410 RX_STACK_POP (tmp2
);
1412 RX_STACK_PUSH (tmp1
);
1422 RX_STACK_PUSH (tmp
);
1430 RX_STACK_POP (tmp1
);
1431 RX_STACK_POP (tmp2
);
1433 RX_STACK_PUSH (tmp1
);
1438 RX_STACK_PUSH (get_romstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1442 RX_STACK_PUSH (get_ramstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1446 r
= bfd_reloc_notsupported
;
1450 if (r
!= bfd_reloc_ok
)
1452 const char * msg
= NULL
;
1456 case bfd_reloc_overflow
:
1457 /* Catch the case of a missing function declaration
1458 and emit a more helpful error message. */
1459 if (r_type
== R_RX_DIR24S_PCREL
)
1460 /* xgettext:c-format */
1461 msg
= _("%pB(%pA): error: call to undefined function '%s'");
1463 (*info
->callbacks
->reloc_overflow
)
1464 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
1465 input_bfd
, input_section
, rel
->r_offset
);
1468 case bfd_reloc_undefined
:
1469 (*info
->callbacks
->undefined_symbol
)
1470 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, true);
1473 case bfd_reloc_other
:
1474 /* xgettext:c-format */
1475 msg
= _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
1478 case bfd_reloc_outofrange
:
1479 /* xgettext:c-format */
1480 msg
= _("%pB(%pA): internal error: out of range error");
1483 case bfd_reloc_notsupported
:
1484 /* xgettext:c-format */
1485 msg
= _("%pB(%pA): internal error: unsupported relocation error");
1488 case bfd_reloc_dangerous
:
1489 /* xgettext:c-format */
1490 msg
= _("%pB(%pA): internal error: dangerous relocation");
1494 /* xgettext:c-format */
1495 msg
= _("%pB(%pA): internal error: unknown error");
1500 _bfd_error_handler (msg
, input_bfd
, input_section
, name
);
1507 /* Relaxation Support. */
1509 /* Progression of relocations from largest operand size to smallest
1513 next_smaller_reloc (int r
)
1517 case R_RX_DIR32
: return R_RX_DIR24S
;
1518 case R_RX_DIR24S
: return R_RX_DIR16S
;
1519 case R_RX_DIR16S
: return R_RX_DIR8S
;
1520 case R_RX_DIR8S
: return R_RX_NONE
;
1522 case R_RX_DIR16
: return R_RX_DIR8
;
1523 case R_RX_DIR8
: return R_RX_NONE
;
1525 case R_RX_DIR16U
: return R_RX_DIR8U
;
1526 case R_RX_DIR8U
: return R_RX_NONE
;
1528 case R_RX_DIR24S_PCREL
: return R_RX_DIR16S_PCREL
;
1529 case R_RX_DIR16S_PCREL
: return R_RX_DIR8S_PCREL
;
1530 case R_RX_DIR8S_PCREL
: return R_RX_DIR3U_PCREL
;
1532 case R_RX_DIR16UL
: return R_RX_DIR8UL
;
1533 case R_RX_DIR8UL
: return R_RX_NONE
;
1534 case R_RX_DIR16UW
: return R_RX_DIR8UW
;
1535 case R_RX_DIR8UW
: return R_RX_NONE
;
1537 case R_RX_RH_32_OP
: return R_RX_RH_24_OP
;
1538 case R_RX_RH_24_OP
: return R_RX_RH_16_OP
;
1539 case R_RX_RH_16_OP
: return R_RX_DIR8
;
1541 case R_RX_ABS32
: return R_RX_ABS24S
;
1542 case R_RX_ABS24S
: return R_RX_ABS16S
;
1543 case R_RX_ABS16
: return R_RX_ABS8
;
1544 case R_RX_ABS16U
: return R_RX_ABS8U
;
1545 case R_RX_ABS16S
: return R_RX_ABS8S
;
1546 case R_RX_ABS8
: return R_RX_NONE
;
1547 case R_RX_ABS8U
: return R_RX_NONE
;
1548 case R_RX_ABS8S
: return R_RX_NONE
;
1549 case R_RX_ABS24S_PCREL
: return R_RX_ABS16S_PCREL
;
1550 case R_RX_ABS16S_PCREL
: return R_RX_ABS8S_PCREL
;
1551 case R_RX_ABS8S_PCREL
: return R_RX_NONE
;
1552 case R_RX_ABS16UL
: return R_RX_ABS8UL
;
1553 case R_RX_ABS16UW
: return R_RX_ABS8UW
;
1554 case R_RX_ABS8UL
: return R_RX_NONE
;
1555 case R_RX_ABS8UW
: return R_RX_NONE
;
1560 /* Delete some bytes from a section while relaxing. */
1563 elf32_rx_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, int count
,
1564 Elf_Internal_Rela
*alignment_rel
, int force_snip
,
1565 Elf_Internal_Rela
*irelstart
)
1567 Elf_Internal_Shdr
* symtab_hdr
;
1568 unsigned int sec_shndx
;
1569 bfd_byte
* contents
;
1570 Elf_Internal_Rela
* irel
;
1571 Elf_Internal_Rela
* irelend
;
1572 Elf_Internal_Sym
* isym
;
1573 Elf_Internal_Sym
* isymend
;
1575 unsigned int symcount
;
1576 struct elf_link_hash_entry
** sym_hashes
;
1577 struct elf_link_hash_entry
** end_hashes
;
1582 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1584 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1586 /* The deletion must stop at the next alignment boundary, if
1587 ALIGNMENT_REL is non-NULL. */
1590 toaddr
= alignment_rel
->r_offset
;
1592 BFD_ASSERT (toaddr
> addr
);
1594 /* Actually delete the bytes. */
1595 memmove (contents
+ addr
, contents
+ addr
+ count
,
1596 (size_t) (toaddr
- addr
- count
));
1598 /* If we don't have an alignment marker to worry about, we can just
1599 shrink the section. Otherwise, we have to fill in the newly
1600 created gap with NOP insns (0x03). */
1604 memset (contents
+ toaddr
- count
, 0x03, count
);
1607 BFD_ASSERT (irel
!= NULL
|| sec
->reloc_count
== 0);
1608 irelend
= irel
+ sec
->reloc_count
;
1610 /* Adjust all the relocs. */
1611 for (; irel
< irelend
; irel
++)
1613 /* Get the new reloc address. */
1614 if (irel
->r_offset
> addr
1615 && (irel
->r_offset
< toaddr
1616 || (force_snip
&& irel
->r_offset
== toaddr
)))
1617 irel
->r_offset
-= count
;
1619 /* If we see an ALIGN marker at the end of the gap, we move it
1620 to the beginning of the gap, since marking these gaps is what
1622 if (irel
->r_offset
== toaddr
1623 && ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
1624 && irel
->r_addend
& RX_RELAXA_ALIGN
)
1625 irel
->r_offset
-= count
;
1628 /* Adjust the local symbols defined in this section. */
1629 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1630 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1631 isymend
= isym
+ symtab_hdr
->sh_info
;
1633 for (; isym
< isymend
; isym
++)
1635 /* If the symbol is in the range of memory we just moved, we
1636 have to adjust its value. */
1637 if (isym
->st_shndx
== sec_shndx
1638 && isym
->st_value
> addr
1639 && isym
->st_value
< toaddr
)
1640 isym
->st_value
-= count
;
1642 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1643 *end* is in the moved bytes but it's *start* isn't), then we
1644 must adjust its size. */
1645 if (isym
->st_shndx
== sec_shndx
1646 && isym
->st_value
< addr
1647 && isym
->st_value
+ isym
->st_size
> addr
1648 && isym
->st_value
+ isym
->st_size
< toaddr
)
1649 isym
->st_size
-= count
;
1652 /* Now adjust the global symbols defined in this section. */
1653 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1654 - symtab_hdr
->sh_info
);
1655 sym_hashes
= elf_sym_hashes (abfd
);
1656 end_hashes
= sym_hashes
+ symcount
;
1658 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1660 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1662 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1663 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1664 && sym_hash
->root
.u
.def
.section
== sec
)
1666 /* As above, adjust the value if needed. */
1667 if (sym_hash
->root
.u
.def
.value
> addr
1668 && sym_hash
->root
.u
.def
.value
< toaddr
)
1669 sym_hash
->root
.u
.def
.value
-= count
;
1671 /* As above, adjust the size if needed. */
1672 if (sym_hash
->root
.u
.def
.value
< addr
1673 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
1674 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
1675 sym_hash
->size
-= count
;
1682 /* Used to sort relocs by address. If relocs have the same address,
1683 we maintain their relative order, except that R_RX_RH_RELAX
1684 alignment relocs must be the first reloc for any given address. */
1687 reloc_bubblesort (Elf_Internal_Rela
* r
, int count
)
1693 /* This is almost a classic bubblesort. It's the slowest sort, but
1694 we're taking advantage of the fact that the relocations are
1695 mostly in order already (the assembler emits them that way) and
1696 we need relocs with the same address to remain in the same
1702 for (i
= 0; i
< count
- 1; i
++)
1704 if (r
[i
].r_offset
> r
[i
+ 1].r_offset
)
1706 else if (r
[i
].r_offset
< r
[i
+ 1].r_offset
)
1708 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1709 && (r
[i
+ 1].r_addend
& RX_RELAXA_ALIGN
))
1711 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1712 && (r
[i
+ 1].r_addend
& RX_RELAXA_ELIGN
)
1713 && !(ELF32_R_TYPE (r
[i
].r_info
) == R_RX_RH_RELAX
1714 && (r
[i
].r_addend
& RX_RELAXA_ALIGN
)))
1721 Elf_Internal_Rela tmp
;
1726 /* If we do move a reloc back, re-scan to see if it
1727 needs to be moved even further back. This avoids
1728 most of the O(n^2) behavior for our cases. */
1738 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1739 rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1740 lrel, abfd, sec, link_info, scale)
1743 rx_offset_for_reloc (bfd
* abfd
,
1744 Elf_Internal_Rela
* rel
,
1745 Elf_Internal_Shdr
* symtab_hdr
,
1746 bfd_byte
* shndx_buf ATTRIBUTE_UNUSED
,
1747 Elf_Internal_Sym
* intsyms
,
1748 Elf_Internal_Rela
** lrel
,
1750 asection
* input_section
,
1751 struct bfd_link_info
* info
,
1755 bfd_reloc_status_type r
;
1759 /* REL is the first of 1..N relocations. We compute the symbol
1760 value for each relocation, then combine them if needed. LREL
1761 gets a pointer to the last relocation used. */
1766 /* Get the value of the symbol referred to by the reloc. */
1767 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1769 /* A local symbol. */
1770 Elf_Internal_Sym
*isym
;
1773 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1775 if (isym
->st_shndx
== SHN_UNDEF
)
1776 ssec
= bfd_und_section_ptr
;
1777 else if (isym
->st_shndx
== SHN_ABS
)
1778 ssec
= bfd_abs_section_ptr
;
1779 else if (isym
->st_shndx
== SHN_COMMON
)
1780 ssec
= bfd_com_section_ptr
;
1782 ssec
= bfd_section_from_elf_index (abfd
,
1785 /* Initial symbol value. */
1786 symval
= isym
->st_value
;
1788 /* GAS may have made this symbol relative to a section, in
1789 which case, we have to add the addend to find the
1791 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
1792 symval
+= rel
->r_addend
;
1796 if ((ssec
->flags
& SEC_MERGE
)
1797 && ssec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
1798 symval
= _bfd_merged_section_offset (abfd
, & ssec
,
1799 elf_section_data (ssec
)->sec_info
,
1803 /* Now make the offset relative to where the linker is putting it. */
1806 ssec
->output_section
->vma
+ ssec
->output_offset
;
1808 symval
+= rel
->r_addend
;
1813 struct elf_link_hash_entry
* h
;
1815 /* An external symbol. */
1816 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1817 h
= elf_sym_hashes (abfd
)[indx
];
1818 BFD_ASSERT (h
!= NULL
);
1820 if (h
->root
.type
!= bfd_link_hash_defined
1821 && h
->root
.type
!= bfd_link_hash_defweak
)
1823 /* This appears to be a reference to an undefined
1824 symbol. Just ignore it--it will be caught by the
1825 regular reloc processing. */
1831 symval
= (h
->root
.u
.def
.value
1832 + h
->root
.u
.def
.section
->output_section
->vma
1833 + h
->root
.u
.def
.section
->output_offset
);
1835 symval
+= rel
->r_addend
;
1838 switch (ELF32_R_TYPE (rel
->r_info
))
1841 RX_STACK_PUSH (symval
);
1845 RX_STACK_POP (tmp1
);
1847 RX_STACK_PUSH (tmp1
);
1851 RX_STACK_POP (tmp1
);
1852 RX_STACK_POP (tmp2
);
1854 RX_STACK_PUSH (tmp1
);
1858 RX_STACK_POP (tmp1
);
1859 RX_STACK_POP (tmp2
);
1861 RX_STACK_PUSH (tmp2
);
1865 RX_STACK_POP (tmp1
);
1866 RX_STACK_POP (tmp2
);
1868 RX_STACK_PUSH (tmp1
);
1872 RX_STACK_POP (tmp1
);
1873 RX_STACK_POP (tmp2
);
1875 RX_STACK_PUSH (tmp1
);
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
);
1892 case R_RX_OPsctsize
:
1893 RX_STACK_PUSH (input_section
->size
);
1897 RX_STACK_PUSH (input_section
->output_section
->vma
);
1901 RX_STACK_POP (tmp1
);
1902 RX_STACK_POP (tmp2
);
1904 RX_STACK_PUSH (tmp1
);
1908 RX_STACK_POP (tmp1
);
1909 RX_STACK_POP (tmp2
);
1911 RX_STACK_PUSH (tmp1
);
1915 RX_STACK_POP (tmp1
);
1916 RX_STACK_POP (tmp2
);
1918 RX_STACK_PUSH (tmp1
);
1922 RX_STACK_POP (tmp1
);
1924 RX_STACK_PUSH (tmp1
);
1928 RX_STACK_POP (tmp1
);
1929 RX_STACK_POP (tmp2
);
1931 RX_STACK_PUSH (tmp1
);
1935 RX_STACK_PUSH (get_romstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1939 RX_STACK_PUSH (get_ramstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1947 RX_STACK_POP (symval
);
1958 RX_STACK_POP (symval
);
1966 RX_STACK_POP (symval
);
1979 move_reloc (Elf_Internal_Rela
* irel
, Elf_Internal_Rela
* srel
, int delta
)
1981 bfd_vma old_offset
= srel
->r_offset
;
1984 while (irel
<= srel
)
1986 if (irel
->r_offset
== old_offset
)
1987 irel
->r_offset
+= delta
;
1992 /* Relax one section. */
1995 elf32_rx_relax_section (bfd
*abfd
,
1997 struct bfd_link_info
*link_info
,
2001 Elf_Internal_Shdr
*symtab_hdr
;
2002 Elf_Internal_Shdr
*shndx_hdr
;
2003 Elf_Internal_Rela
*internal_relocs
;
2004 Elf_Internal_Rela
*irel
;
2005 Elf_Internal_Rela
*srel
;
2006 Elf_Internal_Rela
*irelend
;
2007 Elf_Internal_Rela
*next_alignment
;
2008 Elf_Internal_Rela
*prev_alignment
;
2009 bfd_byte
*contents
= NULL
;
2010 bfd_byte
*free_contents
= NULL
;
2011 Elf_Internal_Sym
*intsyms
= NULL
;
2012 Elf_Internal_Sym
*free_intsyms
= NULL
;
2013 bfd_byte
*shndx_buf
= NULL
;
2019 int section_alignment_glue
;
2020 /* how much to scale the relocation by - 1, 2, or 4. */
2023 /* Assume nothing changes. */
2026 /* We don't have to do anything for a relocatable link, if
2027 this section does not have relocs, or if this is not a
2029 if (bfd_link_relocatable (link_info
)
2030 || (sec
->flags
& SEC_RELOC
) == 0
2031 || sec
->reloc_count
== 0
2032 || (sec
->flags
& SEC_CODE
) == 0)
2035 symtab_hdr
= & elf_symtab_hdr (abfd
);
2036 if (elf_symtab_shndx_list (abfd
))
2037 shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
2041 sec_start
= sec
->output_section
->vma
+ sec
->output_offset
;
2043 /* Get the section contents. */
2044 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2045 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2046 /* Go get them off disk. */
2049 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2051 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2054 /* Read this BFD's symbols. */
2055 /* Get cached copy if it exists. */
2056 if (symtab_hdr
->contents
!= NULL
)
2057 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2060 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
2061 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
2064 if (shndx_hdr
&& shndx_hdr
->sh_size
!= 0)
2068 if (_bfd_mul_overflow (symtab_hdr
->sh_info
,
2069 sizeof (Elf_External_Sym_Shndx
), &amt
))
2071 bfd_set_error (bfd_error_file_too_big
);
2074 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0)
2076 shndx_buf
= _bfd_malloc_and_read (abfd
, amt
, amt
);
2077 if (shndx_buf
== NULL
)
2079 shndx_hdr
->contents
= shndx_buf
;
2082 /* Get a copy of the native relocations. */
2083 /* Note - we ignore the setting of link_info->keep_memory when reading
2084 in these relocs. We have to maintain a permanent copy of the relocs
2085 because we are going to walk over them multiple times, adjusting them
2086 as bytes are deleted from the section, and with this relaxation
2087 function itself being called multiple times on the same section... */
2088 internal_relocs
= _bfd_elf_link_read_relocs
2089 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true);
2090 if (internal_relocs
== NULL
)
2093 /* The RL_ relocs must be just before the operand relocs they go
2094 with, so we must sort them to guarantee this. We use bubblesort
2095 instead of qsort so we can guarantee that relocs with the same
2096 address remain in the same relative order. */
2097 reloc_bubblesort (internal_relocs
, sec
->reloc_count
);
2099 /* Walk through them looking for relaxing opportunities. */
2100 irelend
= internal_relocs
+ sec
->reloc_count
;
2102 /* This will either be NULL or a pointer to the next alignment
2104 next_alignment
= internal_relocs
;
2105 /* This will be the previous alignment, although at first it points
2106 to the first real relocation. */
2107 prev_alignment
= internal_relocs
;
2109 /* We calculate worst case shrinkage caused by alignment directives.
2110 No fool-proof, but better than either ignoring the problem or
2111 doing heavy duty analysis of all the alignment markers in all
2113 section_alignment_glue
= 0;
2114 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2115 if (ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
2116 && irel
->r_addend
& RX_RELAXA_ALIGN
)
2118 int this_glue
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
2120 if (section_alignment_glue
< this_glue
)
2121 section_alignment_glue
= this_glue
;
2123 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2125 section_alignment_glue
*= 2;
2127 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2129 unsigned char *insn
;
2132 /* The insns we care about are all marked with one of these. */
2133 if (ELF32_R_TYPE (irel
->r_info
) != R_RX_RH_RELAX
)
2136 if (irel
->r_addend
& RX_RELAXA_ALIGN
2137 || next_alignment
== internal_relocs
)
2139 /* When we delete bytes, we need to maintain all the alignments
2140 indicated. In addition, we need to be careful about relaxing
2141 jumps across alignment boundaries - these displacements
2142 *grow* when we delete bytes. For now, don't shrink
2143 displacements across an alignment boundary, just in case.
2144 Note that this only affects relocations to the same
2146 prev_alignment
= next_alignment
;
2147 next_alignment
+= 2;
2148 while (next_alignment
< irelend
2149 && (ELF32_R_TYPE (next_alignment
->r_info
) != R_RX_RH_RELAX
2150 || !(next_alignment
->r_addend
& RX_RELAXA_ELIGN
)))
2152 if (next_alignment
>= irelend
|| next_alignment
->r_offset
== 0)
2153 next_alignment
= NULL
;
2156 /* When we hit alignment markers, see if we've shrunk enough
2157 before them to reduce the gap without violating the alignment
2159 if (irel
->r_addend
& RX_RELAXA_ALIGN
)
2161 /* At this point, the next relocation *should* be the ELIGN
2163 Elf_Internal_Rela
*erel
= irel
+ 1;
2164 unsigned int alignment
, nbytes
;
2166 if (ELF32_R_TYPE (erel
->r_info
) != R_RX_RH_RELAX
)
2168 if (!(erel
->r_addend
& RX_RELAXA_ELIGN
))
2171 alignment
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
2173 if (erel
->r_offset
- irel
->r_offset
< alignment
)
2176 nbytes
= erel
->r_offset
- irel
->r_offset
;
2177 nbytes
/= alignment
;
2178 nbytes
*= alignment
;
2180 elf32_rx_relax_delete_bytes (abfd
, sec
, erel
->r_offset
-nbytes
, nbytes
, next_alignment
,
2181 erel
->r_offset
== sec
->size
, internal_relocs
);
2187 if (irel
->r_addend
& RX_RELAXA_ELIGN
)
2190 insn
= contents
+ irel
->r_offset
;
2192 nrelocs
= irel
->r_addend
& RX_RELAXA_RNUM
;
2194 /* At this point, we have an insn that is a candidate for linker
2195 relaxation. There are NRELOCS relocs following that may be
2196 relaxed, although each reloc may be made of more than one
2197 reloc entry (such as gp-rel symbols). */
2199 /* Get the value of the symbol referred to by the reloc. Just
2200 in case this is the last reloc in the list, use the RL's
2201 addend to choose between this reloc (no addend) or the next
2202 (yes addend, which means at least one following reloc). */
2204 /* srel points to the "current" reloction for this insn -
2205 actually the last reloc for a given operand, which is the one
2206 we need to update. We check the relaxations in the same
2207 order that the relocations happen, so we'll just push it
2211 pc
= sec
->output_section
->vma
+ sec
->output_offset
2215 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2216 pcrel = symval - pc + srel->r_addend; \
2219 #define SNIPNR(offset, nbytes) \
2220 elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0, internal_relocs);
2221 #define SNIP(offset, nbytes, newtype) \
2222 SNIPNR (offset, nbytes); \
2223 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2225 /* The order of these bit tests must match the order that the
2226 relocs appear in. Since we sorted those by offset, we can
2229 /* Note that the numbers in, say, DSP6 are the bit offsets of
2230 the code fields that describe the operand. Bits number 0 for
2231 the MSB of insn[0]. */
2238 if (irel
->r_addend
& RX_RELAXA_DSP6
)
2243 if (code
== 2 && symval
/scale
<= 255)
2245 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2248 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2249 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2251 SNIP (3, 1, newrel
);
2256 else if (code
== 1 && symval
== 0)
2259 SNIP (2, 1, R_RX_NONE
);
2263 /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst. */
2264 else if (code
== 1 && symval
/scale
<= 31
2265 /* Decodable bits. */
2266 && (insn
[0] & 0xcc) == 0xcc
2268 && (insn
[0] & 0x30) != 0x30
2269 /* Register MSBs. */
2270 && (insn
[1] & 0x88) == 0x00)
2274 insn
[0] = 0x88 | (insn
[0] & 0x30);
2275 /* The register fields are in the right place already. */
2277 /* We can't relax this new opcode. */
2280 switch ((insn
[0] & 0x30) >> 4)
2283 newrel
= R_RX_RH_ABS5p5B
;
2286 newrel
= R_RX_RH_ABS5p5W
;
2289 newrel
= R_RX_RH_ABS5p5L
;
2293 move_reloc (irel
, srel
, -2);
2294 SNIP (2, 1, newrel
);
2297 /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst. */
2298 else if (code
== 1 && symval
/scale
<= 31
2299 /* Decodable bits. */
2300 && (insn
[0] & 0xf8) == 0x58
2301 /* Register MSBs. */
2302 && (insn
[1] & 0x88) == 0x00)
2306 insn
[0] = 0xb0 | ((insn
[0] & 0x04) << 1);
2307 /* The register fields are in the right place already. */
2309 /* We can't relax this new opcode. */
2312 switch ((insn
[0] & 0x08) >> 3)
2315 newrel
= R_RX_RH_ABS5p5B
;
2318 newrel
= R_RX_RH_ABS5p5W
;
2322 move_reloc (irel
, srel
, -2);
2323 SNIP (2, 1, newrel
);
2327 /* A DSP4 operand always follows a DSP6 operand, even if there's
2328 no relocation for it. We have to read the code out of the
2329 opcode to calculate the offset of the operand. */
2330 if (irel
->r_addend
& RX_RELAXA_DSP4
)
2332 int code6
, offset
= 0;
2336 code6
= insn
[0] & 0x03;
2339 case 0: offset
= 2; break;
2340 case 1: offset
= 3; break;
2341 case 2: offset
= 4; break;
2342 case 3: offset
= 2; break;
2345 code
= (insn
[0] & 0x0c) >> 2;
2347 if (code
== 2 && symval
/ scale
<= 255)
2349 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2353 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2354 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2356 SNIP (offset
+1, 1, newrel
);
2361 else if (code
== 1 && symval
== 0)
2364 SNIP (offset
, 1, R_RX_NONE
);
2367 /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2368 else if (code
== 1 && symval
/scale
<= 31
2369 /* Decodable bits. */
2370 && (insn
[0] & 0xc3) == 0xc3
2372 && (insn
[0] & 0x30) != 0x30
2373 /* Register MSBs. */
2374 && (insn
[1] & 0x88) == 0x00)
2378 insn
[0] = 0x80 | (insn
[0] & 0x30);
2379 /* The register fields are in the right place already. */
2381 /* We can't relax this new opcode. */
2384 switch ((insn
[0] & 0x30) >> 4)
2387 newrel
= R_RX_RH_ABS5p5B
;
2390 newrel
= R_RX_RH_ABS5p5W
;
2393 newrel
= R_RX_RH_ABS5p5L
;
2397 move_reloc (irel
, srel
, -2);
2398 SNIP (2, 1, newrel
);
2402 /* These always occur alone, but the offset depends on whether
2403 it's a MEMEX opcode (0x06) or not. */
2404 if (irel
->r_addend
& RX_RELAXA_DSP14
)
2409 if (insn
[0] == 0x06)
2416 if (code
== 2 && symval
/ scale
<= 255)
2418 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2422 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2423 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2425 SNIP (offset
, 1, newrel
);
2429 else if (code
== 1 && symval
== 0)
2432 SNIP (offset
, 1, R_RX_NONE
);
2443 /* These always occur alone. */
2444 if (irel
->r_addend
& RX_RELAXA_IMM6
)
2450 /* These relocations sign-extend, so we must do signed compares. */
2451 ssymval
= (long) symval
;
2453 code
= insn
[0] & 0x03;
2455 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2457 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2461 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2462 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2464 SNIP (2, 1, newrel
);
2469 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2471 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2475 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2476 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2478 SNIP (2, 1, newrel
);
2483 /* Special case UIMM8 format: CMP #uimm8,Rdst. */
2484 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2485 /* Decodable bits. */
2486 && (insn
[0] & 0xfc) == 0x74
2487 /* Decodable bits. */
2488 && ((insn
[1] & 0xf0) == 0x00))
2493 insn
[1] = 0x50 | (insn
[1] & 0x0f);
2495 /* We can't relax this new opcode. */
2498 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2499 newrel
= R_RX_ABS8U
;
2501 newrel
= R_RX_DIR8U
;
2503 SNIP (2, 1, newrel
);
2507 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2509 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2513 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2514 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2516 SNIP (2, 1, newrel
);
2521 /* Special case UIMM4 format: CMP, MUL, AND, OR. */
2522 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2523 /* Decodable bits and immediate type. */
2525 /* Decodable bits. */
2526 && (insn
[1] & 0xc0) == 0x00)
2528 static const int newop
[4] = { 1, 3, 4, 5 };
2530 insn
[0] = 0x60 | newop
[insn
[1] >> 4];
2531 /* The register number doesn't move. */
2533 /* We can't relax this new opcode. */
2536 move_reloc (irel
, srel
, -1);
2538 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2542 /* Special case UIMM4 format: ADD -> ADD/SUB. */
2543 else if (code
== 1 && ssymval
<= 15 && ssymval
>= -15
2544 /* Decodable bits and immediate type. */
2546 /* Same register for source and destination. */
2547 && ((insn
[1] >> 4) == (insn
[1] & 0x0f)))
2551 /* Note that we can't turn "add $0,Rs" into a NOP
2552 because the flags need to be set right. */
2556 insn
[0] = 0x60; /* Subtract. */
2557 newrel
= R_RX_RH_UNEG4p8
;
2561 insn
[0] = 0x62; /* Add. */
2562 newrel
= R_RX_RH_UIMM4p8
;
2565 /* The register number is in the right place. */
2567 /* We can't relax this new opcode. */
2570 move_reloc (irel
, srel
, -1);
2572 SNIP (2, 1, newrel
);
2577 /* These are either matched with a DSP6 (2-byte base) or an id24
2579 if (irel
->r_addend
& RX_RELAXA_IMM12
)
2581 int dspcode
, offset
= 0;
2586 if ((insn
[0] & 0xfc) == 0xfc)
2587 dspcode
= 1; /* Just something with one byte operand. */
2589 dspcode
= insn
[0] & 3;
2592 case 0: offset
= 2; break;
2593 case 1: offset
= 3; break;
2594 case 2: offset
= 4; break;
2595 case 3: offset
= 2; break;
2598 /* These relocations sign-extend, so we must do signed compares. */
2599 ssymval
= (long) symval
;
2601 code
= (insn
[1] >> 2) & 3;
2602 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2604 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2608 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2609 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2611 SNIP (offset
, 1, newrel
);
2616 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2618 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2622 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2623 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2625 SNIP (offset
, 1, newrel
);
2630 /* Special case UIMM8 format: MOV #uimm8,Rdst. */
2631 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2632 /* Decodable bits. */
2634 /* Decodable bits. */
2635 && ((insn
[1] & 0x03) == 0x02))
2640 insn
[1] = 0x40 | (insn
[1] >> 4);
2642 /* We can't relax this new opcode. */
2645 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2646 newrel
= R_RX_ABS8U
;
2648 newrel
= R_RX_DIR8U
;
2650 SNIP (2, 1, newrel
);
2654 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2656 unsigned int newrel
= ELF32_R_TYPE(srel
->r_info
);
2660 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2661 if (newrel
!= ELF32_R_TYPE(srel
->r_info
))
2663 SNIP (offset
, 1, newrel
);
2668 /* Special case UIMM4 format: MOV #uimm4,Rdst. */
2669 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2670 /* Decodable bits. */
2672 /* Decodable bits. */
2673 && ((insn
[1] & 0x03) == 0x02))
2676 insn
[1] = insn
[1] >> 4;
2678 /* We can't relax this new opcode. */
2681 move_reloc (irel
, srel
, -1);
2683 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2688 if (irel
->r_addend
& RX_RELAXA_BRA
)
2690 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2692 int alignment_glue
= 0;
2696 /* Branches over alignment chunks are problematic, as
2697 deleting bytes here makes the branch *further* away. We
2698 can be agressive with branches within this alignment
2699 block, but not branches outside it. */
2700 if ((prev_alignment
== NULL
2701 || symval
< (bfd_vma
)(sec_start
+ prev_alignment
->r_offset
))
2702 && (next_alignment
== NULL
2703 || symval
> (bfd_vma
)(sec_start
+ next_alignment
->r_offset
)))
2704 alignment_glue
= section_alignment_glue
;
2706 if (ELF32_R_TYPE(srel
[1].r_info
) == R_RX_RH_RELAX
2707 && srel
[1].r_addend
& RX_RELAXA_BRA
2708 && srel
[1].r_offset
< irel
->r_offset
+ pcrel
)
2711 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2713 /* The values we compare PCREL with are not what you'd
2714 expect; they're off by a little to compensate for (1)
2715 where the reloc is relative to the insn, and (2) how much
2716 the insn is going to change when we relax it. */
2718 /* These we have to decode. */
2721 case 0x04: /* BRA pcdsp:24 */
2722 if (-32768 + alignment_glue
<= pcrel
2723 && pcrel
<= 32765 - alignment_glue
)
2726 SNIP (3, 1, newrel
);
2731 case 0x38: /* BRA pcdsp:16 */
2732 if (-128 + alignment_glue
<= pcrel
2733 && pcrel
<= 127 - alignment_glue
)
2736 SNIP (2, 1, newrel
);
2741 case 0x2e: /* BRA pcdsp:8 */
2742 /* Note that there's a risk here of shortening things so
2743 much that we no longer fit this reloc; it *should*
2744 only happen when you branch across a branch, and that
2745 branch also devolves into BRA.S. "Real" code should
2747 if (max_pcrel3
+ alignment_glue
<= pcrel
2748 && pcrel
<= 10 - alignment_glue
2752 SNIP (1, 1, newrel
);
2753 move_reloc (irel
, srel
, -1);
2758 case 0x05: /* BSR pcdsp:24 */
2759 if (-32768 + alignment_glue
<= pcrel
2760 && pcrel
<= 32765 - alignment_glue
)
2763 SNIP (1, 1, newrel
);
2768 case 0x3a: /* BEQ.W pcdsp:16 */
2769 case 0x3b: /* BNE.W pcdsp:16 */
2770 if (-128 + alignment_glue
<= pcrel
2771 && pcrel
<= 127 - alignment_glue
)
2773 insn
[0] = 0x20 | (insn
[0] & 1);
2774 SNIP (1, 1, newrel
);
2779 case 0x20: /* BEQ.B pcdsp:8 */
2780 case 0x21: /* BNE.B pcdsp:8 */
2781 if (max_pcrel3
+ alignment_glue
<= pcrel
2782 && pcrel
- alignment_glue
<= 10
2785 insn
[0] = 0x10 | ((insn
[0] & 1) << 3);
2786 SNIP (1, 1, newrel
);
2787 move_reloc (irel
, srel
, -1);
2792 case 0x16: /* synthetic BNE dsp24 */
2793 case 0x1e: /* synthetic BEQ dsp24 */
2794 if (-32767 + alignment_glue
<= pcrel
2795 && pcrel
<= 32766 - alignment_glue
2798 if (insn
[0] == 0x16)
2802 /* We snip out the bytes at the end else the reloc
2803 will get moved too, and too much. */
2804 SNIP (3, 2, newrel
);
2805 move_reloc (irel
, srel
, -1);
2811 /* Special case - synthetic conditional branches, pcrel24.
2812 Note that EQ and NE have been handled above. */
2813 if ((insn
[0] & 0xf0) == 0x20
2816 && srel
->r_offset
!= irel
->r_offset
+ 1
2817 && -32767 + alignment_glue
<= pcrel
2818 && pcrel
<= 32766 - alignment_glue
)
2822 SNIP (5, 1, newrel
);
2826 /* Special case - synthetic conditional branches, pcrel16 */
2827 if ((insn
[0] & 0xf0) == 0x20
2830 && srel
->r_offset
!= irel
->r_offset
+ 1
2831 && -127 + alignment_glue
<= pcrel
2832 && pcrel
<= 126 - alignment_glue
)
2834 int cond
= (insn
[0] & 0x0f) ^ 0x01;
2836 insn
[0] = 0x20 | cond
;
2837 /* By moving the reloc first, we avoid having
2838 delete_bytes move it also. */
2839 move_reloc (irel
, srel
, -2);
2840 SNIP (2, 3, newrel
);
2845 BFD_ASSERT (nrelocs
== 0);
2847 /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2848 use MOV.bwl #uimm:8, dsp:5[r7] format. This is tricky
2849 because it may have one or two relocations. */
2850 if ((insn
[0] & 0xfc) == 0xf8
2851 && (insn
[1] & 0x80) == 0x00
2852 && (insn
[0] & 0x03) != 0x03)
2854 int dcode
, icode
, reg
, ioff
, dscale
, ilen
;
2855 bfd_vma disp_val
= 0;
2857 Elf_Internal_Rela
* disp_rel
= 0;
2858 Elf_Internal_Rela
* imm_rel
= 0;
2863 dcode
= insn
[0] & 0x03;
2864 icode
= (insn
[1] >> 2) & 0x03;
2865 reg
= (insn
[1] >> 4) & 0x0f;
2867 ioff
= dcode
== 1 ? 3 : dcode
== 2 ? 4 : 2;
2869 /* Figure out what the dispacement is. */
2870 if (dcode
== 1 || dcode
== 2)
2872 /* There's a displacement. See if there's a reloc for it. */
2873 if (srel
[1].r_offset
== irel
->r_offset
+ 2)
2885 #if RX_OPCODE_BIG_ENDIAN
2886 disp_val
= insn
[2] * 256 + insn
[3];
2888 disp_val
= insn
[2] + insn
[3] * 256;
2891 switch (insn
[1] & 3)
2907 /* Figure out what the immediate is. */
2908 if (srel
[1].r_offset
== irel
->r_offset
+ ioff
)
2911 imm_val
= (long) symval
;
2916 unsigned char * ip
= insn
+ ioff
;
2921 /* For byte writes, we don't sign extend. Makes the math easier later. */
2925 imm_val
= (char) ip
[0];
2928 #if RX_OPCODE_BIG_ENDIAN
2929 imm_val
= ((char) ip
[0] << 8) | ip
[1];
2931 imm_val
= ((char) ip
[1] << 8) | ip
[0];
2935 #if RX_OPCODE_BIG_ENDIAN
2936 imm_val
= ((char) ip
[0] << 16) | (ip
[1] << 8) | ip
[2];
2938 imm_val
= ((char) ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2942 #if RX_OPCODE_BIG_ENDIAN
2943 imm_val
= ((unsigned) ip
[0] << 24) | (ip
[1] << 16) | (ip
[2] << 8) | ip
[3];
2945 imm_val
= ((unsigned) ip
[3] << 24) | (ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2979 /* The shortcut happens when the immediate is 0..255,
2980 register r0 to r7, and displacement (scaled) 0..31. */
2982 if (0 <= imm_val
&& imm_val
<= 255
2983 && 0 <= reg
&& reg
<= 7
2984 && disp_val
/ dscale
<= 31)
2986 insn
[0] = 0x3c | (insn
[1] & 0x03);
2987 insn
[1] = (((disp_val
/ dscale
) << 3) & 0x80) | (reg
<< 4) | ((disp_val
/dscale
) & 0x0f);
2992 int newrel
= R_RX_NONE
;
2997 newrel
= R_RX_RH_ABS5p8B
;
3000 newrel
= R_RX_RH_ABS5p8W
;
3003 newrel
= R_RX_RH_ABS5p8L
;
3006 disp_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (disp_rel
->r_info
), newrel
);
3007 move_reloc (irel
, disp_rel
, -1);
3011 imm_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (imm_rel
->r_info
), R_RX_DIR8U
);
3012 move_reloc (disp_rel
? disp_rel
: irel
,
3014 irel
->r_offset
- imm_rel
->r_offset
+ 2);
3017 SNIPNR (3, ilen
- 3);
3020 /* We can't relax this new opcode. */
3026 /* We can't reliably relax branches to DIR3U_PCREL unless we know
3027 whatever they're branching over won't shrink any more. If we're
3028 basically done here, do one more pass just for branches - but
3029 don't request a pass after that one! */
3030 if (!*again
&& !allow_pcrel3
)
3034 elf32_rx_relax_section (abfd
, sec
, link_info
, &ignored
, true);
3040 free (free_contents
);
3042 if (shndx_buf
!= NULL
)
3044 shndx_hdr
->contents
= NULL
;
3048 free (free_intsyms
);
3054 elf32_rx_relax_section_wrapper (bfd
*abfd
,
3056 struct bfd_link_info
*link_info
,
3059 return elf32_rx_relax_section (abfd
, sec
, link_info
, again
, false);
3062 /* Function to set the ELF flag bits. */
3065 rx_elf_set_private_flags (bfd
* abfd
, flagword flags
)
3067 elf_elfheader (abfd
)->e_flags
= flags
;
3068 elf_flags_init (abfd
) = true;
3072 static bool no_warn_mismatch
= false;
3073 static bool ignore_lma
= true;
3075 void bfd_elf32_rx_set_target_flags (bool, bool);
3078 bfd_elf32_rx_set_target_flags (bool user_no_warn_mismatch
,
3079 bool user_ignore_lma
)
3081 no_warn_mismatch
= user_no_warn_mismatch
;
3082 ignore_lma
= user_ignore_lma
;
3085 /* Converts FLAGS into a descriptive string.
3086 Returns a static pointer. */
3089 describe_flags (flagword flags
, char *buf
)
3093 if (flags
& E_FLAG_RX_64BIT_DOUBLES
)
3094 strcat (buf
, "64-bit doubles");
3096 strcat (buf
, "32-bit doubles");
3098 if (flags
& E_FLAG_RX_DSP
)
3099 strcat (buf
, ", dsp");
3101 strcat (buf
, ", no dsp");
3103 if (flags
& E_FLAG_RX_PID
)
3104 strcat (buf
, ", pid");
3106 strcat (buf
, ", no pid");
3108 if (flags
& E_FLAG_RX_ABI
)
3109 strcat (buf
, ", RX ABI");
3111 strcat (buf
, ", GCC ABI");
3113 if (flags
& E_FLAG_RX_SINSNS_SET
)
3114 strcat (buf
, flags
& E_FLAG_RX_SINSNS_YES
? ", uses String instructions" : ", bans String instructions");
3119 /* Merge backend specific data from an object file to the output
3120 object file when linking. */
3123 rx_elf_merge_private_bfd_data (bfd
* ibfd
, struct bfd_link_info
*info
)
3125 bfd
*obfd
= info
->output_bfd
;
3130 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3131 old_flags
= elf_elfheader (obfd
)->e_flags
;
3133 if (!elf_flags_init (obfd
))
3135 /* First call, no flags set. */
3136 elf_flags_init (obfd
) = true;
3137 elf_elfheader (obfd
)->e_flags
= new_flags
;
3139 else if (old_flags
!= new_flags
)
3141 flagword known_flags
;
3143 if (old_flags
& E_FLAG_RX_SINSNS_SET
)
3145 if ((new_flags
& E_FLAG_RX_SINSNS_SET
) == 0)
3147 new_flags
&= ~ E_FLAG_RX_SINSNS_MASK
;
3148 new_flags
|= (old_flags
& E_FLAG_RX_SINSNS_MASK
);
3151 else if (new_flags
& E_FLAG_RX_SINSNS_SET
)
3153 old_flags
&= ~ E_FLAG_RX_SINSNS_MASK
;
3154 old_flags
|= (new_flags
& E_FLAG_RX_SINSNS_MASK
);
3157 known_flags
= E_FLAG_RX_ABI
| E_FLAG_RX_64BIT_DOUBLES
3158 | E_FLAG_RX_DSP
| E_FLAG_RX_PID
| E_FLAG_RX_SINSNS_MASK
;
3160 if ((old_flags
^ new_flags
) & known_flags
)
3162 /* Only complain if flag bits we care about do not match.
3163 Other bits may be set, since older binaries did use some
3164 deprecated flags. */
3165 if (no_warn_mismatch
)
3167 elf_elfheader (obfd
)->e_flags
= (new_flags
| old_flags
) & known_flags
;
3173 _bfd_error_handler (_("there is a conflict merging the"
3174 " ELF header flags from %pB"),
3176 _bfd_error_handler (_(" the input file's flags: %s"),
3177 describe_flags (new_flags
, buf
));
3178 _bfd_error_handler (_(" the output file's flags: %s"),
3179 describe_flags (old_flags
, buf
));
3184 elf_elfheader (obfd
)->e_flags
= new_flags
& known_flags
;
3188 bfd_set_error (bfd_error_bad_value
);
3194 rx_elf_print_private_bfd_data (bfd
* abfd
, void * ptr
)
3196 FILE * file
= (FILE *) ptr
;
3200 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
3202 /* Print normal ELF private data. */
3203 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
3205 flags
= elf_elfheader (abfd
)->e_flags
;
3206 fprintf (file
, _("private flags = 0x%lx:"), (long) flags
);
3208 fprintf (file
, "%s", describe_flags (flags
, buf
));
3212 /* Return the MACH for an e_flags value. */
3215 elf32_rx_machine (bfd
* abfd ATTRIBUTE_UNUSED
)
3217 #if 0 /* FIXME: EF_RX_CPU_MASK collides with E_FLAG_RX_...
3218 Need to sort out how these flag bits are used.
3219 For now we assume that the flags are OK. */
3220 if ((elf_elfheader (abfd
)->e_flags
& EF_RX_CPU_MASK
) == EF_RX_CPU_RX
)
3222 if ((elf_elfheader (abfd
)->e_flags
& E_FLAG_RX_V2
))
3223 return bfd_mach_rx_v2
;
3224 else if ((elf_elfheader (abfd
)->e_flags
& E_FLAG_RX_V3
))
3225 return bfd_mach_rx_v3
;
3233 rx_elf_object_p (bfd
* abfd
)
3237 Elf_Internal_Phdr
*phdr
= elf_tdata (abfd
)->phdr
;
3238 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
3239 int nphdrs
= ehdr
->e_phnum
;
3241 static int saw_be
= false;
3242 bfd_vma end_phdroff
;
3244 /* We never want to automatically choose the non-swapping big-endian
3245 target. The user can only get that explicitly, such as with -I
3247 if (abfd
->xvec
== &rx_elf32_be_ns_vec
3248 && abfd
->target_defaulted
)
3251 /* BFD->target_defaulted is not set to TRUE when a target is chosen
3252 as a fallback, so we check for "scanning" to know when to stop
3253 using the non-swapping target. */
3254 if (abfd
->xvec
== &rx_elf32_be_ns_vec
3257 if (abfd
->xvec
== &rx_elf32_be_vec
)
3260 bfd_default_set_arch_mach (abfd
, bfd_arch_rx
,
3261 elf32_rx_machine (abfd
));
3263 /* For each PHDR in the object, we must find some section that
3264 corresponds (based on matching file offsets) and use its VMA
3265 information to reconstruct the p_vaddr field we clobbered when we
3267 /* If PT_LOAD headers include the ELF file header or program headers
3268 then the PT_LOAD header does not start with some section contents.
3269 Making adjustments based on the difference between sh_offset and
3270 p_offset is nonsense in such cases. Exclude them. Note that
3271 since standard linker scripts for RX do not use SIZEOF_HEADERS,
3272 the linker won't normally create PT_LOAD segments covering the
3273 headers so this is mainly for passing the ld testsuite.
3274 FIXME. Why are we looking at non-PT_LOAD headers here? */
3275 end_phdroff
= ehdr
->e_ehsize
;
3276 if (ehdr
->e_phoff
!= 0)
3277 end_phdroff
= ehdr
->e_phoff
+ nphdrs
* ehdr
->e_phentsize
;
3278 for (i
=0; i
<nphdrs
; i
++)
3280 for (u
=0; u
<elf_tdata(abfd
)->num_elf_sections
; u
++)
3282 Elf_Internal_Shdr
*sec
= elf_tdata(abfd
)->elf_sect_ptr
[u
];
3284 if (phdr
[i
].p_filesz
3285 && phdr
[i
].p_offset
>= end_phdroff
3286 && phdr
[i
].p_offset
<= (bfd_vma
) sec
->sh_offset
3288 && sec
->sh_type
!= SHT_NOBITS
3289 && (bfd_vma
)sec
->sh_offset
<= phdr
[i
].p_offset
+ (phdr
[i
].p_filesz
- 1))
3291 /* Found one! The difference between the two addresses,
3292 plus the difference between the two file offsets, is
3293 enough information to reconstruct the lma. */
3295 /* Example where they aren't:
3296 PHDR[1] = lma fffc0100 offset 00002010 size 00000100
3297 SEC[6] = vma 00000050 offset 00002050 size 00000040
3299 The correct LMA for the section is fffc0140 + (2050-2010).
3302 phdr
[i
].p_vaddr
= sec
->sh_addr
+ (sec
->sh_offset
- phdr
[i
].p_offset
);
3307 /* We must update the bfd sections as well, so we don't stop
3309 bsec
= abfd
->sections
;
3312 if (phdr
[i
].p_filesz
3313 && phdr
[i
].p_vaddr
<= bsec
->vma
3314 && bsec
->vma
<= phdr
[i
].p_vaddr
+ (phdr
[i
].p_filesz
- 1))
3316 bsec
->lma
= phdr
[i
].p_paddr
+ (bsec
->vma
- phdr
[i
].p_vaddr
);
3326 rx_linux_object_p (bfd
* abfd
)
3328 bfd_default_set_arch_mach (abfd
, bfd_arch_rx
, elf32_rx_machine (abfd
));
3335 rx_dump_symtab (bfd
* abfd
, void * internal_syms
, void * external_syms
)
3338 Elf_Internal_Sym
* isymbuf
;
3339 Elf_Internal_Sym
* isymend
;
3340 Elf_Internal_Sym
* isym
;
3341 Elf_Internal_Shdr
* symtab_hdr
;
3343 char * st_info_stb_str
;
3344 char * st_other_str
;
3345 char * st_shndx_str
;
3347 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3348 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3350 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
3351 symtab_hdr
->sh_info
, 0,
3352 internal_syms
, external_syms
, NULL
);
3354 isymbuf
= internal_syms
;
3355 isymend
= isymbuf
+ locsymcount
;
3357 for (isym
= isymbuf
; isym
< isymend
; isym
++)
3359 switch (ELF_ST_TYPE (isym
->st_info
))
3361 case STT_FUNC
: st_info_str
= "STT_FUNC"; break;
3362 case STT_SECTION
: st_info_str
= "STT_SECTION"; break;
3363 case STT_FILE
: st_info_str
= "STT_FILE"; break;
3364 case STT_OBJECT
: st_info_str
= "STT_OBJECT"; break;
3365 case STT_TLS
: st_info_str
= "STT_TLS"; break;
3366 default: st_info_str
= "";
3368 switch (ELF_ST_BIND (isym
->st_info
))
3370 case STB_LOCAL
: st_info_stb_str
= "STB_LOCAL"; break;
3371 case STB_GLOBAL
: st_info_stb_str
= "STB_GLOBAL"; break;
3372 default: st_info_stb_str
= "";
3374 switch (ELF_ST_VISIBILITY (isym
->st_other
))
3376 case STV_DEFAULT
: st_other_str
= "STV_DEFAULT"; break;
3377 case STV_INTERNAL
: st_other_str
= "STV_INTERNAL"; break;
3378 case STV_PROTECTED
: st_other_str
= "STV_PROTECTED"; break;
3379 default: st_other_str
= "";
3381 switch (isym
->st_shndx
)
3383 case SHN_ABS
: st_shndx_str
= "SHN_ABS"; break;
3384 case SHN_COMMON
: st_shndx_str
= "SHN_COMMON"; break;
3385 case SHN_UNDEF
: st_shndx_str
= "SHN_UNDEF"; break;
3386 default: st_shndx_str
= "";
3389 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3390 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3392 (unsigned long) isym
->st_value
,
3393 (unsigned long) isym
->st_size
,
3395 bfd_elf_string_from_elf_section (abfd
, symtab_hdr
->sh_link
,
3397 isym
->st_info
, st_info_str
, st_info_stb_str
,
3398 isym
->st_other
, st_other_str
,
3399 isym
->st_shndx
, st_shndx_str
);
3404 rx_get_reloc (long reloc
)
3406 if (0 <= reloc
&& reloc
< R_RX_max
)
3407 return rx_elf_howto_table
[reloc
].name
;
3413 /* We must take care to keep the on-disk copy of any code sections
3414 that are fully linked swapped if the target is big endian, to match
3415 the Renesas tools. */
3417 /* The rule is: big endian object that are final-link executables,
3418 have code sections stored with 32-bit words swapped relative to
3419 what you'd get by default. */
3422 rx_get_section_contents (bfd
* abfd
,
3426 bfd_size_type count
)
3428 int exec
= (abfd
->flags
& EXEC_P
) ? 1 : 0;
3429 int s_code
= (section
->flags
& SEC_CODE
) ? 1 : 0;
3433 fprintf (stderr
, "dj: get %ld %ld from %s %s e%d sc%d %08lx:%08lx\n",
3434 (long) offset
, (long) count
, section
->name
,
3435 bfd_big_endian(abfd
) ? "be" : "le",
3436 exec
, s_code
, (long unsigned) section
->filepos
,
3437 (long unsigned) offset
);
3440 if (exec
&& s_code
&& bfd_big_endian (abfd
))
3442 char * cloc
= (char *) location
;
3443 bfd_size_type cnt
, end_cnt
;
3447 /* Fetch and swap unaligned bytes at the beginning. */
3452 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3457 bfd_putb32 (bfd_getl32 (buf
), buf
);
3459 cnt
= 4 - (offset
% 4);
3463 memcpy (location
, buf
+ (offset
% 4), cnt
);
3470 end_cnt
= count
% 4;
3472 /* Fetch and swap the middle bytes. */
3475 rv
= _bfd_generic_get_section_contents (abfd
, section
, cloc
, offset
,
3480 for (cnt
= count
; cnt
>= 4; cnt
-= 4, cloc
+= 4)
3481 bfd_putb32 (bfd_getl32 (cloc
), cloc
);
3484 /* Fetch and swap the end bytes. */
3489 /* Fetch the end bytes. */
3490 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3491 offset
+ count
- end_cnt
, 4);
3495 bfd_putb32 (bfd_getl32 (buf
), buf
);
3496 memcpy (cloc
, buf
, end_cnt
);
3500 rv
= _bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
);
3507 rx2_set_section_contents (bfd
* abfd
,
3509 const void * location
,
3511 bfd_size_type count
)
3515 fprintf (stderr
, " set sec %s %08x loc %p offset %#x count %#x\n",
3516 section
->name
, (unsigned) section
->vma
, location
, (int) offset
, (int) count
);
3517 for (i
= 0; i
< count
; i
++)
3519 if (i
% 16 == 0 && i
> 0)
3520 fprintf (stderr
, "\n");
3522 if (i
% 16 && i
% 4 == 0)
3523 fprintf (stderr
, " ");
3526 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3528 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3530 fprintf (stderr
, "\n");
3532 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3534 #define _bfd_elf_set_section_contents rx2_set_section_contents
3538 rx_set_section_contents (bfd
* abfd
,
3540 const void * location
,
3542 bfd_size_type count
)
3544 bool exec
= (abfd
->flags
& EXEC_P
) != 0;
3545 bool s_code
= (section
->flags
& SEC_CODE
) != 0;
3547 char * swapped_data
= NULL
;
3549 bfd_vma caddr
= section
->vma
+ offset
;
3551 bfd_size_type scount
;
3556 fprintf (stderr
, "\ndj: set %ld %ld to %s %s e%d sc%d\n",
3557 (long) offset
, (long) count
, section
->name
,
3558 bfd_big_endian (abfd
) ? "be" : "le",
3561 for (i
= 0; i
< count
; i
++)
3563 int a
= section
->vma
+ offset
+ i
;
3565 if (a
% 16 == 0 && a
> 0)
3566 fprintf (stderr
, "\n");
3568 if (a
% 16 && a
% 4 == 0)
3569 fprintf (stderr
, " ");
3571 if (a
% 16 == 0 || i
== 0)
3572 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3574 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3577 fprintf (stderr
, "\n");
3580 if (! exec
|| ! s_code
|| ! bfd_big_endian (abfd
))
3581 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3583 while (count
> 0 && caddr
> 0 && caddr
% 4)
3587 case 0: faddr
= offset
+ 3; break;
3588 case 1: faddr
= offset
+ 1; break;
3589 case 2: faddr
= offset
- 1; break;
3590 case 3: faddr
= offset
- 3; break;
3593 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3597 location
= (bfd_byte
*) location
+ 1;
3603 scount
= (int)(count
/ 4) * 4;
3606 char * cloc
= (char *) location
;
3608 swapped_data
= (char *) bfd_alloc (abfd
, count
);
3609 if (swapped_data
== NULL
)
3612 for (i
= 0; i
< count
; i
+= 4)
3614 bfd_vma v
= bfd_getl32 (cloc
+ i
);
3615 bfd_putb32 (v
, swapped_data
+ i
);
3618 rv
= _bfd_elf_set_section_contents (abfd
, section
, swapped_data
, offset
, scount
);
3625 location
= (bfd_byte
*) location
+ scount
;
3630 caddr
= section
->vma
+ offset
;
3635 case 0: faddr
= offset
+ 3; break;
3636 case 1: faddr
= offset
+ 1; break;
3637 case 2: faddr
= offset
- 1; break;
3638 case 3: faddr
= offset
- 3; break;
3640 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3644 location
= (bfd_byte
*) location
+ 1;
3655 rx_final_link (bfd
* abfd
, struct bfd_link_info
* info
)
3659 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3662 fprintf (stderr
, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3663 o
->name
, o
->flags
, o
->vma
, o
->lma
, o
->size
, o
->rawsize
);
3665 if (o
->flags
& SEC_CODE
3666 && bfd_big_endian (abfd
)
3670 fprintf (stderr
, "adjusting...\n");
3672 o
->size
+= 4 - (o
->size
% 4);
3676 return bfd_elf_final_link (abfd
, info
);
3680 elf32_rx_modify_headers (bfd
*abfd
, struct bfd_link_info
*info
)
3682 const struct elf_backend_data
* bed
;
3683 struct elf_obj_tdata
* tdata
;
3684 Elf_Internal_Phdr
* phdr
;
3688 bed
= get_elf_backend_data (abfd
);
3689 tdata
= elf_tdata (abfd
);
3691 count
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
3694 for (i
= count
; i
-- != 0;)
3695 if (phdr
[i
].p_type
== PT_LOAD
)
3697 /* The Renesas tools expect p_paddr to be zero. However,
3698 there is no other way to store the writable data in ROM for
3699 startup initialization. So, we let the linker *think*
3700 we're using paddr and vaddr the "usual" way, but at the
3701 last minute we move the paddr into the vaddr (which is what
3702 the simulator uses) and zero out paddr. Note that this
3703 does not affect the section headers, just the program
3704 headers. We hope. */
3705 phdr
[i
].p_vaddr
= phdr
[i
].p_paddr
;
3706 #if 0 /* If we zero out p_paddr, then the LMA in the section table
3708 phdr
[i
].p_paddr
= 0;
3712 return _bfd_elf_modify_headers (abfd
, info
);
3715 /* The default literal sections should always be marked as "code" (i.e.,
3716 SHF_EXECINSTR). This is particularly important for big-endian mode
3717 when we do not want their contents byte reversed. */
3718 static const struct bfd_elf_special_section elf32_rx_special_sections
[] =
3720 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3721 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3722 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3723 { NULL
, 0, 0, 0, 0 }
3728 struct bfd_link_info
*info
;
3729 bfd_vma table_start
;
3731 bfd_vma
*table_handlers
;
3732 bfd_vma table_default_handler
;
3733 struct bfd_link_hash_entry
**table_entries
;
3734 struct bfd_link_hash_entry
*table_default_entry
;
3739 rx_table_find (struct bfd_hash_entry
*vent
, void *vinfo
)
3741 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3742 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3743 const char *name
; /* of the symbol we've found */
3747 const char *tname
; /* name of the table */
3748 bfd_vma start_addr
, end_addr
;
3750 struct bfd_link_hash_entry
* h
;
3752 /* We're looking for globally defined symbols of the form
3753 $tablestart$<NAME>. */
3754 if (ent
->type
!= bfd_link_hash_defined
3755 && ent
->type
!= bfd_link_hash_defweak
)
3758 name
= ent
->root
.string
;
3759 sec
= ent
->u
.def
.section
;
3762 if (!startswith (name
, "$tablestart$"))
3765 sec
->flags
|= SEC_KEEP
;
3769 start_addr
= ent
->u
.def
.value
;
3771 /* At this point, we can't build the table but we can (and must)
3772 find all the related symbols and mark their sections as SEC_KEEP
3773 so we don't garbage collect them. */
3775 buf
= (char *) bfd_malloc (12 + 10 + strlen (tname
));
3779 sprintf (buf
, "$tableend$%s", tname
);
3780 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, false, false, true);
3781 if (!h
|| (h
->type
!= bfd_link_hash_defined
3782 && h
->type
!= bfd_link_hash_defweak
))
3784 /* xgettext:c-format */
3785 _bfd_error_handler (_("%pB:%pA: table %s missing corresponding %s"),
3786 abfd
, sec
, name
, buf
);
3790 if (h
->u
.def
.section
!= ent
->u
.def
.section
)
3792 /* xgettext:c-format */
3793 _bfd_error_handler (_("%pB:%pA: %s and %s must be in the same input section"),
3794 h
->u
.def
.section
->owner
, h
->u
.def
.section
,
3799 end_addr
= h
->u
.def
.value
;
3801 sprintf (buf
, "$tableentry$default$%s", tname
);
3802 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, false, false, true);
3803 if (h
&& (h
->type
== bfd_link_hash_defined
3804 || h
->type
== bfd_link_hash_defweak
))
3806 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3809 for (idx
= 0; idx
< (int) (end_addr
- start_addr
) / 4; idx
++)
3811 sprintf (buf
, "$tableentry$%d$%s", idx
, tname
);
3812 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, false, false, true);
3813 if (h
&& (h
->type
== bfd_link_hash_defined
3814 || h
->type
== bfd_link_hash_defweak
))
3816 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3820 /* Return TRUE to keep scanning, FALSE to end the traversal. */
3824 /* We need to check for table entry symbols and build the tables, and
3825 we need to do it before the linker does garbage collection. This function is
3826 called once per input object file. */
3829 (bfd
* abfd ATTRIBUTE_UNUSED
,
3830 struct bfd_link_info
* info ATTRIBUTE_UNUSED
)
3832 RX_Table_Info stuff
;
3836 bfd_hash_traverse (&(info
->hash
->table
), rx_table_find
, &stuff
);
3843 rx_table_map_2 (struct bfd_hash_entry
*vent
, void *vinfo
)
3845 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3846 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3851 /* See if the symbol ENT has an address listed in the table, and
3852 isn't a debug/special symbol. If so, put it in the table. */
3854 if (ent
->type
!= bfd_link_hash_defined
3855 && ent
->type
!= bfd_link_hash_defweak
)
3858 name
= ent
->root
.string
;
3860 if (name
[0] == '$' || name
[0] == '.' || name
[0] < ' ')
3863 addr
= (ent
->u
.def
.value
3864 + ent
->u
.def
.section
->output_section
->vma
3865 + ent
->u
.def
.section
->output_offset
);
3867 for (idx
= 0; idx
< info
->table_size
; idx
++)
3868 if (addr
== info
->table_handlers
[idx
])
3869 info
->table_entries
[idx
] = ent
;
3871 if (addr
== info
->table_default_handler
)
3872 info
->table_default_entry
= ent
;
3878 rx_table_map (struct bfd_hash_entry
*vent
, void *vinfo
)
3880 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3881 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3882 const char *name
; /* of the symbol we've found */
3884 const char *tname
; /* name of the table */
3885 bfd_vma start_addr
, end_addr
;
3887 struct bfd_link_hash_entry
* h
;
3890 /* We're looking for globally defined symbols of the form
3891 $tablestart$<NAME>. */
3892 if (ent
->type
!= bfd_link_hash_defined
3893 && ent
->type
!= bfd_link_hash_defweak
)
3896 name
= ent
->root
.string
;
3898 if (!startswith (name
, "$tablestart$"))
3902 start_addr
= (ent
->u
.def
.value
3903 + ent
->u
.def
.section
->output_section
->vma
3904 + ent
->u
.def
.section
->output_offset
);
3906 buf
= (char *) bfd_malloc (12 + 10 + strlen (tname
));
3910 sprintf (buf
, "$tableend$%s", tname
);
3911 end_addr
= get_symbol_value_maybe (buf
, info
->info
);
3913 sprintf (buf
, "$tableentry$default$%s", tname
);
3914 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, false, false, true);
3917 info
->table_default_handler
= (h
->u
.def
.value
3918 + h
->u
.def
.section
->output_section
->vma
3919 + h
->u
.def
.section
->output_offset
);
3922 /* Zero is a valid handler address! */
3923 info
->table_default_handler
= (bfd_vma
) (-1);
3924 info
->table_default_entry
= NULL
;
3926 info
->table_start
= start_addr
;
3927 info
->table_size
= (int) (end_addr
- start_addr
) / 4;
3928 info
->table_handlers
= (bfd_vma
*)
3929 bfd_malloc (info
->table_size
* sizeof (bfd_vma
));
3930 if (info
->table_handlers
== NULL
)
3935 info
->table_entries
= (struct bfd_link_hash_entry
**)
3936 bfd_malloc (info
->table_size
* sizeof (struct bfd_link_hash_entry
));
3937 if (info
->table_entries
== NULL
)
3939 free (info
->table_handlers
);
3944 for (idx
= 0; idx
< (int) (end_addr
- start_addr
) / 4; idx
++)
3946 sprintf (buf
, "$tableentry$%d$%s", idx
, tname
);
3947 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, false, false, true);
3948 if (h
&& (h
->type
== bfd_link_hash_defined
3949 || h
->type
== bfd_link_hash_defweak
))
3951 info
->table_handlers
[idx
] = (h
->u
.def
.value
3952 + h
->u
.def
.section
->output_section
->vma
3953 + h
->u
.def
.section
->output_offset
);
3956 info
->table_handlers
[idx
] = info
->table_default_handler
;
3957 info
->table_entries
[idx
] = NULL
;
3962 bfd_hash_traverse (&(info
->info
->hash
->table
), rx_table_map_2
, info
);
3964 fprintf (info
->mapfile
, "\nRX Vector Table: %s has %d entries at 0x%08" BFD_VMA_FMT
"x\n\n",
3965 tname
, info
->table_size
, start_addr
);
3967 if (info
->table_default_entry
)
3968 fprintf (info
->mapfile
, " default handler is: %s at 0x%08" BFD_VMA_FMT
"x\n",
3969 info
->table_default_entry
->root
.string
,
3970 info
->table_default_handler
);
3971 else if (info
->table_default_handler
!= (bfd_vma
)(-1))
3972 fprintf (info
->mapfile
, " default handler is at 0x%08" BFD_VMA_FMT
"x\n",
3973 info
->table_default_handler
);
3975 fprintf (info
->mapfile
, " no default handler\n");
3978 for (idx
= 0; idx
< info
->table_size
; idx
++)
3980 if (info
->table_handlers
[idx
] == info
->table_default_handler
)
3983 fprintf (info
->mapfile
, " . . .\n");
3989 fprintf (info
->mapfile
, " 0x%08" BFD_VMA_FMT
"x [%3d] ", start_addr
+ 4 * idx
, idx
);
3991 if (info
->table_handlers
[idx
] == (bfd_vma
) (-1))
3992 fprintf (info
->mapfile
, "(no handler found)\n");
3994 else if (info
->table_handlers
[idx
] == info
->table_default_handler
)
3996 if (info
->table_default_entry
)
3997 fprintf (info
->mapfile
, "(default)\n");
3999 fprintf (info
->mapfile
, "(default)\n");
4002 else if (info
->table_entries
[idx
])
4004 fprintf (info
->mapfile
, "0x%08" BFD_VMA_FMT
"x %s\n", info
->table_handlers
[idx
], info
->table_entries
[idx
]->root
.string
);
4009 fprintf (info
->mapfile
, "0x%08" BFD_VMA_FMT
"x ???\n", info
->table_handlers
[idx
]);
4013 fprintf (info
->mapfile
, " . . .\n");
4019 rx_additional_link_map_text (bfd
*obfd
, struct bfd_link_info
*info
, FILE *mapfile
)
4021 /* We scan the symbol table looking for $tableentry$'s, and for
4022 each, try to deduce which handlers go with which entries. */
4024 RX_Table_Info stuff
;
4028 stuff
.mapfile
= mapfile
;
4029 bfd_hash_traverse (&(info
->hash
->table
), rx_table_map
, &stuff
);
4033 #define ELF_ARCH bfd_arch_rx
4034 #define ELF_MACHINE_CODE EM_RX
4035 #define ELF_MAXPAGESIZE 0x1000
4037 #define TARGET_BIG_SYM rx_elf32_be_vec
4038 #define TARGET_BIG_NAME "elf32-rx-be"
4040 #define TARGET_LITTLE_SYM rx_elf32_le_vec
4041 #define TARGET_LITTLE_NAME "elf32-rx-le"
4043 #define elf_info_to_howto_rel NULL
4044 #define elf_info_to_howto rx_info_to_howto_rela
4045 #define elf_backend_object_p rx_elf_object_p
4046 #define elf_backend_relocate_section rx_elf_relocate_section
4047 #define elf_symbol_leading_char ('_')
4048 #define elf_backend_can_gc_sections 1
4049 #define elf_backend_modify_headers elf32_rx_modify_headers
4051 #define bfd_elf32_bfd_reloc_type_lookup rx_reloc_type_lookup
4052 #define bfd_elf32_bfd_reloc_name_lookup rx_reloc_name_lookup
4053 #define bfd_elf32_bfd_set_private_flags rx_elf_set_private_flags
4054 #define bfd_elf32_bfd_merge_private_bfd_data rx_elf_merge_private_bfd_data
4055 #define bfd_elf32_bfd_print_private_bfd_data rx_elf_print_private_bfd_data
4056 #define bfd_elf32_get_section_contents rx_get_section_contents
4057 #define bfd_elf32_set_section_contents rx_set_section_contents
4058 #define bfd_elf32_bfd_final_link rx_final_link
4059 #define bfd_elf32_bfd_relax_section elf32_rx_relax_section_wrapper
4060 #define elf_backend_special_sections elf32_rx_special_sections
4061 #define elf_backend_check_directives rx_check_directives
4063 #include "elf32-target.h"
4065 /* We define a second big-endian target that doesn't have the custom
4066 section get/set hooks, for times when we want to preserve the
4067 pre-swapped .text sections (like objcopy). */
4069 #undef TARGET_BIG_SYM
4070 #define TARGET_BIG_SYM rx_elf32_be_ns_vec
4071 #undef TARGET_BIG_NAME
4072 #define TARGET_BIG_NAME "elf32-rx-be-ns"
4073 #undef TARGET_LITTLE_SYM
4075 #undef bfd_elf32_get_section_contents
4076 #undef bfd_elf32_set_section_contents
4079 #define elf32_bed elf32_rx_be_ns_bed
4081 #include "elf32-target.h"
4083 #undef TARGET_LITTLE_SYM
4084 #define TARGET_LITTLE_SYM rx_elf32_linux_le_vec
4085 #undef TARGET_LITTLE_NAME
4086 #define TARGET_LITTLE_NAME "elf32-rx-linux"
4087 #undef TARGET_BIG_SYM
4088 #undef TARGET_BIG_NAME
4090 #undef elf_backend_object_p
4091 #define elf_backend_object_p rx_linux_object_p
4092 #undef elf_symbol_leading_char
4094 #define elf32_bed elf32_rx_le_linux_bed
4096 #include "elf32-target.h"