1 /* Renesas RX specific support for 32-bit ELF.
2 Copyright (C) 2008-2018 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
27 #include "libiberty.h"
30 #define RX_OPCODE_BIG_ENDIAN 0
32 /* This is a meta-target that's used only with objcopy, to avoid the
33 endian-swap we would otherwise get. We check for this in
35 const bfd_target rx_elf32_be_ns_vec
;
36 const bfd_target rx_elf32_be_vec
;
39 char * rx_get_reloc (long);
40 void rx_dump_symtab (bfd
*, void *, void *);
43 #define RXREL(n,sz,bit,shift,complain,pcrel) \
44 HOWTO (R_RX_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
45 bfd_elf_generic_reloc, "R_RX_" #n, FALSE, 0, ~0, FALSE)
47 /* Note that the relocations around 0x7f are internal to this file;
48 feel free to move them as needed to avoid conflicts with published
49 relocation numbers. */
51 static reloc_howto_type rx_elf_howto_table
[] =
53 RXREL (NONE
, 3, 0, 0, dont
, FALSE
),
54 RXREL (DIR32
, 2, 32, 0, signed, FALSE
),
55 RXREL (DIR24S
, 2, 24, 0, signed, FALSE
),
56 RXREL (DIR16
, 1, 16, 0, dont
, FALSE
),
57 RXREL (DIR16U
, 1, 16, 0, unsigned, FALSE
),
58 RXREL (DIR16S
, 1, 16, 0, signed, FALSE
),
59 RXREL (DIR8
, 0, 8, 0, dont
, FALSE
),
60 RXREL (DIR8U
, 0, 8, 0, unsigned, FALSE
),
61 RXREL (DIR8S
, 0, 8, 0, signed, FALSE
),
62 RXREL (DIR24S_PCREL
, 2, 24, 0, signed, TRUE
),
63 RXREL (DIR16S_PCREL
, 1, 16, 0, signed, TRUE
),
64 RXREL (DIR8S_PCREL
, 0, 8, 0, signed, TRUE
),
65 RXREL (DIR16UL
, 1, 16, 2, unsigned, FALSE
),
66 RXREL (DIR16UW
, 1, 16, 1, unsigned, FALSE
),
67 RXREL (DIR8UL
, 0, 8, 2, unsigned, FALSE
),
68 RXREL (DIR8UW
, 0, 8, 1, unsigned, FALSE
),
69 RXREL (DIR32_REV
, 1, 16, 0, dont
, FALSE
),
70 RXREL (DIR16_REV
, 1, 16, 0, dont
, FALSE
),
71 RXREL (DIR3U_PCREL
, 0, 3, 0, dont
, TRUE
),
87 RXREL (RH_3_PCREL
, 0, 3, 0, signed, TRUE
),
88 RXREL (RH_16_OP
, 1, 16, 0, signed, FALSE
),
89 RXREL (RH_24_OP
, 2, 24, 0, signed, FALSE
),
90 RXREL (RH_32_OP
, 2, 32, 0, signed, FALSE
),
91 RXREL (RH_24_UNS
, 2, 24, 0, unsigned, FALSE
),
92 RXREL (RH_8_NEG
, 0, 8, 0, signed, FALSE
),
93 RXREL (RH_16_NEG
, 1, 16, 0, signed, FALSE
),
94 RXREL (RH_24_NEG
, 2, 24, 0, signed, FALSE
),
95 RXREL (RH_32_NEG
, 2, 32, 0, signed, FALSE
),
96 RXREL (RH_DIFF
, 2, 32, 0, signed, FALSE
),
97 RXREL (RH_GPRELB
, 1, 16, 0, unsigned, FALSE
),
98 RXREL (RH_GPRELW
, 1, 16, 0, unsigned, FALSE
),
99 RXREL (RH_GPRELL
, 1, 16, 0, unsigned, FALSE
),
100 RXREL (RH_RELAX
, 0, 0, 0, dont
, FALSE
),
122 RXREL (ABS32
, 2, 32, 0, dont
, FALSE
),
123 RXREL (ABS24S
, 2, 24, 0, signed, FALSE
),
124 RXREL (ABS16
, 1, 16, 0, dont
, FALSE
),
125 RXREL (ABS16U
, 1, 16, 0, unsigned, FALSE
),
126 RXREL (ABS16S
, 1, 16, 0, signed, FALSE
),
127 RXREL (ABS8
, 0, 8, 0, dont
, FALSE
),
128 RXREL (ABS8U
, 0, 8, 0, unsigned, FALSE
),
129 RXREL (ABS8S
, 0, 8, 0, signed, FALSE
),
130 RXREL (ABS24S_PCREL
, 2, 24, 0, signed, TRUE
),
131 RXREL (ABS16S_PCREL
, 1, 16, 0, signed, TRUE
),
132 RXREL (ABS8S_PCREL
, 0, 8, 0, signed, TRUE
),
133 RXREL (ABS16UL
, 1, 16, 0, unsigned, FALSE
),
134 RXREL (ABS16UW
, 1, 16, 0, unsigned, FALSE
),
135 RXREL (ABS8UL
, 0, 8, 0, unsigned, FALSE
),
136 RXREL (ABS8UW
, 0, 8, 0, unsigned, FALSE
),
137 RXREL (ABS32_REV
, 2, 32, 0, dont
, FALSE
),
138 RXREL (ABS16_REV
, 1, 16, 0, dont
, FALSE
),
140 #define STACK_REL_P(x) ((x) <= R_RX_ABS16_REV && (x) >= R_RX_ABS32)
181 /* These are internal. */
182 /* A 5-bit unsigned displacement to a B/W/L address, at bit position 8/12. */
183 /* ---- ---- 4--- 3210. */
184 #define R_RX_RH_ABS5p8B 0x78
185 RXREL (RH_ABS5p8B
, 0, 0, 0, dont
, FALSE
),
186 #define R_RX_RH_ABS5p8W 0x79
187 RXREL (RH_ABS5p8W
, 0, 0, 0, dont
, FALSE
),
188 #define R_RX_RH_ABS5p8L 0x7a
189 RXREL (RH_ABS5p8L
, 0, 0, 0, dont
, FALSE
),
190 /* A 5-bit unsigned displacement to a B/W/L address, at bit position 5/12. */
191 /* ---- -432 1--- 0---. */
192 #define R_RX_RH_ABS5p5B 0x7b
193 RXREL (RH_ABS5p5B
, 0, 0, 0, dont
, FALSE
),
194 #define R_RX_RH_ABS5p5W 0x7c
195 RXREL (RH_ABS5p5W
, 0, 0, 0, dont
, FALSE
),
196 #define R_RX_RH_ABS5p5L 0x7d
197 RXREL (RH_ABS5p5L
, 0, 0, 0, dont
, FALSE
),
198 /* A 4-bit unsigned immediate at bit position 8. */
199 #define R_RX_RH_UIMM4p8 0x7e
200 RXREL (RH_UIMM4p8
, 0, 0, 0, dont
, FALSE
),
201 /* A 4-bit negative unsigned immediate at bit position 8. */
202 #define R_RX_RH_UNEG4p8 0x7f
203 RXREL (RH_UNEG4p8
, 0, 0, 0, dont
, FALSE
),
204 /* End of internal relocs. */
206 RXREL (SYM
, 2, 32, 0, dont
, FALSE
),
207 RXREL (OPneg
, 2, 32, 0, dont
, FALSE
),
208 RXREL (OPadd
, 2, 32, 0, dont
, FALSE
),
209 RXREL (OPsub
, 2, 32, 0, dont
, FALSE
),
210 RXREL (OPmul
, 2, 32, 0, dont
, FALSE
),
211 RXREL (OPdiv
, 2, 32, 0, dont
, FALSE
),
212 RXREL (OPshla
, 2, 32, 0, dont
, FALSE
),
213 RXREL (OPshra
, 2, 32, 0, dont
, FALSE
),
214 RXREL (OPsctsize
, 2, 32, 0, dont
, FALSE
),
215 RXREL (OPscttop
, 2, 32, 0, dont
, FALSE
),
216 RXREL (OPand
, 2, 32, 0, dont
, FALSE
),
217 RXREL (OPor
, 2, 32, 0, dont
, FALSE
),
218 RXREL (OPxor
, 2, 32, 0, dont
, FALSE
),
219 RXREL (OPnot
, 2, 32, 0, dont
, FALSE
),
220 RXREL (OPmod
, 2, 32, 0, dont
, FALSE
),
221 RXREL (OPromtop
, 2, 32, 0, dont
, FALSE
),
222 RXREL (OPramtop
, 2, 32, 0, dont
, FALSE
)
225 /* Map BFD reloc types to RX ELF reloc types. */
229 bfd_reloc_code_real_type bfd_reloc_val
;
230 unsigned int rx_reloc_val
;
233 static const struct rx_reloc_map rx_reloc_map
[] =
235 { BFD_RELOC_NONE
, R_RX_NONE
},
236 { BFD_RELOC_8
, R_RX_DIR8S
},
237 { BFD_RELOC_16
, R_RX_DIR16S
},
238 { BFD_RELOC_24
, R_RX_DIR24S
},
239 { BFD_RELOC_32
, R_RX_DIR32
},
240 { BFD_RELOC_RX_16_OP
, R_RX_DIR16
},
241 { BFD_RELOC_RX_DIR3U_PCREL
, R_RX_DIR3U_PCREL
},
242 { BFD_RELOC_8_PCREL
, R_RX_DIR8S_PCREL
},
243 { BFD_RELOC_16_PCREL
, R_RX_DIR16S_PCREL
},
244 { BFD_RELOC_24_PCREL
, R_RX_DIR24S_PCREL
},
245 { BFD_RELOC_RX_8U
, R_RX_DIR8U
},
246 { BFD_RELOC_RX_16U
, R_RX_DIR16U
},
247 { BFD_RELOC_RX_24U
, R_RX_RH_24_UNS
},
248 { BFD_RELOC_RX_NEG8
, R_RX_RH_8_NEG
},
249 { BFD_RELOC_RX_NEG16
, R_RX_RH_16_NEG
},
250 { BFD_RELOC_RX_NEG24
, R_RX_RH_24_NEG
},
251 { BFD_RELOC_RX_NEG32
, R_RX_RH_32_NEG
},
252 { BFD_RELOC_RX_DIFF
, R_RX_RH_DIFF
},
253 { BFD_RELOC_RX_GPRELB
, R_RX_RH_GPRELB
},
254 { BFD_RELOC_RX_GPRELW
, R_RX_RH_GPRELW
},
255 { BFD_RELOC_RX_GPRELL
, R_RX_RH_GPRELL
},
256 { BFD_RELOC_RX_RELAX
, R_RX_RH_RELAX
},
257 { BFD_RELOC_RX_SYM
, R_RX_SYM
},
258 { BFD_RELOC_RX_OP_SUBTRACT
, R_RX_OPsub
},
259 { BFD_RELOC_RX_OP_NEG
, R_RX_OPneg
},
260 { BFD_RELOC_RX_ABS8
, R_RX_ABS8
},
261 { BFD_RELOC_RX_ABS16
, R_RX_ABS16
},
262 { BFD_RELOC_RX_ABS16_REV
, R_RX_ABS16_REV
},
263 { BFD_RELOC_RX_ABS32
, R_RX_ABS32
},
264 { BFD_RELOC_RX_ABS32_REV
, R_RX_ABS32_REV
},
265 { BFD_RELOC_RX_ABS16UL
, R_RX_ABS16UL
},
266 { BFD_RELOC_RX_ABS16UW
, R_RX_ABS16UW
},
267 { BFD_RELOC_RX_ABS16U
, R_RX_ABS16U
}
270 #define BIGE(abfd) ((abfd)->xvec->byteorder == BFD_ENDIAN_BIG)
272 static reloc_howto_type
*
273 rx_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
274 bfd_reloc_code_real_type code
)
278 if (code
== BFD_RELOC_RX_32_OP
)
279 return rx_elf_howto_table
+ R_RX_DIR32
;
281 for (i
= ARRAY_SIZE (rx_reloc_map
); i
--;)
282 if (rx_reloc_map
[i
].bfd_reloc_val
== code
)
283 return rx_elf_howto_table
+ rx_reloc_map
[i
].rx_reloc_val
;
288 static reloc_howto_type
*
289 rx_reloc_name_lookup (bfd
* abfd ATTRIBUTE_UNUSED
, const char * r_name
)
293 for (i
= 0; i
< ARRAY_SIZE (rx_elf_howto_table
); i
++)
294 if (rx_elf_howto_table
[i
].name
!= NULL
295 && strcasecmp (rx_elf_howto_table
[i
].name
, r_name
) == 0)
296 return rx_elf_howto_table
+ i
;
301 /* Set the howto pointer for an RX ELF reloc. */
304 rx_info_to_howto_rela (bfd
* abfd ATTRIBUTE_UNUSED
,
306 Elf_Internal_Rela
* dst
)
310 r_type
= ELF32_R_TYPE (dst
->r_info
);
311 if (r_type
>= (unsigned int) R_RX_max
)
313 /* xgettext:c-format */
314 _bfd_error_handler (_("%pB: invalid RX reloc number: %d"), abfd
, r_type
);
317 cache_ptr
->howto
= rx_elf_howto_table
+ r_type
;
321 get_symbol_value (const char * name
,
322 struct bfd_link_info
* info
,
324 asection
* input_section
,
328 struct bfd_link_hash_entry
* h
;
330 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
333 || (h
->type
!= bfd_link_hash_defined
334 && h
->type
!= bfd_link_hash_defweak
))
335 (*info
->callbacks
->undefined_symbol
)
336 (info
, name
, input_bfd
, input_section
, offset
, TRUE
);
338 value
= (h
->u
.def
.value
339 + h
->u
.def
.section
->output_section
->vma
340 + h
->u
.def
.section
->output_offset
);
346 get_symbol_value_maybe (const char * name
,
347 struct bfd_link_info
* info
)
350 struct bfd_link_hash_entry
* h
;
352 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
355 || (h
->type
!= bfd_link_hash_defined
356 && h
->type
!= bfd_link_hash_defweak
))
359 value
= (h
->u
.def
.value
360 + h
->u
.def
.section
->output_section
->vma
361 + h
->u
.def
.section
->output_offset
);
367 get_gp (struct bfd_link_info
* info
,
372 static bfd_boolean cached
= FALSE
;
373 static bfd_vma cached_value
= 0;
377 cached_value
= get_symbol_value ("__gp", info
, abfd
, sec
, offset
);
384 get_romstart (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", info
, abfd
, sec
, offset
);
401 get_ramstart (struct bfd_link_info
* info
,
406 static bfd_boolean cached
= FALSE
;
407 static bfd_vma cached_value
= 0;
411 cached_value
= get_symbol_value ("__datastart", info
, abfd
, sec
, offset
);
417 #define NUM_STACK_ENTRIES 16
418 static int32_t rx_stack
[ NUM_STACK_ENTRIES
];
419 static unsigned int rx_stack_top
;
421 #define RX_STACK_PUSH(val) \
424 if (rx_stack_top < NUM_STACK_ENTRIES) \
425 rx_stack [rx_stack_top ++] = (val); \
427 r = bfd_reloc_dangerous; \
431 #define RX_STACK_POP(dest) \
434 if (rx_stack_top > 0) \
435 (dest) = rx_stack [-- rx_stack_top]; \
437 (dest) = 0, r = bfd_reloc_dangerous; \
441 /* Relocate an RX ELF section.
442 There is some attempt to make this function usable for many architectures,
443 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
444 if only to serve as a learning tool.
446 The RELOCATE_SECTION function is called by the new ELF backend linker
447 to handle the relocations for a section.
449 The relocs are always passed as Rela structures; if the section
450 actually uses Rel structures, the r_addend field will always be
453 This function is responsible for adjusting the section contents as
454 necessary, and (if using Rela relocs and generating a relocatable
455 output file) adjusting the reloc addend as necessary.
457 This function does not have to worry about setting the reloc
458 address or the reloc symbol index.
460 LOCAL_SYMS is a pointer to the swapped in local symbols.
462 LOCAL_SECTIONS is an array giving the section in the input file
463 corresponding to the st_shndx field of each local symbol.
465 The global hash table entry for the global symbols can be found
466 via elf_sym_hashes (input_bfd).
468 When generating relocatable output, this function must handle
469 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
470 going to be the section symbol corresponding to the output
471 section, which means that the addend must be adjusted
475 rx_elf_relocate_section
477 struct bfd_link_info
* info
,
479 asection
* input_section
,
481 Elf_Internal_Rela
* relocs
,
482 Elf_Internal_Sym
* local_syms
,
483 asection
** local_sections
)
485 Elf_Internal_Shdr
* symtab_hdr
;
486 struct elf_link_hash_entry
** sym_hashes
;
487 Elf_Internal_Rela
* rel
;
488 Elf_Internal_Rela
* relend
;
489 bfd_boolean pid_mode
;
490 bfd_boolean saw_subtract
= FALSE
;
491 const char * table_default_cache
= NULL
;
492 bfd_vma table_start_cache
= 0;
493 bfd_vma table_end_cache
= 0;
495 if (elf_elfheader (output_bfd
)->e_flags
& E_FLAG_RX_PID
)
500 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
501 sym_hashes
= elf_sym_hashes (input_bfd
);
502 relend
= relocs
+ input_section
->reloc_count
;
503 for (rel
= relocs
; rel
< relend
; rel
++)
505 reloc_howto_type
* howto
;
506 unsigned long r_symndx
;
507 Elf_Internal_Sym
* sym
;
509 struct elf_link_hash_entry
* h
;
511 bfd_reloc_status_type r
;
512 const char * name
= NULL
;
513 bfd_boolean unresolved_reloc
= TRUE
;
516 r_type
= ELF32_R_TYPE (rel
->r_info
);
517 r_symndx
= ELF32_R_SYM (rel
->r_info
);
519 howto
= rx_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
525 if (rx_stack_top
== 0)
526 saw_subtract
= FALSE
;
528 if (r_symndx
< symtab_hdr
->sh_info
)
530 sym
= local_syms
+ r_symndx
;
531 sec
= local_sections
[r_symndx
];
532 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, & sec
, rel
);
534 name
= bfd_elf_string_from_elf_section
535 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
536 name
= (sym
->st_name
== 0) ? bfd_section_name (input_bfd
, sec
) : name
;
540 bfd_boolean warned
, ignored
;
542 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
543 r_symndx
, symtab_hdr
, sym_hashes
, h
,
544 sec
, relocation
, unresolved_reloc
,
547 name
= h
->root
.root
.string
;
550 if (strncmp (name
, "$tableentry$default$", 20) == 0)
556 if (table_default_cache
!= name
)
559 /* All relocs for a given table should be to the same
560 (weak) default symbol) so we can use it to detect a
561 cache miss. We use the offset into the table to find
562 the "real" symbol. Calculate and store the table's
565 table_default_cache
= name
;
567 /* We have already done error checking in rx_table_find(). */
569 buf
= (char *) malloc (13 + strlen (name
+ 20));
571 sprintf (buf
, "$tablestart$%s", name
+ 20);
572 table_start_cache
= get_symbol_value (buf
,
578 sprintf (buf
, "$tableend$%s", name
+ 20);
579 table_end_cache
= get_symbol_value (buf
,
588 entry_vma
= (input_section
->output_section
->vma
589 + input_section
->output_offset
592 if (table_end_cache
<= entry_vma
|| entry_vma
< table_start_cache
)
594 /* xgettext:c-format */
595 _bfd_error_handler (_("%pB:%pA: table entry %s outside table"),
596 input_bfd
, input_section
,
599 else if ((int) (entry_vma
- table_start_cache
) % 4)
601 /* xgettext:c-format */
602 _bfd_error_handler (_("%pB:%pA: table entry %s not word-aligned within table"),
603 input_bfd
, input_section
,
608 idx
= (int) (entry_vma
- table_start_cache
) / 4;
610 /* This will look like $tableentry$<N>$<name> */
611 buf
= (char *) malloc (12 + 20 + strlen (name
+ 20));
612 sprintf (buf
, "$tableentry$%d$%s", idx
, name
+ 20);
614 h
= (struct elf_link_hash_entry
*) bfd_link_hash_lookup (info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
618 relocation
= (h
->root
.u
.def
.value
619 + h
->root
.u
.def
.section
->output_section
->vma
620 + h
->root
.u
.def
.section
->output_offset
);;
627 if (sec
!= NULL
&& discarded_section (sec
))
628 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
629 rel
, 1, relend
, howto
, 0, contents
);
631 if (bfd_link_relocatable (info
))
633 /* This is a relocatable link. We don't have to change
634 anything, unless the reloc is against a section symbol,
635 in which case we have to adjust according to where the
636 section symbol winds up in the output section. */
637 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
638 rel
->r_addend
+= sec
->output_offset
;
642 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
643 /* If the symbol is undefined and weak
644 then the relocation resolves to zero. */
648 if (howto
->pc_relative
)
650 relocation
-= (input_section
->output_section
->vma
651 + input_section
->output_offset
653 if (r_type
!= R_RX_RH_3_PCREL
654 && r_type
!= R_RX_DIR3U_PCREL
)
658 relocation
+= rel
->r_addend
;
663 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
664 #define ALIGN(m) if (relocation & m) r = bfd_reloc_other;
665 #define OP(i) (contents[rel->r_offset + (i)])
666 #define WARN_REDHAT(type) \
667 /* xgettext:c-format */ \
668 _bfd_error_handler (_("%pB:%pA: Warning: deprecated Red Hat reloc " type " detected against: %s."), \
669 input_bfd, input_section, name)
671 /* Check for unsafe relocs in PID mode. These are any relocs where
672 an absolute address is being computed. There are special cases
673 for relocs against symbols that are known to be referenced in
674 crt0.o before the PID base address register has been initialised. */
675 #define UNSAFE_FOR_PID \
680 && sec->flags & SEC_READONLY \
681 && !(input_section->flags & SEC_DEBUGGING) \
682 && strcmp (name, "__pid_base") != 0 \
683 && strcmp (name, "__gp") != 0 \
684 && strcmp (name, "__romdatastart") != 0 \
686 /* xgettext:c-format */ \
687 _bfd_error_handler (_("%pB(%pA): unsafe PID relocation %s " \
688 "at %#" PRIx64 " (against %s in %s)"), \
689 input_bfd, input_section, howto->name, \
690 (uint64_t) (input_section->output_section->vma \
691 + input_section->output_offset \
697 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
706 case R_RX_RH_3_PCREL
:
707 WARN_REDHAT ("RX_RH_3_PCREL");
710 OP (0) |= relocation
& 0x07;
714 WARN_REDHAT ("RX_RH_8_NEG");
715 relocation
= - relocation
;
717 case R_RX_DIR8S_PCREL
:
736 WARN_REDHAT ("RX_RH_16_NEG");
737 relocation
= - relocation
;
739 case R_RX_DIR16S_PCREL
:
741 RANGE (-32768, 32767);
742 #if RX_OPCODE_BIG_ENDIAN
745 OP (1) = relocation
>> 8;
750 WARN_REDHAT ("RX_RH_16_OP");
752 RANGE (-32768, 32767);
753 #if RX_OPCODE_BIG_ENDIAN
755 OP (0) = relocation
>> 8;
758 OP (1) = relocation
>> 8;
764 RANGE (-32768, 65535);
765 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
768 OP (0) = relocation
>> 8;
773 OP (1) = relocation
>> 8;
780 #if RX_OPCODE_BIG_ENDIAN
782 OP (0) = relocation
>> 8;
785 OP (1) = relocation
>> 8;
791 RANGE (-32768, 65536);
792 #if RX_OPCODE_BIG_ENDIAN
794 OP (0) = relocation
>> 8;
797 OP (1) = relocation
>> 8;
803 RANGE (-32768, 65536);
804 #if RX_OPCODE_BIG_ENDIAN
806 OP (1) = relocation
>> 8;
809 OP (0) = relocation
>> 8;
813 case R_RX_DIR3U_PCREL
:
816 OP (0) |= relocation
& 0x07;
821 WARN_REDHAT ("RX_RH_24_NEG");
822 relocation
= - relocation
;
824 case R_RX_DIR24S_PCREL
:
825 RANGE (-0x800000, 0x7fffff);
826 #if RX_OPCODE_BIG_ENDIAN
828 OP (1) = relocation
>> 8;
829 OP (0) = relocation
>> 16;
832 OP (1) = relocation
>> 8;
833 OP (2) = relocation
>> 16;
839 WARN_REDHAT ("RX_RH_24_OP");
840 RANGE (-0x800000, 0x7fffff);
841 #if RX_OPCODE_BIG_ENDIAN
843 OP (1) = relocation
>> 8;
844 OP (0) = relocation
>> 16;
847 OP (1) = relocation
>> 8;
848 OP (2) = relocation
>> 16;
854 RANGE (-0x800000, 0x7fffff);
855 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
858 OP (1) = relocation
>> 8;
859 OP (0) = relocation
>> 16;
864 OP (1) = relocation
>> 8;
865 OP (2) = relocation
>> 16;
871 WARN_REDHAT ("RX_RH_24_UNS");
873 #if RX_OPCODE_BIG_ENDIAN
875 OP (1) = relocation
>> 8;
876 OP (0) = relocation
>> 16;
879 OP (1) = relocation
>> 8;
880 OP (2) = relocation
>> 16;
886 WARN_REDHAT ("RX_RH_32_NEG");
887 relocation
= - relocation
;
888 #if RX_OPCODE_BIG_ENDIAN
890 OP (2) = relocation
>> 8;
891 OP (1) = relocation
>> 16;
892 OP (0) = relocation
>> 24;
895 OP (1) = relocation
>> 8;
896 OP (2) = relocation
>> 16;
897 OP (3) = relocation
>> 24;
903 WARN_REDHAT ("RX_RH_32_OP");
904 #if RX_OPCODE_BIG_ENDIAN
906 OP (2) = relocation
>> 8;
907 OP (1) = relocation
>> 16;
908 OP (0) = relocation
>> 24;
911 OP (1) = relocation
>> 8;
912 OP (2) = relocation
>> 16;
913 OP (3) = relocation
>> 24;
918 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
921 OP (2) = relocation
>> 8;
922 OP (1) = relocation
>> 16;
923 OP (0) = relocation
>> 24;
928 OP (1) = relocation
>> 8;
929 OP (2) = relocation
>> 16;
930 OP (3) = relocation
>> 24;
935 if (BIGE (output_bfd
))
938 OP (1) = relocation
>> 8;
939 OP (2) = relocation
>> 16;
940 OP (3) = relocation
>> 24;
945 OP (2) = relocation
>> 8;
946 OP (1) = relocation
>> 16;
947 OP (0) = relocation
>> 24;
954 WARN_REDHAT ("RX_RH_DIFF");
955 val
= bfd_get_32 (output_bfd
, & OP (0));
957 bfd_put_32 (output_bfd
, val
, & OP (0));
962 WARN_REDHAT ("RX_RH_GPRELB");
963 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
965 #if RX_OPCODE_BIG_ENDIAN
967 OP (0) = relocation
>> 8;
970 OP (1) = relocation
>> 8;
975 WARN_REDHAT ("RX_RH_GPRELW");
976 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
980 #if RX_OPCODE_BIG_ENDIAN
982 OP (0) = relocation
>> 8;
985 OP (1) = relocation
>> 8;
990 WARN_REDHAT ("RX_RH_GPRELL");
991 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
995 #if RX_OPCODE_BIG_ENDIAN
997 OP (0) = relocation
>> 8;
1000 OP (1) = relocation
>> 8;
1004 /* Internal relocations just for relaxation: */
1005 case R_RX_RH_ABS5p5B
:
1006 RX_STACK_POP (relocation
);
1009 OP (0) |= relocation
>> 2;
1011 OP (1) |= (relocation
<< 6) & 0x80;
1012 OP (1) |= (relocation
<< 3) & 0x08;
1015 case R_RX_RH_ABS5p5W
:
1016 RX_STACK_POP (relocation
);
1021 OP (0) |= relocation
>> 2;
1023 OP (1) |= (relocation
<< 6) & 0x80;
1024 OP (1) |= (relocation
<< 3) & 0x08;
1027 case R_RX_RH_ABS5p5L
:
1028 RX_STACK_POP (relocation
);
1033 OP (0) |= relocation
>> 2;
1035 OP (1) |= (relocation
<< 6) & 0x80;
1036 OP (1) |= (relocation
<< 3) & 0x08;
1039 case R_RX_RH_ABS5p8B
:
1040 RX_STACK_POP (relocation
);
1043 OP (0) |= (relocation
<< 3) & 0x80;
1044 OP (0) |= relocation
& 0x0f;
1047 case R_RX_RH_ABS5p8W
:
1048 RX_STACK_POP (relocation
);
1053 OP (0) |= (relocation
<< 3) & 0x80;
1054 OP (0) |= relocation
& 0x0f;
1057 case R_RX_RH_ABS5p8L
:
1058 RX_STACK_POP (relocation
);
1063 OP (0) |= (relocation
<< 3) & 0x80;
1064 OP (0) |= relocation
& 0x0f;
1067 case R_RX_RH_UIMM4p8
:
1070 OP (0) |= relocation
<< 4;
1073 case R_RX_RH_UNEG4p8
:
1076 OP (0) |= (-relocation
) << 4;
1079 /* Complex reloc handling: */
1083 RX_STACK_POP (relocation
);
1084 #if RX_OPCODE_BIG_ENDIAN
1085 OP (3) = relocation
;
1086 OP (2) = relocation
>> 8;
1087 OP (1) = relocation
>> 16;
1088 OP (0) = relocation
>> 24;
1090 OP (0) = relocation
;
1091 OP (1) = relocation
>> 8;
1092 OP (2) = relocation
>> 16;
1093 OP (3) = relocation
>> 24;
1097 case R_RX_ABS32_REV
:
1099 RX_STACK_POP (relocation
);
1100 #if RX_OPCODE_BIG_ENDIAN
1101 OP (0) = relocation
;
1102 OP (1) = relocation
>> 8;
1103 OP (2) = relocation
>> 16;
1104 OP (3) = relocation
>> 24;
1106 OP (3) = relocation
;
1107 OP (2) = relocation
>> 8;
1108 OP (1) = relocation
>> 16;
1109 OP (0) = relocation
>> 24;
1113 case R_RX_ABS24S_PCREL
:
1116 RX_STACK_POP (relocation
);
1117 RANGE (-0x800000, 0x7fffff);
1118 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
1120 OP (2) = relocation
;
1121 OP (1) = relocation
>> 8;
1122 OP (0) = relocation
>> 16;
1126 OP (0) = relocation
;
1127 OP (1) = relocation
>> 8;
1128 OP (2) = relocation
>> 16;
1134 RX_STACK_POP (relocation
);
1135 RANGE (-32768, 65535);
1136 #if RX_OPCODE_BIG_ENDIAN
1137 OP (1) = relocation
;
1138 OP (0) = relocation
>> 8;
1140 OP (0) = relocation
;
1141 OP (1) = relocation
>> 8;
1145 case R_RX_ABS16_REV
:
1147 RX_STACK_POP (relocation
);
1148 RANGE (-32768, 65535);
1149 #if RX_OPCODE_BIG_ENDIAN
1150 OP (0) = relocation
;
1151 OP (1) = relocation
>> 8;
1153 OP (1) = relocation
;
1154 OP (0) = relocation
>> 8;
1158 case R_RX_ABS16S_PCREL
:
1160 RX_STACK_POP (relocation
);
1161 RANGE (-32768, 32767);
1162 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
1164 OP (1) = relocation
;
1165 OP (0) = relocation
>> 8;
1169 OP (0) = relocation
;
1170 OP (1) = relocation
>> 8;
1176 RX_STACK_POP (relocation
);
1178 #if RX_OPCODE_BIG_ENDIAN
1179 OP (1) = relocation
;
1180 OP (0) = relocation
>> 8;
1182 OP (0) = relocation
;
1183 OP (1) = relocation
>> 8;
1189 RX_STACK_POP (relocation
);
1192 #if RX_OPCODE_BIG_ENDIAN
1193 OP (1) = relocation
;
1194 OP (0) = relocation
>> 8;
1196 OP (0) = relocation
;
1197 OP (1) = relocation
>> 8;
1203 RX_STACK_POP (relocation
);
1206 #if RX_OPCODE_BIG_ENDIAN
1207 OP (1) = relocation
;
1208 OP (0) = relocation
>> 8;
1210 OP (0) = relocation
;
1211 OP (1) = relocation
>> 8;
1217 RX_STACK_POP (relocation
);
1219 OP (0) = relocation
;
1224 RX_STACK_POP (relocation
);
1226 OP (0) = relocation
;
1231 RX_STACK_POP (relocation
);
1234 OP (0) = relocation
;
1239 RX_STACK_POP (relocation
);
1242 OP (0) = relocation
;
1248 case R_RX_ABS8S_PCREL
:
1249 RX_STACK_POP (relocation
);
1251 OP (0) = relocation
;
1255 if (r_symndx
< symtab_hdr
->sh_info
)
1256 RX_STACK_PUSH (sec
->output_section
->vma
1257 + sec
->output_offset
1263 && (h
->root
.type
== bfd_link_hash_defined
1264 || h
->root
.type
== bfd_link_hash_defweak
))
1265 RX_STACK_PUSH (h
->root
.u
.def
.value
1266 + sec
->output_section
->vma
1267 + sec
->output_offset
1270 _bfd_error_handler (_("Warning: RX_SYM reloc with an unknown symbol"));
1278 saw_subtract
= TRUE
;
1281 RX_STACK_PUSH (tmp
);
1289 RX_STACK_POP (tmp1
);
1290 RX_STACK_POP (tmp2
);
1292 RX_STACK_PUSH (tmp1
);
1300 saw_subtract
= TRUE
;
1301 RX_STACK_POP (tmp1
);
1302 RX_STACK_POP (tmp2
);
1304 RX_STACK_PUSH (tmp2
);
1312 RX_STACK_POP (tmp1
);
1313 RX_STACK_POP (tmp2
);
1315 RX_STACK_PUSH (tmp1
);
1323 RX_STACK_POP (tmp1
);
1324 RX_STACK_POP (tmp2
);
1326 RX_STACK_PUSH (tmp1
);
1334 RX_STACK_POP (tmp1
);
1335 RX_STACK_POP (tmp2
);
1337 RX_STACK_PUSH (tmp1
);
1345 RX_STACK_POP (tmp1
);
1346 RX_STACK_POP (tmp2
);
1348 RX_STACK_PUSH (tmp1
);
1352 case R_RX_OPsctsize
:
1353 RX_STACK_PUSH (input_section
->size
);
1357 RX_STACK_PUSH (input_section
->output_section
->vma
);
1364 RX_STACK_POP (tmp1
);
1365 RX_STACK_POP (tmp2
);
1367 RX_STACK_PUSH (tmp1
);
1375 RX_STACK_POP (tmp1
);
1376 RX_STACK_POP (tmp2
);
1378 RX_STACK_PUSH (tmp1
);
1386 RX_STACK_POP (tmp1
);
1387 RX_STACK_POP (tmp2
);
1389 RX_STACK_PUSH (tmp1
);
1399 RX_STACK_PUSH (tmp
);
1407 RX_STACK_POP (tmp1
);
1408 RX_STACK_POP (tmp2
);
1410 RX_STACK_PUSH (tmp1
);
1415 RX_STACK_PUSH (get_romstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1419 RX_STACK_PUSH (get_ramstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1423 r
= bfd_reloc_notsupported
;
1427 if (r
!= bfd_reloc_ok
)
1429 const char * msg
= NULL
;
1433 case bfd_reloc_overflow
:
1434 /* Catch the case of a missing function declaration
1435 and emit a more helpful error message. */
1436 if (r_type
== R_RX_DIR24S_PCREL
)
1437 /* xgettext:c-format */
1438 msg
= _("%pB(%pA): error: call to undefined function '%s'");
1440 (*info
->callbacks
->reloc_overflow
)
1441 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
1442 input_bfd
, input_section
, rel
->r_offset
);
1445 case bfd_reloc_undefined
:
1446 (*info
->callbacks
->undefined_symbol
)
1447 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1450 case bfd_reloc_other
:
1451 /* xgettext:c-format */
1452 msg
= _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
1455 case bfd_reloc_outofrange
:
1456 /* xgettext:c-format */
1457 msg
= _("%pB(%pA): internal error: out of range error");
1460 case bfd_reloc_notsupported
:
1461 /* xgettext:c-format */
1462 msg
= _("%pB(%pA): internal error: unsupported relocation error");
1465 case bfd_reloc_dangerous
:
1466 /* xgettext:c-format */
1467 msg
= _("%pB(%pA): internal error: dangerous relocation");
1471 /* xgettext:c-format */
1472 msg
= _("%pB(%pA): internal error: unknown error");
1477 _bfd_error_handler (msg
, input_bfd
, input_section
, name
);
1484 /* Relaxation Support. */
1486 /* Progression of relocations from largest operand size to smallest
1490 next_smaller_reloc (int r
)
1494 case R_RX_DIR32
: return R_RX_DIR24S
;
1495 case R_RX_DIR24S
: return R_RX_DIR16S
;
1496 case R_RX_DIR16S
: return R_RX_DIR8S
;
1497 case R_RX_DIR8S
: return R_RX_NONE
;
1499 case R_RX_DIR16
: return R_RX_DIR8
;
1500 case R_RX_DIR8
: return R_RX_NONE
;
1502 case R_RX_DIR16U
: return R_RX_DIR8U
;
1503 case R_RX_DIR8U
: return R_RX_NONE
;
1505 case R_RX_DIR24S_PCREL
: return R_RX_DIR16S_PCREL
;
1506 case R_RX_DIR16S_PCREL
: return R_RX_DIR8S_PCREL
;
1507 case R_RX_DIR8S_PCREL
: return R_RX_DIR3U_PCREL
;
1509 case R_RX_DIR16UL
: return R_RX_DIR8UL
;
1510 case R_RX_DIR8UL
: return R_RX_NONE
;
1511 case R_RX_DIR16UW
: return R_RX_DIR8UW
;
1512 case R_RX_DIR8UW
: return R_RX_NONE
;
1514 case R_RX_RH_32_OP
: return R_RX_RH_24_OP
;
1515 case R_RX_RH_24_OP
: return R_RX_RH_16_OP
;
1516 case R_RX_RH_16_OP
: return R_RX_DIR8
;
1518 case R_RX_ABS32
: return R_RX_ABS24S
;
1519 case R_RX_ABS24S
: return R_RX_ABS16S
;
1520 case R_RX_ABS16
: return R_RX_ABS8
;
1521 case R_RX_ABS16U
: return R_RX_ABS8U
;
1522 case R_RX_ABS16S
: return R_RX_ABS8S
;
1523 case R_RX_ABS8
: return R_RX_NONE
;
1524 case R_RX_ABS8U
: return R_RX_NONE
;
1525 case R_RX_ABS8S
: return R_RX_NONE
;
1526 case R_RX_ABS24S_PCREL
: return R_RX_ABS16S_PCREL
;
1527 case R_RX_ABS16S_PCREL
: return R_RX_ABS8S_PCREL
;
1528 case R_RX_ABS8S_PCREL
: return R_RX_NONE
;
1529 case R_RX_ABS16UL
: return R_RX_ABS8UL
;
1530 case R_RX_ABS16UW
: return R_RX_ABS8UW
;
1531 case R_RX_ABS8UL
: return R_RX_NONE
;
1532 case R_RX_ABS8UW
: return R_RX_NONE
;
1537 /* Delete some bytes from a section while relaxing. */
1540 elf32_rx_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, int count
,
1541 Elf_Internal_Rela
*alignment_rel
, int force_snip
,
1542 Elf_Internal_Rela
*irelstart
)
1544 Elf_Internal_Shdr
* symtab_hdr
;
1545 unsigned int sec_shndx
;
1546 bfd_byte
* contents
;
1547 Elf_Internal_Rela
* irel
;
1548 Elf_Internal_Rela
* irelend
;
1549 Elf_Internal_Sym
* isym
;
1550 Elf_Internal_Sym
* isymend
;
1552 unsigned int symcount
;
1553 struct elf_link_hash_entry
** sym_hashes
;
1554 struct elf_link_hash_entry
** end_hashes
;
1559 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1561 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1563 /* The deletion must stop at the next alignment boundary, if
1564 ALIGNMENT_REL is non-NULL. */
1567 toaddr
= alignment_rel
->r_offset
;
1569 BFD_ASSERT (toaddr
> addr
);
1571 /* Actually delete the bytes. */
1572 memmove (contents
+ addr
, contents
+ addr
+ count
,
1573 (size_t) (toaddr
- addr
- count
));
1575 /* If we don't have an alignment marker to worry about, we can just
1576 shrink the section. Otherwise, we have to fill in the newly
1577 created gap with NOP insns (0x03). */
1581 memset (contents
+ toaddr
- count
, 0x03, count
);
1584 BFD_ASSERT (irel
!= NULL
|| sec
->reloc_count
== 0);
1585 irelend
= irel
+ sec
->reloc_count
;
1587 /* Adjust all the relocs. */
1588 for (; irel
< irelend
; irel
++)
1590 /* Get the new reloc address. */
1591 if (irel
->r_offset
> addr
1592 && (irel
->r_offset
< toaddr
1593 || (force_snip
&& irel
->r_offset
== toaddr
)))
1594 irel
->r_offset
-= count
;
1596 /* If we see an ALIGN marker at the end of the gap, we move it
1597 to the beginning of the gap, since marking these gaps is what
1599 if (irel
->r_offset
== toaddr
1600 && ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
1601 && irel
->r_addend
& RX_RELAXA_ALIGN
)
1602 irel
->r_offset
-= count
;
1605 /* Adjust the local symbols defined in this section. */
1606 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1607 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1608 isymend
= isym
+ symtab_hdr
->sh_info
;
1610 for (; isym
< isymend
; isym
++)
1612 /* If the symbol is in the range of memory we just moved, we
1613 have to adjust its value. */
1614 if (isym
->st_shndx
== sec_shndx
1615 && isym
->st_value
> addr
1616 && isym
->st_value
< toaddr
)
1617 isym
->st_value
-= count
;
1619 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1620 *end* is in the moved bytes but it's *start* isn't), then we
1621 must adjust its size. */
1622 if (isym
->st_shndx
== sec_shndx
1623 && isym
->st_value
< addr
1624 && isym
->st_value
+ isym
->st_size
> addr
1625 && isym
->st_value
+ isym
->st_size
< toaddr
)
1626 isym
->st_size
-= count
;
1629 /* Now adjust the global symbols defined in this section. */
1630 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1631 - symtab_hdr
->sh_info
);
1632 sym_hashes
= elf_sym_hashes (abfd
);
1633 end_hashes
= sym_hashes
+ symcount
;
1635 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1637 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1639 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1640 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1641 && sym_hash
->root
.u
.def
.section
== sec
)
1643 /* As above, adjust the value if needed. */
1644 if (sym_hash
->root
.u
.def
.value
> addr
1645 && sym_hash
->root
.u
.def
.value
< toaddr
)
1646 sym_hash
->root
.u
.def
.value
-= count
;
1648 /* As above, adjust the size if needed. */
1649 if (sym_hash
->root
.u
.def
.value
< addr
1650 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
1651 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
1652 sym_hash
->size
-= count
;
1659 /* Used to sort relocs by address. If relocs have the same address,
1660 we maintain their relative order, except that R_RX_RH_RELAX
1661 alignment relocs must be the first reloc for any given address. */
1664 reloc_bubblesort (Elf_Internal_Rela
* r
, int count
)
1668 bfd_boolean swappit
;
1670 /* This is almost a classic bubblesort. It's the slowest sort, but
1671 we're taking advantage of the fact that the relocations are
1672 mostly in order already (the assembler emits them that way) and
1673 we need relocs with the same address to remain in the same
1679 for (i
= 0; i
< count
- 1; i
++)
1681 if (r
[i
].r_offset
> r
[i
+ 1].r_offset
)
1683 else if (r
[i
].r_offset
< r
[i
+ 1].r_offset
)
1685 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1686 && (r
[i
+ 1].r_addend
& RX_RELAXA_ALIGN
))
1688 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1689 && (r
[i
+ 1].r_addend
& RX_RELAXA_ELIGN
)
1690 && !(ELF32_R_TYPE (r
[i
].r_info
) == R_RX_RH_RELAX
1691 && (r
[i
].r_addend
& RX_RELAXA_ALIGN
)))
1698 Elf_Internal_Rela tmp
;
1703 /* If we do move a reloc back, re-scan to see if it
1704 needs to be moved even further back. This avoids
1705 most of the O(n^2) behavior for our cases. */
1715 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1716 rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1717 lrel, abfd, sec, link_info, scale)
1720 rx_offset_for_reloc (bfd
* abfd
,
1721 Elf_Internal_Rela
* rel
,
1722 Elf_Internal_Shdr
* symtab_hdr
,
1723 Elf_External_Sym_Shndx
* shndx_buf ATTRIBUTE_UNUSED
,
1724 Elf_Internal_Sym
* intsyms
,
1725 Elf_Internal_Rela
** lrel
,
1727 asection
* input_section
,
1728 struct bfd_link_info
* info
,
1732 bfd_reloc_status_type r
;
1736 /* REL is the first of 1..N relocations. We compute the symbol
1737 value for each relocation, then combine them if needed. LREL
1738 gets a pointer to the last relocation used. */
1743 /* Get the value of the symbol referred to by the reloc. */
1744 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1746 /* A local symbol. */
1747 Elf_Internal_Sym
*isym
;
1750 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1752 if (isym
->st_shndx
== SHN_UNDEF
)
1753 ssec
= bfd_und_section_ptr
;
1754 else if (isym
->st_shndx
== SHN_ABS
)
1755 ssec
= bfd_abs_section_ptr
;
1756 else if (isym
->st_shndx
== SHN_COMMON
)
1757 ssec
= bfd_com_section_ptr
;
1759 ssec
= bfd_section_from_elf_index (abfd
,
1762 /* Initial symbol value. */
1763 symval
= isym
->st_value
;
1765 /* GAS may have made this symbol relative to a section, in
1766 which case, we have to add the addend to find the
1768 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
1769 symval
+= rel
->r_addend
;
1773 if ((ssec
->flags
& SEC_MERGE
)
1774 && ssec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
1775 symval
= _bfd_merged_section_offset (abfd
, & ssec
,
1776 elf_section_data (ssec
)->sec_info
,
1780 /* Now make the offset relative to where the linker is putting it. */
1783 ssec
->output_section
->vma
+ ssec
->output_offset
;
1785 symval
+= rel
->r_addend
;
1790 struct elf_link_hash_entry
* h
;
1792 /* An external symbol. */
1793 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1794 h
= elf_sym_hashes (abfd
)[indx
];
1795 BFD_ASSERT (h
!= NULL
);
1797 if (h
->root
.type
!= bfd_link_hash_defined
1798 && h
->root
.type
!= bfd_link_hash_defweak
)
1800 /* This appears to be a reference to an undefined
1801 symbol. Just ignore it--it will be caught by the
1802 regular reloc processing. */
1808 symval
= (h
->root
.u
.def
.value
1809 + h
->root
.u
.def
.section
->output_section
->vma
1810 + h
->root
.u
.def
.section
->output_offset
);
1812 symval
+= rel
->r_addend
;
1815 switch (ELF32_R_TYPE (rel
->r_info
))
1818 RX_STACK_PUSH (symval
);
1822 RX_STACK_POP (tmp1
);
1824 RX_STACK_PUSH (tmp1
);
1828 RX_STACK_POP (tmp1
);
1829 RX_STACK_POP (tmp2
);
1831 RX_STACK_PUSH (tmp1
);
1835 RX_STACK_POP (tmp1
);
1836 RX_STACK_POP (tmp2
);
1838 RX_STACK_PUSH (tmp2
);
1842 RX_STACK_POP (tmp1
);
1843 RX_STACK_POP (tmp2
);
1845 RX_STACK_PUSH (tmp1
);
1849 RX_STACK_POP (tmp1
);
1850 RX_STACK_POP (tmp2
);
1852 RX_STACK_PUSH (tmp1
);
1856 RX_STACK_POP (tmp1
);
1857 RX_STACK_POP (tmp2
);
1859 RX_STACK_PUSH (tmp1
);
1863 RX_STACK_POP (tmp1
);
1864 RX_STACK_POP (tmp2
);
1866 RX_STACK_PUSH (tmp1
);
1869 case R_RX_OPsctsize
:
1870 RX_STACK_PUSH (input_section
->size
);
1874 RX_STACK_PUSH (input_section
->output_section
->vma
);
1878 RX_STACK_POP (tmp1
);
1879 RX_STACK_POP (tmp2
);
1881 RX_STACK_PUSH (tmp1
);
1885 RX_STACK_POP (tmp1
);
1886 RX_STACK_POP (tmp2
);
1888 RX_STACK_PUSH (tmp1
);
1892 RX_STACK_POP (tmp1
);
1893 RX_STACK_POP (tmp2
);
1895 RX_STACK_PUSH (tmp1
);
1899 RX_STACK_POP (tmp1
);
1901 RX_STACK_PUSH (tmp1
);
1905 RX_STACK_POP (tmp1
);
1906 RX_STACK_POP (tmp2
);
1908 RX_STACK_PUSH (tmp1
);
1912 RX_STACK_PUSH (get_romstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1916 RX_STACK_PUSH (get_ramstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1924 RX_STACK_POP (symval
);
1935 RX_STACK_POP (symval
);
1943 RX_STACK_POP (symval
);
1956 move_reloc (Elf_Internal_Rela
* irel
, Elf_Internal_Rela
* srel
, int delta
)
1958 bfd_vma old_offset
= srel
->r_offset
;
1961 while (irel
<= srel
)
1963 if (irel
->r_offset
== old_offset
)
1964 irel
->r_offset
+= delta
;
1969 /* Relax one section. */
1972 elf32_rx_relax_section (bfd
* abfd
,
1974 struct bfd_link_info
* link_info
,
1975 bfd_boolean
* again
,
1976 bfd_boolean allow_pcrel3
)
1978 Elf_Internal_Shdr
* symtab_hdr
;
1979 Elf_Internal_Shdr
* shndx_hdr
;
1980 Elf_Internal_Rela
* internal_relocs
;
1981 Elf_Internal_Rela
* irel
;
1982 Elf_Internal_Rela
* srel
;
1983 Elf_Internal_Rela
* irelend
;
1984 Elf_Internal_Rela
* next_alignment
;
1985 Elf_Internal_Rela
* prev_alignment
;
1986 bfd_byte
* contents
= NULL
;
1987 bfd_byte
* free_contents
= NULL
;
1988 Elf_Internal_Sym
* intsyms
= NULL
;
1989 Elf_Internal_Sym
* free_intsyms
= NULL
;
1990 Elf_External_Sym_Shndx
* shndx_buf
= NULL
;
1996 int section_alignment_glue
;
1997 /* how much to scale the relocation by - 1, 2, or 4. */
2000 /* Assume nothing changes. */
2003 /* We don't have to do anything for a relocatable link, if
2004 this section does not have relocs, or if this is not a
2006 if (bfd_link_relocatable (link_info
)
2007 || (sec
->flags
& SEC_RELOC
) == 0
2008 || sec
->reloc_count
== 0
2009 || (sec
->flags
& SEC_CODE
) == 0)
2012 symtab_hdr
= & elf_symtab_hdr (abfd
);
2013 if (elf_symtab_shndx_list (abfd
))
2014 shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
2018 sec_start
= sec
->output_section
->vma
+ sec
->output_offset
;
2020 /* Get the section contents. */
2021 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2022 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2023 /* Go get them off disk. */
2026 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2028 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2031 /* Read this BFD's symbols. */
2032 /* Get cached copy if it exists. */
2033 if (symtab_hdr
->contents
!= NULL
)
2034 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2037 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
2038 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
2041 if (shndx_hdr
&& shndx_hdr
->sh_size
!= 0)
2045 amt
= symtab_hdr
->sh_info
;
2046 amt
*= sizeof (Elf_External_Sym_Shndx
);
2047 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
2048 if (shndx_buf
== NULL
)
2050 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
2051 || bfd_bread (shndx_buf
, amt
, abfd
) != amt
)
2053 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
2056 /* Get a copy of the native relocations. */
2057 /* Note - we ignore the setting of link_info->keep_memory when reading
2058 in these relocs. We have to maintain a permanent copy of the relocs
2059 because we are going to walk over them multiple times, adjusting them
2060 as bytes are deleted from the section, and with this relaxation
2061 function itself being called multiple times on the same section... */
2062 internal_relocs
= _bfd_elf_link_read_relocs
2063 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
);
2064 if (internal_relocs
== NULL
)
2067 /* The RL_ relocs must be just before the operand relocs they go
2068 with, so we must sort them to guarantee this. We use bubblesort
2069 instead of qsort so we can guarantee that relocs with the same
2070 address remain in the same relative order. */
2071 reloc_bubblesort (internal_relocs
, sec
->reloc_count
);
2073 /* Walk through them looking for relaxing opportunities. */
2074 irelend
= internal_relocs
+ sec
->reloc_count
;
2076 /* This will either be NULL or a pointer to the next alignment
2078 next_alignment
= internal_relocs
;
2079 /* This will be the previous alignment, although at first it points
2080 to the first real relocation. */
2081 prev_alignment
= internal_relocs
;
2083 /* We calculate worst case shrinkage caused by alignment directives.
2084 No fool-proof, but better than either ignoring the problem or
2085 doing heavy duty analysis of all the alignment markers in all
2087 section_alignment_glue
= 0;
2088 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2089 if (ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
2090 && irel
->r_addend
& RX_RELAXA_ALIGN
)
2092 int this_glue
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
2094 if (section_alignment_glue
< this_glue
)
2095 section_alignment_glue
= this_glue
;
2097 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2099 section_alignment_glue
*= 2;
2101 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2103 unsigned char *insn
;
2106 /* The insns we care about are all marked with one of these. */
2107 if (ELF32_R_TYPE (irel
->r_info
) != R_RX_RH_RELAX
)
2110 if (irel
->r_addend
& RX_RELAXA_ALIGN
2111 || next_alignment
== internal_relocs
)
2113 /* When we delete bytes, we need to maintain all the alignments
2114 indicated. In addition, we need to be careful about relaxing
2115 jumps across alignment boundaries - these displacements
2116 *grow* when we delete bytes. For now, don't shrink
2117 displacements across an alignment boundary, just in case.
2118 Note that this only affects relocations to the same
2120 prev_alignment
= next_alignment
;
2121 next_alignment
+= 2;
2122 while (next_alignment
< irelend
2123 && (ELF32_R_TYPE (next_alignment
->r_info
) != R_RX_RH_RELAX
2124 || !(next_alignment
->r_addend
& RX_RELAXA_ELIGN
)))
2126 if (next_alignment
>= irelend
|| next_alignment
->r_offset
== 0)
2127 next_alignment
= NULL
;
2130 /* When we hit alignment markers, see if we've shrunk enough
2131 before them to reduce the gap without violating the alignment
2133 if (irel
->r_addend
& RX_RELAXA_ALIGN
)
2135 /* At this point, the next relocation *should* be the ELIGN
2137 Elf_Internal_Rela
*erel
= irel
+ 1;
2138 unsigned int alignment
, nbytes
;
2140 if (ELF32_R_TYPE (erel
->r_info
) != R_RX_RH_RELAX
)
2142 if (!(erel
->r_addend
& RX_RELAXA_ELIGN
))
2145 alignment
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
2147 if (erel
->r_offset
- irel
->r_offset
< alignment
)
2150 nbytes
= erel
->r_offset
- irel
->r_offset
;
2151 nbytes
/= alignment
;
2152 nbytes
*= alignment
;
2154 elf32_rx_relax_delete_bytes (abfd
, sec
, erel
->r_offset
-nbytes
, nbytes
, next_alignment
,
2155 erel
->r_offset
== sec
->size
, internal_relocs
);
2161 if (irel
->r_addend
& RX_RELAXA_ELIGN
)
2164 insn
= contents
+ irel
->r_offset
;
2166 nrelocs
= irel
->r_addend
& RX_RELAXA_RNUM
;
2168 /* At this point, we have an insn that is a candidate for linker
2169 relaxation. There are NRELOCS relocs following that may be
2170 relaxed, although each reloc may be made of more than one
2171 reloc entry (such as gp-rel symbols). */
2173 /* Get the value of the symbol referred to by the reloc. Just
2174 in case this is the last reloc in the list, use the RL's
2175 addend to choose between this reloc (no addend) or the next
2176 (yes addend, which means at least one following reloc). */
2178 /* srel points to the "current" reloction for this insn -
2179 actually the last reloc for a given operand, which is the one
2180 we need to update. We check the relaxations in the same
2181 order that the relocations happen, so we'll just push it
2185 pc
= sec
->output_section
->vma
+ sec
->output_offset
2189 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2190 pcrel = symval - pc + srel->r_addend; \
2193 #define SNIPNR(offset, nbytes) \
2194 elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0, internal_relocs);
2195 #define SNIP(offset, nbytes, newtype) \
2196 SNIPNR (offset, nbytes); \
2197 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2199 /* The order of these bit tests must match the order that the
2200 relocs appear in. Since we sorted those by offset, we can
2203 /* Note that the numbers in, say, DSP6 are the bit offsets of
2204 the code fields that describe the operand. Bits number 0 for
2205 the MSB of insn[0]. */
2212 if (irel
->r_addend
& RX_RELAXA_DSP6
)
2217 if (code
== 2 && symval
/scale
<= 255)
2219 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2222 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2223 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2225 SNIP (3, 1, newrel
);
2230 else if (code
== 1 && symval
== 0)
2233 SNIP (2, 1, R_RX_NONE
);
2237 /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst. */
2238 else if (code
== 1 && symval
/scale
<= 31
2239 /* Decodable bits. */
2240 && (insn
[0] & 0xcc) == 0xcc
2242 && (insn
[0] & 0x30) != 0x30
2243 /* Register MSBs. */
2244 && (insn
[1] & 0x88) == 0x00)
2248 insn
[0] = 0x88 | (insn
[0] & 0x30);
2249 /* The register fields are in the right place already. */
2251 /* We can't relax this new opcode. */
2254 switch ((insn
[0] & 0x30) >> 4)
2257 newrel
= R_RX_RH_ABS5p5B
;
2260 newrel
= R_RX_RH_ABS5p5W
;
2263 newrel
= R_RX_RH_ABS5p5L
;
2267 move_reloc (irel
, srel
, -2);
2268 SNIP (2, 1, newrel
);
2271 /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst. */
2272 else if (code
== 1 && symval
/scale
<= 31
2273 /* Decodable bits. */
2274 && (insn
[0] & 0xf8) == 0x58
2275 /* Register MSBs. */
2276 && (insn
[1] & 0x88) == 0x00)
2280 insn
[0] = 0xb0 | ((insn
[0] & 0x04) << 1);
2281 /* The register fields are in the right place already. */
2283 /* We can't relax this new opcode. */
2286 switch ((insn
[0] & 0x08) >> 3)
2289 newrel
= R_RX_RH_ABS5p5B
;
2292 newrel
= R_RX_RH_ABS5p5W
;
2296 move_reloc (irel
, srel
, -2);
2297 SNIP (2, 1, newrel
);
2301 /* A DSP4 operand always follows a DSP6 operand, even if there's
2302 no relocation for it. We have to read the code out of the
2303 opcode to calculate the offset of the operand. */
2304 if (irel
->r_addend
& RX_RELAXA_DSP4
)
2306 int code6
, offset
= 0;
2310 code6
= insn
[0] & 0x03;
2313 case 0: offset
= 2; break;
2314 case 1: offset
= 3; break;
2315 case 2: offset
= 4; break;
2316 case 3: offset
= 2; break;
2319 code
= (insn
[0] & 0x0c) >> 2;
2321 if (code
== 2 && symval
/ scale
<= 255)
2323 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2327 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2328 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2330 SNIP (offset
+1, 1, newrel
);
2335 else if (code
== 1 && symval
== 0)
2338 SNIP (offset
, 1, R_RX_NONE
);
2341 /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2342 else if (code
== 1 && symval
/scale
<= 31
2343 /* Decodable bits. */
2344 && (insn
[0] & 0xc3) == 0xc3
2346 && (insn
[0] & 0x30) != 0x30
2347 /* Register MSBs. */
2348 && (insn
[1] & 0x88) == 0x00)
2352 insn
[0] = 0x80 | (insn
[0] & 0x30);
2353 /* The register fields are in the right place already. */
2355 /* We can't relax this new opcode. */
2358 switch ((insn
[0] & 0x30) >> 4)
2361 newrel
= R_RX_RH_ABS5p5B
;
2364 newrel
= R_RX_RH_ABS5p5W
;
2367 newrel
= R_RX_RH_ABS5p5L
;
2371 move_reloc (irel
, srel
, -2);
2372 SNIP (2, 1, newrel
);
2376 /* These always occur alone, but the offset depends on whether
2377 it's a MEMEX opcode (0x06) or not. */
2378 if (irel
->r_addend
& RX_RELAXA_DSP14
)
2383 if (insn
[0] == 0x06)
2390 if (code
== 2 && symval
/ scale
<= 255)
2392 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2396 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2397 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2399 SNIP (offset
, 1, newrel
);
2403 else if (code
== 1 && symval
== 0)
2406 SNIP (offset
, 1, R_RX_NONE
);
2417 /* These always occur alone. */
2418 if (irel
->r_addend
& RX_RELAXA_IMM6
)
2424 /* These relocations sign-extend, so we must do signed compares. */
2425 ssymval
= (long) symval
;
2427 code
= insn
[0] & 0x03;
2429 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2431 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2435 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2436 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2438 SNIP (2, 1, newrel
);
2443 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2445 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2449 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2450 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2452 SNIP (2, 1, newrel
);
2457 /* Special case UIMM8 format: CMP #uimm8,Rdst. */
2458 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2459 /* Decodable bits. */
2460 && (insn
[0] & 0xfc) == 0x74
2461 /* Decodable bits. */
2462 && ((insn
[1] & 0xf0) == 0x00))
2467 insn
[1] = 0x50 | (insn
[1] & 0x0f);
2469 /* We can't relax this new opcode. */
2472 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2473 newrel
= R_RX_ABS8U
;
2475 newrel
= R_RX_DIR8U
;
2477 SNIP (2, 1, newrel
);
2481 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2483 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2487 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2488 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2490 SNIP (2, 1, newrel
);
2495 /* Special case UIMM4 format: CMP, MUL, AND, OR. */
2496 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2497 /* Decodable bits and immediate type. */
2499 /* Decodable bits. */
2500 && (insn
[1] & 0xc0) == 0x00)
2502 static const int newop
[4] = { 1, 3, 4, 5 };
2504 insn
[0] = 0x60 | newop
[insn
[1] >> 4];
2505 /* The register number doesn't move. */
2507 /* We can't relax this new opcode. */
2510 move_reloc (irel
, srel
, -1);
2512 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2516 /* Special case UIMM4 format: ADD -> ADD/SUB. */
2517 else if (code
== 1 && ssymval
<= 15 && ssymval
>= -15
2518 /* Decodable bits and immediate type. */
2520 /* Same register for source and destination. */
2521 && ((insn
[1] >> 4) == (insn
[1] & 0x0f)))
2525 /* Note that we can't turn "add $0,Rs" into a NOP
2526 because the flags need to be set right. */
2530 insn
[0] = 0x60; /* Subtract. */
2531 newrel
= R_RX_RH_UNEG4p8
;
2535 insn
[0] = 0x62; /* Add. */
2536 newrel
= R_RX_RH_UIMM4p8
;
2539 /* The register number is in the right place. */
2541 /* We can't relax this new opcode. */
2544 move_reloc (irel
, srel
, -1);
2546 SNIP (2, 1, newrel
);
2551 /* These are either matched with a DSP6 (2-byte base) or an id24
2553 if (irel
->r_addend
& RX_RELAXA_IMM12
)
2555 int dspcode
, offset
= 0;
2560 if ((insn
[0] & 0xfc) == 0xfc)
2561 dspcode
= 1; /* Just something with one byte operand. */
2563 dspcode
= insn
[0] & 3;
2566 case 0: offset
= 2; break;
2567 case 1: offset
= 3; break;
2568 case 2: offset
= 4; break;
2569 case 3: offset
= 2; break;
2572 /* These relocations sign-extend, so we must do signed compares. */
2573 ssymval
= (long) symval
;
2575 code
= (insn
[1] >> 2) & 3;
2576 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2578 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2582 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2583 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2585 SNIP (offset
, 1, newrel
);
2590 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2592 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2596 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2597 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2599 SNIP (offset
, 1, newrel
);
2604 /* Special case UIMM8 format: MOV #uimm8,Rdst. */
2605 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2606 /* Decodable bits. */
2608 /* Decodable bits. */
2609 && ((insn
[1] & 0x03) == 0x02))
2614 insn
[1] = 0x40 | (insn
[1] >> 4);
2616 /* We can't relax this new opcode. */
2619 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2620 newrel
= R_RX_ABS8U
;
2622 newrel
= R_RX_DIR8U
;
2624 SNIP (2, 1, newrel
);
2628 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2630 unsigned int newrel
= ELF32_R_TYPE(srel
->r_info
);
2634 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2635 if (newrel
!= ELF32_R_TYPE(srel
->r_info
))
2637 SNIP (offset
, 1, newrel
);
2642 /* Special case UIMM4 format: MOV #uimm4,Rdst. */
2643 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2644 /* Decodable bits. */
2646 /* Decodable bits. */
2647 && ((insn
[1] & 0x03) == 0x02))
2650 insn
[1] = insn
[1] >> 4;
2652 /* We can't relax this new opcode. */
2655 move_reloc (irel
, srel
, -1);
2657 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2662 if (irel
->r_addend
& RX_RELAXA_BRA
)
2664 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2666 int alignment_glue
= 0;
2670 /* Branches over alignment chunks are problematic, as
2671 deleting bytes here makes the branch *further* away. We
2672 can be agressive with branches within this alignment
2673 block, but not branches outside it. */
2674 if ((prev_alignment
== NULL
2675 || symval
< (bfd_vma
)(sec_start
+ prev_alignment
->r_offset
))
2676 && (next_alignment
== NULL
2677 || symval
> (bfd_vma
)(sec_start
+ next_alignment
->r_offset
)))
2678 alignment_glue
= section_alignment_glue
;
2680 if (ELF32_R_TYPE(srel
[1].r_info
) == R_RX_RH_RELAX
2681 && srel
[1].r_addend
& RX_RELAXA_BRA
2682 && srel
[1].r_offset
< irel
->r_offset
+ pcrel
)
2685 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2687 /* The values we compare PCREL with are not what you'd
2688 expect; they're off by a little to compensate for (1)
2689 where the reloc is relative to the insn, and (2) how much
2690 the insn is going to change when we relax it. */
2692 /* These we have to decode. */
2695 case 0x04: /* BRA pcdsp:24 */
2696 if (-32768 + alignment_glue
<= pcrel
2697 && pcrel
<= 32765 - alignment_glue
)
2700 SNIP (3, 1, newrel
);
2705 case 0x38: /* BRA pcdsp:16 */
2706 if (-128 + alignment_glue
<= pcrel
2707 && pcrel
<= 127 - alignment_glue
)
2710 SNIP (2, 1, newrel
);
2715 case 0x2e: /* BRA pcdsp:8 */
2716 /* Note that there's a risk here of shortening things so
2717 much that we no longer fit this reloc; it *should*
2718 only happen when you branch across a branch, and that
2719 branch also devolves into BRA.S. "Real" code should
2721 if (max_pcrel3
+ alignment_glue
<= pcrel
2722 && pcrel
<= 10 - alignment_glue
2726 SNIP (1, 1, newrel
);
2727 move_reloc (irel
, srel
, -1);
2732 case 0x05: /* BSR pcdsp:24 */
2733 if (-32768 + alignment_glue
<= pcrel
2734 && pcrel
<= 32765 - alignment_glue
)
2737 SNIP (1, 1, newrel
);
2742 case 0x3a: /* BEQ.W pcdsp:16 */
2743 case 0x3b: /* BNE.W pcdsp:16 */
2744 if (-128 + alignment_glue
<= pcrel
2745 && pcrel
<= 127 - alignment_glue
)
2747 insn
[0] = 0x20 | (insn
[0] & 1);
2748 SNIP (1, 1, newrel
);
2753 case 0x20: /* BEQ.B pcdsp:8 */
2754 case 0x21: /* BNE.B pcdsp:8 */
2755 if (max_pcrel3
+ alignment_glue
<= pcrel
2756 && pcrel
- alignment_glue
<= 10
2759 insn
[0] = 0x10 | ((insn
[0] & 1) << 3);
2760 SNIP (1, 1, newrel
);
2761 move_reloc (irel
, srel
, -1);
2766 case 0x16: /* synthetic BNE dsp24 */
2767 case 0x1e: /* synthetic BEQ dsp24 */
2768 if (-32767 + alignment_glue
<= pcrel
2769 && pcrel
<= 32766 - alignment_glue
2772 if (insn
[0] == 0x16)
2776 /* We snip out the bytes at the end else the reloc
2777 will get moved too, and too much. */
2778 SNIP (3, 2, newrel
);
2779 move_reloc (irel
, srel
, -1);
2785 /* Special case - synthetic conditional branches, pcrel24.
2786 Note that EQ and NE have been handled above. */
2787 if ((insn
[0] & 0xf0) == 0x20
2790 && srel
->r_offset
!= irel
->r_offset
+ 1
2791 && -32767 + alignment_glue
<= pcrel
2792 && pcrel
<= 32766 - alignment_glue
)
2796 SNIP (5, 1, newrel
);
2800 /* Special case - synthetic conditional branches, pcrel16 */
2801 if ((insn
[0] & 0xf0) == 0x20
2804 && srel
->r_offset
!= irel
->r_offset
+ 1
2805 && -127 + alignment_glue
<= pcrel
2806 && pcrel
<= 126 - alignment_glue
)
2808 int cond
= (insn
[0] & 0x0f) ^ 0x01;
2810 insn
[0] = 0x20 | cond
;
2811 /* By moving the reloc first, we avoid having
2812 delete_bytes move it also. */
2813 move_reloc (irel
, srel
, -2);
2814 SNIP (2, 3, newrel
);
2819 BFD_ASSERT (nrelocs
== 0);
2821 /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2822 use MOV.bwl #uimm:8, dsp:5[r7] format. This is tricky
2823 because it may have one or two relocations. */
2824 if ((insn
[0] & 0xfc) == 0xf8
2825 && (insn
[1] & 0x80) == 0x00
2826 && (insn
[0] & 0x03) != 0x03)
2828 int dcode
, icode
, reg
, ioff
, dscale
, ilen
;
2829 bfd_vma disp_val
= 0;
2831 Elf_Internal_Rela
* disp_rel
= 0;
2832 Elf_Internal_Rela
* imm_rel
= 0;
2837 dcode
= insn
[0] & 0x03;
2838 icode
= (insn
[1] >> 2) & 0x03;
2839 reg
= (insn
[1] >> 4) & 0x0f;
2841 ioff
= dcode
== 1 ? 3 : dcode
== 2 ? 4 : 2;
2843 /* Figure out what the dispacement is. */
2844 if (dcode
== 1 || dcode
== 2)
2846 /* There's a displacement. See if there's a reloc for it. */
2847 if (srel
[1].r_offset
== irel
->r_offset
+ 2)
2859 #if RX_OPCODE_BIG_ENDIAN
2860 disp_val
= insn
[2] * 256 + insn
[3];
2862 disp_val
= insn
[2] + insn
[3] * 256;
2865 switch (insn
[1] & 3)
2881 /* Figure out what the immediate is. */
2882 if (srel
[1].r_offset
== irel
->r_offset
+ ioff
)
2885 imm_val
= (long) symval
;
2890 unsigned char * ip
= insn
+ ioff
;
2895 /* For byte writes, we don't sign extend. Makes the math easier later. */
2899 imm_val
= (char) ip
[0];
2902 #if RX_OPCODE_BIG_ENDIAN
2903 imm_val
= ((char) ip
[0] << 8) | ip
[1];
2905 imm_val
= ((char) ip
[1] << 8) | ip
[0];
2909 #if RX_OPCODE_BIG_ENDIAN
2910 imm_val
= ((char) ip
[0] << 16) | (ip
[1] << 8) | ip
[2];
2912 imm_val
= ((char) ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2916 #if RX_OPCODE_BIG_ENDIAN
2917 imm_val
= (ip
[0] << 24) | (ip
[1] << 16) | (ip
[2] << 8) | ip
[3];
2919 imm_val
= (ip
[3] << 24) | (ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2953 /* The shortcut happens when the immediate is 0..255,
2954 register r0 to r7, and displacement (scaled) 0..31. */
2956 if (0 <= imm_val
&& imm_val
<= 255
2957 && 0 <= reg
&& reg
<= 7
2958 && disp_val
/ dscale
<= 31)
2960 insn
[0] = 0x3c | (insn
[1] & 0x03);
2961 insn
[1] = (((disp_val
/ dscale
) << 3) & 0x80) | (reg
<< 4) | ((disp_val
/dscale
) & 0x0f);
2966 int newrel
= R_RX_NONE
;
2971 newrel
= R_RX_RH_ABS5p8B
;
2974 newrel
= R_RX_RH_ABS5p8W
;
2977 newrel
= R_RX_RH_ABS5p8L
;
2980 disp_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (disp_rel
->r_info
), newrel
);
2981 move_reloc (irel
, disp_rel
, -1);
2985 imm_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (imm_rel
->r_info
), R_RX_DIR8U
);
2986 move_reloc (disp_rel
? disp_rel
: irel
,
2988 irel
->r_offset
- imm_rel
->r_offset
+ 2);
2991 SNIPNR (3, ilen
- 3);
2994 /* We can't relax this new opcode. */
3000 /* We can't reliably relax branches to DIR3U_PCREL unless we know
3001 whatever they're branching over won't shrink any more. If we're
3002 basically done here, do one more pass just for branches - but
3003 don't request a pass after that one! */
3004 if (!*again
&& !allow_pcrel3
)
3006 bfd_boolean ignored
;
3008 elf32_rx_relax_section (abfd
, sec
, link_info
, &ignored
, TRUE
);
3014 if (free_contents
!= NULL
)
3015 free (free_contents
);
3017 if (shndx_buf
!= NULL
)
3019 shndx_hdr
->contents
= NULL
;
3023 if (free_intsyms
!= NULL
)
3024 free (free_intsyms
);
3030 elf32_rx_relax_section_wrapper (bfd
* abfd
,
3032 struct bfd_link_info
* link_info
,
3033 bfd_boolean
* again
)
3035 return elf32_rx_relax_section (abfd
, sec
, link_info
, again
, FALSE
);
3038 /* Function to set the ELF flag bits. */
3041 rx_elf_set_private_flags (bfd
* abfd
, flagword flags
)
3043 elf_elfheader (abfd
)->e_flags
= flags
;
3044 elf_flags_init (abfd
) = TRUE
;
3048 static bfd_boolean no_warn_mismatch
= FALSE
;
3049 static bfd_boolean ignore_lma
= TRUE
;
3051 void bfd_elf32_rx_set_target_flags (bfd_boolean
, bfd_boolean
);
3054 bfd_elf32_rx_set_target_flags (bfd_boolean user_no_warn_mismatch
,
3055 bfd_boolean user_ignore_lma
)
3057 no_warn_mismatch
= user_no_warn_mismatch
;
3058 ignore_lma
= user_ignore_lma
;
3061 /* Converts FLAGS into a descriptive string.
3062 Returns a static pointer. */
3065 describe_flags (flagword flags
)
3067 static char buf
[128];
3071 if (flags
& E_FLAG_RX_64BIT_DOUBLES
)
3072 strcat (buf
, "64-bit doubles");
3074 strcat (buf
, "32-bit doubles");
3076 if (flags
& E_FLAG_RX_DSP
)
3077 strcat (buf
, ", dsp");
3079 strcat (buf
, ", no dsp");
3081 if (flags
& E_FLAG_RX_PID
)
3082 strcat (buf
, ", pid");
3084 strcat (buf
, ", no pid");
3086 if (flags
& E_FLAG_RX_ABI
)
3087 strcat (buf
, ", RX ABI");
3089 strcat (buf
, ", GCC ABI");
3091 if (flags
& E_FLAG_RX_SINSNS_SET
)
3092 strcat (buf
, flags
& E_FLAG_RX_SINSNS_YES
? ", uses String instructions" : ", bans String instructions");
3097 /* Merge backend specific data from an object file to the output
3098 object file when linking. */
3101 rx_elf_merge_private_bfd_data (bfd
* ibfd
, struct bfd_link_info
*info
)
3103 bfd
*obfd
= info
->output_bfd
;
3106 bfd_boolean error
= FALSE
;
3108 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3109 old_flags
= elf_elfheader (obfd
)->e_flags
;
3111 if (!elf_flags_init (obfd
))
3113 /* First call, no flags set. */
3114 elf_flags_init (obfd
) = TRUE
;
3115 elf_elfheader (obfd
)->e_flags
= new_flags
;
3117 else if (old_flags
!= new_flags
)
3119 flagword known_flags
;
3121 if (old_flags
& E_FLAG_RX_SINSNS_SET
)
3123 if ((new_flags
& E_FLAG_RX_SINSNS_SET
) == 0)
3125 new_flags
&= ~ E_FLAG_RX_SINSNS_MASK
;
3126 new_flags
|= (old_flags
& E_FLAG_RX_SINSNS_MASK
);
3129 else if (new_flags
& E_FLAG_RX_SINSNS_SET
)
3131 old_flags
&= ~ E_FLAG_RX_SINSNS_MASK
;
3132 old_flags
|= (new_flags
& E_FLAG_RX_SINSNS_MASK
);
3135 known_flags
= E_FLAG_RX_ABI
| E_FLAG_RX_64BIT_DOUBLES
3136 | E_FLAG_RX_DSP
| E_FLAG_RX_PID
| E_FLAG_RX_SINSNS_MASK
;
3138 if ((old_flags
^ new_flags
) & known_flags
)
3140 /* Only complain if flag bits we care about do not match.
3141 Other bits may be set, since older binaries did use some
3142 deprecated flags. */
3143 if (no_warn_mismatch
)
3145 elf_elfheader (obfd
)->e_flags
= (new_flags
| old_flags
) & known_flags
;
3149 _bfd_error_handler (_("There is a conflict merging the"
3150 " ELF header flags from %pB"),
3152 _bfd_error_handler (_(" the input file's flags: %s"),
3153 describe_flags (new_flags
));
3154 _bfd_error_handler (_(" the output file's flags: %s"),
3155 describe_flags (old_flags
));
3160 elf_elfheader (obfd
)->e_flags
= new_flags
& known_flags
;
3164 bfd_set_error (bfd_error_bad_value
);
3170 rx_elf_print_private_bfd_data (bfd
* abfd
, void * ptr
)
3172 FILE * file
= (FILE *) ptr
;
3175 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
3177 /* Print normal ELF private data. */
3178 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
3180 flags
= elf_elfheader (abfd
)->e_flags
;
3181 fprintf (file
, _("private flags = 0x%lx:"), (long) flags
);
3183 fprintf (file
, "%s", describe_flags (flags
));
3187 /* Return the MACH for an e_flags value. */
3190 elf32_rx_machine (bfd
* abfd ATTRIBUTE_UNUSED
)
3192 #if 0 /* FIXME: EF_RX_CPU_MASK collides with E_FLAG_RX_...
3193 Need to sort out how these flag bits are used.
3194 For now we assume that the flags are OK. */
3195 if ((elf_elfheader (abfd
)->e_flags
& EF_RX_CPU_MASK
) == EF_RX_CPU_RX
)
3203 rx_elf_object_p (bfd
* abfd
)
3207 Elf_Internal_Phdr
*phdr
= elf_tdata (abfd
)->phdr
;
3208 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
3209 int nphdrs
= ehdr
->e_phnum
;
3211 static int saw_be
= FALSE
;
3212 bfd_vma end_phdroff
;
3214 /* We never want to automatically choose the non-swapping big-endian
3215 target. The user can only get that explicitly, such as with -I
3217 if (abfd
->xvec
== &rx_elf32_be_ns_vec
3218 && abfd
->target_defaulted
)
3221 /* BFD->target_defaulted is not set to TRUE when a target is chosen
3222 as a fallback, so we check for "scanning" to know when to stop
3223 using the non-swapping target. */
3224 if (abfd
->xvec
== &rx_elf32_be_ns_vec
3227 if (abfd
->xvec
== &rx_elf32_be_vec
)
3230 bfd_default_set_arch_mach (abfd
, bfd_arch_rx
,
3231 elf32_rx_machine (abfd
));
3233 /* For each PHDR in the object, we must find some section that
3234 corresponds (based on matching file offsets) and use its VMA
3235 information to reconstruct the p_vaddr field we clobbered when we
3237 /* If PT_LOAD headers include the ELF file header or program headers
3238 then the PT_LOAD header does not start with some section contents.
3239 Making adjustments based on the difference between sh_offset and
3240 p_offset is nonsense in such cases. Exclude them. Note that
3241 since standard linker scripts for RX do not use SIZEOF_HEADERS,
3242 the linker won't normally create PT_LOAD segments covering the
3243 headers so this is mainly for passing the ld testsuite.
3244 FIXME. Why are we looking at non-PT_LOAD headers here? */
3245 end_phdroff
= ehdr
->e_ehsize
;
3246 if (ehdr
->e_phoff
!= 0)
3247 end_phdroff
= ehdr
->e_phoff
+ nphdrs
* ehdr
->e_phentsize
;
3248 for (i
=0; i
<nphdrs
; i
++)
3250 for (u
=0; u
<elf_tdata(abfd
)->num_elf_sections
; u
++)
3252 Elf_Internal_Shdr
*sec
= elf_tdata(abfd
)->elf_sect_ptr
[u
];
3254 if (phdr
[i
].p_filesz
3255 && phdr
[i
].p_offset
>= end_phdroff
3256 && phdr
[i
].p_offset
<= (bfd_vma
) sec
->sh_offset
3258 && sec
->sh_type
!= SHT_NOBITS
3259 && (bfd_vma
)sec
->sh_offset
<= phdr
[i
].p_offset
+ (phdr
[i
].p_filesz
- 1))
3261 /* Found one! The difference between the two addresses,
3262 plus the difference between the two file offsets, is
3263 enough information to reconstruct the lma. */
3265 /* Example where they aren't:
3266 PHDR[1] = lma fffc0100 offset 00002010 size 00000100
3267 SEC[6] = vma 00000050 offset 00002050 size 00000040
3269 The correct LMA for the section is fffc0140 + (2050-2010).
3272 phdr
[i
].p_vaddr
= sec
->sh_addr
+ (sec
->sh_offset
- phdr
[i
].p_offset
);
3277 /* We must update the bfd sections as well, so we don't stop
3279 bsec
= abfd
->sections
;
3282 if (phdr
[i
].p_filesz
3283 && phdr
[i
].p_vaddr
<= bsec
->vma
3284 && bsec
->vma
<= phdr
[i
].p_vaddr
+ (phdr
[i
].p_filesz
- 1))
3286 bsec
->lma
= phdr
[i
].p_paddr
+ (bsec
->vma
- phdr
[i
].p_vaddr
);
3298 rx_dump_symtab (bfd
* abfd
, void * internal_syms
, void * external_syms
)
3301 Elf_Internal_Sym
* isymbuf
;
3302 Elf_Internal_Sym
* isymend
;
3303 Elf_Internal_Sym
* isym
;
3304 Elf_Internal_Shdr
* symtab_hdr
;
3305 bfd_boolean free_internal
= FALSE
, free_external
= FALSE
;
3307 char * st_info_stb_str
;
3308 char * st_other_str
;
3309 char * st_shndx_str
;
3311 if (! internal_syms
)
3313 internal_syms
= bfd_malloc (1000);
3316 if (! external_syms
)
3318 external_syms
= bfd_malloc (1000);
3322 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3323 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3325 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
3326 symtab_hdr
->sh_info
, 0,
3327 internal_syms
, external_syms
, NULL
);
3329 isymbuf
= internal_syms
;
3330 isymend
= isymbuf
+ locsymcount
;
3332 for (isym
= isymbuf
; isym
< isymend
; isym
++)
3334 switch (ELF_ST_TYPE (isym
->st_info
))
3336 case STT_FUNC
: st_info_str
= "STT_FUNC"; break;
3337 case STT_SECTION
: st_info_str
= "STT_SECTION"; break;
3338 case STT_FILE
: st_info_str
= "STT_FILE"; break;
3339 case STT_OBJECT
: st_info_str
= "STT_OBJECT"; break;
3340 case STT_TLS
: st_info_str
= "STT_TLS"; break;
3341 default: st_info_str
= "";
3343 switch (ELF_ST_BIND (isym
->st_info
))
3345 case STB_LOCAL
: st_info_stb_str
= "STB_LOCAL"; break;
3346 case STB_GLOBAL
: st_info_stb_str
= "STB_GLOBAL"; break;
3347 default: st_info_stb_str
= "";
3349 switch (ELF_ST_VISIBILITY (isym
->st_other
))
3351 case STV_DEFAULT
: st_other_str
= "STV_DEFAULT"; break;
3352 case STV_INTERNAL
: st_other_str
= "STV_INTERNAL"; break;
3353 case STV_PROTECTED
: st_other_str
= "STV_PROTECTED"; break;
3354 default: st_other_str
= "";
3356 switch (isym
->st_shndx
)
3358 case SHN_ABS
: st_shndx_str
= "SHN_ABS"; break;
3359 case SHN_COMMON
: st_shndx_str
= "SHN_COMMON"; break;
3360 case SHN_UNDEF
: st_shndx_str
= "SHN_UNDEF"; break;
3361 default: st_shndx_str
= "";
3364 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3365 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3367 (unsigned long) isym
->st_value
,
3368 (unsigned long) isym
->st_size
,
3370 bfd_elf_string_from_elf_section (abfd
, symtab_hdr
->sh_link
,
3372 isym
->st_info
, st_info_str
, st_info_stb_str
,
3373 isym
->st_other
, st_other_str
,
3374 isym
->st_shndx
, st_shndx_str
);
3377 free (internal_syms
);
3379 free (external_syms
);
3383 rx_get_reloc (long reloc
)
3385 if (0 <= reloc
&& reloc
< R_RX_max
)
3386 return rx_elf_howto_table
[reloc
].name
;
3392 /* We must take care to keep the on-disk copy of any code sections
3393 that are fully linked swapped if the target is big endian, to match
3394 the Renesas tools. */
3396 /* The rule is: big endian object that are final-link executables,
3397 have code sections stored with 32-bit words swapped relative to
3398 what you'd get by default. */
3401 rx_get_section_contents (bfd
* abfd
,
3405 bfd_size_type count
)
3407 int exec
= (abfd
->flags
& EXEC_P
) ? 1 : 0;
3408 int s_code
= (section
->flags
& SEC_CODE
) ? 1 : 0;
3412 fprintf (stderr
, "dj: get %ld %ld from %s %s e%d sc%d %08lx:%08lx\n",
3413 (long) offset
, (long) count
, section
->name
,
3414 bfd_big_endian(abfd
) ? "be" : "le",
3415 exec
, s_code
, (long unsigned) section
->filepos
,
3416 (long unsigned) offset
);
3419 if (exec
&& s_code
&& bfd_big_endian (abfd
))
3421 char * cloc
= (char *) location
;
3422 bfd_size_type cnt
, end_cnt
;
3426 /* Fetch and swap unaligned bytes at the beginning. */
3431 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3436 bfd_putb32 (bfd_getl32 (buf
), buf
);
3438 cnt
= 4 - (offset
% 4);
3442 memcpy (location
, buf
+ (offset
% 4), cnt
);
3449 end_cnt
= count
% 4;
3451 /* Fetch and swap the middle bytes. */
3454 rv
= _bfd_generic_get_section_contents (abfd
, section
, cloc
, offset
,
3459 for (cnt
= count
; cnt
>= 4; cnt
-= 4, cloc
+= 4)
3460 bfd_putb32 (bfd_getl32 (cloc
), cloc
);
3463 /* Fetch and swap the end bytes. */
3468 /* Fetch the end bytes. */
3469 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3470 offset
+ count
- end_cnt
, 4);
3474 bfd_putb32 (bfd_getl32 (buf
), buf
);
3475 memcpy (cloc
, buf
, end_cnt
);
3479 rv
= _bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
);
3486 rx2_set_section_contents (bfd
* abfd
,
3488 const void * location
,
3490 bfd_size_type count
)
3494 fprintf (stderr
, " set sec %s %08x loc %p offset %#x count %#x\n",
3495 section
->name
, (unsigned) section
->vma
, location
, (int) offset
, (int) count
);
3496 for (i
= 0; i
< count
; i
++)
3498 if (i
% 16 == 0 && i
> 0)
3499 fprintf (stderr
, "\n");
3501 if (i
% 16 && i
% 4 == 0)
3502 fprintf (stderr
, " ");
3505 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3507 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3509 fprintf (stderr
, "\n");
3511 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3513 #define _bfd_elf_set_section_contents rx2_set_section_contents
3517 rx_set_section_contents (bfd
* abfd
,
3519 const void * location
,
3521 bfd_size_type count
)
3523 bfd_boolean exec
= (abfd
->flags
& EXEC_P
) ? TRUE
: FALSE
;
3524 bfd_boolean s_code
= (section
->flags
& SEC_CODE
) ? TRUE
: FALSE
;
3526 char * swapped_data
= NULL
;
3528 bfd_vma caddr
= section
->vma
+ offset
;
3530 bfd_size_type scount
;
3535 fprintf (stderr
, "\ndj: set %ld %ld to %s %s e%d sc%d\n",
3536 (long) offset
, (long) count
, section
->name
,
3537 bfd_big_endian (abfd
) ? "be" : "le",
3540 for (i
= 0; i
< count
; i
++)
3542 int a
= section
->vma
+ offset
+ i
;
3544 if (a
% 16 == 0 && a
> 0)
3545 fprintf (stderr
, "\n");
3547 if (a
% 16 && a
% 4 == 0)
3548 fprintf (stderr
, " ");
3550 if (a
% 16 == 0 || i
== 0)
3551 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3553 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3556 fprintf (stderr
, "\n");
3559 if (! exec
|| ! s_code
|| ! bfd_big_endian (abfd
))
3560 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3562 while (count
> 0 && caddr
> 0 && caddr
% 4)
3566 case 0: faddr
= offset
+ 3; break;
3567 case 1: faddr
= offset
+ 1; break;
3568 case 2: faddr
= offset
- 1; break;
3569 case 3: faddr
= offset
- 3; break;
3572 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3576 location
= (bfd_byte
*) location
+ 1;
3582 scount
= (int)(count
/ 4) * 4;
3585 char * cloc
= (char *) location
;
3587 swapped_data
= (char *) bfd_alloc (abfd
, count
);
3589 for (i
= 0; i
< count
; i
+= 4)
3591 bfd_vma v
= bfd_getl32 (cloc
+ i
);
3592 bfd_putb32 (v
, swapped_data
+ i
);
3595 rv
= _bfd_elf_set_section_contents (abfd
, section
, swapped_data
, offset
, scount
);
3602 location
= (bfd_byte
*) location
+ scount
;
3607 caddr
= section
->vma
+ offset
;
3612 case 0: faddr
= offset
+ 3; break;
3613 case 1: faddr
= offset
+ 1; break;
3614 case 2: faddr
= offset
- 1; break;
3615 case 3: faddr
= offset
- 3; break;
3617 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3621 location
= (bfd_byte
*) location
+ 1;
3632 rx_final_link (bfd
* abfd
, struct bfd_link_info
* info
)
3636 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3639 fprintf (stderr
, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3640 o
->name
, o
->flags
, o
->vma
, o
->lma
, o
->size
, o
->rawsize
);
3642 if (o
->flags
& SEC_CODE
3643 && bfd_big_endian (abfd
)
3647 fprintf (stderr
, "adjusting...\n");
3649 o
->size
+= 4 - (o
->size
% 4);
3653 return bfd_elf_final_link (abfd
, info
);
3657 elf32_rx_modify_program_headers (bfd
* abfd ATTRIBUTE_UNUSED
,
3658 struct bfd_link_info
* info ATTRIBUTE_UNUSED
)
3660 const struct elf_backend_data
* bed
;
3661 struct elf_obj_tdata
* tdata
;
3662 Elf_Internal_Phdr
* phdr
;
3666 bed
= get_elf_backend_data (abfd
);
3667 tdata
= elf_tdata (abfd
);
3669 count
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
3672 for (i
= count
; i
-- != 0;)
3673 if (phdr
[i
].p_type
== PT_LOAD
)
3675 /* The Renesas tools expect p_paddr to be zero. However,
3676 there is no other way to store the writable data in ROM for
3677 startup initialization. So, we let the linker *think*
3678 we're using paddr and vaddr the "usual" way, but at the
3679 last minute we move the paddr into the vaddr (which is what
3680 the simulator uses) and zero out paddr. Note that this
3681 does not affect the section headers, just the program
3682 headers. We hope. */
3683 phdr
[i
].p_vaddr
= phdr
[i
].p_paddr
;
3684 #if 0 /* If we zero out p_paddr, then the LMA in the section table
3686 phdr
[i
].p_paddr
= 0;
3693 /* The default literal sections should always be marked as "code" (i.e.,
3694 SHF_EXECINSTR). This is particularly important for big-endian mode
3695 when we do not want their contents byte reversed. */
3696 static const struct bfd_elf_special_section elf32_rx_special_sections
[] =
3698 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3699 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3700 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3701 { NULL
, 0, 0, 0, 0 }
3706 struct bfd_link_info
*info
;
3707 bfd_vma table_start
;
3709 bfd_vma
*table_handlers
;
3710 bfd_vma table_default_handler
;
3711 struct bfd_link_hash_entry
**table_entries
;
3712 struct bfd_link_hash_entry
*table_default_entry
;
3717 rx_table_find (struct bfd_hash_entry
*vent
, void *vinfo
)
3719 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3720 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3721 const char *name
; /* of the symbol we've found */
3725 const char *tname
; /* name of the table */
3726 bfd_vma start_addr
, end_addr
;
3728 struct bfd_link_hash_entry
* h
;
3730 /* We're looking for globally defined symbols of the form
3731 $tablestart$<NAME>. */
3732 if (ent
->type
!= bfd_link_hash_defined
3733 && ent
->type
!= bfd_link_hash_defweak
)
3736 name
= ent
->root
.string
;
3737 sec
= ent
->u
.def
.section
;
3740 if (strncmp (name
, "$tablestart$", 12))
3743 sec
->flags
|= SEC_KEEP
;
3747 start_addr
= ent
->u
.def
.value
;
3749 /* At this point, we can't build the table but we can (and must)
3750 find all the related symbols and mark their sections as SEC_KEEP
3751 so we don't garbage collect them. */
3753 buf
= (char *) malloc (12 + 10 + strlen (tname
));
3755 sprintf (buf
, "$tableend$%s", tname
);
3756 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3757 if (!h
|| (h
->type
!= bfd_link_hash_defined
3758 && h
->type
!= bfd_link_hash_defweak
))
3760 /* xgettext:c-format */
3761 _bfd_error_handler (_("%pB:%pA: table %s missing corresponding %s"),
3762 abfd
, sec
, name
, buf
);
3766 if (h
->u
.def
.section
!= ent
->u
.def
.section
)
3768 /* xgettext:c-format */
3769 _bfd_error_handler (_("%pB:%pA: %s and %s must be in the same input section"),
3770 h
->u
.def
.section
->owner
, h
->u
.def
.section
,
3775 end_addr
= h
->u
.def
.value
;
3777 sprintf (buf
, "$tableentry$default$%s", tname
);
3778 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3779 if (h
&& (h
->type
== bfd_link_hash_defined
3780 || h
->type
== bfd_link_hash_defweak
))
3782 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3785 for (idx
= 0; idx
< (int) (end_addr
- start_addr
) / 4; idx
++)
3787 sprintf (buf
, "$tableentry$%d$%s", idx
, tname
);
3788 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3789 if (h
&& (h
->type
== bfd_link_hash_defined
3790 || h
->type
== bfd_link_hash_defweak
))
3792 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3796 /* Return TRUE to keep scanning, FALSE to end the traversal. */
3800 /* We need to check for table entry symbols and build the tables, and
3801 we need to do it before the linker does garbage collection. This function is
3802 called once per input object file. */
3805 (bfd
* abfd ATTRIBUTE_UNUSED
,
3806 struct bfd_link_info
* info ATTRIBUTE_UNUSED
)
3808 RX_Table_Info stuff
;
3812 bfd_hash_traverse (&(info
->hash
->table
), rx_table_find
, &stuff
);
3819 rx_table_map_2 (struct bfd_hash_entry
*vent
, void *vinfo
)
3821 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3822 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3827 /* See if the symbol ENT has an address listed in the table, and
3828 isn't a debug/special symbol. If so, put it in the table. */
3830 if (ent
->type
!= bfd_link_hash_defined
3831 && ent
->type
!= bfd_link_hash_defweak
)
3834 name
= ent
->root
.string
;
3836 if (name
[0] == '$' || name
[0] == '.' || name
[0] < ' ')
3839 addr
= (ent
->u
.def
.value
3840 + ent
->u
.def
.section
->output_section
->vma
3841 + ent
->u
.def
.section
->output_offset
);
3843 for (idx
= 0; idx
< info
->table_size
; idx
++)
3844 if (addr
== info
->table_handlers
[idx
])
3845 info
->table_entries
[idx
] = ent
;
3847 if (addr
== info
->table_default_handler
)
3848 info
->table_default_entry
= ent
;
3854 rx_table_map (struct bfd_hash_entry
*vent
, void *vinfo
)
3856 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3857 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3858 const char *name
; /* of the symbol we've found */
3860 const char *tname
; /* name of the table */
3861 bfd_vma start_addr
, end_addr
;
3863 struct bfd_link_hash_entry
* h
;
3866 /* We're looking for globally defined symbols of the form
3867 $tablestart$<NAME>. */
3868 if (ent
->type
!= bfd_link_hash_defined
3869 && ent
->type
!= bfd_link_hash_defweak
)
3872 name
= ent
->root
.string
;
3874 if (strncmp (name
, "$tablestart$", 12))
3878 start_addr
= (ent
->u
.def
.value
3879 + ent
->u
.def
.section
->output_section
->vma
3880 + ent
->u
.def
.section
->output_offset
);
3882 buf
= (char *) malloc (12 + 10 + strlen (tname
));
3884 sprintf (buf
, "$tableend$%s", tname
);
3885 end_addr
= get_symbol_value_maybe (buf
, info
->info
);
3887 sprintf (buf
, "$tableentry$default$%s", tname
);
3888 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3891 info
->table_default_handler
= (h
->u
.def
.value
3892 + h
->u
.def
.section
->output_section
->vma
3893 + h
->u
.def
.section
->output_offset
);
3896 /* Zero is a valid handler address! */
3897 info
->table_default_handler
= (bfd_vma
) (-1);
3898 info
->table_default_entry
= NULL
;
3900 info
->table_start
= start_addr
;
3901 info
->table_size
= (int) (end_addr
- start_addr
) / 4;
3902 info
->table_handlers
= (bfd_vma
*) malloc (info
->table_size
* sizeof (bfd_vma
));
3903 info
->table_entries
= (struct bfd_link_hash_entry
**) malloc (info
->table_size
* sizeof (struct bfd_link_hash_entry
));
3905 for (idx
= 0; idx
< (int) (end_addr
- start_addr
) / 4; idx
++)
3907 sprintf (buf
, "$tableentry$%d$%s", idx
, tname
);
3908 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3909 if (h
&& (h
->type
== bfd_link_hash_defined
3910 || h
->type
== bfd_link_hash_defweak
))
3912 info
->table_handlers
[idx
] = (h
->u
.def
.value
3913 + h
->u
.def
.section
->output_section
->vma
3914 + h
->u
.def
.section
->output_offset
);
3917 info
->table_handlers
[idx
] = info
->table_default_handler
;
3918 info
->table_entries
[idx
] = NULL
;
3923 bfd_hash_traverse (&(info
->info
->hash
->table
), rx_table_map_2
, info
);
3925 fprintf (info
->mapfile
, "\nRX Vector Table: %s has %d entries at 0x%08" BFD_VMA_FMT
"x\n\n",
3926 tname
, info
->table_size
, start_addr
);
3928 if (info
->table_default_entry
)
3929 fprintf (info
->mapfile
, " default handler is: %s at 0x%08" BFD_VMA_FMT
"x\n",
3930 info
->table_default_entry
->root
.string
,
3931 info
->table_default_handler
);
3932 else if (info
->table_default_handler
!= (bfd_vma
)(-1))
3933 fprintf (info
->mapfile
, " default handler is at 0x%08" BFD_VMA_FMT
"x\n",
3934 info
->table_default_handler
);
3936 fprintf (info
->mapfile
, " no default handler\n");
3939 for (idx
= 0; idx
< info
->table_size
; idx
++)
3941 if (info
->table_handlers
[idx
] == info
->table_default_handler
)
3944 fprintf (info
->mapfile
, " . . .\n");
3950 fprintf (info
->mapfile
, " 0x%08" BFD_VMA_FMT
"x [%3d] ", start_addr
+ 4 * idx
, idx
);
3952 if (info
->table_handlers
[idx
] == (bfd_vma
) (-1))
3953 fprintf (info
->mapfile
, "(no handler found)\n");
3955 else if (info
->table_handlers
[idx
] == info
->table_default_handler
)
3957 if (info
->table_default_entry
)
3958 fprintf (info
->mapfile
, "(default)\n");
3960 fprintf (info
->mapfile
, "(default)\n");
3963 else if (info
->table_entries
[idx
])
3965 fprintf (info
->mapfile
, "0x%08" BFD_VMA_FMT
"x %s\n", info
->table_handlers
[idx
], info
->table_entries
[idx
]->root
.string
);
3970 fprintf (info
->mapfile
, "0x%08" BFD_VMA_FMT
"x ???\n", info
->table_handlers
[idx
]);
3974 fprintf (info
->mapfile
, " . . .\n");
3980 rx_additional_link_map_text (bfd
*obfd
, struct bfd_link_info
*info
, FILE *mapfile
)
3982 /* We scan the symbol table looking for $tableentry$'s, and for
3983 each, try to deduce which handlers go with which entries. */
3985 RX_Table_Info stuff
;
3989 stuff
.mapfile
= mapfile
;
3990 bfd_hash_traverse (&(info
->hash
->table
), rx_table_map
, &stuff
);
3994 #define ELF_ARCH bfd_arch_rx
3995 #define ELF_MACHINE_CODE EM_RX
3996 #define ELF_MAXPAGESIZE 0x1000
3998 #define TARGET_BIG_SYM rx_elf32_be_vec
3999 #define TARGET_BIG_NAME "elf32-rx-be"
4001 #define TARGET_LITTLE_SYM rx_elf32_le_vec
4002 #define TARGET_LITTLE_NAME "elf32-rx-le"
4004 #define elf_info_to_howto_rel NULL
4005 #define elf_info_to_howto rx_info_to_howto_rela
4006 #define elf_backend_object_p rx_elf_object_p
4007 #define elf_backend_relocate_section rx_elf_relocate_section
4008 #define elf_symbol_leading_char ('_')
4009 #define elf_backend_can_gc_sections 1
4010 #define elf_backend_modify_program_headers elf32_rx_modify_program_headers
4012 #define bfd_elf32_bfd_reloc_type_lookup rx_reloc_type_lookup
4013 #define bfd_elf32_bfd_reloc_name_lookup rx_reloc_name_lookup
4014 #define bfd_elf32_bfd_set_private_flags rx_elf_set_private_flags
4015 #define bfd_elf32_bfd_merge_private_bfd_data rx_elf_merge_private_bfd_data
4016 #define bfd_elf32_bfd_print_private_bfd_data rx_elf_print_private_bfd_data
4017 #define bfd_elf32_get_section_contents rx_get_section_contents
4018 #define bfd_elf32_set_section_contents rx_set_section_contents
4019 #define bfd_elf32_bfd_final_link rx_final_link
4020 #define bfd_elf32_bfd_relax_section elf32_rx_relax_section_wrapper
4021 #define elf_backend_special_sections elf32_rx_special_sections
4022 #define elf_backend_check_directives rx_check_directives
4024 #include "elf32-target.h"
4026 /* We define a second big-endian target that doesn't have the custom
4027 section get/set hooks, for times when we want to preserve the
4028 pre-swapped .text sections (like objcopy). */
4030 #undef TARGET_BIG_SYM
4031 #define TARGET_BIG_SYM rx_elf32_be_ns_vec
4032 #undef TARGET_BIG_NAME
4033 #define TARGET_BIG_NAME "elf32-rx-be-ns"
4034 #undef TARGET_LITTLE_SYM
4036 #undef bfd_elf32_get_section_contents
4037 #undef bfd_elf32_set_section_contents
4040 #define elf32_bed elf32_rx_be_ns_bed
4042 #include "elf32-target.h"