1 /* Renesas RX specific support for 32-bit ELF.
2 Copyright (C) 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "libiberty.h"
28 #define RX_OPCODE_BIG_ENDIAN 0
31 char * rx_get_reloc (long);
32 void dump_symtab (bfd
*, void *, void *);
35 #define RXREL(n,sz,bit,shift,complain,pcrel) \
36 HOWTO (R_RX_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
37 bfd_elf_generic_reloc, "R_RX_" #n, FALSE, 0, ~0, FALSE)
39 /* Note that the relocations around 0x7f are internal to this file;
40 feel free to move them as needed to avoid conflicts with published
41 relocation numbers. */
43 static reloc_howto_type rx_elf_howto_table
[] =
45 RXREL (NONE
, 0, 0, 0, dont
, FALSE
),
46 RXREL (DIR32
, 2, 32, 0, signed, FALSE
),
47 RXREL (DIR24S
, 2, 24, 0, signed, FALSE
),
48 RXREL (DIR16
, 1, 16, 0, dont
, FALSE
),
49 RXREL (DIR16U
, 1, 16, 0, unsigned, FALSE
),
50 RXREL (DIR16S
, 1, 16, 0, signed, FALSE
),
51 RXREL (DIR8
, 0, 8, 0, dont
, FALSE
),
52 RXREL (DIR8U
, 0, 8, 0, unsigned, FALSE
),
53 RXREL (DIR8S
, 0, 8, 0, signed, FALSE
),
54 RXREL (DIR24S_PCREL
, 2, 24, 0, signed, TRUE
),
55 RXREL (DIR16S_PCREL
, 1, 16, 0, signed, TRUE
),
56 RXREL (DIR8S_PCREL
, 0, 8, 0, signed, TRUE
),
57 RXREL (DIR16UL
, 1, 16, 2, unsigned, FALSE
),
58 RXREL (DIR16UW
, 1, 16, 1, unsigned, FALSE
),
59 RXREL (DIR8UL
, 0, 8, 2, unsigned, FALSE
),
60 RXREL (DIR8UW
, 0, 8, 1, unsigned, FALSE
),
61 RXREL (DIR32_REV
, 1, 16, 0, dont
, FALSE
),
62 RXREL (DIR16_REV
, 1, 16, 0, dont
, FALSE
),
63 RXREL (DIR3U_PCREL
, 0, 3, 0, dont
, TRUE
),
79 RXREL (RH_3_PCREL
, 0, 3, 0, signed, TRUE
),
80 RXREL (RH_16_OP
, 1, 16, 0, signed, FALSE
),
81 RXREL (RH_24_OP
, 2, 24, 0, signed, FALSE
),
82 RXREL (RH_32_OP
, 2, 32, 0, signed, FALSE
),
83 RXREL (RH_24_UNS
, 2, 24, 0, unsigned, FALSE
),
84 RXREL (RH_8_NEG
, 0, 8, 0, signed, FALSE
),
85 RXREL (RH_16_NEG
, 1, 16, 0, signed, FALSE
),
86 RXREL (RH_24_NEG
, 2, 24, 0, signed, FALSE
),
87 RXREL (RH_32_NEG
, 2, 32, 0, signed, FALSE
),
88 RXREL (RH_DIFF
, 2, 32, 0, signed, FALSE
),
89 RXREL (RH_GPRELB
, 1, 16, 0, unsigned, FALSE
),
90 RXREL (RH_GPRELW
, 1, 16, 0, unsigned, FALSE
),
91 RXREL (RH_GPRELL
, 1, 16, 0, unsigned, FALSE
),
92 RXREL (RH_RELAX
, 0, 0, 0, dont
, FALSE
),
114 RXREL (ABS32
, 2, 32, 0, dont
, FALSE
),
115 RXREL (ABS24S
, 2, 24, 0, signed, FALSE
),
116 RXREL (ABS16
, 1, 16, 0, dont
, FALSE
),
117 RXREL (ABS16U
, 1, 16, 0, unsigned, FALSE
),
118 RXREL (ABS16S
, 1, 16, 0, signed, FALSE
),
119 RXREL (ABS8
, 0, 8, 0, dont
, FALSE
),
120 RXREL (ABS8U
, 0, 8, 0, unsigned, FALSE
),
121 RXREL (ABS8S
, 0, 8, 0, signed, FALSE
),
122 RXREL (ABS24S_PCREL
, 2, 24, 0, signed, TRUE
),
123 RXREL (ABS16S_PCREL
, 1, 16, 0, signed, TRUE
),
124 RXREL (ABS8S_PCREL
, 0, 8, 0, signed, TRUE
),
125 RXREL (ABS16UL
, 1, 16, 0, unsigned, FALSE
),
126 RXREL (ABS16UW
, 1, 16, 0, unsigned, FALSE
),
127 RXREL (ABS8UL
, 0, 8, 0, unsigned, FALSE
),
128 RXREL (ABS8UW
, 0, 8, 0, unsigned, FALSE
),
129 RXREL (ABS32_REV
, 2, 32, 0, dont
, FALSE
),
130 RXREL (ABS16_REV
, 1, 16, 0, dont
, FALSE
),
132 #define STACK_REL_P(x) ((x) <= R_RX_ABS16_REV && (x) >= R_RX_ABS32)
173 /* These are internal. */
174 /* A 5-bit unsigned displacement to a B/W/L address, at bit position 8/12. */
175 /* ---- ---- 4--- 3210. */
176 #define R_RX_RH_ABS5p8B 0x78
177 RXREL (RH_ABS5p8B
, 0, 0, 0, dont
, FALSE
),
178 #define R_RX_RH_ABS5p8W 0x79
179 RXREL (RH_ABS5p8W
, 0, 0, 0, dont
, FALSE
),
180 #define R_RX_RH_ABS5p8L 0x7a
181 RXREL (RH_ABS5p8L
, 0, 0, 0, dont
, FALSE
),
182 /* A 5-bit unsigned displacement to a B/W/L address, at bit position 5/12. */
183 /* ---- -432 1--- 0---. */
184 #define R_RX_RH_ABS5p5B 0x7b
185 RXREL (RH_ABS5p5B
, 0, 0, 0, dont
, FALSE
),
186 #define R_RX_RH_ABS5p5W 0x7c
187 RXREL (RH_ABS5p5W
, 0, 0, 0, dont
, FALSE
),
188 #define R_RX_RH_ABS5p5L 0x7d
189 RXREL (RH_ABS5p5L
, 0, 0, 0, dont
, FALSE
),
190 /* A 4-bit unsigned immediate at bit position 8. */
191 #define R_RX_RH_UIMM4p8 0x7e
192 RXREL (RH_UIMM4p8
, 0, 0, 0, dont
, FALSE
),
193 /* A 4-bit negative unsigned immediate at bit position 8. */
194 #define R_RX_RH_UNEG4p8 0x7f
195 RXREL (RH_UNEG4p8
, 0, 0, 0, dont
, FALSE
),
196 /* End of internal relocs. */
198 RXREL (SYM
, 2, 32, 0, dont
, FALSE
),
199 RXREL (OPneg
, 2, 32, 0, dont
, FALSE
),
200 RXREL (OPadd
, 2, 32, 0, dont
, FALSE
),
201 RXREL (OPsub
, 2, 32, 0, dont
, FALSE
),
202 RXREL (OPmul
, 2, 32, 0, dont
, FALSE
),
203 RXREL (OPdiv
, 2, 32, 0, dont
, FALSE
),
204 RXREL (OPshla
, 2, 32, 0, dont
, FALSE
),
205 RXREL (OPshra
, 2, 32, 0, dont
, FALSE
),
206 RXREL (OPsctsize
, 2, 32, 0, dont
, FALSE
),
207 RXREL (OPscttop
, 2, 32, 0, dont
, FALSE
),
208 RXREL (OPand
, 2, 32, 0, dont
, FALSE
),
209 RXREL (OPor
, 2, 32, 0, dont
, FALSE
),
210 RXREL (OPxor
, 2, 32, 0, dont
, FALSE
),
211 RXREL (OPnot
, 2, 32, 0, dont
, FALSE
),
212 RXREL (OPmod
, 2, 32, 0, dont
, FALSE
),
213 RXREL (OPromtop
, 2, 32, 0, dont
, FALSE
),
214 RXREL (OPramtop
, 2, 32, 0, dont
, FALSE
)
217 /* Map BFD reloc types to RX ELF reloc types. */
221 bfd_reloc_code_real_type bfd_reloc_val
;
222 unsigned int rx_reloc_val
;
225 static const struct rx_reloc_map rx_reloc_map
[] =
227 { BFD_RELOC_NONE
, R_RX_NONE
},
228 { BFD_RELOC_8
, R_RX_DIR8S
},
229 { BFD_RELOC_16
, R_RX_DIR16S
},
230 { BFD_RELOC_24
, R_RX_DIR24S
},
231 { BFD_RELOC_32
, R_RX_DIR32
},
232 { BFD_RELOC_RX_16_OP
, R_RX_DIR16
},
233 { BFD_RELOC_RX_DIR3U_PCREL
, R_RX_DIR3U_PCREL
},
234 { BFD_RELOC_8_PCREL
, R_RX_DIR8S_PCREL
},
235 { BFD_RELOC_16_PCREL
, R_RX_DIR16S_PCREL
},
236 { BFD_RELOC_24_PCREL
, R_RX_DIR24S_PCREL
},
237 { BFD_RELOC_RX_8U
, R_RX_DIR8U
},
238 { BFD_RELOC_RX_16U
, R_RX_DIR16U
},
239 { BFD_RELOC_RX_24U
, R_RX_RH_24_UNS
},
240 { BFD_RELOC_RX_NEG8
, R_RX_RH_8_NEG
},
241 { BFD_RELOC_RX_NEG16
, R_RX_RH_16_NEG
},
242 { BFD_RELOC_RX_NEG24
, R_RX_RH_24_NEG
},
243 { BFD_RELOC_RX_NEG32
, R_RX_RH_32_NEG
},
244 { BFD_RELOC_RX_DIFF
, R_RX_RH_DIFF
},
245 { BFD_RELOC_RX_GPRELB
, R_RX_RH_GPRELB
},
246 { BFD_RELOC_RX_GPRELW
, R_RX_RH_GPRELW
},
247 { BFD_RELOC_RX_GPRELL
, R_RX_RH_GPRELL
},
248 { BFD_RELOC_RX_RELAX
, R_RX_RH_RELAX
},
249 { BFD_RELOC_RX_SYM
, R_RX_SYM
},
250 { BFD_RELOC_RX_OP_SUBTRACT
, R_RX_OPsub
},
251 { BFD_RELOC_RX_ABS8
, R_RX_ABS8
},
252 { BFD_RELOC_RX_ABS16
, R_RX_ABS16
},
253 { BFD_RELOC_RX_ABS32
, R_RX_ABS32
},
254 { BFD_RELOC_RX_ABS16UL
, R_RX_ABS16UL
},
255 { BFD_RELOC_RX_ABS16UW
, R_RX_ABS16UW
},
256 { BFD_RELOC_RX_ABS16U
, R_RX_ABS16U
}
259 #define BIGE(abfd) ((abfd)->xvec->byteorder == BFD_ENDIAN_BIG)
261 static reloc_howto_type
*
262 rx_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
263 bfd_reloc_code_real_type code
)
267 if (code
== BFD_RELOC_RX_32_OP
)
268 return rx_elf_howto_table
+ R_RX_DIR32
;
270 for (i
= ARRAY_SIZE (rx_reloc_map
); --i
;)
271 if (rx_reloc_map
[i
].bfd_reloc_val
== code
)
272 return rx_elf_howto_table
+ rx_reloc_map
[i
].rx_reloc_val
;
277 static reloc_howto_type
*
278 rx_reloc_name_lookup (bfd
* abfd ATTRIBUTE_UNUSED
, const char * r_name
)
282 for (i
= 0; i
< ARRAY_SIZE (rx_elf_howto_table
); i
++)
283 if (rx_elf_howto_table
[i
].name
!= NULL
284 && strcasecmp (rx_elf_howto_table
[i
].name
, r_name
) == 0)
285 return rx_elf_howto_table
+ i
;
290 /* Set the howto pointer for an RX ELF reloc. */
293 rx_info_to_howto_rela (bfd
* abfd ATTRIBUTE_UNUSED
,
295 Elf_Internal_Rela
* dst
)
299 r_type
= ELF32_R_TYPE (dst
->r_info
);
300 BFD_ASSERT (r_type
< (unsigned int) R_RX_max
);
301 cache_ptr
->howto
= rx_elf_howto_table
+ r_type
;
305 get_symbol_value (const char * name
,
306 bfd_reloc_status_type
* status
,
307 struct bfd_link_info
* info
,
309 asection
* input_section
,
313 struct bfd_link_hash_entry
* h
;
315 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
318 || (h
->type
!= bfd_link_hash_defined
319 && h
->type
!= bfd_link_hash_defweak
))
320 * status
= info
->callbacks
->undefined_symbol
321 (info
, name
, input_bfd
, input_section
, offset
, TRUE
);
323 value
= (h
->u
.def
.value
324 + h
->u
.def
.section
->output_section
->vma
325 + h
->u
.def
.section
->output_offset
);
331 get_gp (bfd_reloc_status_type
* status
,
332 struct bfd_link_info
* info
,
337 static bfd_boolean cached
= FALSE
;
338 static bfd_vma cached_value
= 0;
342 cached_value
= get_symbol_value ("__gp", status
, info
, abfd
, sec
, offset
);
349 get_romstart (bfd_reloc_status_type
* status
,
350 struct bfd_link_info
* info
,
355 static bfd_boolean cached
= FALSE
;
356 static bfd_vma cached_value
= 0;
360 cached_value
= get_symbol_value ("_start", status
, info
, abfd
, sec
, offset
);
367 get_ramstart (bfd_reloc_status_type
* status
,
368 struct bfd_link_info
* info
,
373 static bfd_boolean cached
= FALSE
;
374 static bfd_vma cached_value
= 0;
378 cached_value
= get_symbol_value ("__datastart", status
, info
, abfd
, sec
, offset
);
384 #define NUM_STACK_ENTRIES 16
385 static int32_t rx_stack
[ NUM_STACK_ENTRIES
];
386 static unsigned int rx_stack_top
;
388 #define RX_STACK_PUSH(val) \
391 if (rx_stack_top < NUM_STACK_ENTRIES) \
392 rx_stack [rx_stack_top ++] = (val); \
394 r = bfd_reloc_dangerous; \
398 #define RX_STACK_POP(dest) \
401 if (rx_stack_top > 0) \
402 (dest) = rx_stack [-- rx_stack_top]; \
404 (dest) = 0, r = bfd_reloc_dangerous; \
408 /* Relocate an RX ELF section.
409 There is some attempt to make this function usable for many architectures,
410 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
411 if only to serve as a learning tool.
413 The RELOCATE_SECTION function is called by the new ELF backend linker
414 to handle the relocations for a section.
416 The relocs are always passed as Rela structures; if the section
417 actually uses Rel structures, the r_addend field will always be
420 This function is responsible for adjusting the section contents as
421 necessary, and (if using Rela relocs and generating a relocatable
422 output file) adjusting the reloc addend as necessary.
424 This function does not have to worry about setting the reloc
425 address or the reloc symbol index.
427 LOCAL_SYMS is a pointer to the swapped in local symbols.
429 LOCAL_SECTIONS is an array giving the section in the input file
430 corresponding to the st_shndx field of each local symbol.
432 The global hash table entry for the global symbols can be found
433 via elf_sym_hashes (input_bfd).
435 When generating relocatable output, this function must handle
436 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
437 going to be the section symbol corresponding to the output
438 section, which means that the addend must be adjusted
442 rx_elf_relocate_section
444 struct bfd_link_info
* info
,
446 asection
* input_section
,
448 Elf_Internal_Rela
* relocs
,
449 Elf_Internal_Sym
* local_syms
,
450 asection
** local_sections
)
452 Elf_Internal_Shdr
* symtab_hdr
;
453 struct elf_link_hash_entry
** sym_hashes
;
454 Elf_Internal_Rela
* rel
;
455 Elf_Internal_Rela
* relend
;
459 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
460 sym_hashes
= elf_sym_hashes (input_bfd
);
461 relend
= relocs
+ input_section
->reloc_count
;
463 dynobj
= elf_hash_table (info
)->dynobj
;
466 splt
= bfd_get_section_by_name (dynobj
, ".plt");
468 for (rel
= relocs
; rel
< relend
; rel
++)
470 reloc_howto_type
* howto
;
471 unsigned long r_symndx
;
472 Elf_Internal_Sym
* sym
;
474 struct elf_link_hash_entry
* h
;
476 bfd_reloc_status_type r
;
477 const char * name
= NULL
;
478 bfd_boolean unresolved_reloc
= TRUE
;
481 r_type
= ELF32_R_TYPE (rel
->r_info
);
482 r_symndx
= ELF32_R_SYM (rel
->r_info
);
484 howto
= rx_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
490 if (r_symndx
< symtab_hdr
->sh_info
)
492 sym
= local_syms
+ r_symndx
;
493 sec
= local_sections
[r_symndx
];
494 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, & sec
, rel
);
496 name
= bfd_elf_string_from_elf_section
497 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
498 name
= (sym
->st_name
== 0) ? bfd_section_name (input_bfd
, sec
) : name
;
504 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
505 r_symndx
, symtab_hdr
, sym_hashes
, h
,
506 sec
, relocation
, unresolved_reloc
,
509 name
= h
->root
.root
.string
;
512 if (sec
!= NULL
&& elf_discarded_section (sec
))
514 /* For relocs against symbols from removed linkonce sections,
515 or sections discarded by a linker script, we just want the
516 section contents zeroed. Avoid any special processing. */
517 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
523 if (info
->relocatable
)
525 /* This is a relocatable link. We don't have to change
526 anything, unless the reloc is against a section symbol,
527 in which case we have to adjust according to where the
528 section symbol winds up in the output section. */
529 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
530 rel
->r_addend
+= sec
->output_offset
;
534 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
535 /* If the symbol is undefined and weak
536 then the relocation resolves to zero. */
540 if (howto
->pc_relative
)
542 relocation
-= (input_section
->output_section
->vma
543 + input_section
->output_offset
545 if (r_type
!= R_RX_RH_3_PCREL
546 && r_type
!= R_RX_DIR3U_PCREL
)
550 relocation
+= rel
->r_addend
;
555 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
556 #define ALIGN(m) if (relocation & m) r = bfd_reloc_other;
557 #define OP(i) (contents[rel->r_offset + (i)])
558 #define WARN_REDHAT(type) \
559 _bfd_error_handler (_("%B:%A: Warning: deprecated Red Hat reloc " type " detected against: %s."), \
560 input_bfd, input_section, name)
562 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
571 case R_RX_RH_3_PCREL
:
572 WARN_REDHAT ("RX_RH_3_PCREL");
575 OP (0) |= relocation
& 0x07;
579 WARN_REDHAT ("RX_RH_8_NEG");
580 relocation
= - relocation
;
581 case R_RX_DIR8S_PCREL
:
597 WARN_REDHAT ("RX_RH_16_NEG");
598 relocation
= - relocation
;
599 case R_RX_DIR16S_PCREL
:
600 RANGE (-32768, 32767);
601 #if RX_OPCODE_BIG_ENDIAN
604 OP (1) = relocation
>> 8;
609 WARN_REDHAT ("RX_RH_16_OP");
610 RANGE (-32768, 32767);
611 #if RX_OPCODE_BIG_ENDIAN
613 OP (0) = relocation
>> 8;
616 OP (1) = relocation
>> 8;
621 RANGE (-32768, 65535);
622 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
625 OP (0) = relocation
>> 8;
630 OP (1) = relocation
>> 8;
636 #if RX_OPCODE_BIG_ENDIAN
638 OP (0) = relocation
>> 8;
641 OP (1) = relocation
>> 8;
646 RANGE (-32768, 65536);
647 #if RX_OPCODE_BIG_ENDIAN
649 OP (0) = relocation
>> 8;
652 OP (1) = relocation
>> 8;
657 RANGE (-32768, 65536);
658 #if RX_OPCODE_BIG_ENDIAN
660 OP (1) = relocation
>> 8;
663 OP (0) = relocation
>> 8;
667 case R_RX_DIR3U_PCREL
:
670 OP (0) |= relocation
& 0x07;
674 WARN_REDHAT ("RX_RH_24_NEG");
675 relocation
= - relocation
;
676 case R_RX_DIR24S_PCREL
:
677 RANGE (-0x800000, 0x7fffff);
678 #if RX_OPCODE_BIG_ENDIAN
680 OP (1) = relocation
>> 8;
681 OP (0) = relocation
>> 16;
684 OP (1) = relocation
>> 8;
685 OP (2) = relocation
>> 16;
690 WARN_REDHAT ("RX_RH_24_OP");
691 RANGE (-0x800000, 0x7fffff);
692 #if RX_OPCODE_BIG_ENDIAN
694 OP (1) = relocation
>> 8;
695 OP (0) = relocation
>> 16;
698 OP (1) = relocation
>> 8;
699 OP (2) = relocation
>> 16;
704 RANGE (-0x800000, 0x7fffff);
705 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
708 OP (1) = relocation
>> 8;
709 OP (0) = relocation
>> 16;
714 OP (1) = relocation
>> 8;
715 OP (2) = relocation
>> 16;
720 WARN_REDHAT ("RX_RH_24_UNS");
722 #if RX_OPCODE_BIG_ENDIAN
724 OP (1) = relocation
>> 8;
725 OP (0) = relocation
>> 16;
728 OP (1) = relocation
>> 8;
729 OP (2) = relocation
>> 16;
734 WARN_REDHAT ("RX_RH_32_NEG");
735 relocation
= - relocation
;
736 #if RX_OPCODE_BIG_ENDIAN
738 OP (2) = relocation
>> 8;
739 OP (1) = relocation
>> 16;
740 OP (0) = relocation
>> 24;
743 OP (1) = relocation
>> 8;
744 OP (2) = relocation
>> 16;
745 OP (3) = relocation
>> 24;
750 WARN_REDHAT ("RX_RH_32_OP");
751 #if RX_OPCODE_BIG_ENDIAN
753 OP (2) = relocation
>> 8;
754 OP (1) = relocation
>> 16;
755 OP (0) = relocation
>> 24;
758 OP (1) = relocation
>> 8;
759 OP (2) = relocation
>> 16;
760 OP (3) = relocation
>> 24;
765 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
768 OP (2) = relocation
>> 8;
769 OP (1) = relocation
>> 16;
770 OP (0) = relocation
>> 24;
775 OP (1) = relocation
>> 8;
776 OP (2) = relocation
>> 16;
777 OP (3) = relocation
>> 24;
782 if (BIGE (output_bfd
))
785 OP (1) = relocation
>> 8;
786 OP (2) = relocation
>> 16;
787 OP (3) = relocation
>> 24;
792 OP (2) = relocation
>> 8;
793 OP (1) = relocation
>> 16;
794 OP (0) = relocation
>> 24;
801 WARN_REDHAT ("RX_RH_DIFF");
802 val
= bfd_get_32 (output_bfd
, & OP (0));
804 bfd_put_32 (output_bfd
, val
, & OP (0));
809 WARN_REDHAT ("RX_RH_GPRELB");
810 relocation
-= get_gp (&r
, info
, input_bfd
, input_section
, rel
->r_offset
);
812 #if RX_OPCODE_BIG_ENDIAN
814 OP (0) = relocation
>> 8;
817 OP (1) = relocation
>> 8;
822 WARN_REDHAT ("RX_RH_GPRELW");
823 relocation
-= get_gp (&r
, info
, input_bfd
, input_section
, rel
->r_offset
);
827 #if RX_OPCODE_BIG_ENDIAN
829 OP (0) = relocation
>> 8;
832 OP (1) = relocation
>> 8;
837 WARN_REDHAT ("RX_RH_GPRELL");
838 relocation
-= get_gp (&r
, info
, input_bfd
, input_section
, rel
->r_offset
);
842 #if RX_OPCODE_BIG_ENDIAN
844 OP (0) = relocation
>> 8;
847 OP (1) = relocation
>> 8;
851 /* Internal relocations just for relaxation: */
852 case R_RX_RH_ABS5p5B
:
853 RX_STACK_POP (relocation
);
856 OP (0) |= relocation
>> 2;
858 OP (1) |= (relocation
<< 6) & 0x80;
859 OP (1) |= (relocation
<< 3) & 0x08;
862 case R_RX_RH_ABS5p5W
:
863 RX_STACK_POP (relocation
);
868 OP (0) |= relocation
>> 2;
870 OP (1) |= (relocation
<< 6) & 0x80;
871 OP (1) |= (relocation
<< 3) & 0x08;
874 case R_RX_RH_ABS5p5L
:
875 RX_STACK_POP (relocation
);
880 OP (0) |= relocation
>> 2;
882 OP (1) |= (relocation
<< 6) & 0x80;
883 OP (1) |= (relocation
<< 3) & 0x08;
886 case R_RX_RH_ABS5p8B
:
887 RX_STACK_POP (relocation
);
890 OP (0) |= (relocation
<< 3) & 0x80;
891 OP (0) |= relocation
& 0x0f;
894 case R_RX_RH_ABS5p8W
:
895 RX_STACK_POP (relocation
);
900 OP (0) |= (relocation
<< 3) & 0x80;
901 OP (0) |= relocation
& 0x0f;
904 case R_RX_RH_ABS5p8L
:
905 RX_STACK_POP (relocation
);
910 OP (0) |= (relocation
<< 3) & 0x80;
911 OP (0) |= relocation
& 0x0f;
914 case R_RX_RH_UIMM4p8
:
917 OP (0) |= relocation
<< 4;
920 case R_RX_RH_UNEG4p8
:
923 OP (0) |= (-relocation
) << 4;
926 /* Complex reloc handling: */
929 RX_STACK_POP (relocation
);
930 #if RX_OPCODE_BIG_ENDIAN
932 OP (2) = relocation
>> 8;
933 OP (1) = relocation
>> 16;
934 OP (0) = relocation
>> 24;
937 OP (1) = relocation
>> 8;
938 OP (2) = relocation
>> 16;
939 OP (3) = relocation
>> 24;
944 RX_STACK_POP (relocation
);
945 #if RX_OPCODE_BIG_ENDIAN
947 OP (1) = relocation
>> 8;
948 OP (2) = relocation
>> 16;
949 OP (3) = relocation
>> 24;
952 OP (2) = relocation
>> 8;
953 OP (1) = relocation
>> 16;
954 OP (0) = relocation
>> 24;
958 case R_RX_ABS24S_PCREL
:
960 RX_STACK_POP (relocation
);
961 RANGE (-0x800000, 0x7fffff);
962 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
965 OP (1) = relocation
>> 8;
966 OP (0) = relocation
>> 16;
971 OP (1) = relocation
>> 8;
972 OP (2) = relocation
>> 16;
977 RX_STACK_POP (relocation
);
978 RANGE (-32768, 65535);
979 #if RX_OPCODE_BIG_ENDIAN
981 OP (0) = relocation
>> 8;
984 OP (1) = relocation
>> 8;
989 RX_STACK_POP (relocation
);
990 RANGE (-32768, 65535);
991 #if RX_OPCODE_BIG_ENDIAN
993 OP (1) = relocation
>> 8;
996 OP (0) = relocation
>> 8;
1000 case R_RX_ABS16S_PCREL
:
1002 RX_STACK_POP (relocation
);
1003 RANGE (-32768, 32767);
1004 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
1006 OP (1) = relocation
;
1007 OP (0) = relocation
>> 8;
1011 OP (0) = relocation
;
1012 OP (1) = relocation
>> 8;
1017 RX_STACK_POP (relocation
);
1019 #if RX_OPCODE_BIG_ENDIAN
1020 OP (1) = relocation
;
1021 OP (0) = relocation
>> 8;
1023 OP (0) = relocation
;
1024 OP (1) = relocation
>> 8;
1029 RX_STACK_POP (relocation
);
1032 #if RX_OPCODE_BIG_ENDIAN
1033 OP (1) = relocation
;
1034 OP (0) = relocation
>> 8;
1036 OP (0) = relocation
;
1037 OP (1) = relocation
>> 8;
1042 RX_STACK_POP (relocation
);
1045 #if RX_OPCODE_BIG_ENDIAN
1046 OP (1) = relocation
;
1047 OP (0) = relocation
>> 8;
1049 OP (0) = relocation
;
1050 OP (1) = relocation
>> 8;
1055 RX_STACK_POP (relocation
);
1057 OP (0) = relocation
;
1061 RX_STACK_POP (relocation
);
1063 OP (0) = relocation
;
1067 RX_STACK_POP (relocation
);
1070 OP (0) = relocation
;
1074 RX_STACK_POP (relocation
);
1077 OP (0) = relocation
;
1080 case R_RX_ABS8S_PCREL
:
1082 RX_STACK_POP (relocation
);
1084 OP (0) = relocation
;
1088 if (r_symndx
< symtab_hdr
->sh_info
)
1089 RX_STACK_PUSH (sec
->output_section
->vma
1090 + sec
->output_offset
1095 && (h
->root
.type
== bfd_link_hash_defined
1096 || h
->root
.type
== bfd_link_hash_defweak
))
1097 RX_STACK_PUSH (h
->root
.u
.def
.value
1098 + sec
->output_section
->vma
1099 + sec
->output_offset
);
1101 _bfd_error_handler (_("Warning: RX_SYM reloc with an unknown symbol"));
1111 RX_STACK_PUSH (tmp
);
1119 RX_STACK_POP (tmp1
);
1120 RX_STACK_POP (tmp2
);
1122 RX_STACK_PUSH (tmp1
);
1130 RX_STACK_POP (tmp1
);
1131 RX_STACK_POP (tmp2
);
1133 RX_STACK_PUSH (tmp2
);
1141 RX_STACK_POP (tmp1
);
1142 RX_STACK_POP (tmp2
);
1144 RX_STACK_PUSH (tmp1
);
1152 RX_STACK_POP (tmp1
);
1153 RX_STACK_POP (tmp2
);
1155 RX_STACK_PUSH (tmp1
);
1163 RX_STACK_POP (tmp1
);
1164 RX_STACK_POP (tmp2
);
1166 RX_STACK_PUSH (tmp1
);
1174 RX_STACK_POP (tmp1
);
1175 RX_STACK_POP (tmp2
);
1177 RX_STACK_PUSH (tmp1
);
1181 case R_RX_OPsctsize
:
1182 RX_STACK_PUSH (input_section
->size
);
1186 RX_STACK_PUSH (input_section
->output_section
->vma
);
1193 RX_STACK_POP (tmp1
);
1194 RX_STACK_POP (tmp2
);
1196 RX_STACK_PUSH (tmp1
);
1204 RX_STACK_POP (tmp1
);
1205 RX_STACK_POP (tmp2
);
1207 RX_STACK_PUSH (tmp1
);
1215 RX_STACK_POP (tmp1
);
1216 RX_STACK_POP (tmp2
);
1218 RX_STACK_PUSH (tmp1
);
1228 RX_STACK_PUSH (tmp
);
1236 RX_STACK_POP (tmp1
);
1237 RX_STACK_POP (tmp2
);
1239 RX_STACK_PUSH (tmp1
);
1244 RX_STACK_PUSH (get_romstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1248 RX_STACK_PUSH (get_ramstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1252 r
= bfd_reloc_notsupported
;
1256 if (r
!= bfd_reloc_ok
)
1258 const char * msg
= NULL
;
1262 case bfd_reloc_overflow
:
1263 /* Catch the case of a missing function declaration
1264 and emit a more helpful error message. */
1265 if (r_type
== R_RX_DIR24S_PCREL
)
1266 msg
= _("%B(%A): error: call to undefined function '%s'");
1268 r
= info
->callbacks
->reloc_overflow
1269 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
1270 input_bfd
, input_section
, rel
->r_offset
);
1273 case bfd_reloc_undefined
:
1274 r
= info
->callbacks
->undefined_symbol
1275 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
1279 case bfd_reloc_other
:
1280 msg
= _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
1283 case bfd_reloc_outofrange
:
1284 msg
= _("%B(%A): internal error: out of range error");
1287 case bfd_reloc_notsupported
:
1288 msg
= _("%B(%A): internal error: unsupported relocation error");
1291 case bfd_reloc_dangerous
:
1292 msg
= _("%B(%A): internal error: dangerous relocation");
1296 msg
= _("%B(%A): internal error: unknown error");
1301 _bfd_error_handler (msg
, input_bfd
, input_section
, name
);
1311 /* Relaxation Support. */
1313 /* Progression of relocations from largest operand size to smallest
1317 next_smaller_reloc (int r
)
1321 case R_RX_DIR32
: return R_RX_DIR24S
;
1322 case R_RX_DIR24S
: return R_RX_DIR16S
;
1323 case R_RX_DIR16S
: return R_RX_DIR8S
;
1324 case R_RX_DIR8S
: return R_RX_NONE
;
1326 case R_RX_DIR16
: return R_RX_DIR8
;
1327 case R_RX_DIR8
: return R_RX_NONE
;
1329 case R_RX_DIR16U
: return R_RX_DIR8U
;
1330 case R_RX_DIR8U
: return R_RX_NONE
;
1332 case R_RX_DIR24S_PCREL
: return R_RX_DIR16S_PCREL
;
1333 case R_RX_DIR16S_PCREL
: return R_RX_DIR8S_PCREL
;
1334 case R_RX_DIR8S_PCREL
: return R_RX_DIR3U_PCREL
;
1336 case R_RX_DIR16UL
: return R_RX_DIR8UL
;
1337 case R_RX_DIR8UL
: return R_RX_NONE
;
1338 case R_RX_DIR16UW
: return R_RX_DIR8UW
;
1339 case R_RX_DIR8UW
: return R_RX_NONE
;
1341 case R_RX_RH_32_OP
: return R_RX_RH_24_OP
;
1342 case R_RX_RH_24_OP
: return R_RX_RH_16_OP
;
1343 case R_RX_RH_16_OP
: return R_RX_DIR8
;
1345 case R_RX_ABS32
: return R_RX_ABS24S
;
1346 case R_RX_ABS24S
: return R_RX_ABS16S
;
1347 case R_RX_ABS16
: return R_RX_ABS8
;
1348 case R_RX_ABS16U
: return R_RX_ABS8U
;
1349 case R_RX_ABS16S
: return R_RX_ABS8S
;
1350 case R_RX_ABS8
: return R_RX_NONE
;
1351 case R_RX_ABS8U
: return R_RX_NONE
;
1352 case R_RX_ABS8S
: return R_RX_NONE
;
1353 case R_RX_ABS24S_PCREL
: return R_RX_ABS16S_PCREL
;
1354 case R_RX_ABS16S_PCREL
: return R_RX_ABS8S_PCREL
;
1355 case R_RX_ABS8S_PCREL
: return R_RX_NONE
;
1356 case R_RX_ABS16UL
: return R_RX_ABS8UL
;
1357 case R_RX_ABS16UW
: return R_RX_ABS8UW
;
1358 case R_RX_ABS8UL
: return R_RX_NONE
;
1359 case R_RX_ABS8UW
: return R_RX_NONE
;
1364 /* Delete some bytes from a section while relaxing. */
1367 elf32_rx_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, int count
,
1368 Elf_Internal_Rela
*alignment_rel
, int force_snip
)
1370 Elf_Internal_Shdr
* symtab_hdr
;
1371 unsigned int sec_shndx
;
1372 bfd_byte
* contents
;
1373 Elf_Internal_Rela
* irel
;
1374 Elf_Internal_Rela
* irelend
;
1375 Elf_Internal_Rela
* irelalign
;
1376 Elf_Internal_Sym
* isym
;
1377 Elf_Internal_Sym
* isymend
;
1379 unsigned int symcount
;
1380 struct elf_link_hash_entry
** sym_hashes
;
1381 struct elf_link_hash_entry
** end_hashes
;
1386 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1388 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1390 /* The deletion must stop at the next alignment boundary, if
1391 ALIGNMENT_REL is non-NULL. */
1395 toaddr
= alignment_rel
->r_offset
;
1397 irel
= elf_section_data (sec
)->relocs
;
1398 irelend
= irel
+ sec
->reloc_count
;
1400 /* Actually delete the bytes. */
1401 memmove (contents
+ addr
, contents
+ addr
+ count
,
1402 (size_t) (toaddr
- addr
- count
));
1404 /* If we don't have an alignment marker to worry about, we can just
1405 shrink the section. Otherwise, we have to fill in the newly
1406 created gap with NOP insns (0x03). */
1410 memset (contents
+ toaddr
- count
, 0x03, count
);
1412 /* Adjust all the relocs. */
1413 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1415 /* Get the new reloc address. */
1416 if (irel
->r_offset
> addr
1417 && (irel
->r_offset
< toaddr
1418 || (force_snip
&& irel
->r_offset
== toaddr
)))
1419 irel
->r_offset
-= count
;
1421 /* If we see an ALIGN marker at the end of the gap, we move it
1422 to the beginning of the gap, since marking these gaps is what
1424 if (irel
->r_offset
== toaddr
1425 && ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
1426 && irel
->r_addend
& RX_RELAXA_ALIGN
)
1427 irel
->r_offset
-= count
;
1430 /* Adjust the local symbols defined in this section. */
1431 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1432 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1433 isymend
= isym
+ symtab_hdr
->sh_info
;
1435 for (; isym
< isymend
; isym
++)
1437 /* If the symbol is in the range of memory we just moved, we
1438 have to adjust its value. */
1439 if (isym
->st_shndx
== sec_shndx
1440 && isym
->st_value
> addr
1441 && isym
->st_value
< toaddr
)
1442 isym
->st_value
-= count
;
1444 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1445 *end* is in the moved bytes but it's *start* isn't), then we
1446 must adjust its size. */
1447 if (isym
->st_shndx
== sec_shndx
1448 && isym
->st_value
< addr
1449 && isym
->st_value
+ isym
->st_size
> addr
1450 && isym
->st_value
+ isym
->st_size
< toaddr
)
1451 isym
->st_size
-= count
;
1454 /* Now adjust the global symbols defined in this section. */
1455 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1456 - symtab_hdr
->sh_info
);
1457 sym_hashes
= elf_sym_hashes (abfd
);
1458 end_hashes
= sym_hashes
+ symcount
;
1460 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1462 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1464 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1465 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1466 && sym_hash
->root
.u
.def
.section
== sec
)
1468 /* As above, adjust the value if needed. */
1469 if (sym_hash
->root
.u
.def
.value
> addr
1470 && sym_hash
->root
.u
.def
.value
< toaddr
)
1471 sym_hash
->root
.u
.def
.value
-= count
;
1473 /* As above, adjust the size if needed. */
1474 if (sym_hash
->root
.u
.def
.value
< addr
1475 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
1476 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
1477 sym_hash
->size
-= count
;
1484 /* Used to sort relocs by address. If relocs have the same address,
1485 we maintain their relative order, except that R_RX_RH_RELAX
1486 alignment relocs must be the first reloc for any given address. */
1489 reloc_bubblesort (Elf_Internal_Rela
* r
, int count
)
1493 bfd_boolean swappit
;
1495 /* This is almost a classic bubblesort. It's the slowest sort, but
1496 we're taking advantage of the fact that the relocations are
1497 mostly in order already (the assembler emits them that way) and
1498 we need relocs with the same address to remain in the same
1504 for (i
= 0; i
< count
- 1; i
++)
1506 if (r
[i
].r_offset
> r
[i
+ 1].r_offset
)
1508 else if (r
[i
].r_offset
< r
[i
+ 1].r_offset
)
1510 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1511 && (r
[i
+ 1].r_addend
& RX_RELAXA_ALIGN
))
1513 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1514 && (r
[i
+ 1].r_addend
& RX_RELAXA_ELIGN
)
1515 && !(ELF32_R_TYPE (r
[i
].r_info
) == R_RX_RH_RELAX
1516 && (r
[i
].r_addend
& RX_RELAXA_ALIGN
)))
1523 Elf_Internal_Rela tmp
;
1528 /* If we do move a reloc back, re-scan to see if it
1529 needs to be moved even further back. This avoids
1530 most of the O(n^2) behavior for our cases. */
1540 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1541 rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1542 lrel, abfd, sec, link_info, scale)
1545 rx_offset_for_reloc (bfd
* abfd
,
1546 Elf_Internal_Rela
* rel
,
1547 Elf_Internal_Shdr
* symtab_hdr
,
1548 Elf_External_Sym_Shndx
* shndx_buf
,
1549 Elf_Internal_Sym
* intsyms
,
1550 Elf_Internal_Rela
** lrel
,
1552 asection
* input_section
,
1553 struct bfd_link_info
* info
,
1557 bfd_reloc_status_type r
;
1561 /* REL is the first of 1..N relocations. We compute the symbol
1562 value for each relocation, then combine them if needed. LREL
1563 gets a pointer to the last relocation used. */
1568 /* Get the value of the symbol referred to by the reloc. */
1569 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1571 /* A local symbol. */
1572 Elf_Internal_Sym
*isym
;
1573 Elf_External_Sym_Shndx
*shndx
;
1576 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1578 if (isym
->st_shndx
== SHN_UNDEF
)
1579 ssec
= bfd_und_section_ptr
;
1580 else if (isym
->st_shndx
== SHN_ABS
)
1581 ssec
= bfd_abs_section_ptr
;
1582 else if (isym
->st_shndx
== SHN_COMMON
)
1583 ssec
= bfd_com_section_ptr
;
1585 ssec
= bfd_section_from_elf_index (abfd
,
1588 shndx
= shndx_buf
+ (shndx_buf
? ELF32_R_SYM (rel
->r_info
) : 0);
1590 /* Initial symbol value. */
1591 symval
= isym
->st_value
;
1593 /* GAS may have made this symbol relative to a section, in
1594 which case, we have to add the addend to find the
1596 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
1597 symval
+= rel
->r_addend
;
1601 if ((ssec
->flags
& SEC_MERGE
)
1602 && ssec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
1603 symval
= _bfd_merged_section_offset (abfd
, & ssec
,
1604 elf_section_data (ssec
)->sec_info
,
1608 /* Now make the offset relative to where the linker is putting it. */
1611 ssec
->output_section
->vma
+ ssec
->output_offset
;
1613 symval
+= rel
->r_addend
;
1618 struct elf_link_hash_entry
* h
;
1620 /* An external symbol. */
1621 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1622 h
= elf_sym_hashes (abfd
)[indx
];
1623 BFD_ASSERT (h
!= NULL
);
1625 if (h
->root
.type
!= bfd_link_hash_defined
1626 && h
->root
.type
!= bfd_link_hash_defweak
)
1628 /* This appears to be a reference to an undefined
1629 symbol. Just ignore it--it will be caught by the
1630 regular reloc processing. */
1636 symval
= (h
->root
.u
.def
.value
1637 + h
->root
.u
.def
.section
->output_section
->vma
1638 + h
->root
.u
.def
.section
->output_offset
);
1640 symval
+= rel
->r_addend
;
1643 switch (ELF32_R_TYPE (rel
->r_info
))
1646 RX_STACK_PUSH (symval
);
1650 RX_STACK_POP (tmp1
);
1652 RX_STACK_PUSH (tmp1
);
1656 RX_STACK_POP (tmp1
);
1657 RX_STACK_POP (tmp2
);
1659 RX_STACK_PUSH (tmp1
);
1663 RX_STACK_POP (tmp1
);
1664 RX_STACK_POP (tmp2
);
1666 RX_STACK_PUSH (tmp2
);
1670 RX_STACK_POP (tmp1
);
1671 RX_STACK_POP (tmp2
);
1673 RX_STACK_PUSH (tmp1
);
1677 RX_STACK_POP (tmp1
);
1678 RX_STACK_POP (tmp2
);
1680 RX_STACK_PUSH (tmp1
);
1684 RX_STACK_POP (tmp1
);
1685 RX_STACK_POP (tmp2
);
1687 RX_STACK_PUSH (tmp1
);
1691 RX_STACK_POP (tmp1
);
1692 RX_STACK_POP (tmp2
);
1694 RX_STACK_PUSH (tmp1
);
1697 case R_RX_OPsctsize
:
1698 RX_STACK_PUSH (input_section
->size
);
1702 RX_STACK_PUSH (input_section
->output_section
->vma
);
1706 RX_STACK_POP (tmp1
);
1707 RX_STACK_POP (tmp2
);
1709 RX_STACK_PUSH (tmp1
);
1713 RX_STACK_POP (tmp1
);
1714 RX_STACK_POP (tmp2
);
1716 RX_STACK_PUSH (tmp1
);
1720 RX_STACK_POP (tmp1
);
1721 RX_STACK_POP (tmp2
);
1723 RX_STACK_PUSH (tmp1
);
1727 RX_STACK_POP (tmp1
);
1729 RX_STACK_PUSH (tmp1
);
1733 RX_STACK_POP (tmp1
);
1734 RX_STACK_POP (tmp2
);
1736 RX_STACK_PUSH (tmp1
);
1740 RX_STACK_PUSH (get_romstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1744 RX_STACK_PUSH (get_ramstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1752 RX_STACK_POP (symval
);
1763 RX_STACK_POP (symval
);
1771 RX_STACK_POP (symval
);
1782 move_reloc (Elf_Internal_Rela
* irel
, Elf_Internal_Rela
* srel
, int delta
)
1784 bfd_vma old_offset
= srel
->r_offset
;
1787 while (irel
<= srel
)
1789 if (irel
->r_offset
== old_offset
)
1790 irel
->r_offset
+= delta
;
1795 /* Relax one section. */
1798 elf32_rx_relax_section (bfd
* abfd
,
1800 struct bfd_link_info
* link_info
,
1801 bfd_boolean
* again
,
1802 bfd_boolean allow_pcrel3
)
1804 Elf_Internal_Shdr
* symtab_hdr
;
1805 Elf_Internal_Shdr
* shndx_hdr
;
1806 Elf_Internal_Rela
* internal_relocs
;
1807 Elf_Internal_Rela
* free_relocs
= NULL
;
1808 Elf_Internal_Rela
* irel
;
1809 Elf_Internal_Rela
* srel
;
1810 Elf_Internal_Rela
* irelend
;
1811 Elf_Internal_Rela
* next_alignment
;
1812 Elf_Internal_Rela
* prev_alignment
;
1813 bfd_byte
* contents
= NULL
;
1814 bfd_byte
* free_contents
= NULL
;
1815 Elf_Internal_Sym
* intsyms
= NULL
;
1816 Elf_Internal_Sym
* free_intsyms
= NULL
;
1817 Elf_External_Sym_Shndx
* shndx_buf
= NULL
;
1824 int section_alignment_glue
;
1825 /* how much to scale the relocation by - 1, 2, or 4. */
1828 /* Assume nothing changes. */
1831 /* We don't have to do anything for a relocatable link, if
1832 this section does not have relocs, or if this is not a
1834 if (link_info
->relocatable
1835 || (sec
->flags
& SEC_RELOC
) == 0
1836 || sec
->reloc_count
== 0
1837 || (sec
->flags
& SEC_CODE
) == 0)
1840 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1841 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1843 sec_start
= sec
->output_section
->vma
+ sec
->output_offset
;
1844 sec_end
= sec
->output_section
->vma
+ sec
->output_offset
+ sec
->size
;
1846 /* Get the section contents. */
1847 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1848 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1849 /* Go get them off disk. */
1852 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1854 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1857 /* Read this BFD's symbols. */
1858 /* Get cached copy if it exists. */
1859 if (symtab_hdr
->contents
!= NULL
)
1860 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1863 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
1864 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
1867 if (shndx_hdr
->sh_size
!= 0)
1871 amt
= symtab_hdr
->sh_info
;
1872 amt
*= sizeof (Elf_External_Sym_Shndx
);
1873 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
1874 if (shndx_buf
== NULL
)
1876 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
1877 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
1879 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
1882 /* Get a copy of the native relocations. */
1883 internal_relocs
= (_bfd_elf_link_read_relocs
1884 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1885 link_info
->keep_memory
));
1886 if (internal_relocs
== NULL
)
1888 if (! link_info
->keep_memory
)
1889 free_relocs
= internal_relocs
;
1891 /* The RL_ relocs must be just before the operand relocs they go
1892 with, so we must sort them to guarantee this. We use bubblesort
1893 instead of qsort so we can guarantee that relocs with the same
1894 address remain in the same relative order. */
1895 reloc_bubblesort (internal_relocs
, sec
->reloc_count
);
1897 /* Walk through them looking for relaxing opportunities. */
1898 irelend
= internal_relocs
+ sec
->reloc_count
;
1900 /* This will either be NULL or a pointer to the next alignment
1902 next_alignment
= internal_relocs
;
1903 /* This will be the previous alignment, although at first it points
1904 to the first real relocation. */
1905 prev_alignment
= internal_relocs
;
1907 /* We calculate worst case shrinkage caused by alignment directives.
1908 No fool-proof, but better than either ignoring the problem or
1909 doing heavy duty analysis of all the alignment markers in all
1911 section_alignment_glue
= 0;
1912 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1913 if (ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
1914 && irel
->r_addend
& RX_RELAXA_ALIGN
)
1916 int this_glue
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
1918 if (section_alignment_glue
< this_glue
)
1919 section_alignment_glue
= this_glue
;
1921 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
1923 section_alignment_glue
*= 2;
1925 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1927 unsigned char *insn
;
1930 /* The insns we care about are all marked with one of these. */
1931 if (ELF32_R_TYPE (irel
->r_info
) != R_RX_RH_RELAX
)
1934 if (irel
->r_addend
& RX_RELAXA_ALIGN
1935 || next_alignment
== internal_relocs
)
1937 /* When we delete bytes, we need to maintain all the alignments
1938 indicated. In addition, we need to be careful about relaxing
1939 jumps across alignment boundaries - these displacements
1940 *grow* when we delete bytes. For now, don't shrink
1941 displacements across an alignment boundary, just in case.
1942 Note that this only affects relocations to the same
1944 prev_alignment
= next_alignment
;
1945 next_alignment
+= 2;
1946 while (next_alignment
< irelend
1947 && (ELF32_R_TYPE (next_alignment
->r_info
) != R_RX_RH_RELAX
1948 || !(next_alignment
->r_addend
& RX_RELAXA_ELIGN
)))
1950 if (next_alignment
>= irelend
|| next_alignment
->r_offset
== 0)
1951 next_alignment
= NULL
;
1954 /* When we hit alignment markers, see if we've shrunk enough
1955 before them to reduce the gap without violating the alignment
1957 if (irel
->r_addend
& RX_RELAXA_ALIGN
)
1959 /* At this point, the next relocation *should* be the ELIGN
1961 Elf_Internal_Rela
*erel
= irel
+ 1;
1962 unsigned int alignment
, nbytes
;
1964 if (ELF32_R_TYPE (erel
->r_info
) != R_RX_RH_RELAX
)
1966 if (!(erel
->r_addend
& RX_RELAXA_ELIGN
))
1969 alignment
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
1971 if (erel
->r_offset
- irel
->r_offset
< alignment
)
1974 nbytes
= erel
->r_offset
- irel
->r_offset
;
1975 nbytes
/= alignment
;
1976 nbytes
*= alignment
;
1978 elf32_rx_relax_delete_bytes (abfd
, sec
, erel
->r_offset
-nbytes
, nbytes
, next_alignment
,
1979 erel
->r_offset
== sec
->size
);
1985 if (irel
->r_addend
& RX_RELAXA_ELIGN
)
1988 insn
= contents
+ irel
->r_offset
;
1990 nrelocs
= irel
->r_addend
& RX_RELAXA_RNUM
;
1992 /* At this point, we have an insn that is a candidate for linker
1993 relaxation. There are NRELOCS relocs following that may be
1994 relaxed, although each reloc may be made of more than one
1995 reloc entry (such as gp-rel symbols). */
1997 /* Get the value of the symbol referred to by the reloc. Just
1998 in case this is the last reloc in the list, use the RL's
1999 addend to choose between this reloc (no addend) or the next
2000 (yes addend, which means at least one following reloc). */
2002 /* srel points to the "current" reloction for this insn -
2003 actually the last reloc for a given operand, which is the one
2004 we need to update. We check the relaxations in the same
2005 order that the relocations happen, so we'll just push it
2009 pc
= sec
->output_section
->vma
+ sec
->output_offset
2013 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2014 pcrel = symval - pc + srel->r_addend; \
2017 #define SNIPNR(offset, nbytes) \
2018 elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0);
2019 #define SNIP(offset, nbytes, newtype) \
2020 SNIPNR (offset, nbytes); \
2021 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2023 /* The order of these bit tests must match the order that the
2024 relocs appear in. Since we sorted those by offset, we can
2027 /* Note that the numbers in, say, DSP6 are the bit offsets of
2028 the code fields that describe the operand. Bits number 0 for
2029 the MSB of insn[0]. */
2036 if (irel
->r_addend
& RX_RELAXA_DSP6
)
2041 if (code
== 2 && symval
/scale
<= 255)
2043 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2046 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2047 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2049 SNIP (3, 1, newrel
);
2054 else if (code
== 1 && symval
== 0)
2057 SNIP (2, 1, R_RX_NONE
);
2061 /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst. */
2062 else if (code
== 1 && symval
/scale
<= 31
2063 /* Decodable bits. */
2064 && (insn
[0] & 0xcc) == 0xcc
2066 && (insn
[0] & 0x30) != 3
2067 /* Register MSBs. */
2068 && (insn
[1] & 0x88) == 0x00)
2072 insn
[0] = 0x88 | (insn
[0] & 0x30);
2073 /* The register fields are in the right place already. */
2075 /* We can't relax this new opcode. */
2078 switch ((insn
[0] & 0x30) >> 4)
2081 newrel
= R_RX_RH_ABS5p5B
;
2084 newrel
= R_RX_RH_ABS5p5W
;
2087 newrel
= R_RX_RH_ABS5p5L
;
2091 move_reloc (irel
, srel
, -2);
2092 SNIP (2, 1, newrel
);
2095 /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst. */
2096 else if (code
== 1 && symval
/scale
<= 31
2097 /* Decodable bits. */
2098 && (insn
[0] & 0xf8) == 0x58
2099 /* Register MSBs. */
2100 && (insn
[1] & 0x88) == 0x00)
2104 insn
[0] = 0xb0 | ((insn
[0] & 0x04) << 1);
2105 /* The register fields are in the right place already. */
2107 /* We can't relax this new opcode. */
2110 switch ((insn
[0] & 0x08) >> 3)
2113 newrel
= R_RX_RH_ABS5p5B
;
2116 newrel
= R_RX_RH_ABS5p5W
;
2120 move_reloc (irel
, srel
, -2);
2121 SNIP (2, 1, newrel
);
2125 /* A DSP4 operand always follows a DSP6 operand, even if there's
2126 no relocation for it. We have to read the code out of the
2127 opcode to calculate the offset of the operand. */
2128 if (irel
->r_addend
& RX_RELAXA_DSP4
)
2130 int code6
, offset
= 0;
2134 code6
= insn
[0] & 0x03;
2137 case 0: offset
= 2; break;
2138 case 1: offset
= 3; break;
2139 case 2: offset
= 4; break;
2140 case 3: offset
= 2; break;
2143 code
= (insn
[0] & 0x0c) >> 2;
2145 if (code
== 2 && symval
/ scale
<= 255)
2147 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2151 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2152 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2154 SNIP (offset
+1, 1, newrel
);
2159 else if (code
== 1 && symval
== 0)
2162 SNIP (offset
, 1, R_RX_NONE
);
2165 /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2166 else if (code
== 1 && symval
/scale
<= 31
2167 /* Decodable bits. */
2168 && (insn
[0] & 0xc3) == 0xc3
2170 && (insn
[0] & 0x30) != 3
2171 /* Register MSBs. */
2172 && (insn
[1] & 0x88) == 0x00)
2176 insn
[0] = 0x80 | (insn
[0] & 0x30);
2177 /* The register fields are in the right place already. */
2179 /* We can't relax this new opcode. */
2182 switch ((insn
[0] & 0x30) >> 4)
2185 newrel
= R_RX_RH_ABS5p5B
;
2188 newrel
= R_RX_RH_ABS5p5W
;
2191 newrel
= R_RX_RH_ABS5p5L
;
2195 move_reloc (irel
, srel
, -2);
2196 SNIP (2, 1, newrel
);
2200 /* These always occur alone, but the offset depends on whether
2201 it's a MEMEX opcode (0x06) or not. */
2202 if (irel
->r_addend
& RX_RELAXA_DSP14
)
2207 if (insn
[0] == 0x06)
2214 if (code
== 2 && symval
/ scale
<= 255)
2216 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2220 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2221 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2223 SNIP (offset
, 1, newrel
);
2227 else if (code
== 1 && symval
== 0)
2230 SNIP (offset
, 1, R_RX_NONE
);
2241 /* These always occur alone. */
2242 if (irel
->r_addend
& RX_RELAXA_IMM6
)
2248 /* These relocations sign-extend, so we must do signed compares. */
2249 ssymval
= (long) symval
;
2251 code
= insn
[0] & 0x03;
2253 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2255 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2259 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2260 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2262 SNIP (2, 1, newrel
);
2267 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2269 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2273 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2274 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2276 SNIP (2, 1, newrel
);
2281 /* Special case UIMM8 format: CMP #uimm8,Rdst. */
2282 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2283 /* Decodable bits. */
2284 && (insn
[0] & 0xfc) == 0x74
2285 /* Decodable bits. */
2286 && ((insn
[1] & 0xf0) == 0x00))
2291 insn
[1] = 0x50 | (insn
[1] & 0x0f);
2293 /* We can't relax this new opcode. */
2296 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2297 newrel
= R_RX_ABS8U
;
2299 newrel
= R_RX_DIR8U
;
2301 SNIP (2, 1, newrel
);
2305 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2307 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2311 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2312 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2314 SNIP (2, 1, newrel
);
2319 /* Special case UIMM4 format: CMP, MUL, AND, OR. */
2320 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2321 /* Decodable bits and immediate type. */
2323 /* Decodable bits. */
2324 && (insn
[1] & 0xc0) == 0x00)
2326 static const int newop
[4] = { 1, 3, 4, 5 };
2328 insn
[0] = 0x60 | newop
[insn
[1] >> 4];
2329 /* The register number doesn't move. */
2331 /* We can't relax this new opcode. */
2334 move_reloc (irel
, srel
, -1);
2336 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2340 /* Special case UIMM4 format: ADD -> ADD/SUB. */
2341 else if (code
== 1 && ssymval
<= 15 && ssymval
>= -15
2342 /* Decodable bits and immediate type. */
2344 /* Same register for source and destination. */
2345 && ((insn
[1] >> 4) == (insn
[1] & 0x0f)))
2349 /* Note that we can't turn "add $0,Rs" into a NOP
2350 because the flags need to be set right. */
2354 insn
[0] = 0x60; /* Subtract. */
2355 newrel
= R_RX_RH_UNEG4p8
;
2359 insn
[0] = 0x62; /* Add. */
2360 newrel
= R_RX_RH_UIMM4p8
;
2363 /* The register number is in the right place. */
2365 /* We can't relax this new opcode. */
2368 move_reloc (irel
, srel
, -1);
2370 SNIP (2, 1, newrel
);
2375 /* These are either matched with a DSP6 (2-byte base) or an id24
2377 if (irel
->r_addend
& RX_RELAXA_IMM12
)
2379 int dspcode
, offset
= 0;
2384 if ((insn
[0] & 0xfc) == 0xfc)
2385 dspcode
= 1; /* Just something with one byte operand. */
2387 dspcode
= insn
[0] & 3;
2390 case 0: offset
= 2; break;
2391 case 1: offset
= 3; break;
2392 case 2: offset
= 4; break;
2393 case 3: offset
= 2; break;
2396 /* These relocations sign-extend, so we must do signed compares. */
2397 ssymval
= (long) symval
;
2399 code
= (insn
[1] >> 2) & 3;
2400 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2402 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2406 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2407 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2409 SNIP (offset
, 1, newrel
);
2414 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2416 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2420 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2421 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2423 SNIP (offset
, 1, newrel
);
2428 /* Special case UIMM8 format: MOV #uimm8,Rdst. */
2429 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2430 /* Decodable bits. */
2432 /* Decodable bits. */
2433 && ((insn
[1] & 0x03) == 0x02))
2438 insn
[1] = 0x40 | (insn
[1] >> 4);
2440 /* We can't relax this new opcode. */
2443 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2444 newrel
= R_RX_ABS8U
;
2446 newrel
= R_RX_DIR8U
;
2448 SNIP (2, 1, newrel
);
2452 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2454 unsigned int newrel
= ELF32_R_TYPE(srel
->r_info
);
2458 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2459 if (newrel
!= ELF32_R_TYPE(srel
->r_info
))
2461 SNIP (offset
, 1, newrel
);
2466 /* Special case UIMM4 format: MOV #uimm4,Rdst. */
2467 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2468 /* Decodable bits. */
2470 /* Decodable bits. */
2471 && ((insn
[1] & 0x03) == 0x02))
2474 insn
[1] = insn
[1] >> 4;
2476 /* We can't relax this new opcode. */
2479 move_reloc (irel
, srel
, -1);
2481 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2486 if (irel
->r_addend
& RX_RELAXA_BRA
)
2488 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2490 int alignment_glue
= 0;
2494 /* Branches over alignment chunks are problematic, as
2495 deleting bytes here makes the branch *further* away. We
2496 can be agressive with branches within this alignment
2497 block, but not branches outside it. */
2498 if ((prev_alignment
== NULL
2499 || symval
< (bfd_vma
)(sec_start
+ prev_alignment
->r_offset
))
2500 && (next_alignment
== NULL
2501 || symval
> (bfd_vma
)(sec_start
+ next_alignment
->r_offset
)))
2502 alignment_glue
= section_alignment_glue
;
2504 if (ELF32_R_TYPE(srel
[1].r_info
) == R_RX_RH_RELAX
2505 && srel
[1].r_addend
& RX_RELAXA_BRA
2506 && srel
[1].r_offset
< irel
->r_offset
+ pcrel
)
2509 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2511 /* The values we compare PCREL with are not what you'd
2512 expect; they're off by a little to compensate for (1)
2513 where the reloc is relative to the insn, and (2) how much
2514 the insn is going to change when we relax it. */
2516 /* These we have to decode. */
2519 case 0x04: /* BRA pcdsp:24 */
2520 if (-32768 + alignment_glue
<= pcrel
2521 && pcrel
<= 32765 - alignment_glue
)
2524 SNIP (3, 1, newrel
);
2529 case 0x38: /* BRA pcdsp:16 */
2530 if (-128 + alignment_glue
<= pcrel
2531 && pcrel
<= 127 - alignment_glue
)
2534 SNIP (2, 1, newrel
);
2539 case 0x2e: /* BRA pcdsp:8 */
2540 /* Note that there's a risk here of shortening things so
2541 much that we no longer fit this reloc; it *should*
2542 only happen when you branch across a branch, and that
2543 branch also devolves into BRA.S. "Real" code should
2545 if (max_pcrel3
+ alignment_glue
<= pcrel
2546 && pcrel
<= 10 - alignment_glue
2550 SNIP (1, 1, newrel
);
2551 move_reloc (irel
, srel
, -1);
2556 case 0x05: /* BSR pcdsp:24 */
2557 if (-32768 + alignment_glue
<= pcrel
2558 && pcrel
<= 32765 - alignment_glue
)
2561 SNIP (1, 1, newrel
);
2566 case 0x3a: /* BEQ.W pcdsp:16 */
2567 case 0x3b: /* BNE.W pcdsp:16 */
2568 if (-128 + alignment_glue
<= pcrel
2569 && pcrel
<= 127 - alignment_glue
)
2571 insn
[0] = 0x20 | (insn
[0] & 1);
2572 SNIP (1, 1, newrel
);
2577 case 0x20: /* BEQ.B pcdsp:8 */
2578 case 0x21: /* BNE.B pcdsp:8 */
2579 if (max_pcrel3
+ alignment_glue
<= pcrel
2580 && pcrel
- alignment_glue
<= 10
2583 insn
[0] = 0x10 | ((insn
[0] & 1) << 3);
2584 SNIP (1, 1, newrel
);
2585 move_reloc (irel
, srel
, -1);
2590 case 0x16: /* synthetic BNE dsp24 */
2591 case 0x1e: /* synthetic BEQ dsp24 */
2592 if (-32767 + alignment_glue
<= pcrel
2593 && pcrel
<= 32766 - alignment_glue
2596 if (insn
[0] == 0x16)
2600 /* We snip out the bytes at the end else the reloc
2601 will get moved too, and too much. */
2602 SNIP (3, 2, newrel
);
2603 move_reloc (irel
, srel
, -1);
2609 /* Special case - synthetic conditional branches, pcrel24.
2610 Note that EQ and NE have been handled above. */
2611 if ((insn
[0] & 0xf0) == 0x20
2614 && srel
->r_offset
!= irel
->r_offset
+ 1
2615 && -32767 + alignment_glue
<= pcrel
2616 && pcrel
<= 32766 - alignment_glue
)
2620 SNIP (5, 1, newrel
);
2624 /* Special case - synthetic conditional branches, pcrel16 */
2625 if ((insn
[0] & 0xf0) == 0x20
2628 && srel
->r_offset
!= irel
->r_offset
+ 1
2629 && -127 + alignment_glue
<= pcrel
2630 && pcrel
<= 126 - alignment_glue
)
2632 int cond
= (insn
[0] & 0x0f) ^ 0x01;
2634 insn
[0] = 0x20 | cond
;
2635 /* By moving the reloc first, we avoid having
2636 delete_bytes move it also. */
2637 move_reloc (irel
, srel
, -2);
2638 SNIP (2, 3, newrel
);
2643 BFD_ASSERT (nrelocs
== 0);
2645 /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2646 use MOV.bwl #uimm:8, dsp:5[r7] format. This is tricky
2647 because it may have one or two relocations. */
2648 if ((insn
[0] & 0xfc) == 0xf8
2649 && (insn
[1] & 0x80) == 0x00
2650 && (insn
[0] & 0x03) != 0x03)
2652 int dcode
, icode
, reg
, ioff
, dscale
, ilen
;
2653 bfd_vma disp_val
= 0;
2655 Elf_Internal_Rela
* disp_rel
= 0;
2656 Elf_Internal_Rela
* imm_rel
= 0;
2661 dcode
= insn
[0] & 0x03;
2662 icode
= (insn
[1] >> 2) & 0x03;
2663 reg
= (insn
[1] >> 4) & 0x0f;
2665 ioff
= dcode
== 1 ? 3 : dcode
== 2 ? 4 : 2;
2667 /* Figure out what the dispacement is. */
2668 if (dcode
== 1 || dcode
== 2)
2670 /* There's a displacement. See if there's a reloc for it. */
2671 if (srel
[1].r_offset
== irel
->r_offset
+ 2)
2683 #if RX_OPCODE_BIG_ENDIAN
2684 disp_val
= insn
[2] * 256 + insn
[3];
2686 disp_val
= insn
[2] + insn
[3] * 256;
2689 switch (insn
[1] & 3)
2705 /* Figure out what the immediate is. */
2706 if (srel
[1].r_offset
== irel
->r_offset
+ ioff
)
2709 imm_val
= (long) symval
;
2714 unsigned char * ip
= insn
+ ioff
;
2719 /* For byte writes, we don't sign extend. Makes the math easier later. */
2723 imm_val
= (char) ip
[0];
2726 #if RX_OPCODE_BIG_ENDIAN
2727 imm_val
= ((char) ip
[0] << 8) | ip
[1];
2729 imm_val
= ((char) ip
[1] << 8) | ip
[0];
2733 #if RX_OPCODE_BIG_ENDIAN
2734 imm_val
= ((char) ip
[0] << 16) | (ip
[1] << 8) | ip
[2];
2736 imm_val
= ((char) ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2740 #if RX_OPCODE_BIG_ENDIAN
2741 imm_val
= (ip
[0] << 24) | (ip
[1] << 16) | (ip
[2] << 8) | ip
[3];
2743 imm_val
= (ip
[3] << 24) | (ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2777 /* The shortcut happens when the immediate is 0..255,
2778 register r0 to r7, and displacement (scaled) 0..31. */
2780 if (0 <= imm_val
&& imm_val
<= 255
2781 && 0 <= reg
&& reg
<= 7
2782 && disp_val
/ dscale
<= 31)
2784 insn
[0] = 0x3c | (insn
[1] & 0x03);
2785 insn
[1] = (((disp_val
/ dscale
) << 3) & 0x80) | (reg
<< 4) | ((disp_val
/dscale
) & 0x0f);
2790 int newrel
= R_RX_NONE
;
2795 newrel
= R_RX_RH_ABS5p8B
;
2798 newrel
= R_RX_RH_ABS5p8W
;
2801 newrel
= R_RX_RH_ABS5p8L
;
2804 disp_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (disp_rel
->r_info
), newrel
);
2805 move_reloc (irel
, disp_rel
, -1);
2809 imm_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (imm_rel
->r_info
), R_RX_DIR8U
);
2810 move_reloc (disp_rel
? disp_rel
: irel
,
2812 irel
->r_offset
- imm_rel
->r_offset
+ 2);
2815 SNIPNR (3, ilen
- 3);
2818 /* We can't relax this new opcode. */
2824 /* We can't reliably relax branches to DIR3U_PCREL unless we know
2825 whatever they're branching over won't shrink any more. If we're
2826 basically done here, do one more pass just for branches - but
2827 don't request a pass after that one! */
2828 if (!*again
&& !allow_pcrel3
)
2830 bfd_boolean ignored
;
2832 elf32_rx_relax_section (abfd
, sec
, link_info
, &ignored
, TRUE
);
2838 if (free_relocs
!= NULL
)
2841 if (free_contents
!= NULL
)
2842 free (free_contents
);
2844 if (shndx_buf
!= NULL
)
2846 shndx_hdr
->contents
= NULL
;
2850 if (free_intsyms
!= NULL
)
2851 free (free_intsyms
);
2857 elf32_rx_relax_section_wrapper (bfd
* abfd
,
2859 struct bfd_link_info
* link_info
,
2860 bfd_boolean
* again
)
2862 return elf32_rx_relax_section (abfd
, sec
, link_info
, again
, FALSE
);
2865 /* Function to set the ELF flag bits. */
2868 rx_elf_set_private_flags (bfd
* abfd
, flagword flags
)
2870 elf_elfheader (abfd
)->e_flags
= flags
;
2871 elf_flags_init (abfd
) = TRUE
;
2875 static bfd_boolean no_warn_mismatch
= FALSE
;
2877 void bfd_elf32_rx_set_target_flags (bfd_boolean
);
2880 bfd_elf32_rx_set_target_flags (bfd_boolean user_no_warn_mismatch
)
2882 no_warn_mismatch
= user_no_warn_mismatch
;
2885 /* Merge backend specific data from an object file to the output
2886 object file when linking. */
2889 rx_elf_merge_private_bfd_data (bfd
* ibfd
, bfd
* obfd
)
2893 bfd_boolean error
= FALSE
;
2895 new_flags
= elf_elfheader (ibfd
)->e_flags
;
2896 old_flags
= elf_elfheader (obfd
)->e_flags
;
2898 if (!elf_flags_init (obfd
))
2900 /* First call, no flags set. */
2901 elf_flags_init (obfd
) = TRUE
;
2902 elf_elfheader (obfd
)->e_flags
= new_flags
;
2904 else if (old_flags
!= new_flags
)
2906 flagword known_flags
= E_FLAG_RX_64BIT_DOUBLES
| E_FLAG_RX_DSP
;
2908 if ((old_flags
^ new_flags
) & known_flags
)
2910 /* Only complain if flag bits we care about do not match.
2911 Other bits may be set, since older binaries did use some
2912 deprecated flags. */
2913 if (no_warn_mismatch
)
2915 elf_elfheader (obfd
)->e_flags
= (new_flags
| old_flags
) & known_flags
;
2919 (*_bfd_error_handler
)
2920 ("ELF header flags mismatch: old_flags = 0x%.8lx, new_flags = 0x%.8lx, filename = %s",
2921 old_flags
, new_flags
, bfd_get_filename (ibfd
));
2926 elf_elfheader (obfd
)->e_flags
= new_flags
& known_flags
;
2930 bfd_set_error (bfd_error_bad_value
);
2936 rx_elf_print_private_bfd_data (bfd
* abfd
, void * ptr
)
2938 FILE * file
= (FILE *) ptr
;
2941 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
2943 /* Print normal ELF private data. */
2944 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
2946 flags
= elf_elfheader (abfd
)->e_flags
;
2947 fprintf (file
, _("private flags = 0x%lx:"), (long) flags
);
2949 if (flags
& E_FLAG_RX_64BIT_DOUBLES
)
2950 fprintf (file
, _(" [64-bit doubles]"));
2951 if (flags
& E_FLAG_RX_DSP
)
2952 fprintf (file
, _(" [dsp]"));
2958 /* Return the MACH for an e_flags value. */
2961 elf32_rx_machine (bfd
* abfd
)
2963 if ((elf_elfheader (abfd
)->e_flags
& EF_RX_CPU_MASK
) == EF_RX_CPU_RX
)
2970 rx_elf_object_p (bfd
* abfd
)
2972 bfd_default_set_arch_mach (abfd
, bfd_arch_rx
,
2973 elf32_rx_machine (abfd
));
2980 dump_symtab (bfd
* abfd
, void * internal_syms
, void * external_syms
)
2983 Elf_Internal_Sym
* isymbuf
;
2984 Elf_Internal_Sym
* isymend
;
2985 Elf_Internal_Sym
* isym
;
2986 Elf_Internal_Shdr
* symtab_hdr
;
2987 bfd_boolean free_internal
= FALSE
, free_external
= FALSE
;
2989 char * st_info_stb_str
;
2990 char * st_other_str
;
2991 char * st_shndx_str
;
2993 if (! internal_syms
)
2995 internal_syms
= bfd_malloc (1000);
2998 if (! external_syms
)
3000 external_syms
= bfd_malloc (1000);
3004 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3005 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3007 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
3008 symtab_hdr
->sh_info
, 0,
3009 internal_syms
, external_syms
, NULL
);
3011 isymbuf
= internal_syms
;
3012 isymend
= isymbuf
+ locsymcount
;
3014 for (isym
= isymbuf
; isym
< isymend
; isym
++)
3016 switch (ELF_ST_TYPE (isym
->st_info
))
3018 case STT_FUNC
: st_info_str
= "STT_FUNC";
3019 case STT_SECTION
: st_info_str
= "STT_SECTION";
3020 case STT_FILE
: st_info_str
= "STT_FILE";
3021 case STT_OBJECT
: st_info_str
= "STT_OBJECT";
3022 case STT_TLS
: st_info_str
= "STT_TLS";
3023 default: st_info_str
= "";
3025 switch (ELF_ST_BIND (isym
->st_info
))
3027 case STB_LOCAL
: st_info_stb_str
= "STB_LOCAL";
3028 case STB_GLOBAL
: st_info_stb_str
= "STB_GLOBAL";
3029 default: st_info_stb_str
= "";
3031 switch (ELF_ST_VISIBILITY (isym
->st_other
))
3033 case STV_DEFAULT
: st_other_str
= "STV_DEFAULT";
3034 case STV_INTERNAL
: st_other_str
= "STV_INTERNAL";
3035 case STV_PROTECTED
: st_other_str
= "STV_PROTECTED";
3036 default: st_other_str
= "";
3038 switch (isym
->st_shndx
)
3040 case SHN_ABS
: st_shndx_str
= "SHN_ABS";
3041 case SHN_COMMON
: st_shndx_str
= "SHN_COMMON";
3042 case SHN_UNDEF
: st_shndx_str
= "SHN_UNDEF";
3043 default: st_shndx_str
= "";
3046 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3047 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3049 (unsigned long) isym
->st_value
,
3050 (unsigned long) isym
->st_size
,
3052 bfd_elf_string_from_elf_section (abfd
, symtab_hdr
->sh_link
,
3054 isym
->st_info
, st_info_str
, st_info_stb_str
,
3055 isym
->st_other
, st_other_str
,
3056 isym
->st_shndx
, st_shndx_str
);
3059 free (internal_syms
);
3061 free (external_syms
);
3065 rx_get_reloc (long reloc
)
3067 if (0 <= reloc
&& reloc
< R_RX_max
)
3068 return rx_elf_howto_table
[reloc
].name
;
3074 /* We must take care to keep the on-disk copy of any code sections
3075 that are fully linked swapped if the target is big endian, to match
3076 the Renesas tools. */
3078 /* The rule is: big endian object that are final-link executables,
3079 have code sections stored with 32-bit words swapped relative to
3080 what you'd get by default. */
3083 rx_get_section_contents (bfd
* abfd
,
3087 bfd_size_type count
)
3089 int exec
= (abfd
->flags
& EXEC_P
) ? 1 : 0;
3090 int s_code
= (section
->flags
& SEC_CODE
) ? 1 : 0;
3094 fprintf (stderr
, "dj: get %ld %ld from %s %s e%d sc%d %08lx:%08lx\n",
3095 (long) offset
, (long) count
, section
->name
,
3096 bfd_big_endian(abfd
) ? "be" : "le",
3097 exec
, s_code
, (long unsigned) section
->filepos
,
3098 (long unsigned) offset
);
3101 if (exec
&& s_code
&& bfd_big_endian (abfd
))
3103 char * cloc
= (char *) location
;
3104 bfd_size_type cnt
, end_cnt
;
3108 /* Fetch and swap unaligned bytes at the beginning. */
3113 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3118 bfd_putb32 (bfd_getl32 (buf
), buf
);
3120 cnt
= 4 - (offset
% 4);
3124 memcpy (location
, buf
+ (offset
% 4), cnt
);
3131 end_cnt
= count
% 4;
3133 /* Fetch and swap the middle bytes. */
3136 rv
= _bfd_generic_get_section_contents (abfd
, section
, cloc
, offset
,
3141 for (cnt
= count
; cnt
>= 4; cnt
-= 4, cloc
+= 4)
3142 bfd_putb32 (bfd_getl32 (cloc
), cloc
);
3145 /* Fetch and swap the end bytes. */
3150 /* Fetch the end bytes. */
3151 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3152 offset
+ count
- end_cnt
, 4);
3156 bfd_putb32 (bfd_getl32 (buf
), buf
);
3157 memcpy (cloc
, buf
, end_cnt
);
3161 rv
= _bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
);
3168 rx2_set_section_contents (bfd
* abfd
,
3170 const void * location
,
3172 bfd_size_type count
)
3176 fprintf (stderr
, " set sec %s %08x loc %p offset %#x count %#x\n",
3177 section
->name
, (unsigned) section
->vma
, location
, (int) offset
, (int) count
);
3178 for (i
= 0; i
< count
; i
++)
3180 if (i
% 16 == 0 && i
> 0)
3181 fprintf (stderr
, "\n");
3183 if (i
% 16 && i
% 4 == 0)
3184 fprintf (stderr
, " ");
3187 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3189 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3191 fprintf (stderr
, "\n");
3193 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3195 #define _bfd_elf_set_section_contents rx2_set_section_contents
3199 rx_set_section_contents (bfd
* abfd
,
3201 const void * location
,
3203 bfd_size_type count
)
3205 bfd_boolean exec
= (abfd
->flags
& EXEC_P
) ? TRUE
: FALSE
;
3206 bfd_boolean s_code
= (section
->flags
& SEC_CODE
) ? TRUE
: FALSE
;
3208 char * swapped_data
= NULL
;
3210 bfd_vma caddr
= section
->vma
+ offset
;
3212 bfd_size_type scount
;
3217 fprintf (stderr
, "\ndj: set %ld %ld to %s %s e%d sc%d\n",
3218 (long) offset
, (long) count
, section
->name
,
3219 bfd_big_endian (abfd
) ? "be" : "le",
3222 for (i
= 0; i
< count
; i
++)
3224 int a
= section
->vma
+ offset
+ i
;
3226 if (a
% 16 == 0 && a
> 0)
3227 fprintf (stderr
, "\n");
3229 if (a
% 16 && a
% 4 == 0)
3230 fprintf (stderr
, " ");
3232 if (a
% 16 == 0 || i
== 0)
3233 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3235 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3238 fprintf (stderr
, "\n");
3241 if (! exec
|| ! s_code
|| ! bfd_big_endian (abfd
))
3242 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3244 while (count
> 0 && caddr
> 0 && caddr
% 4)
3248 case 0: faddr
= offset
+ 3; break;
3249 case 1: faddr
= offset
+ 1; break;
3250 case 2: faddr
= offset
- 1; break;
3251 case 3: faddr
= offset
- 3; break;
3254 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3264 scount
= (int)(count
/ 4) * 4;
3267 char * cloc
= (char *) location
;
3269 swapped_data
= (char *) bfd_alloc (abfd
, count
);
3271 for (i
= 0; i
< count
; i
+= 4)
3273 bfd_vma v
= bfd_getl32 (cloc
+ i
);
3274 bfd_putb32 (v
, swapped_data
+ i
);
3277 rv
= _bfd_elf_set_section_contents (abfd
, section
, swapped_data
, offset
, scount
);
3289 caddr
= section
->vma
+ offset
;
3294 case 0: faddr
= offset
+ 3; break;
3295 case 1: faddr
= offset
+ 1; break;
3296 case 2: faddr
= offset
- 1; break;
3297 case 3: faddr
= offset
- 3; break;
3299 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3314 rx_final_link (bfd
* abfd
, struct bfd_link_info
* info
)
3318 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3321 fprintf (stderr
, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3322 o
->name
, o
->flags
, o
->vma
, o
->lma
, o
->size
, o
->rawsize
);
3324 if (o
->flags
& SEC_CODE
3325 && bfd_big_endian (abfd
)
3326 && (o
->size
% 4 || o
->rawsize
% 4))
3329 fprintf (stderr
, "adjusting...\n");
3331 o
->size
+= 4 - (o
->size
% 4);
3332 o
->rawsize
+= 4 - (o
->rawsize
% 4);
3336 return bfd_elf_final_link (abfd
, info
);
3340 elf32_rx_modify_program_headers (bfd
* abfd ATTRIBUTE_UNUSED
,
3341 struct bfd_link_info
* info ATTRIBUTE_UNUSED
)
3343 const struct elf_backend_data
* bed
;
3344 struct elf_obj_tdata
* tdata
;
3345 Elf_Internal_Phdr
* phdr
;
3349 bed
= get_elf_backend_data (abfd
);
3350 tdata
= elf_tdata (abfd
);
3352 count
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
3354 for (i
= count
; i
-- != 0; )
3355 if (phdr
[i
].p_type
== PT_LOAD
)
3357 /* The Renesas tools expect p_paddr to be zero. However,
3358 there is no other way to store the writable data in ROM for
3359 startup initialization. So, we let the linker *think*
3360 we're using paddr and vaddr the "usual" way, but at the
3361 last minute we move the paddr into the vaddr (which is what
3362 the simulator uses) and zero out paddr. Note that this
3363 does not affect the section headers, just the program
3364 headers. We hope. */
3365 phdr
[i
].p_vaddr
= phdr
[i
].p_paddr
;
3366 /* If we zero out p_paddr, then the LMA in the section table
3368 /*phdr[i].p_paddr = 0;*/
3374 #define ELF_ARCH bfd_arch_rx
3375 #define ELF_MACHINE_CODE EM_RX
3376 #define ELF_MAXPAGESIZE 0x1000
3378 #define TARGET_BIG_SYM bfd_elf32_rx_be_vec
3379 #define TARGET_BIG_NAME "elf32-rx-be"
3381 #define TARGET_LITTLE_SYM bfd_elf32_rx_le_vec
3382 #define TARGET_LITTLE_NAME "elf32-rx-le"
3384 #define elf_info_to_howto_rel NULL
3385 #define elf_info_to_howto rx_info_to_howto_rela
3386 #define elf_backend_object_p rx_elf_object_p
3387 #define elf_backend_relocate_section rx_elf_relocate_section
3388 #define elf_symbol_leading_char ('_')
3389 #define elf_backend_can_gc_sections 1
3390 #define elf_backend_modify_program_headers elf32_rx_modify_program_headers
3392 #define bfd_elf32_bfd_reloc_type_lookup rx_reloc_type_lookup
3393 #define bfd_elf32_bfd_reloc_name_lookup rx_reloc_name_lookup
3394 #define bfd_elf32_bfd_set_private_flags rx_elf_set_private_flags
3395 #define bfd_elf32_bfd_merge_private_bfd_data rx_elf_merge_private_bfd_data
3396 #define bfd_elf32_bfd_print_private_bfd_data rx_elf_print_private_bfd_data
3397 #define bfd_elf32_get_section_contents rx_get_section_contents
3398 #define bfd_elf32_set_section_contents rx_set_section_contents
3399 #define bfd_elf32_bfd_final_link rx_final_link
3400 #define bfd_elf32_bfd_relax_section elf32_rx_relax_section_wrapper
3402 #include "elf32-target.h"