1 /* Renesas RX specific support for 32-bit ELF.
2 Copyright (C) 2008-2017 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 (_("%B: 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 (_("%B:%A: 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 (_("%B:%A: 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 (_("%B:%A: 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 (_("%B(%A): unsafe PID relocation %s at %#Lx (against %s in %s)"), \
688 input_bfd, input_section, howto->name, \
689 input_section->output_section->vma + input_section->output_offset + rel->r_offset, \
694 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
703 case R_RX_RH_3_PCREL
:
704 WARN_REDHAT ("RX_RH_3_PCREL");
707 OP (0) |= relocation
& 0x07;
711 WARN_REDHAT ("RX_RH_8_NEG");
712 relocation
= - relocation
;
714 case R_RX_DIR8S_PCREL
:
733 WARN_REDHAT ("RX_RH_16_NEG");
734 relocation
= - relocation
;
736 case R_RX_DIR16S_PCREL
:
738 RANGE (-32768, 32767);
739 #if RX_OPCODE_BIG_ENDIAN
742 OP (1) = relocation
>> 8;
747 WARN_REDHAT ("RX_RH_16_OP");
749 RANGE (-32768, 32767);
750 #if RX_OPCODE_BIG_ENDIAN
752 OP (0) = relocation
>> 8;
755 OP (1) = relocation
>> 8;
761 RANGE (-32768, 65535);
762 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
765 OP (0) = relocation
>> 8;
770 OP (1) = relocation
>> 8;
777 #if RX_OPCODE_BIG_ENDIAN
779 OP (0) = relocation
>> 8;
782 OP (1) = relocation
>> 8;
788 RANGE (-32768, 65536);
789 #if RX_OPCODE_BIG_ENDIAN
791 OP (0) = relocation
>> 8;
794 OP (1) = relocation
>> 8;
800 RANGE (-32768, 65536);
801 #if RX_OPCODE_BIG_ENDIAN
803 OP (1) = relocation
>> 8;
806 OP (0) = relocation
>> 8;
810 case R_RX_DIR3U_PCREL
:
813 OP (0) |= relocation
& 0x07;
818 WARN_REDHAT ("RX_RH_24_NEG");
819 relocation
= - relocation
;
821 case R_RX_DIR24S_PCREL
:
822 RANGE (-0x800000, 0x7fffff);
823 #if RX_OPCODE_BIG_ENDIAN
825 OP (1) = relocation
>> 8;
826 OP (0) = relocation
>> 16;
829 OP (1) = relocation
>> 8;
830 OP (2) = relocation
>> 16;
836 WARN_REDHAT ("RX_RH_24_OP");
837 RANGE (-0x800000, 0x7fffff);
838 #if RX_OPCODE_BIG_ENDIAN
840 OP (1) = relocation
>> 8;
841 OP (0) = relocation
>> 16;
844 OP (1) = relocation
>> 8;
845 OP (2) = relocation
>> 16;
851 RANGE (-0x800000, 0x7fffff);
852 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
855 OP (1) = relocation
>> 8;
856 OP (0) = relocation
>> 16;
861 OP (1) = relocation
>> 8;
862 OP (2) = relocation
>> 16;
868 WARN_REDHAT ("RX_RH_24_UNS");
870 #if RX_OPCODE_BIG_ENDIAN
872 OP (1) = relocation
>> 8;
873 OP (0) = relocation
>> 16;
876 OP (1) = relocation
>> 8;
877 OP (2) = relocation
>> 16;
883 WARN_REDHAT ("RX_RH_32_NEG");
884 relocation
= - relocation
;
885 #if RX_OPCODE_BIG_ENDIAN
887 OP (2) = relocation
>> 8;
888 OP (1) = relocation
>> 16;
889 OP (0) = relocation
>> 24;
892 OP (1) = relocation
>> 8;
893 OP (2) = relocation
>> 16;
894 OP (3) = relocation
>> 24;
900 WARN_REDHAT ("RX_RH_32_OP");
901 #if RX_OPCODE_BIG_ENDIAN
903 OP (2) = relocation
>> 8;
904 OP (1) = relocation
>> 16;
905 OP (0) = relocation
>> 24;
908 OP (1) = relocation
>> 8;
909 OP (2) = relocation
>> 16;
910 OP (3) = relocation
>> 24;
915 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
918 OP (2) = relocation
>> 8;
919 OP (1) = relocation
>> 16;
920 OP (0) = relocation
>> 24;
925 OP (1) = relocation
>> 8;
926 OP (2) = relocation
>> 16;
927 OP (3) = relocation
>> 24;
932 if (BIGE (output_bfd
))
935 OP (1) = relocation
>> 8;
936 OP (2) = relocation
>> 16;
937 OP (3) = relocation
>> 24;
942 OP (2) = relocation
>> 8;
943 OP (1) = relocation
>> 16;
944 OP (0) = relocation
>> 24;
951 WARN_REDHAT ("RX_RH_DIFF");
952 val
= bfd_get_32 (output_bfd
, & OP (0));
954 bfd_put_32 (output_bfd
, val
, & OP (0));
959 WARN_REDHAT ("RX_RH_GPRELB");
960 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
962 #if RX_OPCODE_BIG_ENDIAN
964 OP (0) = relocation
>> 8;
967 OP (1) = relocation
>> 8;
972 WARN_REDHAT ("RX_RH_GPRELW");
973 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
977 #if RX_OPCODE_BIG_ENDIAN
979 OP (0) = relocation
>> 8;
982 OP (1) = relocation
>> 8;
987 WARN_REDHAT ("RX_RH_GPRELL");
988 relocation
-= get_gp (info
, input_bfd
, input_section
, rel
->r_offset
);
992 #if RX_OPCODE_BIG_ENDIAN
994 OP (0) = relocation
>> 8;
997 OP (1) = relocation
>> 8;
1001 /* Internal relocations just for relaxation: */
1002 case R_RX_RH_ABS5p5B
:
1003 RX_STACK_POP (relocation
);
1006 OP (0) |= relocation
>> 2;
1008 OP (1) |= (relocation
<< 6) & 0x80;
1009 OP (1) |= (relocation
<< 3) & 0x08;
1012 case R_RX_RH_ABS5p5W
:
1013 RX_STACK_POP (relocation
);
1018 OP (0) |= relocation
>> 2;
1020 OP (1) |= (relocation
<< 6) & 0x80;
1021 OP (1) |= (relocation
<< 3) & 0x08;
1024 case R_RX_RH_ABS5p5L
:
1025 RX_STACK_POP (relocation
);
1030 OP (0) |= relocation
>> 2;
1032 OP (1) |= (relocation
<< 6) & 0x80;
1033 OP (1) |= (relocation
<< 3) & 0x08;
1036 case R_RX_RH_ABS5p8B
:
1037 RX_STACK_POP (relocation
);
1040 OP (0) |= (relocation
<< 3) & 0x80;
1041 OP (0) |= relocation
& 0x0f;
1044 case R_RX_RH_ABS5p8W
:
1045 RX_STACK_POP (relocation
);
1050 OP (0) |= (relocation
<< 3) & 0x80;
1051 OP (0) |= relocation
& 0x0f;
1054 case R_RX_RH_ABS5p8L
:
1055 RX_STACK_POP (relocation
);
1060 OP (0) |= (relocation
<< 3) & 0x80;
1061 OP (0) |= relocation
& 0x0f;
1064 case R_RX_RH_UIMM4p8
:
1067 OP (0) |= relocation
<< 4;
1070 case R_RX_RH_UNEG4p8
:
1073 OP (0) |= (-relocation
) << 4;
1076 /* Complex reloc handling: */
1080 RX_STACK_POP (relocation
);
1081 #if RX_OPCODE_BIG_ENDIAN
1082 OP (3) = relocation
;
1083 OP (2) = relocation
>> 8;
1084 OP (1) = relocation
>> 16;
1085 OP (0) = relocation
>> 24;
1087 OP (0) = relocation
;
1088 OP (1) = relocation
>> 8;
1089 OP (2) = relocation
>> 16;
1090 OP (3) = relocation
>> 24;
1094 case R_RX_ABS32_REV
:
1096 RX_STACK_POP (relocation
);
1097 #if RX_OPCODE_BIG_ENDIAN
1098 OP (0) = relocation
;
1099 OP (1) = relocation
>> 8;
1100 OP (2) = relocation
>> 16;
1101 OP (3) = relocation
>> 24;
1103 OP (3) = relocation
;
1104 OP (2) = relocation
>> 8;
1105 OP (1) = relocation
>> 16;
1106 OP (0) = relocation
>> 24;
1110 case R_RX_ABS24S_PCREL
:
1113 RX_STACK_POP (relocation
);
1114 RANGE (-0x800000, 0x7fffff);
1115 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
1117 OP (2) = relocation
;
1118 OP (1) = relocation
>> 8;
1119 OP (0) = relocation
>> 16;
1123 OP (0) = relocation
;
1124 OP (1) = relocation
>> 8;
1125 OP (2) = relocation
>> 16;
1131 RX_STACK_POP (relocation
);
1132 RANGE (-32768, 65535);
1133 #if RX_OPCODE_BIG_ENDIAN
1134 OP (1) = relocation
;
1135 OP (0) = relocation
>> 8;
1137 OP (0) = relocation
;
1138 OP (1) = relocation
>> 8;
1142 case R_RX_ABS16_REV
:
1144 RX_STACK_POP (relocation
);
1145 RANGE (-32768, 65535);
1146 #if RX_OPCODE_BIG_ENDIAN
1147 OP (0) = relocation
;
1148 OP (1) = relocation
>> 8;
1150 OP (1) = relocation
;
1151 OP (0) = relocation
>> 8;
1155 case R_RX_ABS16S_PCREL
:
1157 RX_STACK_POP (relocation
);
1158 RANGE (-32768, 32767);
1159 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
1161 OP (1) = relocation
;
1162 OP (0) = relocation
>> 8;
1166 OP (0) = relocation
;
1167 OP (1) = relocation
>> 8;
1173 RX_STACK_POP (relocation
);
1175 #if RX_OPCODE_BIG_ENDIAN
1176 OP (1) = relocation
;
1177 OP (0) = relocation
>> 8;
1179 OP (0) = relocation
;
1180 OP (1) = relocation
>> 8;
1186 RX_STACK_POP (relocation
);
1189 #if RX_OPCODE_BIG_ENDIAN
1190 OP (1) = relocation
;
1191 OP (0) = relocation
>> 8;
1193 OP (0) = relocation
;
1194 OP (1) = relocation
>> 8;
1200 RX_STACK_POP (relocation
);
1203 #if RX_OPCODE_BIG_ENDIAN
1204 OP (1) = relocation
;
1205 OP (0) = relocation
>> 8;
1207 OP (0) = relocation
;
1208 OP (1) = relocation
>> 8;
1214 RX_STACK_POP (relocation
);
1216 OP (0) = relocation
;
1221 RX_STACK_POP (relocation
);
1223 OP (0) = relocation
;
1228 RX_STACK_POP (relocation
);
1231 OP (0) = relocation
;
1236 RX_STACK_POP (relocation
);
1239 OP (0) = relocation
;
1245 case R_RX_ABS8S_PCREL
:
1246 RX_STACK_POP (relocation
);
1248 OP (0) = relocation
;
1252 if (r_symndx
< symtab_hdr
->sh_info
)
1253 RX_STACK_PUSH (sec
->output_section
->vma
1254 + sec
->output_offset
1260 && (h
->root
.type
== bfd_link_hash_defined
1261 || h
->root
.type
== bfd_link_hash_defweak
))
1262 RX_STACK_PUSH (h
->root
.u
.def
.value
1263 + sec
->output_section
->vma
1264 + sec
->output_offset
1267 _bfd_error_handler (_("Warning: RX_SYM reloc with an unknown symbol"));
1275 saw_subtract
= TRUE
;
1278 RX_STACK_PUSH (tmp
);
1286 RX_STACK_POP (tmp1
);
1287 RX_STACK_POP (tmp2
);
1289 RX_STACK_PUSH (tmp1
);
1297 saw_subtract
= TRUE
;
1298 RX_STACK_POP (tmp1
);
1299 RX_STACK_POP (tmp2
);
1301 RX_STACK_PUSH (tmp2
);
1309 RX_STACK_POP (tmp1
);
1310 RX_STACK_POP (tmp2
);
1312 RX_STACK_PUSH (tmp1
);
1320 RX_STACK_POP (tmp1
);
1321 RX_STACK_POP (tmp2
);
1323 RX_STACK_PUSH (tmp1
);
1331 RX_STACK_POP (tmp1
);
1332 RX_STACK_POP (tmp2
);
1334 RX_STACK_PUSH (tmp1
);
1342 RX_STACK_POP (tmp1
);
1343 RX_STACK_POP (tmp2
);
1345 RX_STACK_PUSH (tmp1
);
1349 case R_RX_OPsctsize
:
1350 RX_STACK_PUSH (input_section
->size
);
1354 RX_STACK_PUSH (input_section
->output_section
->vma
);
1361 RX_STACK_POP (tmp1
);
1362 RX_STACK_POP (tmp2
);
1364 RX_STACK_PUSH (tmp1
);
1372 RX_STACK_POP (tmp1
);
1373 RX_STACK_POP (tmp2
);
1375 RX_STACK_PUSH (tmp1
);
1383 RX_STACK_POP (tmp1
);
1384 RX_STACK_POP (tmp2
);
1386 RX_STACK_PUSH (tmp1
);
1396 RX_STACK_PUSH (tmp
);
1404 RX_STACK_POP (tmp1
);
1405 RX_STACK_POP (tmp2
);
1407 RX_STACK_PUSH (tmp1
);
1412 RX_STACK_PUSH (get_romstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1416 RX_STACK_PUSH (get_ramstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1420 r
= bfd_reloc_notsupported
;
1424 if (r
!= bfd_reloc_ok
)
1426 const char * msg
= NULL
;
1430 case bfd_reloc_overflow
:
1431 /* Catch the case of a missing function declaration
1432 and emit a more helpful error message. */
1433 if (r_type
== R_RX_DIR24S_PCREL
)
1434 /* xgettext:c-format */
1435 msg
= _("%B(%A): error: call to undefined function '%s'");
1437 (*info
->callbacks
->reloc_overflow
)
1438 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
1439 input_bfd
, input_section
, rel
->r_offset
);
1442 case bfd_reloc_undefined
:
1443 (*info
->callbacks
->undefined_symbol
)
1444 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1447 case bfd_reloc_other
:
1448 /* xgettext:c-format */
1449 msg
= _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
1452 case bfd_reloc_outofrange
:
1453 /* xgettext:c-format */
1454 msg
= _("%B(%A): internal error: out of range error");
1457 case bfd_reloc_notsupported
:
1458 /* xgettext:c-format */
1459 msg
= _("%B(%A): internal error: unsupported relocation error");
1462 case bfd_reloc_dangerous
:
1463 /* xgettext:c-format */
1464 msg
= _("%B(%A): internal error: dangerous relocation");
1468 /* xgettext:c-format */
1469 msg
= _("%B(%A): internal error: unknown error");
1474 _bfd_error_handler (msg
, input_bfd
, input_section
, name
);
1481 /* Relaxation Support. */
1483 /* Progression of relocations from largest operand size to smallest
1487 next_smaller_reloc (int r
)
1491 case R_RX_DIR32
: return R_RX_DIR24S
;
1492 case R_RX_DIR24S
: return R_RX_DIR16S
;
1493 case R_RX_DIR16S
: return R_RX_DIR8S
;
1494 case R_RX_DIR8S
: return R_RX_NONE
;
1496 case R_RX_DIR16
: return R_RX_DIR8
;
1497 case R_RX_DIR8
: return R_RX_NONE
;
1499 case R_RX_DIR16U
: return R_RX_DIR8U
;
1500 case R_RX_DIR8U
: return R_RX_NONE
;
1502 case R_RX_DIR24S_PCREL
: return R_RX_DIR16S_PCREL
;
1503 case R_RX_DIR16S_PCREL
: return R_RX_DIR8S_PCREL
;
1504 case R_RX_DIR8S_PCREL
: return R_RX_DIR3U_PCREL
;
1506 case R_RX_DIR16UL
: return R_RX_DIR8UL
;
1507 case R_RX_DIR8UL
: return R_RX_NONE
;
1508 case R_RX_DIR16UW
: return R_RX_DIR8UW
;
1509 case R_RX_DIR8UW
: return R_RX_NONE
;
1511 case R_RX_RH_32_OP
: return R_RX_RH_24_OP
;
1512 case R_RX_RH_24_OP
: return R_RX_RH_16_OP
;
1513 case R_RX_RH_16_OP
: return R_RX_DIR8
;
1515 case R_RX_ABS32
: return R_RX_ABS24S
;
1516 case R_RX_ABS24S
: return R_RX_ABS16S
;
1517 case R_RX_ABS16
: return R_RX_ABS8
;
1518 case R_RX_ABS16U
: return R_RX_ABS8U
;
1519 case R_RX_ABS16S
: return R_RX_ABS8S
;
1520 case R_RX_ABS8
: return R_RX_NONE
;
1521 case R_RX_ABS8U
: return R_RX_NONE
;
1522 case R_RX_ABS8S
: return R_RX_NONE
;
1523 case R_RX_ABS24S_PCREL
: return R_RX_ABS16S_PCREL
;
1524 case R_RX_ABS16S_PCREL
: return R_RX_ABS8S_PCREL
;
1525 case R_RX_ABS8S_PCREL
: return R_RX_NONE
;
1526 case R_RX_ABS16UL
: return R_RX_ABS8UL
;
1527 case R_RX_ABS16UW
: return R_RX_ABS8UW
;
1528 case R_RX_ABS8UL
: return R_RX_NONE
;
1529 case R_RX_ABS8UW
: return R_RX_NONE
;
1534 /* Delete some bytes from a section while relaxing. */
1537 elf32_rx_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, int count
,
1538 Elf_Internal_Rela
*alignment_rel
, int force_snip
,
1539 Elf_Internal_Rela
*irelstart
)
1541 Elf_Internal_Shdr
* symtab_hdr
;
1542 unsigned int sec_shndx
;
1543 bfd_byte
* contents
;
1544 Elf_Internal_Rela
* irel
;
1545 Elf_Internal_Rela
* irelend
;
1546 Elf_Internal_Sym
* isym
;
1547 Elf_Internal_Sym
* isymend
;
1549 unsigned int symcount
;
1550 struct elf_link_hash_entry
** sym_hashes
;
1551 struct elf_link_hash_entry
** end_hashes
;
1556 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1558 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1560 /* The deletion must stop at the next alignment boundary, if
1561 ALIGNMENT_REL is non-NULL. */
1564 toaddr
= alignment_rel
->r_offset
;
1566 BFD_ASSERT (toaddr
> addr
);
1568 /* Actually delete the bytes. */
1569 memmove (contents
+ addr
, contents
+ addr
+ count
,
1570 (size_t) (toaddr
- addr
- count
));
1572 /* If we don't have an alignment marker to worry about, we can just
1573 shrink the section. Otherwise, we have to fill in the newly
1574 created gap with NOP insns (0x03). */
1578 memset (contents
+ toaddr
- count
, 0x03, count
);
1581 BFD_ASSERT (irel
!= NULL
|| sec
->reloc_count
== 0);
1582 irelend
= irel
+ sec
->reloc_count
;
1584 /* Adjust all the relocs. */
1585 for (; irel
< irelend
; irel
++)
1587 /* Get the new reloc address. */
1588 if (irel
->r_offset
> addr
1589 && (irel
->r_offset
< toaddr
1590 || (force_snip
&& irel
->r_offset
== toaddr
)))
1591 irel
->r_offset
-= count
;
1593 /* If we see an ALIGN marker at the end of the gap, we move it
1594 to the beginning of the gap, since marking these gaps is what
1596 if (irel
->r_offset
== toaddr
1597 && ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
1598 && irel
->r_addend
& RX_RELAXA_ALIGN
)
1599 irel
->r_offset
-= count
;
1602 /* Adjust the local symbols defined in this section. */
1603 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1604 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1605 isymend
= isym
+ symtab_hdr
->sh_info
;
1607 for (; isym
< isymend
; isym
++)
1609 /* If the symbol is in the range of memory we just moved, we
1610 have to adjust its value. */
1611 if (isym
->st_shndx
== sec_shndx
1612 && isym
->st_value
> addr
1613 && isym
->st_value
< toaddr
)
1614 isym
->st_value
-= count
;
1616 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1617 *end* is in the moved bytes but it's *start* isn't), then we
1618 must adjust its size. */
1619 if (isym
->st_shndx
== sec_shndx
1620 && isym
->st_value
< addr
1621 && isym
->st_value
+ isym
->st_size
> addr
1622 && isym
->st_value
+ isym
->st_size
< toaddr
)
1623 isym
->st_size
-= count
;
1626 /* Now adjust the global symbols defined in this section. */
1627 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1628 - symtab_hdr
->sh_info
);
1629 sym_hashes
= elf_sym_hashes (abfd
);
1630 end_hashes
= sym_hashes
+ symcount
;
1632 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1634 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1636 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1637 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1638 && sym_hash
->root
.u
.def
.section
== sec
)
1640 /* As above, adjust the value if needed. */
1641 if (sym_hash
->root
.u
.def
.value
> addr
1642 && sym_hash
->root
.u
.def
.value
< toaddr
)
1643 sym_hash
->root
.u
.def
.value
-= count
;
1645 /* As above, adjust the size if needed. */
1646 if (sym_hash
->root
.u
.def
.value
< addr
1647 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
1648 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
1649 sym_hash
->size
-= count
;
1656 /* Used to sort relocs by address. If relocs have the same address,
1657 we maintain their relative order, except that R_RX_RH_RELAX
1658 alignment relocs must be the first reloc for any given address. */
1661 reloc_bubblesort (Elf_Internal_Rela
* r
, int count
)
1665 bfd_boolean swappit
;
1667 /* This is almost a classic bubblesort. It's the slowest sort, but
1668 we're taking advantage of the fact that the relocations are
1669 mostly in order already (the assembler emits them that way) and
1670 we need relocs with the same address to remain in the same
1676 for (i
= 0; i
< count
- 1; i
++)
1678 if (r
[i
].r_offset
> r
[i
+ 1].r_offset
)
1680 else if (r
[i
].r_offset
< r
[i
+ 1].r_offset
)
1682 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1683 && (r
[i
+ 1].r_addend
& RX_RELAXA_ALIGN
))
1685 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1686 && (r
[i
+ 1].r_addend
& RX_RELAXA_ELIGN
)
1687 && !(ELF32_R_TYPE (r
[i
].r_info
) == R_RX_RH_RELAX
1688 && (r
[i
].r_addend
& RX_RELAXA_ALIGN
)))
1695 Elf_Internal_Rela tmp
;
1700 /* If we do move a reloc back, re-scan to see if it
1701 needs to be moved even further back. This avoids
1702 most of the O(n^2) behavior for our cases. */
1712 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1713 rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1714 lrel, abfd, sec, link_info, scale)
1717 rx_offset_for_reloc (bfd
* abfd
,
1718 Elf_Internal_Rela
* rel
,
1719 Elf_Internal_Shdr
* symtab_hdr
,
1720 Elf_External_Sym_Shndx
* shndx_buf ATTRIBUTE_UNUSED
,
1721 Elf_Internal_Sym
* intsyms
,
1722 Elf_Internal_Rela
** lrel
,
1724 asection
* input_section
,
1725 struct bfd_link_info
* info
,
1729 bfd_reloc_status_type r
;
1733 /* REL is the first of 1..N relocations. We compute the symbol
1734 value for each relocation, then combine them if needed. LREL
1735 gets a pointer to the last relocation used. */
1740 /* Get the value of the symbol referred to by the reloc. */
1741 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1743 /* A local symbol. */
1744 Elf_Internal_Sym
*isym
;
1747 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1749 if (isym
->st_shndx
== SHN_UNDEF
)
1750 ssec
= bfd_und_section_ptr
;
1751 else if (isym
->st_shndx
== SHN_ABS
)
1752 ssec
= bfd_abs_section_ptr
;
1753 else if (isym
->st_shndx
== SHN_COMMON
)
1754 ssec
= bfd_com_section_ptr
;
1756 ssec
= bfd_section_from_elf_index (abfd
,
1759 /* Initial symbol value. */
1760 symval
= isym
->st_value
;
1762 /* GAS may have made this symbol relative to a section, in
1763 which case, we have to add the addend to find the
1765 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
1766 symval
+= rel
->r_addend
;
1770 if ((ssec
->flags
& SEC_MERGE
)
1771 && ssec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
1772 symval
= _bfd_merged_section_offset (abfd
, & ssec
,
1773 elf_section_data (ssec
)->sec_info
,
1777 /* Now make the offset relative to where the linker is putting it. */
1780 ssec
->output_section
->vma
+ ssec
->output_offset
;
1782 symval
+= rel
->r_addend
;
1787 struct elf_link_hash_entry
* h
;
1789 /* An external symbol. */
1790 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1791 h
= elf_sym_hashes (abfd
)[indx
];
1792 BFD_ASSERT (h
!= NULL
);
1794 if (h
->root
.type
!= bfd_link_hash_defined
1795 && h
->root
.type
!= bfd_link_hash_defweak
)
1797 /* This appears to be a reference to an undefined
1798 symbol. Just ignore it--it will be caught by the
1799 regular reloc processing. */
1805 symval
= (h
->root
.u
.def
.value
1806 + h
->root
.u
.def
.section
->output_section
->vma
1807 + h
->root
.u
.def
.section
->output_offset
);
1809 symval
+= rel
->r_addend
;
1812 switch (ELF32_R_TYPE (rel
->r_info
))
1815 RX_STACK_PUSH (symval
);
1819 RX_STACK_POP (tmp1
);
1821 RX_STACK_PUSH (tmp1
);
1825 RX_STACK_POP (tmp1
);
1826 RX_STACK_POP (tmp2
);
1828 RX_STACK_PUSH (tmp1
);
1832 RX_STACK_POP (tmp1
);
1833 RX_STACK_POP (tmp2
);
1835 RX_STACK_PUSH (tmp2
);
1839 RX_STACK_POP (tmp1
);
1840 RX_STACK_POP (tmp2
);
1842 RX_STACK_PUSH (tmp1
);
1846 RX_STACK_POP (tmp1
);
1847 RX_STACK_POP (tmp2
);
1849 RX_STACK_PUSH (tmp1
);
1853 RX_STACK_POP (tmp1
);
1854 RX_STACK_POP (tmp2
);
1856 RX_STACK_PUSH (tmp1
);
1860 RX_STACK_POP (tmp1
);
1861 RX_STACK_POP (tmp2
);
1863 RX_STACK_PUSH (tmp1
);
1866 case R_RX_OPsctsize
:
1867 RX_STACK_PUSH (input_section
->size
);
1871 RX_STACK_PUSH (input_section
->output_section
->vma
);
1875 RX_STACK_POP (tmp1
);
1876 RX_STACK_POP (tmp2
);
1878 RX_STACK_PUSH (tmp1
);
1882 RX_STACK_POP (tmp1
);
1883 RX_STACK_POP (tmp2
);
1885 RX_STACK_PUSH (tmp1
);
1889 RX_STACK_POP (tmp1
);
1890 RX_STACK_POP (tmp2
);
1892 RX_STACK_PUSH (tmp1
);
1896 RX_STACK_POP (tmp1
);
1898 RX_STACK_PUSH (tmp1
);
1902 RX_STACK_POP (tmp1
);
1903 RX_STACK_POP (tmp2
);
1905 RX_STACK_PUSH (tmp1
);
1909 RX_STACK_PUSH (get_romstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1913 RX_STACK_PUSH (get_ramstart (info
, input_bfd
, input_section
, rel
->r_offset
));
1921 RX_STACK_POP (symval
);
1932 RX_STACK_POP (symval
);
1940 RX_STACK_POP (symval
);
1953 move_reloc (Elf_Internal_Rela
* irel
, Elf_Internal_Rela
* srel
, int delta
)
1955 bfd_vma old_offset
= srel
->r_offset
;
1958 while (irel
<= srel
)
1960 if (irel
->r_offset
== old_offset
)
1961 irel
->r_offset
+= delta
;
1966 /* Relax one section. */
1969 elf32_rx_relax_section (bfd
* abfd
,
1971 struct bfd_link_info
* link_info
,
1972 bfd_boolean
* again
,
1973 bfd_boolean allow_pcrel3
)
1975 Elf_Internal_Shdr
* symtab_hdr
;
1976 Elf_Internal_Shdr
* shndx_hdr
;
1977 Elf_Internal_Rela
* internal_relocs
;
1978 Elf_Internal_Rela
* irel
;
1979 Elf_Internal_Rela
* srel
;
1980 Elf_Internal_Rela
* irelend
;
1981 Elf_Internal_Rela
* next_alignment
;
1982 Elf_Internal_Rela
* prev_alignment
;
1983 bfd_byte
* contents
= NULL
;
1984 bfd_byte
* free_contents
= NULL
;
1985 Elf_Internal_Sym
* intsyms
= NULL
;
1986 Elf_Internal_Sym
* free_intsyms
= NULL
;
1987 Elf_External_Sym_Shndx
* shndx_buf
= NULL
;
1993 int section_alignment_glue
;
1994 /* how much to scale the relocation by - 1, 2, or 4. */
1997 /* Assume nothing changes. */
2000 /* We don't have to do anything for a relocatable link, if
2001 this section does not have relocs, or if this is not a
2003 if (bfd_link_relocatable (link_info
)
2004 || (sec
->flags
& SEC_RELOC
) == 0
2005 || sec
->reloc_count
== 0
2006 || (sec
->flags
& SEC_CODE
) == 0)
2009 symtab_hdr
= & elf_symtab_hdr (abfd
);
2010 if (elf_symtab_shndx_list (abfd
))
2011 shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
2015 sec_start
= sec
->output_section
->vma
+ sec
->output_offset
;
2017 /* Get the section contents. */
2018 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2019 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2020 /* Go get them off disk. */
2023 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2025 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2028 /* Read this BFD's symbols. */
2029 /* Get cached copy if it exists. */
2030 if (symtab_hdr
->contents
!= NULL
)
2031 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2034 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
2035 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
2038 if (shndx_hdr
&& shndx_hdr
->sh_size
!= 0)
2042 amt
= symtab_hdr
->sh_info
;
2043 amt
*= sizeof (Elf_External_Sym_Shndx
);
2044 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
2045 if (shndx_buf
== NULL
)
2047 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
2048 || bfd_bread (shndx_buf
, amt
, abfd
) != amt
)
2050 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
2053 /* Get a copy of the native relocations. */
2054 /* Note - we ignore the setting of link_info->keep_memory when reading
2055 in these relocs. We have to maintain a permanent copy of the relocs
2056 because we are going to walk over them multiple times, adjusting them
2057 as bytes are deleted from the section, and with this relaxation
2058 function itself being called multiple times on the same section... */
2059 internal_relocs
= _bfd_elf_link_read_relocs
2060 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
);
2061 if (internal_relocs
== NULL
)
2064 /* The RL_ relocs must be just before the operand relocs they go
2065 with, so we must sort them to guarantee this. We use bubblesort
2066 instead of qsort so we can guarantee that relocs with the same
2067 address remain in the same relative order. */
2068 reloc_bubblesort (internal_relocs
, sec
->reloc_count
);
2070 /* Walk through them looking for relaxing opportunities. */
2071 irelend
= internal_relocs
+ sec
->reloc_count
;
2073 /* This will either be NULL or a pointer to the next alignment
2075 next_alignment
= internal_relocs
;
2076 /* This will be the previous alignment, although at first it points
2077 to the first real relocation. */
2078 prev_alignment
= internal_relocs
;
2080 /* We calculate worst case shrinkage caused by alignment directives.
2081 No fool-proof, but better than either ignoring the problem or
2082 doing heavy duty analysis of all the alignment markers in all
2084 section_alignment_glue
= 0;
2085 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2086 if (ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
2087 && irel
->r_addend
& RX_RELAXA_ALIGN
)
2089 int this_glue
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
2091 if (section_alignment_glue
< this_glue
)
2092 section_alignment_glue
= this_glue
;
2094 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2096 section_alignment_glue
*= 2;
2098 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2100 unsigned char *insn
;
2103 /* The insns we care about are all marked with one of these. */
2104 if (ELF32_R_TYPE (irel
->r_info
) != R_RX_RH_RELAX
)
2107 if (irel
->r_addend
& RX_RELAXA_ALIGN
2108 || next_alignment
== internal_relocs
)
2110 /* When we delete bytes, we need to maintain all the alignments
2111 indicated. In addition, we need to be careful about relaxing
2112 jumps across alignment boundaries - these displacements
2113 *grow* when we delete bytes. For now, don't shrink
2114 displacements across an alignment boundary, just in case.
2115 Note that this only affects relocations to the same
2117 prev_alignment
= next_alignment
;
2118 next_alignment
+= 2;
2119 while (next_alignment
< irelend
2120 && (ELF32_R_TYPE (next_alignment
->r_info
) != R_RX_RH_RELAX
2121 || !(next_alignment
->r_addend
& RX_RELAXA_ELIGN
)))
2123 if (next_alignment
>= irelend
|| next_alignment
->r_offset
== 0)
2124 next_alignment
= NULL
;
2127 /* When we hit alignment markers, see if we've shrunk enough
2128 before them to reduce the gap without violating the alignment
2130 if (irel
->r_addend
& RX_RELAXA_ALIGN
)
2132 /* At this point, the next relocation *should* be the ELIGN
2134 Elf_Internal_Rela
*erel
= irel
+ 1;
2135 unsigned int alignment
, nbytes
;
2137 if (ELF32_R_TYPE (erel
->r_info
) != R_RX_RH_RELAX
)
2139 if (!(erel
->r_addend
& RX_RELAXA_ELIGN
))
2142 alignment
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
2144 if (erel
->r_offset
- irel
->r_offset
< alignment
)
2147 nbytes
= erel
->r_offset
- irel
->r_offset
;
2148 nbytes
/= alignment
;
2149 nbytes
*= alignment
;
2151 elf32_rx_relax_delete_bytes (abfd
, sec
, erel
->r_offset
-nbytes
, nbytes
, next_alignment
,
2152 erel
->r_offset
== sec
->size
, internal_relocs
);
2158 if (irel
->r_addend
& RX_RELAXA_ELIGN
)
2161 insn
= contents
+ irel
->r_offset
;
2163 nrelocs
= irel
->r_addend
& RX_RELAXA_RNUM
;
2165 /* At this point, we have an insn that is a candidate for linker
2166 relaxation. There are NRELOCS relocs following that may be
2167 relaxed, although each reloc may be made of more than one
2168 reloc entry (such as gp-rel symbols). */
2170 /* Get the value of the symbol referred to by the reloc. Just
2171 in case this is the last reloc in the list, use the RL's
2172 addend to choose between this reloc (no addend) or the next
2173 (yes addend, which means at least one following reloc). */
2175 /* srel points to the "current" reloction for this insn -
2176 actually the last reloc for a given operand, which is the one
2177 we need to update. We check the relaxations in the same
2178 order that the relocations happen, so we'll just push it
2182 pc
= sec
->output_section
->vma
+ sec
->output_offset
2186 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2187 pcrel = symval - pc + srel->r_addend; \
2190 #define SNIPNR(offset, nbytes) \
2191 elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0, internal_relocs);
2192 #define SNIP(offset, nbytes, newtype) \
2193 SNIPNR (offset, nbytes); \
2194 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2196 /* The order of these bit tests must match the order that the
2197 relocs appear in. Since we sorted those by offset, we can
2200 /* Note that the numbers in, say, DSP6 are the bit offsets of
2201 the code fields that describe the operand. Bits number 0 for
2202 the MSB of insn[0]. */
2209 if (irel
->r_addend
& RX_RELAXA_DSP6
)
2214 if (code
== 2 && symval
/scale
<= 255)
2216 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2219 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2220 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2222 SNIP (3, 1, newrel
);
2227 else if (code
== 1 && symval
== 0)
2230 SNIP (2, 1, R_RX_NONE
);
2234 /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst. */
2235 else if (code
== 1 && symval
/scale
<= 31
2236 /* Decodable bits. */
2237 && (insn
[0] & 0xcc) == 0xcc
2239 && (insn
[0] & 0x30) != 0x30
2240 /* Register MSBs. */
2241 && (insn
[1] & 0x88) == 0x00)
2245 insn
[0] = 0x88 | (insn
[0] & 0x30);
2246 /* The register fields are in the right place already. */
2248 /* We can't relax this new opcode. */
2251 switch ((insn
[0] & 0x30) >> 4)
2254 newrel
= R_RX_RH_ABS5p5B
;
2257 newrel
= R_RX_RH_ABS5p5W
;
2260 newrel
= R_RX_RH_ABS5p5L
;
2264 move_reloc (irel
, srel
, -2);
2265 SNIP (2, 1, newrel
);
2268 /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst. */
2269 else if (code
== 1 && symval
/scale
<= 31
2270 /* Decodable bits. */
2271 && (insn
[0] & 0xf8) == 0x58
2272 /* Register MSBs. */
2273 && (insn
[1] & 0x88) == 0x00)
2277 insn
[0] = 0xb0 | ((insn
[0] & 0x04) << 1);
2278 /* The register fields are in the right place already. */
2280 /* We can't relax this new opcode. */
2283 switch ((insn
[0] & 0x08) >> 3)
2286 newrel
= R_RX_RH_ABS5p5B
;
2289 newrel
= R_RX_RH_ABS5p5W
;
2293 move_reloc (irel
, srel
, -2);
2294 SNIP (2, 1, newrel
);
2298 /* A DSP4 operand always follows a DSP6 operand, even if there's
2299 no relocation for it. We have to read the code out of the
2300 opcode to calculate the offset of the operand. */
2301 if (irel
->r_addend
& RX_RELAXA_DSP4
)
2303 int code6
, offset
= 0;
2307 code6
= insn
[0] & 0x03;
2310 case 0: offset
= 2; break;
2311 case 1: offset
= 3; break;
2312 case 2: offset
= 4; break;
2313 case 3: offset
= 2; break;
2316 code
= (insn
[0] & 0x0c) >> 2;
2318 if (code
== 2 && symval
/ scale
<= 255)
2320 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2324 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2325 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2327 SNIP (offset
+1, 1, newrel
);
2332 else if (code
== 1 && symval
== 0)
2335 SNIP (offset
, 1, R_RX_NONE
);
2338 /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2339 else if (code
== 1 && symval
/scale
<= 31
2340 /* Decodable bits. */
2341 && (insn
[0] & 0xc3) == 0xc3
2343 && (insn
[0] & 0x30) != 0x30
2344 /* Register MSBs. */
2345 && (insn
[1] & 0x88) == 0x00)
2349 insn
[0] = 0x80 | (insn
[0] & 0x30);
2350 /* The register fields are in the right place already. */
2352 /* We can't relax this new opcode. */
2355 switch ((insn
[0] & 0x30) >> 4)
2358 newrel
= R_RX_RH_ABS5p5B
;
2361 newrel
= R_RX_RH_ABS5p5W
;
2364 newrel
= R_RX_RH_ABS5p5L
;
2368 move_reloc (irel
, srel
, -2);
2369 SNIP (2, 1, newrel
);
2373 /* These always occur alone, but the offset depends on whether
2374 it's a MEMEX opcode (0x06) or not. */
2375 if (irel
->r_addend
& RX_RELAXA_DSP14
)
2380 if (insn
[0] == 0x06)
2387 if (code
== 2 && symval
/ scale
<= 255)
2389 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2393 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2394 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2396 SNIP (offset
, 1, newrel
);
2400 else if (code
== 1 && symval
== 0)
2403 SNIP (offset
, 1, R_RX_NONE
);
2414 /* These always occur alone. */
2415 if (irel
->r_addend
& RX_RELAXA_IMM6
)
2421 /* These relocations sign-extend, so we must do signed compares. */
2422 ssymval
= (long) symval
;
2424 code
= insn
[0] & 0x03;
2426 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2428 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2432 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2433 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2435 SNIP (2, 1, newrel
);
2440 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2442 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2446 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2447 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2449 SNIP (2, 1, newrel
);
2454 /* Special case UIMM8 format: CMP #uimm8,Rdst. */
2455 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2456 /* Decodable bits. */
2457 && (insn
[0] & 0xfc) == 0x74
2458 /* Decodable bits. */
2459 && ((insn
[1] & 0xf0) == 0x00))
2464 insn
[1] = 0x50 | (insn
[1] & 0x0f);
2466 /* We can't relax this new opcode. */
2469 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2470 newrel
= R_RX_ABS8U
;
2472 newrel
= R_RX_DIR8U
;
2474 SNIP (2, 1, newrel
);
2478 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2480 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2484 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2485 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2487 SNIP (2, 1, newrel
);
2492 /* Special case UIMM4 format: CMP, MUL, AND, OR. */
2493 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2494 /* Decodable bits and immediate type. */
2496 /* Decodable bits. */
2497 && (insn
[1] & 0xc0) == 0x00)
2499 static const int newop
[4] = { 1, 3, 4, 5 };
2501 insn
[0] = 0x60 | newop
[insn
[1] >> 4];
2502 /* The register number doesn't move. */
2504 /* We can't relax this new opcode. */
2507 move_reloc (irel
, srel
, -1);
2509 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2513 /* Special case UIMM4 format: ADD -> ADD/SUB. */
2514 else if (code
== 1 && ssymval
<= 15 && ssymval
>= -15
2515 /* Decodable bits and immediate type. */
2517 /* Same register for source and destination. */
2518 && ((insn
[1] >> 4) == (insn
[1] & 0x0f)))
2522 /* Note that we can't turn "add $0,Rs" into a NOP
2523 because the flags need to be set right. */
2527 insn
[0] = 0x60; /* Subtract. */
2528 newrel
= R_RX_RH_UNEG4p8
;
2532 insn
[0] = 0x62; /* Add. */
2533 newrel
= R_RX_RH_UIMM4p8
;
2536 /* The register number is in the right place. */
2538 /* We can't relax this new opcode. */
2541 move_reloc (irel
, srel
, -1);
2543 SNIP (2, 1, newrel
);
2548 /* These are either matched with a DSP6 (2-byte base) or an id24
2550 if (irel
->r_addend
& RX_RELAXA_IMM12
)
2552 int dspcode
, offset
= 0;
2557 if ((insn
[0] & 0xfc) == 0xfc)
2558 dspcode
= 1; /* Just something with one byte operand. */
2560 dspcode
= insn
[0] & 3;
2563 case 0: offset
= 2; break;
2564 case 1: offset
= 3; break;
2565 case 2: offset
= 4; break;
2566 case 3: offset
= 2; break;
2569 /* These relocations sign-extend, so we must do signed compares. */
2570 ssymval
= (long) symval
;
2572 code
= (insn
[1] >> 2) & 3;
2573 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2575 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2579 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2580 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2582 SNIP (offset
, 1, newrel
);
2587 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2589 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2593 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2594 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2596 SNIP (offset
, 1, newrel
);
2601 /* Special case UIMM8 format: MOV #uimm8,Rdst. */
2602 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2603 /* Decodable bits. */
2605 /* Decodable bits. */
2606 && ((insn
[1] & 0x03) == 0x02))
2611 insn
[1] = 0x40 | (insn
[1] >> 4);
2613 /* We can't relax this new opcode. */
2616 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2617 newrel
= R_RX_ABS8U
;
2619 newrel
= R_RX_DIR8U
;
2621 SNIP (2, 1, newrel
);
2625 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2627 unsigned int newrel
= ELF32_R_TYPE(srel
->r_info
);
2631 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2632 if (newrel
!= ELF32_R_TYPE(srel
->r_info
))
2634 SNIP (offset
, 1, newrel
);
2639 /* Special case UIMM4 format: MOV #uimm4,Rdst. */
2640 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2641 /* Decodable bits. */
2643 /* Decodable bits. */
2644 && ((insn
[1] & 0x03) == 0x02))
2647 insn
[1] = insn
[1] >> 4;
2649 /* We can't relax this new opcode. */
2652 move_reloc (irel
, srel
, -1);
2654 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2659 if (irel
->r_addend
& RX_RELAXA_BRA
)
2661 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2663 int alignment_glue
= 0;
2667 /* Branches over alignment chunks are problematic, as
2668 deleting bytes here makes the branch *further* away. We
2669 can be agressive with branches within this alignment
2670 block, but not branches outside it. */
2671 if ((prev_alignment
== NULL
2672 || symval
< (bfd_vma
)(sec_start
+ prev_alignment
->r_offset
))
2673 && (next_alignment
== NULL
2674 || symval
> (bfd_vma
)(sec_start
+ next_alignment
->r_offset
)))
2675 alignment_glue
= section_alignment_glue
;
2677 if (ELF32_R_TYPE(srel
[1].r_info
) == R_RX_RH_RELAX
2678 && srel
[1].r_addend
& RX_RELAXA_BRA
2679 && srel
[1].r_offset
< irel
->r_offset
+ pcrel
)
2682 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2684 /* The values we compare PCREL with are not what you'd
2685 expect; they're off by a little to compensate for (1)
2686 where the reloc is relative to the insn, and (2) how much
2687 the insn is going to change when we relax it. */
2689 /* These we have to decode. */
2692 case 0x04: /* BRA pcdsp:24 */
2693 if (-32768 + alignment_glue
<= pcrel
2694 && pcrel
<= 32765 - alignment_glue
)
2697 SNIP (3, 1, newrel
);
2702 case 0x38: /* BRA pcdsp:16 */
2703 if (-128 + alignment_glue
<= pcrel
2704 && pcrel
<= 127 - alignment_glue
)
2707 SNIP (2, 1, newrel
);
2712 case 0x2e: /* BRA pcdsp:8 */
2713 /* Note that there's a risk here of shortening things so
2714 much that we no longer fit this reloc; it *should*
2715 only happen when you branch across a branch, and that
2716 branch also devolves into BRA.S. "Real" code should
2718 if (max_pcrel3
+ alignment_glue
<= pcrel
2719 && pcrel
<= 10 - alignment_glue
2723 SNIP (1, 1, newrel
);
2724 move_reloc (irel
, srel
, -1);
2729 case 0x05: /* BSR pcdsp:24 */
2730 if (-32768 + alignment_glue
<= pcrel
2731 && pcrel
<= 32765 - alignment_glue
)
2734 SNIP (1, 1, newrel
);
2739 case 0x3a: /* BEQ.W pcdsp:16 */
2740 case 0x3b: /* BNE.W pcdsp:16 */
2741 if (-128 + alignment_glue
<= pcrel
2742 && pcrel
<= 127 - alignment_glue
)
2744 insn
[0] = 0x20 | (insn
[0] & 1);
2745 SNIP (1, 1, newrel
);
2750 case 0x20: /* BEQ.B pcdsp:8 */
2751 case 0x21: /* BNE.B pcdsp:8 */
2752 if (max_pcrel3
+ alignment_glue
<= pcrel
2753 && pcrel
- alignment_glue
<= 10
2756 insn
[0] = 0x10 | ((insn
[0] & 1) << 3);
2757 SNIP (1, 1, newrel
);
2758 move_reloc (irel
, srel
, -1);
2763 case 0x16: /* synthetic BNE dsp24 */
2764 case 0x1e: /* synthetic BEQ dsp24 */
2765 if (-32767 + alignment_glue
<= pcrel
2766 && pcrel
<= 32766 - alignment_glue
2769 if (insn
[0] == 0x16)
2773 /* We snip out the bytes at the end else the reloc
2774 will get moved too, and too much. */
2775 SNIP (3, 2, newrel
);
2776 move_reloc (irel
, srel
, -1);
2782 /* Special case - synthetic conditional branches, pcrel24.
2783 Note that EQ and NE have been handled above. */
2784 if ((insn
[0] & 0xf0) == 0x20
2787 && srel
->r_offset
!= irel
->r_offset
+ 1
2788 && -32767 + alignment_glue
<= pcrel
2789 && pcrel
<= 32766 - alignment_glue
)
2793 SNIP (5, 1, newrel
);
2797 /* Special case - synthetic conditional branches, pcrel16 */
2798 if ((insn
[0] & 0xf0) == 0x20
2801 && srel
->r_offset
!= irel
->r_offset
+ 1
2802 && -127 + alignment_glue
<= pcrel
2803 && pcrel
<= 126 - alignment_glue
)
2805 int cond
= (insn
[0] & 0x0f) ^ 0x01;
2807 insn
[0] = 0x20 | cond
;
2808 /* By moving the reloc first, we avoid having
2809 delete_bytes move it also. */
2810 move_reloc (irel
, srel
, -2);
2811 SNIP (2, 3, newrel
);
2816 BFD_ASSERT (nrelocs
== 0);
2818 /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2819 use MOV.bwl #uimm:8, dsp:5[r7] format. This is tricky
2820 because it may have one or two relocations. */
2821 if ((insn
[0] & 0xfc) == 0xf8
2822 && (insn
[1] & 0x80) == 0x00
2823 && (insn
[0] & 0x03) != 0x03)
2825 int dcode
, icode
, reg
, ioff
, dscale
, ilen
;
2826 bfd_vma disp_val
= 0;
2828 Elf_Internal_Rela
* disp_rel
= 0;
2829 Elf_Internal_Rela
* imm_rel
= 0;
2834 dcode
= insn
[0] & 0x03;
2835 icode
= (insn
[1] >> 2) & 0x03;
2836 reg
= (insn
[1] >> 4) & 0x0f;
2838 ioff
= dcode
== 1 ? 3 : dcode
== 2 ? 4 : 2;
2840 /* Figure out what the dispacement is. */
2841 if (dcode
== 1 || dcode
== 2)
2843 /* There's a displacement. See if there's a reloc for it. */
2844 if (srel
[1].r_offset
== irel
->r_offset
+ 2)
2856 #if RX_OPCODE_BIG_ENDIAN
2857 disp_val
= insn
[2] * 256 + insn
[3];
2859 disp_val
= insn
[2] + insn
[3] * 256;
2862 switch (insn
[1] & 3)
2878 /* Figure out what the immediate is. */
2879 if (srel
[1].r_offset
== irel
->r_offset
+ ioff
)
2882 imm_val
= (long) symval
;
2887 unsigned char * ip
= insn
+ ioff
;
2892 /* For byte writes, we don't sign extend. Makes the math easier later. */
2896 imm_val
= (char) ip
[0];
2899 #if RX_OPCODE_BIG_ENDIAN
2900 imm_val
= ((char) ip
[0] << 8) | ip
[1];
2902 imm_val
= ((char) ip
[1] << 8) | ip
[0];
2906 #if RX_OPCODE_BIG_ENDIAN
2907 imm_val
= ((char) ip
[0] << 16) | (ip
[1] << 8) | ip
[2];
2909 imm_val
= ((char) ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2913 #if RX_OPCODE_BIG_ENDIAN
2914 imm_val
= (ip
[0] << 24) | (ip
[1] << 16) | (ip
[2] << 8) | ip
[3];
2916 imm_val
= (ip
[3] << 24) | (ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2950 /* The shortcut happens when the immediate is 0..255,
2951 register r0 to r7, and displacement (scaled) 0..31. */
2953 if (0 <= imm_val
&& imm_val
<= 255
2954 && 0 <= reg
&& reg
<= 7
2955 && disp_val
/ dscale
<= 31)
2957 insn
[0] = 0x3c | (insn
[1] & 0x03);
2958 insn
[1] = (((disp_val
/ dscale
) << 3) & 0x80) | (reg
<< 4) | ((disp_val
/dscale
) & 0x0f);
2963 int newrel
= R_RX_NONE
;
2968 newrel
= R_RX_RH_ABS5p8B
;
2971 newrel
= R_RX_RH_ABS5p8W
;
2974 newrel
= R_RX_RH_ABS5p8L
;
2977 disp_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (disp_rel
->r_info
), newrel
);
2978 move_reloc (irel
, disp_rel
, -1);
2982 imm_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (imm_rel
->r_info
), R_RX_DIR8U
);
2983 move_reloc (disp_rel
? disp_rel
: irel
,
2985 irel
->r_offset
- imm_rel
->r_offset
+ 2);
2988 SNIPNR (3, ilen
- 3);
2991 /* We can't relax this new opcode. */
2997 /* We can't reliably relax branches to DIR3U_PCREL unless we know
2998 whatever they're branching over won't shrink any more. If we're
2999 basically done here, do one more pass just for branches - but
3000 don't request a pass after that one! */
3001 if (!*again
&& !allow_pcrel3
)
3003 bfd_boolean ignored
;
3005 elf32_rx_relax_section (abfd
, sec
, link_info
, &ignored
, TRUE
);
3011 if (free_contents
!= NULL
)
3012 free (free_contents
);
3014 if (shndx_buf
!= NULL
)
3016 shndx_hdr
->contents
= NULL
;
3020 if (free_intsyms
!= NULL
)
3021 free (free_intsyms
);
3027 elf32_rx_relax_section_wrapper (bfd
* abfd
,
3029 struct bfd_link_info
* link_info
,
3030 bfd_boolean
* again
)
3032 return elf32_rx_relax_section (abfd
, sec
, link_info
, again
, FALSE
);
3035 /* Function to set the ELF flag bits. */
3038 rx_elf_set_private_flags (bfd
* abfd
, flagword flags
)
3040 elf_elfheader (abfd
)->e_flags
= flags
;
3041 elf_flags_init (abfd
) = TRUE
;
3045 static bfd_boolean no_warn_mismatch
= FALSE
;
3046 static bfd_boolean ignore_lma
= TRUE
;
3048 void bfd_elf32_rx_set_target_flags (bfd_boolean
, bfd_boolean
);
3051 bfd_elf32_rx_set_target_flags (bfd_boolean user_no_warn_mismatch
,
3052 bfd_boolean user_ignore_lma
)
3054 no_warn_mismatch
= user_no_warn_mismatch
;
3055 ignore_lma
= user_ignore_lma
;
3058 /* Converts FLAGS into a descriptive string.
3059 Returns a static pointer. */
3062 describe_flags (flagword flags
)
3064 static char buf
[128];
3068 if (flags
& E_FLAG_RX_64BIT_DOUBLES
)
3069 strcat (buf
, "64-bit doubles");
3071 strcat (buf
, "32-bit doubles");
3073 if (flags
& E_FLAG_RX_DSP
)
3074 strcat (buf
, ", dsp");
3076 strcat (buf
, ", no dsp");
3078 if (flags
& E_FLAG_RX_PID
)
3079 strcat (buf
, ", pid");
3081 strcat (buf
, ", no pid");
3083 if (flags
& E_FLAG_RX_ABI
)
3084 strcat (buf
, ", RX ABI");
3086 strcat (buf
, ", GCC ABI");
3088 if (flags
& E_FLAG_RX_SINSNS_SET
)
3089 strcat (buf
, flags
& E_FLAG_RX_SINSNS_YES
? ", uses String instructions" : ", bans String instructions");
3094 /* Merge backend specific data from an object file to the output
3095 object file when linking. */
3098 rx_elf_merge_private_bfd_data (bfd
* ibfd
, struct bfd_link_info
*info
)
3100 bfd
*obfd
= info
->output_bfd
;
3103 bfd_boolean error
= FALSE
;
3105 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3106 old_flags
= elf_elfheader (obfd
)->e_flags
;
3108 if (!elf_flags_init (obfd
))
3110 /* First call, no flags set. */
3111 elf_flags_init (obfd
) = TRUE
;
3112 elf_elfheader (obfd
)->e_flags
= new_flags
;
3114 else if (old_flags
!= new_flags
)
3116 flagword known_flags
;
3118 if (old_flags
& E_FLAG_RX_SINSNS_SET
)
3120 if ((new_flags
& E_FLAG_RX_SINSNS_SET
) == 0)
3122 new_flags
&= ~ E_FLAG_RX_SINSNS_MASK
;
3123 new_flags
|= (old_flags
& E_FLAG_RX_SINSNS_MASK
);
3126 else if (new_flags
& E_FLAG_RX_SINSNS_SET
)
3128 old_flags
&= ~ E_FLAG_RX_SINSNS_MASK
;
3129 old_flags
|= (new_flags
& E_FLAG_RX_SINSNS_MASK
);
3132 known_flags
= E_FLAG_RX_ABI
| E_FLAG_RX_64BIT_DOUBLES
3133 | E_FLAG_RX_DSP
| E_FLAG_RX_PID
| E_FLAG_RX_SINSNS_MASK
;
3135 if ((old_flags
^ new_flags
) & known_flags
)
3137 /* Only complain if flag bits we care about do not match.
3138 Other bits may be set, since older binaries did use some
3139 deprecated flags. */
3140 if (no_warn_mismatch
)
3142 elf_elfheader (obfd
)->e_flags
= (new_flags
| old_flags
) & known_flags
;
3146 _bfd_error_handler (_("There is a conflict merging the"
3147 " ELF header flags from %B"),
3149 _bfd_error_handler (_(" the input file's flags: %s"),
3150 describe_flags (new_flags
));
3151 _bfd_error_handler (_(" the output file's flags: %s"),
3152 describe_flags (old_flags
));
3157 elf_elfheader (obfd
)->e_flags
= new_flags
& known_flags
;
3161 bfd_set_error (bfd_error_bad_value
);
3167 rx_elf_print_private_bfd_data (bfd
* abfd
, void * ptr
)
3169 FILE * file
= (FILE *) ptr
;
3172 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
3174 /* Print normal ELF private data. */
3175 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
3177 flags
= elf_elfheader (abfd
)->e_flags
;
3178 fprintf (file
, _("private flags = 0x%lx:"), (long) flags
);
3180 fprintf (file
, "%s", describe_flags (flags
));
3184 /* Return the MACH for an e_flags value. */
3187 elf32_rx_machine (bfd
* abfd ATTRIBUTE_UNUSED
)
3189 #if 0 /* FIXME: EF_RX_CPU_MASK collides with E_FLAG_RX_...
3190 Need to sort out how these flag bits are used.
3191 For now we assume that the flags are OK. */
3192 if ((elf_elfheader (abfd
)->e_flags
& EF_RX_CPU_MASK
) == EF_RX_CPU_RX
)
3200 rx_elf_object_p (bfd
* abfd
)
3204 Elf_Internal_Phdr
*phdr
= elf_tdata (abfd
)->phdr
;
3205 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
3206 int nphdrs
= ehdr
->e_phnum
;
3208 static int saw_be
= FALSE
;
3209 bfd_vma end_phdroff
;
3211 /* We never want to automatically choose the non-swapping big-endian
3212 target. The user can only get that explicitly, such as with -I
3214 if (abfd
->xvec
== &rx_elf32_be_ns_vec
3215 && abfd
->target_defaulted
)
3218 /* BFD->target_defaulted is not set to TRUE when a target is chosen
3219 as a fallback, so we check for "scanning" to know when to stop
3220 using the non-swapping target. */
3221 if (abfd
->xvec
== &rx_elf32_be_ns_vec
3224 if (abfd
->xvec
== &rx_elf32_be_vec
)
3227 bfd_default_set_arch_mach (abfd
, bfd_arch_rx
,
3228 elf32_rx_machine (abfd
));
3230 /* For each PHDR in the object, we must find some section that
3231 corresponds (based on matching file offsets) and use its VMA
3232 information to reconstruct the p_vaddr field we clobbered when we
3234 /* If PT_LOAD headers include the ELF file header or program headers
3235 then the PT_LOAD header does not start with some section contents.
3236 Making adjustments based on the difference between sh_offset and
3237 p_offset is nonsense in such cases. Exclude them. Note that
3238 since standard linker scripts for RX do not use SIZEOF_HEADERS,
3239 the linker won't normally create PT_LOAD segments covering the
3240 headers so this is mainly for passing the ld testsuite.
3241 FIXME. Why are we looking at non-PT_LOAD headers here? */
3242 end_phdroff
= ehdr
->e_ehsize
;
3243 if (ehdr
->e_phoff
!= 0)
3244 end_phdroff
= ehdr
->e_phoff
+ nphdrs
* ehdr
->e_phentsize
;
3245 for (i
=0; i
<nphdrs
; i
++)
3247 for (u
=0; u
<elf_tdata(abfd
)->num_elf_sections
; u
++)
3249 Elf_Internal_Shdr
*sec
= elf_tdata(abfd
)->elf_sect_ptr
[u
];
3251 if (phdr
[i
].p_filesz
3252 && phdr
[i
].p_offset
>= end_phdroff
3253 && phdr
[i
].p_offset
<= (bfd_vma
) sec
->sh_offset
3255 && sec
->sh_type
!= SHT_NOBITS
3256 && (bfd_vma
)sec
->sh_offset
<= phdr
[i
].p_offset
+ (phdr
[i
].p_filesz
- 1))
3258 /* Found one! The difference between the two addresses,
3259 plus the difference between the two file offsets, is
3260 enough information to reconstruct the lma. */
3262 /* Example where they aren't:
3263 PHDR[1] = lma fffc0100 offset 00002010 size 00000100
3264 SEC[6] = vma 00000050 offset 00002050 size 00000040
3266 The correct LMA for the section is fffc0140 + (2050-2010).
3269 phdr
[i
].p_vaddr
= sec
->sh_addr
+ (sec
->sh_offset
- phdr
[i
].p_offset
);
3274 /* We must update the bfd sections as well, so we don't stop
3276 bsec
= abfd
->sections
;
3279 if (phdr
[i
].p_filesz
3280 && phdr
[i
].p_vaddr
<= bsec
->vma
3281 && bsec
->vma
<= phdr
[i
].p_vaddr
+ (phdr
[i
].p_filesz
- 1))
3283 bsec
->lma
= phdr
[i
].p_paddr
+ (bsec
->vma
- phdr
[i
].p_vaddr
);
3295 rx_dump_symtab (bfd
* abfd
, void * internal_syms
, void * external_syms
)
3298 Elf_Internal_Sym
* isymbuf
;
3299 Elf_Internal_Sym
* isymend
;
3300 Elf_Internal_Sym
* isym
;
3301 Elf_Internal_Shdr
* symtab_hdr
;
3302 bfd_boolean free_internal
= FALSE
, free_external
= FALSE
;
3304 char * st_info_stb_str
;
3305 char * st_other_str
;
3306 char * st_shndx_str
;
3308 if (! internal_syms
)
3310 internal_syms
= bfd_malloc (1000);
3313 if (! external_syms
)
3315 external_syms
= bfd_malloc (1000);
3319 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3320 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3322 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
3323 symtab_hdr
->sh_info
, 0,
3324 internal_syms
, external_syms
, NULL
);
3326 isymbuf
= internal_syms
;
3327 isymend
= isymbuf
+ locsymcount
;
3329 for (isym
= isymbuf
; isym
< isymend
; isym
++)
3331 switch (ELF_ST_TYPE (isym
->st_info
))
3333 case STT_FUNC
: st_info_str
= "STT_FUNC"; break;
3334 case STT_SECTION
: st_info_str
= "STT_SECTION"; break;
3335 case STT_FILE
: st_info_str
= "STT_FILE"; break;
3336 case STT_OBJECT
: st_info_str
= "STT_OBJECT"; break;
3337 case STT_TLS
: st_info_str
= "STT_TLS"; break;
3338 default: st_info_str
= "";
3340 switch (ELF_ST_BIND (isym
->st_info
))
3342 case STB_LOCAL
: st_info_stb_str
= "STB_LOCAL"; break;
3343 case STB_GLOBAL
: st_info_stb_str
= "STB_GLOBAL"; break;
3344 default: st_info_stb_str
= "";
3346 switch (ELF_ST_VISIBILITY (isym
->st_other
))
3348 case STV_DEFAULT
: st_other_str
= "STV_DEFAULT"; break;
3349 case STV_INTERNAL
: st_other_str
= "STV_INTERNAL"; break;
3350 case STV_PROTECTED
: st_other_str
= "STV_PROTECTED"; break;
3351 default: st_other_str
= "";
3353 switch (isym
->st_shndx
)
3355 case SHN_ABS
: st_shndx_str
= "SHN_ABS"; break;
3356 case SHN_COMMON
: st_shndx_str
= "SHN_COMMON"; break;
3357 case SHN_UNDEF
: st_shndx_str
= "SHN_UNDEF"; break;
3358 default: st_shndx_str
= "";
3361 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3362 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3364 (unsigned long) isym
->st_value
,
3365 (unsigned long) isym
->st_size
,
3367 bfd_elf_string_from_elf_section (abfd
, symtab_hdr
->sh_link
,
3369 isym
->st_info
, st_info_str
, st_info_stb_str
,
3370 isym
->st_other
, st_other_str
,
3371 isym
->st_shndx
, st_shndx_str
);
3374 free (internal_syms
);
3376 free (external_syms
);
3380 rx_get_reloc (long reloc
)
3382 if (0 <= reloc
&& reloc
< R_RX_max
)
3383 return rx_elf_howto_table
[reloc
].name
;
3389 /* We must take care to keep the on-disk copy of any code sections
3390 that are fully linked swapped if the target is big endian, to match
3391 the Renesas tools. */
3393 /* The rule is: big endian object that are final-link executables,
3394 have code sections stored with 32-bit words swapped relative to
3395 what you'd get by default. */
3398 rx_get_section_contents (bfd
* abfd
,
3402 bfd_size_type count
)
3404 int exec
= (abfd
->flags
& EXEC_P
) ? 1 : 0;
3405 int s_code
= (section
->flags
& SEC_CODE
) ? 1 : 0;
3409 fprintf (stderr
, "dj: get %ld %ld from %s %s e%d sc%d %08lx:%08lx\n",
3410 (long) offset
, (long) count
, section
->name
,
3411 bfd_big_endian(abfd
) ? "be" : "le",
3412 exec
, s_code
, (long unsigned) section
->filepos
,
3413 (long unsigned) offset
);
3416 if (exec
&& s_code
&& bfd_big_endian (abfd
))
3418 char * cloc
= (char *) location
;
3419 bfd_size_type cnt
, end_cnt
;
3423 /* Fetch and swap unaligned bytes at the beginning. */
3428 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3433 bfd_putb32 (bfd_getl32 (buf
), buf
);
3435 cnt
= 4 - (offset
% 4);
3439 memcpy (location
, buf
+ (offset
% 4), cnt
);
3446 end_cnt
= count
% 4;
3448 /* Fetch and swap the middle bytes. */
3451 rv
= _bfd_generic_get_section_contents (abfd
, section
, cloc
, offset
,
3456 for (cnt
= count
; cnt
>= 4; cnt
-= 4, cloc
+= 4)
3457 bfd_putb32 (bfd_getl32 (cloc
), cloc
);
3460 /* Fetch and swap the end bytes. */
3465 /* Fetch the end bytes. */
3466 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3467 offset
+ count
- end_cnt
, 4);
3471 bfd_putb32 (bfd_getl32 (buf
), buf
);
3472 memcpy (cloc
, buf
, end_cnt
);
3476 rv
= _bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
);
3483 rx2_set_section_contents (bfd
* abfd
,
3485 const void * location
,
3487 bfd_size_type count
)
3491 fprintf (stderr
, " set sec %s %08x loc %p offset %#x count %#x\n",
3492 section
->name
, (unsigned) section
->vma
, location
, (int) offset
, (int) count
);
3493 for (i
= 0; i
< count
; i
++)
3495 if (i
% 16 == 0 && i
> 0)
3496 fprintf (stderr
, "\n");
3498 if (i
% 16 && i
% 4 == 0)
3499 fprintf (stderr
, " ");
3502 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3504 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3506 fprintf (stderr
, "\n");
3508 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3510 #define _bfd_elf_set_section_contents rx2_set_section_contents
3514 rx_set_section_contents (bfd
* abfd
,
3516 const void * location
,
3518 bfd_size_type count
)
3520 bfd_boolean exec
= (abfd
->flags
& EXEC_P
) ? TRUE
: FALSE
;
3521 bfd_boolean s_code
= (section
->flags
& SEC_CODE
) ? TRUE
: FALSE
;
3523 char * swapped_data
= NULL
;
3525 bfd_vma caddr
= section
->vma
+ offset
;
3527 bfd_size_type scount
;
3532 fprintf (stderr
, "\ndj: set %ld %ld to %s %s e%d sc%d\n",
3533 (long) offset
, (long) count
, section
->name
,
3534 bfd_big_endian (abfd
) ? "be" : "le",
3537 for (i
= 0; i
< count
; i
++)
3539 int a
= section
->vma
+ offset
+ i
;
3541 if (a
% 16 == 0 && a
> 0)
3542 fprintf (stderr
, "\n");
3544 if (a
% 16 && a
% 4 == 0)
3545 fprintf (stderr
, " ");
3547 if (a
% 16 == 0 || i
== 0)
3548 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3550 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3553 fprintf (stderr
, "\n");
3556 if (! exec
|| ! s_code
|| ! bfd_big_endian (abfd
))
3557 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3559 while (count
> 0 && caddr
> 0 && caddr
% 4)
3563 case 0: faddr
= offset
+ 3; break;
3564 case 1: faddr
= offset
+ 1; break;
3565 case 2: faddr
= offset
- 1; break;
3566 case 3: faddr
= offset
- 3; break;
3569 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3573 location
= (bfd_byte
*) location
+ 1;
3579 scount
= (int)(count
/ 4) * 4;
3582 char * cloc
= (char *) location
;
3584 swapped_data
= (char *) bfd_alloc (abfd
, count
);
3586 for (i
= 0; i
< count
; i
+= 4)
3588 bfd_vma v
= bfd_getl32 (cloc
+ i
);
3589 bfd_putb32 (v
, swapped_data
+ i
);
3592 rv
= _bfd_elf_set_section_contents (abfd
, section
, swapped_data
, offset
, scount
);
3599 location
= (bfd_byte
*) location
+ scount
;
3604 caddr
= section
->vma
+ offset
;
3609 case 0: faddr
= offset
+ 3; break;
3610 case 1: faddr
= offset
+ 1; break;
3611 case 2: faddr
= offset
- 1; break;
3612 case 3: faddr
= offset
- 3; break;
3614 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3618 location
= (bfd_byte
*) location
+ 1;
3629 rx_final_link (bfd
* abfd
, struct bfd_link_info
* info
)
3633 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3636 fprintf (stderr
, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3637 o
->name
, o
->flags
, o
->vma
, o
->lma
, o
->size
, o
->rawsize
);
3639 if (o
->flags
& SEC_CODE
3640 && bfd_big_endian (abfd
)
3644 fprintf (stderr
, "adjusting...\n");
3646 o
->size
+= 4 - (o
->size
% 4);
3650 return bfd_elf_final_link (abfd
, info
);
3654 elf32_rx_modify_program_headers (bfd
* abfd ATTRIBUTE_UNUSED
,
3655 struct bfd_link_info
* info ATTRIBUTE_UNUSED
)
3657 const struct elf_backend_data
* bed
;
3658 struct elf_obj_tdata
* tdata
;
3659 Elf_Internal_Phdr
* phdr
;
3663 bed
= get_elf_backend_data (abfd
);
3664 tdata
= elf_tdata (abfd
);
3666 count
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
3669 for (i
= count
; i
-- != 0;)
3670 if (phdr
[i
].p_type
== PT_LOAD
)
3672 /* The Renesas tools expect p_paddr to be zero. However,
3673 there is no other way to store the writable data in ROM for
3674 startup initialization. So, we let the linker *think*
3675 we're using paddr and vaddr the "usual" way, but at the
3676 last minute we move the paddr into the vaddr (which is what
3677 the simulator uses) and zero out paddr. Note that this
3678 does not affect the section headers, just the program
3679 headers. We hope. */
3680 phdr
[i
].p_vaddr
= phdr
[i
].p_paddr
;
3681 #if 0 /* If we zero out p_paddr, then the LMA in the section table
3683 phdr
[i
].p_paddr
= 0;
3690 /* The default literal sections should always be marked as "code" (i.e.,
3691 SHF_EXECINSTR). This is particularly important for big-endian mode
3692 when we do not want their contents byte reversed. */
3693 static const struct bfd_elf_special_section elf32_rx_special_sections
[] =
3695 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3696 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3697 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_EXECINSTR
},
3698 { NULL
, 0, 0, 0, 0 }
3703 struct bfd_link_info
*info
;
3704 bfd_vma table_start
;
3706 bfd_vma
*table_handlers
;
3707 bfd_vma table_default_handler
;
3708 struct bfd_link_hash_entry
**table_entries
;
3709 struct bfd_link_hash_entry
*table_default_entry
;
3714 rx_table_find (struct bfd_hash_entry
*vent
, void *vinfo
)
3716 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3717 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3718 const char *name
; /* of the symbol we've found */
3722 const char *tname
; /* name of the table */
3723 bfd_vma start_addr
, end_addr
;
3725 struct bfd_link_hash_entry
* h
;
3727 /* We're looking for globally defined symbols of the form
3728 $tablestart$<NAME>. */
3729 if (ent
->type
!= bfd_link_hash_defined
3730 && ent
->type
!= bfd_link_hash_defweak
)
3733 name
= ent
->root
.string
;
3734 sec
= ent
->u
.def
.section
;
3737 if (strncmp (name
, "$tablestart$", 12))
3740 sec
->flags
|= SEC_KEEP
;
3744 start_addr
= ent
->u
.def
.value
;
3746 /* At this point, we can't build the table but we can (and must)
3747 find all the related symbols and mark their sections as SEC_KEEP
3748 so we don't garbage collect them. */
3750 buf
= (char *) malloc (12 + 10 + strlen (tname
));
3752 sprintf (buf
, "$tableend$%s", tname
);
3753 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3754 if (!h
|| (h
->type
!= bfd_link_hash_defined
3755 && h
->type
!= bfd_link_hash_defweak
))
3757 /* xgettext:c-format */
3758 _bfd_error_handler (_("%B:%A: table %s missing corresponding %s"),
3759 abfd
, sec
, name
, buf
);
3763 if (h
->u
.def
.section
!= ent
->u
.def
.section
)
3765 /* xgettext:c-format */
3766 _bfd_error_handler (_("%B:%A: %s and %s must be in the same input section"),
3767 h
->u
.def
.section
->owner
, h
->u
.def
.section
,
3772 end_addr
= h
->u
.def
.value
;
3774 sprintf (buf
, "$tableentry$default$%s", tname
);
3775 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3776 if (h
&& (h
->type
== bfd_link_hash_defined
3777 || h
->type
== bfd_link_hash_defweak
))
3779 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3782 for (idx
= 0; idx
< (int) (end_addr
- start_addr
) / 4; idx
++)
3784 sprintf (buf
, "$tableentry$%d$%s", idx
, tname
);
3785 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3786 if (h
&& (h
->type
== bfd_link_hash_defined
3787 || h
->type
== bfd_link_hash_defweak
))
3789 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3793 /* Return TRUE to keep scanning, FALSE to end the traversal. */
3797 /* We need to check for table entry symbols and build the tables, and
3798 we need to do it before the linker does garbage collection. This function is
3799 called once per input object file. */
3802 (bfd
* abfd ATTRIBUTE_UNUSED
,
3803 struct bfd_link_info
* info ATTRIBUTE_UNUSED
)
3805 RX_Table_Info stuff
;
3809 bfd_hash_traverse (&(info
->hash
->table
), rx_table_find
, &stuff
);
3816 rx_table_map_2 (struct bfd_hash_entry
*vent
, void *vinfo
)
3818 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3819 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3824 /* See if the symbol ENT has an address listed in the table, and
3825 isn't a debug/special symbol. If so, put it in the table. */
3827 if (ent
->type
!= bfd_link_hash_defined
3828 && ent
->type
!= bfd_link_hash_defweak
)
3831 name
= ent
->root
.string
;
3833 if (name
[0] == '$' || name
[0] == '.' || name
[0] < ' ')
3836 addr
= (ent
->u
.def
.value
3837 + ent
->u
.def
.section
->output_section
->vma
3838 + ent
->u
.def
.section
->output_offset
);
3840 for (idx
= 0; idx
< info
->table_size
; idx
++)
3841 if (addr
== info
->table_handlers
[idx
])
3842 info
->table_entries
[idx
] = ent
;
3844 if (addr
== info
->table_default_handler
)
3845 info
->table_default_entry
= ent
;
3851 rx_table_map (struct bfd_hash_entry
*vent
, void *vinfo
)
3853 RX_Table_Info
*info
= (RX_Table_Info
*)vinfo
;
3854 struct bfd_link_hash_entry
*ent
= (struct bfd_link_hash_entry
*)vent
;
3855 const char *name
; /* of the symbol we've found */
3857 const char *tname
; /* name of the table */
3858 bfd_vma start_addr
, end_addr
;
3860 struct bfd_link_hash_entry
* h
;
3863 /* We're looking for globally defined symbols of the form
3864 $tablestart$<NAME>. */
3865 if (ent
->type
!= bfd_link_hash_defined
3866 && ent
->type
!= bfd_link_hash_defweak
)
3869 name
= ent
->root
.string
;
3871 if (strncmp (name
, "$tablestart$", 12))
3875 start_addr
= (ent
->u
.def
.value
3876 + ent
->u
.def
.section
->output_section
->vma
3877 + ent
->u
.def
.section
->output_offset
);
3879 buf
= (char *) malloc (12 + 10 + strlen (tname
));
3881 sprintf (buf
, "$tableend$%s", tname
);
3882 end_addr
= get_symbol_value_maybe (buf
, info
->info
);
3884 sprintf (buf
, "$tableentry$default$%s", tname
);
3885 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3888 info
->table_default_handler
= (h
->u
.def
.value
3889 + h
->u
.def
.section
->output_section
->vma
3890 + h
->u
.def
.section
->output_offset
);
3893 /* Zero is a valid handler address! */
3894 info
->table_default_handler
= (bfd_vma
) (-1);
3895 info
->table_default_entry
= NULL
;
3897 info
->table_start
= start_addr
;
3898 info
->table_size
= (int) (end_addr
- start_addr
) / 4;
3899 info
->table_handlers
= (bfd_vma
*) malloc (info
->table_size
* sizeof (bfd_vma
));
3900 info
->table_entries
= (struct bfd_link_hash_entry
**) malloc (info
->table_size
* sizeof (struct bfd_link_hash_entry
));
3902 for (idx
= 0; idx
< (int) (end_addr
- start_addr
) / 4; idx
++)
3904 sprintf (buf
, "$tableentry$%d$%s", idx
, tname
);
3905 h
= bfd_link_hash_lookup (info
->info
->hash
, buf
, FALSE
, FALSE
, TRUE
);
3906 if (h
&& (h
->type
== bfd_link_hash_defined
3907 || h
->type
== bfd_link_hash_defweak
))
3909 info
->table_handlers
[idx
] = (h
->u
.def
.value
3910 + h
->u
.def
.section
->output_section
->vma
3911 + h
->u
.def
.section
->output_offset
);
3914 info
->table_handlers
[idx
] = info
->table_default_handler
;
3915 info
->table_entries
[idx
] = NULL
;
3920 bfd_hash_traverse (&(info
->info
->hash
->table
), rx_table_map_2
, info
);
3922 fprintf (info
->mapfile
, "\nRX Vector Table: %s has %d entries at 0x%08" BFD_VMA_FMT
"x\n\n",
3923 tname
, info
->table_size
, start_addr
);
3925 if (info
->table_default_entry
)
3926 fprintf (info
->mapfile
, " default handler is: %s at 0x%08" BFD_VMA_FMT
"x\n",
3927 info
->table_default_entry
->root
.string
,
3928 info
->table_default_handler
);
3929 else if (info
->table_default_handler
!= (bfd_vma
)(-1))
3930 fprintf (info
->mapfile
, " default handler is at 0x%08" BFD_VMA_FMT
"x\n",
3931 info
->table_default_handler
);
3933 fprintf (info
->mapfile
, " no default handler\n");
3936 for (idx
= 0; idx
< info
->table_size
; idx
++)
3938 if (info
->table_handlers
[idx
] == info
->table_default_handler
)
3941 fprintf (info
->mapfile
, " . . .\n");
3947 fprintf (info
->mapfile
, " 0x%08" BFD_VMA_FMT
"x [%3d] ", start_addr
+ 4 * idx
, idx
);
3949 if (info
->table_handlers
[idx
] == (bfd_vma
) (-1))
3950 fprintf (info
->mapfile
, "(no handler found)\n");
3952 else if (info
->table_handlers
[idx
] == info
->table_default_handler
)
3954 if (info
->table_default_entry
)
3955 fprintf (info
->mapfile
, "(default)\n");
3957 fprintf (info
->mapfile
, "(default)\n");
3960 else if (info
->table_entries
[idx
])
3962 fprintf (info
->mapfile
, "0x%08" BFD_VMA_FMT
"x %s\n", info
->table_handlers
[idx
], info
->table_entries
[idx
]->root
.string
);
3967 fprintf (info
->mapfile
, "0x%08" BFD_VMA_FMT
"x ???\n", info
->table_handlers
[idx
]);
3971 fprintf (info
->mapfile
, " . . .\n");
3977 rx_additional_link_map_text (bfd
*obfd
, struct bfd_link_info
*info
, FILE *mapfile
)
3979 /* We scan the symbol table looking for $tableentry$'s, and for
3980 each, try to deduce which handlers go with which entries. */
3982 RX_Table_Info stuff
;
3986 stuff
.mapfile
= mapfile
;
3987 bfd_hash_traverse (&(info
->hash
->table
), rx_table_map
, &stuff
);
3991 #define ELF_ARCH bfd_arch_rx
3992 #define ELF_MACHINE_CODE EM_RX
3993 #define ELF_MAXPAGESIZE 0x1000
3995 #define TARGET_BIG_SYM rx_elf32_be_vec
3996 #define TARGET_BIG_NAME "elf32-rx-be"
3998 #define TARGET_LITTLE_SYM rx_elf32_le_vec
3999 #define TARGET_LITTLE_NAME "elf32-rx-le"
4001 #define elf_info_to_howto_rel NULL
4002 #define elf_info_to_howto rx_info_to_howto_rela
4003 #define elf_backend_object_p rx_elf_object_p
4004 #define elf_backend_relocate_section rx_elf_relocate_section
4005 #define elf_symbol_leading_char ('_')
4006 #define elf_backend_can_gc_sections 1
4007 #define elf_backend_modify_program_headers elf32_rx_modify_program_headers
4009 #define bfd_elf32_bfd_reloc_type_lookup rx_reloc_type_lookup
4010 #define bfd_elf32_bfd_reloc_name_lookup rx_reloc_name_lookup
4011 #define bfd_elf32_bfd_set_private_flags rx_elf_set_private_flags
4012 #define bfd_elf32_bfd_merge_private_bfd_data rx_elf_merge_private_bfd_data
4013 #define bfd_elf32_bfd_print_private_bfd_data rx_elf_print_private_bfd_data
4014 #define bfd_elf32_get_section_contents rx_get_section_contents
4015 #define bfd_elf32_set_section_contents rx_set_section_contents
4016 #define bfd_elf32_bfd_final_link rx_final_link
4017 #define bfd_elf32_bfd_relax_section elf32_rx_relax_section_wrapper
4018 #define elf_backend_special_sections elf32_rx_special_sections
4019 #define elf_backend_check_directives rx_check_directives
4021 #include "elf32-target.h"
4023 /* We define a second big-endian target that doesn't have the custom
4024 section get/set hooks, for times when we want to preserve the
4025 pre-swapped .text sections (like objcopy). */
4027 #undef TARGET_BIG_SYM
4028 #define TARGET_BIG_SYM rx_elf32_be_ns_vec
4029 #undef TARGET_BIG_NAME
4030 #define TARGET_BIG_NAME "elf32-rx-be-ns"
4031 #undef TARGET_LITTLE_SYM
4033 #undef bfd_elf32_get_section_contents
4034 #undef bfd_elf32_set_section_contents
4037 #define elf32_bed elf32_rx_be_ns_bed
4039 #include "elf32-target.h"