1 /* Renesas RX specific support for 32-bit ELF.
2 Copyright (C) 2008, 2009, 2010
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
;
457 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
458 sym_hashes
= elf_sym_hashes (input_bfd
);
459 relend
= relocs
+ input_section
->reloc_count
;
460 for (rel
= relocs
; rel
< relend
; rel
++)
462 reloc_howto_type
* howto
;
463 unsigned long r_symndx
;
464 Elf_Internal_Sym
* sym
;
466 struct elf_link_hash_entry
* h
;
468 bfd_reloc_status_type r
;
469 const char * name
= NULL
;
470 bfd_boolean unresolved_reloc
= TRUE
;
473 r_type
= ELF32_R_TYPE (rel
->r_info
);
474 r_symndx
= ELF32_R_SYM (rel
->r_info
);
476 howto
= rx_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
482 if (r_symndx
< symtab_hdr
->sh_info
)
484 sym
= local_syms
+ r_symndx
;
485 sec
= local_sections
[r_symndx
];
486 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, & sec
, rel
);
488 name
= bfd_elf_string_from_elf_section
489 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
490 name
= (sym
->st_name
== 0) ? bfd_section_name (input_bfd
, sec
) : name
;
496 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
497 r_symndx
, symtab_hdr
, sym_hashes
, h
,
498 sec
, relocation
, unresolved_reloc
,
501 name
= h
->root
.root
.string
;
504 if (sec
!= NULL
&& elf_discarded_section (sec
))
506 /* For relocs against symbols from removed linkonce sections,
507 or sections discarded by a linker script, we just want the
508 section contents zeroed. Avoid any special processing. */
509 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
515 if (info
->relocatable
)
517 /* This is a relocatable link. We don't have to change
518 anything, unless the reloc is against a section symbol,
519 in which case we have to adjust according to where the
520 section symbol winds up in the output section. */
521 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
522 rel
->r_addend
+= sec
->output_offset
;
526 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
527 /* If the symbol is undefined and weak
528 then the relocation resolves to zero. */
532 if (howto
->pc_relative
)
534 relocation
-= (input_section
->output_section
->vma
535 + input_section
->output_offset
537 if (r_type
!= R_RX_RH_3_PCREL
538 && r_type
!= R_RX_DIR3U_PCREL
)
542 relocation
+= rel
->r_addend
;
547 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
548 #define ALIGN(m) if (relocation & m) r = bfd_reloc_other;
549 #define OP(i) (contents[rel->r_offset + (i)])
550 #define WARN_REDHAT(type) \
551 _bfd_error_handler (_("%B:%A: Warning: deprecated Red Hat reloc " type " detected against: %s."), \
552 input_bfd, input_section, name)
554 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
563 case R_RX_RH_3_PCREL
:
564 WARN_REDHAT ("RX_RH_3_PCREL");
567 OP (0) |= relocation
& 0x07;
571 WARN_REDHAT ("RX_RH_8_NEG");
572 relocation
= - relocation
;
573 case R_RX_DIR8S_PCREL
:
589 WARN_REDHAT ("RX_RH_16_NEG");
590 relocation
= - relocation
;
591 case R_RX_DIR16S_PCREL
:
592 RANGE (-32768, 32767);
593 #if RX_OPCODE_BIG_ENDIAN
596 OP (1) = relocation
>> 8;
601 WARN_REDHAT ("RX_RH_16_OP");
602 RANGE (-32768, 32767);
603 #if RX_OPCODE_BIG_ENDIAN
605 OP (0) = relocation
>> 8;
608 OP (1) = relocation
>> 8;
613 RANGE (-32768, 65535);
614 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
617 OP (0) = relocation
>> 8;
622 OP (1) = relocation
>> 8;
628 #if RX_OPCODE_BIG_ENDIAN
630 OP (0) = relocation
>> 8;
633 OP (1) = relocation
>> 8;
638 RANGE (-32768, 65536);
639 #if RX_OPCODE_BIG_ENDIAN
641 OP (0) = relocation
>> 8;
644 OP (1) = relocation
>> 8;
649 RANGE (-32768, 65536);
650 #if RX_OPCODE_BIG_ENDIAN
652 OP (1) = relocation
>> 8;
655 OP (0) = relocation
>> 8;
659 case R_RX_DIR3U_PCREL
:
662 OP (0) |= relocation
& 0x07;
666 WARN_REDHAT ("RX_RH_24_NEG");
667 relocation
= - relocation
;
668 case R_RX_DIR24S_PCREL
:
669 RANGE (-0x800000, 0x7fffff);
670 #if RX_OPCODE_BIG_ENDIAN
672 OP (1) = relocation
>> 8;
673 OP (0) = relocation
>> 16;
676 OP (1) = relocation
>> 8;
677 OP (2) = relocation
>> 16;
682 WARN_REDHAT ("RX_RH_24_OP");
683 RANGE (-0x800000, 0x7fffff);
684 #if RX_OPCODE_BIG_ENDIAN
686 OP (1) = relocation
>> 8;
687 OP (0) = relocation
>> 16;
690 OP (1) = relocation
>> 8;
691 OP (2) = relocation
>> 16;
696 RANGE (-0x800000, 0x7fffff);
697 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
700 OP (1) = relocation
>> 8;
701 OP (0) = relocation
>> 16;
706 OP (1) = relocation
>> 8;
707 OP (2) = relocation
>> 16;
712 WARN_REDHAT ("RX_RH_24_UNS");
714 #if RX_OPCODE_BIG_ENDIAN
716 OP (1) = relocation
>> 8;
717 OP (0) = relocation
>> 16;
720 OP (1) = relocation
>> 8;
721 OP (2) = relocation
>> 16;
726 WARN_REDHAT ("RX_RH_32_NEG");
727 relocation
= - relocation
;
728 #if RX_OPCODE_BIG_ENDIAN
730 OP (2) = relocation
>> 8;
731 OP (1) = relocation
>> 16;
732 OP (0) = relocation
>> 24;
735 OP (1) = relocation
>> 8;
736 OP (2) = relocation
>> 16;
737 OP (3) = relocation
>> 24;
742 WARN_REDHAT ("RX_RH_32_OP");
743 #if RX_OPCODE_BIG_ENDIAN
745 OP (2) = relocation
>> 8;
746 OP (1) = relocation
>> 16;
747 OP (0) = relocation
>> 24;
750 OP (1) = relocation
>> 8;
751 OP (2) = relocation
>> 16;
752 OP (3) = relocation
>> 24;
757 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
760 OP (2) = relocation
>> 8;
761 OP (1) = relocation
>> 16;
762 OP (0) = relocation
>> 24;
767 OP (1) = relocation
>> 8;
768 OP (2) = relocation
>> 16;
769 OP (3) = relocation
>> 24;
774 if (BIGE (output_bfd
))
777 OP (1) = relocation
>> 8;
778 OP (2) = relocation
>> 16;
779 OP (3) = relocation
>> 24;
784 OP (2) = relocation
>> 8;
785 OP (1) = relocation
>> 16;
786 OP (0) = relocation
>> 24;
793 WARN_REDHAT ("RX_RH_DIFF");
794 val
= bfd_get_32 (output_bfd
, & OP (0));
796 bfd_put_32 (output_bfd
, val
, & OP (0));
801 WARN_REDHAT ("RX_RH_GPRELB");
802 relocation
-= get_gp (&r
, info
, input_bfd
, input_section
, rel
->r_offset
);
804 #if RX_OPCODE_BIG_ENDIAN
806 OP (0) = relocation
>> 8;
809 OP (1) = relocation
>> 8;
814 WARN_REDHAT ("RX_RH_GPRELW");
815 relocation
-= get_gp (&r
, info
, input_bfd
, input_section
, rel
->r_offset
);
819 #if RX_OPCODE_BIG_ENDIAN
821 OP (0) = relocation
>> 8;
824 OP (1) = relocation
>> 8;
829 WARN_REDHAT ("RX_RH_GPRELL");
830 relocation
-= get_gp (&r
, info
, input_bfd
, input_section
, rel
->r_offset
);
834 #if RX_OPCODE_BIG_ENDIAN
836 OP (0) = relocation
>> 8;
839 OP (1) = relocation
>> 8;
843 /* Internal relocations just for relaxation: */
844 case R_RX_RH_ABS5p5B
:
845 RX_STACK_POP (relocation
);
848 OP (0) |= relocation
>> 2;
850 OP (1) |= (relocation
<< 6) & 0x80;
851 OP (1) |= (relocation
<< 3) & 0x08;
854 case R_RX_RH_ABS5p5W
:
855 RX_STACK_POP (relocation
);
860 OP (0) |= relocation
>> 2;
862 OP (1) |= (relocation
<< 6) & 0x80;
863 OP (1) |= (relocation
<< 3) & 0x08;
866 case R_RX_RH_ABS5p5L
:
867 RX_STACK_POP (relocation
);
872 OP (0) |= relocation
>> 2;
874 OP (1) |= (relocation
<< 6) & 0x80;
875 OP (1) |= (relocation
<< 3) & 0x08;
878 case R_RX_RH_ABS5p8B
:
879 RX_STACK_POP (relocation
);
882 OP (0) |= (relocation
<< 3) & 0x80;
883 OP (0) |= relocation
& 0x0f;
886 case R_RX_RH_ABS5p8W
:
887 RX_STACK_POP (relocation
);
892 OP (0) |= (relocation
<< 3) & 0x80;
893 OP (0) |= relocation
& 0x0f;
896 case R_RX_RH_ABS5p8L
:
897 RX_STACK_POP (relocation
);
902 OP (0) |= (relocation
<< 3) & 0x80;
903 OP (0) |= relocation
& 0x0f;
906 case R_RX_RH_UIMM4p8
:
909 OP (0) |= relocation
<< 4;
912 case R_RX_RH_UNEG4p8
:
915 OP (0) |= (-relocation
) << 4;
918 /* Complex reloc handling: */
921 RX_STACK_POP (relocation
);
922 #if RX_OPCODE_BIG_ENDIAN
924 OP (2) = relocation
>> 8;
925 OP (1) = relocation
>> 16;
926 OP (0) = relocation
>> 24;
929 OP (1) = relocation
>> 8;
930 OP (2) = relocation
>> 16;
931 OP (3) = relocation
>> 24;
936 RX_STACK_POP (relocation
);
937 #if RX_OPCODE_BIG_ENDIAN
939 OP (1) = relocation
>> 8;
940 OP (2) = relocation
>> 16;
941 OP (3) = relocation
>> 24;
944 OP (2) = relocation
>> 8;
945 OP (1) = relocation
>> 16;
946 OP (0) = relocation
>> 24;
950 case R_RX_ABS24S_PCREL
:
952 RX_STACK_POP (relocation
);
953 RANGE (-0x800000, 0x7fffff);
954 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
957 OP (1) = relocation
>> 8;
958 OP (0) = relocation
>> 16;
963 OP (1) = relocation
>> 8;
964 OP (2) = relocation
>> 16;
969 RX_STACK_POP (relocation
);
970 RANGE (-32768, 65535);
971 #if RX_OPCODE_BIG_ENDIAN
973 OP (0) = relocation
>> 8;
976 OP (1) = relocation
>> 8;
981 RX_STACK_POP (relocation
);
982 RANGE (-32768, 65535);
983 #if RX_OPCODE_BIG_ENDIAN
985 OP (1) = relocation
>> 8;
988 OP (0) = relocation
>> 8;
992 case R_RX_ABS16S_PCREL
:
994 RX_STACK_POP (relocation
);
995 RANGE (-32768, 32767);
996 if (BIGE (output_bfd
) && !(input_section
->flags
& SEC_CODE
))
999 OP (0) = relocation
>> 8;
1003 OP (0) = relocation
;
1004 OP (1) = relocation
>> 8;
1009 RX_STACK_POP (relocation
);
1011 #if RX_OPCODE_BIG_ENDIAN
1012 OP (1) = relocation
;
1013 OP (0) = relocation
>> 8;
1015 OP (0) = relocation
;
1016 OP (1) = relocation
>> 8;
1021 RX_STACK_POP (relocation
);
1024 #if RX_OPCODE_BIG_ENDIAN
1025 OP (1) = relocation
;
1026 OP (0) = relocation
>> 8;
1028 OP (0) = relocation
;
1029 OP (1) = relocation
>> 8;
1034 RX_STACK_POP (relocation
);
1037 #if RX_OPCODE_BIG_ENDIAN
1038 OP (1) = relocation
;
1039 OP (0) = relocation
>> 8;
1041 OP (0) = relocation
;
1042 OP (1) = relocation
>> 8;
1047 RX_STACK_POP (relocation
);
1049 OP (0) = relocation
;
1053 RX_STACK_POP (relocation
);
1055 OP (0) = relocation
;
1059 RX_STACK_POP (relocation
);
1062 OP (0) = relocation
;
1066 RX_STACK_POP (relocation
);
1069 OP (0) = relocation
;
1072 case R_RX_ABS8S_PCREL
:
1074 RX_STACK_POP (relocation
);
1076 OP (0) = relocation
;
1080 if (r_symndx
< symtab_hdr
->sh_info
)
1081 RX_STACK_PUSH (sec
->output_section
->vma
1082 + sec
->output_offset
1087 && (h
->root
.type
== bfd_link_hash_defined
1088 || h
->root
.type
== bfd_link_hash_defweak
))
1089 RX_STACK_PUSH (h
->root
.u
.def
.value
1090 + sec
->output_section
->vma
1091 + sec
->output_offset
);
1093 _bfd_error_handler (_("Warning: RX_SYM reloc with an unknown symbol"));
1103 RX_STACK_PUSH (tmp
);
1111 RX_STACK_POP (tmp1
);
1112 RX_STACK_POP (tmp2
);
1114 RX_STACK_PUSH (tmp1
);
1122 RX_STACK_POP (tmp1
);
1123 RX_STACK_POP (tmp2
);
1125 RX_STACK_PUSH (tmp2
);
1133 RX_STACK_POP (tmp1
);
1134 RX_STACK_POP (tmp2
);
1136 RX_STACK_PUSH (tmp1
);
1144 RX_STACK_POP (tmp1
);
1145 RX_STACK_POP (tmp2
);
1147 RX_STACK_PUSH (tmp1
);
1155 RX_STACK_POP (tmp1
);
1156 RX_STACK_POP (tmp2
);
1158 RX_STACK_PUSH (tmp1
);
1166 RX_STACK_POP (tmp1
);
1167 RX_STACK_POP (tmp2
);
1169 RX_STACK_PUSH (tmp1
);
1173 case R_RX_OPsctsize
:
1174 RX_STACK_PUSH (input_section
->size
);
1178 RX_STACK_PUSH (input_section
->output_section
->vma
);
1185 RX_STACK_POP (tmp1
);
1186 RX_STACK_POP (tmp2
);
1188 RX_STACK_PUSH (tmp1
);
1196 RX_STACK_POP (tmp1
);
1197 RX_STACK_POP (tmp2
);
1199 RX_STACK_PUSH (tmp1
);
1207 RX_STACK_POP (tmp1
);
1208 RX_STACK_POP (tmp2
);
1210 RX_STACK_PUSH (tmp1
);
1220 RX_STACK_PUSH (tmp
);
1228 RX_STACK_POP (tmp1
);
1229 RX_STACK_POP (tmp2
);
1231 RX_STACK_PUSH (tmp1
);
1236 RX_STACK_PUSH (get_romstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1240 RX_STACK_PUSH (get_ramstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1244 r
= bfd_reloc_notsupported
;
1248 if (r
!= bfd_reloc_ok
)
1250 const char * msg
= NULL
;
1254 case bfd_reloc_overflow
:
1255 /* Catch the case of a missing function declaration
1256 and emit a more helpful error message. */
1257 if (r_type
== R_RX_DIR24S_PCREL
)
1258 msg
= _("%B(%A): error: call to undefined function '%s'");
1260 r
= info
->callbacks
->reloc_overflow
1261 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
1262 input_bfd
, input_section
, rel
->r_offset
);
1265 case bfd_reloc_undefined
:
1266 r
= info
->callbacks
->undefined_symbol
1267 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
1271 case bfd_reloc_other
:
1272 msg
= _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
1275 case bfd_reloc_outofrange
:
1276 msg
= _("%B(%A): internal error: out of range error");
1279 case bfd_reloc_notsupported
:
1280 msg
= _("%B(%A): internal error: unsupported relocation error");
1283 case bfd_reloc_dangerous
:
1284 msg
= _("%B(%A): internal error: dangerous relocation");
1288 msg
= _("%B(%A): internal error: unknown error");
1293 _bfd_error_handler (msg
, input_bfd
, input_section
, name
);
1303 /* Relaxation Support. */
1305 /* Progression of relocations from largest operand size to smallest
1309 next_smaller_reloc (int r
)
1313 case R_RX_DIR32
: return R_RX_DIR24S
;
1314 case R_RX_DIR24S
: return R_RX_DIR16S
;
1315 case R_RX_DIR16S
: return R_RX_DIR8S
;
1316 case R_RX_DIR8S
: return R_RX_NONE
;
1318 case R_RX_DIR16
: return R_RX_DIR8
;
1319 case R_RX_DIR8
: return R_RX_NONE
;
1321 case R_RX_DIR16U
: return R_RX_DIR8U
;
1322 case R_RX_DIR8U
: return R_RX_NONE
;
1324 case R_RX_DIR24S_PCREL
: return R_RX_DIR16S_PCREL
;
1325 case R_RX_DIR16S_PCREL
: return R_RX_DIR8S_PCREL
;
1326 case R_RX_DIR8S_PCREL
: return R_RX_DIR3U_PCREL
;
1328 case R_RX_DIR16UL
: return R_RX_DIR8UL
;
1329 case R_RX_DIR8UL
: return R_RX_NONE
;
1330 case R_RX_DIR16UW
: return R_RX_DIR8UW
;
1331 case R_RX_DIR8UW
: return R_RX_NONE
;
1333 case R_RX_RH_32_OP
: return R_RX_RH_24_OP
;
1334 case R_RX_RH_24_OP
: return R_RX_RH_16_OP
;
1335 case R_RX_RH_16_OP
: return R_RX_DIR8
;
1337 case R_RX_ABS32
: return R_RX_ABS24S
;
1338 case R_RX_ABS24S
: return R_RX_ABS16S
;
1339 case R_RX_ABS16
: return R_RX_ABS8
;
1340 case R_RX_ABS16U
: return R_RX_ABS8U
;
1341 case R_RX_ABS16S
: return R_RX_ABS8S
;
1342 case R_RX_ABS8
: return R_RX_NONE
;
1343 case R_RX_ABS8U
: return R_RX_NONE
;
1344 case R_RX_ABS8S
: return R_RX_NONE
;
1345 case R_RX_ABS24S_PCREL
: return R_RX_ABS16S_PCREL
;
1346 case R_RX_ABS16S_PCREL
: return R_RX_ABS8S_PCREL
;
1347 case R_RX_ABS8S_PCREL
: return R_RX_NONE
;
1348 case R_RX_ABS16UL
: return R_RX_ABS8UL
;
1349 case R_RX_ABS16UW
: return R_RX_ABS8UW
;
1350 case R_RX_ABS8UL
: return R_RX_NONE
;
1351 case R_RX_ABS8UW
: return R_RX_NONE
;
1356 /* Delete some bytes from a section while relaxing. */
1359 elf32_rx_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, int count
,
1360 Elf_Internal_Rela
*alignment_rel
, int force_snip
)
1362 Elf_Internal_Shdr
* symtab_hdr
;
1363 unsigned int sec_shndx
;
1364 bfd_byte
* contents
;
1365 Elf_Internal_Rela
* irel
;
1366 Elf_Internal_Rela
* irelend
;
1367 Elf_Internal_Sym
* isym
;
1368 Elf_Internal_Sym
* isymend
;
1370 unsigned int symcount
;
1371 struct elf_link_hash_entry
** sym_hashes
;
1372 struct elf_link_hash_entry
** end_hashes
;
1377 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1379 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1381 /* The deletion must stop at the next alignment boundary, if
1382 ALIGNMENT_REL is non-NULL. */
1385 toaddr
= alignment_rel
->r_offset
;
1387 irel
= elf_section_data (sec
)->relocs
;
1388 irelend
= irel
+ sec
->reloc_count
;
1390 /* Actually delete the bytes. */
1391 memmove (contents
+ addr
, contents
+ addr
+ count
,
1392 (size_t) (toaddr
- addr
- count
));
1394 /* If we don't have an alignment marker to worry about, we can just
1395 shrink the section. Otherwise, we have to fill in the newly
1396 created gap with NOP insns (0x03). */
1400 memset (contents
+ toaddr
- count
, 0x03, count
);
1402 /* Adjust all the relocs. */
1403 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1405 /* Get the new reloc address. */
1406 if (irel
->r_offset
> addr
1407 && (irel
->r_offset
< toaddr
1408 || (force_snip
&& irel
->r_offset
== toaddr
)))
1409 irel
->r_offset
-= count
;
1411 /* If we see an ALIGN marker at the end of the gap, we move it
1412 to the beginning of the gap, since marking these gaps is what
1414 if (irel
->r_offset
== toaddr
1415 && ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
1416 && irel
->r_addend
& RX_RELAXA_ALIGN
)
1417 irel
->r_offset
-= count
;
1420 /* Adjust the local symbols defined in this section. */
1421 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1422 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1423 isymend
= isym
+ symtab_hdr
->sh_info
;
1425 for (; isym
< isymend
; isym
++)
1427 /* If the symbol is in the range of memory we just moved, we
1428 have to adjust its value. */
1429 if (isym
->st_shndx
== sec_shndx
1430 && isym
->st_value
> addr
1431 && isym
->st_value
< toaddr
)
1432 isym
->st_value
-= count
;
1434 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1435 *end* is in the moved bytes but it's *start* isn't), then we
1436 must adjust its size. */
1437 if (isym
->st_shndx
== sec_shndx
1438 && isym
->st_value
< addr
1439 && isym
->st_value
+ isym
->st_size
> addr
1440 && isym
->st_value
+ isym
->st_size
< toaddr
)
1441 isym
->st_size
-= count
;
1444 /* Now adjust the global symbols defined in this section. */
1445 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1446 - symtab_hdr
->sh_info
);
1447 sym_hashes
= elf_sym_hashes (abfd
);
1448 end_hashes
= sym_hashes
+ symcount
;
1450 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1452 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1454 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1455 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1456 && sym_hash
->root
.u
.def
.section
== sec
)
1458 /* As above, adjust the value if needed. */
1459 if (sym_hash
->root
.u
.def
.value
> addr
1460 && sym_hash
->root
.u
.def
.value
< toaddr
)
1461 sym_hash
->root
.u
.def
.value
-= count
;
1463 /* As above, adjust the size if needed. */
1464 if (sym_hash
->root
.u
.def
.value
< addr
1465 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
1466 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
1467 sym_hash
->size
-= count
;
1474 /* Used to sort relocs by address. If relocs have the same address,
1475 we maintain their relative order, except that R_RX_RH_RELAX
1476 alignment relocs must be the first reloc for any given address. */
1479 reloc_bubblesort (Elf_Internal_Rela
* r
, int count
)
1483 bfd_boolean swappit
;
1485 /* This is almost a classic bubblesort. It's the slowest sort, but
1486 we're taking advantage of the fact that the relocations are
1487 mostly in order already (the assembler emits them that way) and
1488 we need relocs with the same address to remain in the same
1494 for (i
= 0; i
< count
- 1; i
++)
1496 if (r
[i
].r_offset
> r
[i
+ 1].r_offset
)
1498 else if (r
[i
].r_offset
< r
[i
+ 1].r_offset
)
1500 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1501 && (r
[i
+ 1].r_addend
& RX_RELAXA_ALIGN
))
1503 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RX_RH_RELAX
1504 && (r
[i
+ 1].r_addend
& RX_RELAXA_ELIGN
)
1505 && !(ELF32_R_TYPE (r
[i
].r_info
) == R_RX_RH_RELAX
1506 && (r
[i
].r_addend
& RX_RELAXA_ALIGN
)))
1513 Elf_Internal_Rela tmp
;
1518 /* If we do move a reloc back, re-scan to see if it
1519 needs to be moved even further back. This avoids
1520 most of the O(n^2) behavior for our cases. */
1530 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1531 rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1532 lrel, abfd, sec, link_info, scale)
1535 rx_offset_for_reloc (bfd
* abfd
,
1536 Elf_Internal_Rela
* rel
,
1537 Elf_Internal_Shdr
* symtab_hdr
,
1538 Elf_External_Sym_Shndx
* shndx_buf ATTRIBUTE_UNUSED
,
1539 Elf_Internal_Sym
* intsyms
,
1540 Elf_Internal_Rela
** lrel
,
1542 asection
* input_section
,
1543 struct bfd_link_info
* info
,
1547 bfd_reloc_status_type r
;
1551 /* REL is the first of 1..N relocations. We compute the symbol
1552 value for each relocation, then combine them if needed. LREL
1553 gets a pointer to the last relocation used. */
1558 /* Get the value of the symbol referred to by the reloc. */
1559 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1561 /* A local symbol. */
1562 Elf_Internal_Sym
*isym
;
1565 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1567 if (isym
->st_shndx
== SHN_UNDEF
)
1568 ssec
= bfd_und_section_ptr
;
1569 else if (isym
->st_shndx
== SHN_ABS
)
1570 ssec
= bfd_abs_section_ptr
;
1571 else if (isym
->st_shndx
== SHN_COMMON
)
1572 ssec
= bfd_com_section_ptr
;
1574 ssec
= bfd_section_from_elf_index (abfd
,
1577 /* Initial symbol value. */
1578 symval
= isym
->st_value
;
1580 /* GAS may have made this symbol relative to a section, in
1581 which case, we have to add the addend to find the
1583 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
1584 symval
+= rel
->r_addend
;
1588 if ((ssec
->flags
& SEC_MERGE
)
1589 && ssec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
1590 symval
= _bfd_merged_section_offset (abfd
, & ssec
,
1591 elf_section_data (ssec
)->sec_info
,
1595 /* Now make the offset relative to where the linker is putting it. */
1598 ssec
->output_section
->vma
+ ssec
->output_offset
;
1600 symval
+= rel
->r_addend
;
1605 struct elf_link_hash_entry
* h
;
1607 /* An external symbol. */
1608 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1609 h
= elf_sym_hashes (abfd
)[indx
];
1610 BFD_ASSERT (h
!= NULL
);
1612 if (h
->root
.type
!= bfd_link_hash_defined
1613 && h
->root
.type
!= bfd_link_hash_defweak
)
1615 /* This appears to be a reference to an undefined
1616 symbol. Just ignore it--it will be caught by the
1617 regular reloc processing. */
1623 symval
= (h
->root
.u
.def
.value
1624 + h
->root
.u
.def
.section
->output_section
->vma
1625 + h
->root
.u
.def
.section
->output_offset
);
1627 symval
+= rel
->r_addend
;
1630 switch (ELF32_R_TYPE (rel
->r_info
))
1633 RX_STACK_PUSH (symval
);
1637 RX_STACK_POP (tmp1
);
1639 RX_STACK_PUSH (tmp1
);
1643 RX_STACK_POP (tmp1
);
1644 RX_STACK_POP (tmp2
);
1646 RX_STACK_PUSH (tmp1
);
1650 RX_STACK_POP (tmp1
);
1651 RX_STACK_POP (tmp2
);
1653 RX_STACK_PUSH (tmp2
);
1657 RX_STACK_POP (tmp1
);
1658 RX_STACK_POP (tmp2
);
1660 RX_STACK_PUSH (tmp1
);
1664 RX_STACK_POP (tmp1
);
1665 RX_STACK_POP (tmp2
);
1667 RX_STACK_PUSH (tmp1
);
1671 RX_STACK_POP (tmp1
);
1672 RX_STACK_POP (tmp2
);
1674 RX_STACK_PUSH (tmp1
);
1678 RX_STACK_POP (tmp1
);
1679 RX_STACK_POP (tmp2
);
1681 RX_STACK_PUSH (tmp1
);
1684 case R_RX_OPsctsize
:
1685 RX_STACK_PUSH (input_section
->size
);
1689 RX_STACK_PUSH (input_section
->output_section
->vma
);
1693 RX_STACK_POP (tmp1
);
1694 RX_STACK_POP (tmp2
);
1696 RX_STACK_PUSH (tmp1
);
1700 RX_STACK_POP (tmp1
);
1701 RX_STACK_POP (tmp2
);
1703 RX_STACK_PUSH (tmp1
);
1707 RX_STACK_POP (tmp1
);
1708 RX_STACK_POP (tmp2
);
1710 RX_STACK_PUSH (tmp1
);
1714 RX_STACK_POP (tmp1
);
1716 RX_STACK_PUSH (tmp1
);
1720 RX_STACK_POP (tmp1
);
1721 RX_STACK_POP (tmp2
);
1723 RX_STACK_PUSH (tmp1
);
1727 RX_STACK_PUSH (get_romstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1731 RX_STACK_PUSH (get_ramstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1739 RX_STACK_POP (symval
);
1750 RX_STACK_POP (symval
);
1758 RX_STACK_POP (symval
);
1769 move_reloc (Elf_Internal_Rela
* irel
, Elf_Internal_Rela
* srel
, int delta
)
1771 bfd_vma old_offset
= srel
->r_offset
;
1774 while (irel
<= srel
)
1776 if (irel
->r_offset
== old_offset
)
1777 irel
->r_offset
+= delta
;
1782 /* Relax one section. */
1785 elf32_rx_relax_section (bfd
* abfd
,
1787 struct bfd_link_info
* link_info
,
1788 bfd_boolean
* again
,
1789 bfd_boolean allow_pcrel3
)
1791 Elf_Internal_Shdr
* symtab_hdr
;
1792 Elf_Internal_Shdr
* shndx_hdr
;
1793 Elf_Internal_Rela
* internal_relocs
;
1794 Elf_Internal_Rela
* free_relocs
= NULL
;
1795 Elf_Internal_Rela
* irel
;
1796 Elf_Internal_Rela
* srel
;
1797 Elf_Internal_Rela
* irelend
;
1798 Elf_Internal_Rela
* next_alignment
;
1799 Elf_Internal_Rela
* prev_alignment
;
1800 bfd_byte
* contents
= NULL
;
1801 bfd_byte
* free_contents
= NULL
;
1802 Elf_Internal_Sym
* intsyms
= NULL
;
1803 Elf_Internal_Sym
* free_intsyms
= NULL
;
1804 Elf_External_Sym_Shndx
* shndx_buf
= NULL
;
1810 int section_alignment_glue
;
1811 /* how much to scale the relocation by - 1, 2, or 4. */
1814 /* Assume nothing changes. */
1817 /* We don't have to do anything for a relocatable link, if
1818 this section does not have relocs, or if this is not a
1820 if (link_info
->relocatable
1821 || (sec
->flags
& SEC_RELOC
) == 0
1822 || sec
->reloc_count
== 0
1823 || (sec
->flags
& SEC_CODE
) == 0)
1826 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1827 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1829 sec_start
= sec
->output_section
->vma
+ sec
->output_offset
;
1831 /* Get the section contents. */
1832 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1833 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1834 /* Go get them off disk. */
1837 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1839 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1842 /* Read this BFD's symbols. */
1843 /* Get cached copy if it exists. */
1844 if (symtab_hdr
->contents
!= NULL
)
1845 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1848 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
1849 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
1852 if (shndx_hdr
->sh_size
!= 0)
1856 amt
= symtab_hdr
->sh_info
;
1857 amt
*= sizeof (Elf_External_Sym_Shndx
);
1858 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
1859 if (shndx_buf
== NULL
)
1861 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
1862 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
1864 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
1867 /* Get a copy of the native relocations. */
1868 internal_relocs
= (_bfd_elf_link_read_relocs
1869 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1870 link_info
->keep_memory
));
1871 if (internal_relocs
== NULL
)
1873 if (! link_info
->keep_memory
)
1874 free_relocs
= internal_relocs
;
1876 /* The RL_ relocs must be just before the operand relocs they go
1877 with, so we must sort them to guarantee this. We use bubblesort
1878 instead of qsort so we can guarantee that relocs with the same
1879 address remain in the same relative order. */
1880 reloc_bubblesort (internal_relocs
, sec
->reloc_count
);
1882 /* Walk through them looking for relaxing opportunities. */
1883 irelend
= internal_relocs
+ sec
->reloc_count
;
1885 /* This will either be NULL or a pointer to the next alignment
1887 next_alignment
= internal_relocs
;
1888 /* This will be the previous alignment, although at first it points
1889 to the first real relocation. */
1890 prev_alignment
= internal_relocs
;
1892 /* We calculate worst case shrinkage caused by alignment directives.
1893 No fool-proof, but better than either ignoring the problem or
1894 doing heavy duty analysis of all the alignment markers in all
1896 section_alignment_glue
= 0;
1897 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1898 if (ELF32_R_TYPE (irel
->r_info
) == R_RX_RH_RELAX
1899 && irel
->r_addend
& RX_RELAXA_ALIGN
)
1901 int this_glue
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
1903 if (section_alignment_glue
< this_glue
)
1904 section_alignment_glue
= this_glue
;
1906 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
1908 section_alignment_glue
*= 2;
1910 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1912 unsigned char *insn
;
1915 /* The insns we care about are all marked with one of these. */
1916 if (ELF32_R_TYPE (irel
->r_info
) != R_RX_RH_RELAX
)
1919 if (irel
->r_addend
& RX_RELAXA_ALIGN
1920 || next_alignment
== internal_relocs
)
1922 /* When we delete bytes, we need to maintain all the alignments
1923 indicated. In addition, we need to be careful about relaxing
1924 jumps across alignment boundaries - these displacements
1925 *grow* when we delete bytes. For now, don't shrink
1926 displacements across an alignment boundary, just in case.
1927 Note that this only affects relocations to the same
1929 prev_alignment
= next_alignment
;
1930 next_alignment
+= 2;
1931 while (next_alignment
< irelend
1932 && (ELF32_R_TYPE (next_alignment
->r_info
) != R_RX_RH_RELAX
1933 || !(next_alignment
->r_addend
& RX_RELAXA_ELIGN
)))
1935 if (next_alignment
>= irelend
|| next_alignment
->r_offset
== 0)
1936 next_alignment
= NULL
;
1939 /* When we hit alignment markers, see if we've shrunk enough
1940 before them to reduce the gap without violating the alignment
1942 if (irel
->r_addend
& RX_RELAXA_ALIGN
)
1944 /* At this point, the next relocation *should* be the ELIGN
1946 Elf_Internal_Rela
*erel
= irel
+ 1;
1947 unsigned int alignment
, nbytes
;
1949 if (ELF32_R_TYPE (erel
->r_info
) != R_RX_RH_RELAX
)
1951 if (!(erel
->r_addend
& RX_RELAXA_ELIGN
))
1954 alignment
= 1 << (irel
->r_addend
& RX_RELAXA_ANUM
);
1956 if (erel
->r_offset
- irel
->r_offset
< alignment
)
1959 nbytes
= erel
->r_offset
- irel
->r_offset
;
1960 nbytes
/= alignment
;
1961 nbytes
*= alignment
;
1963 elf32_rx_relax_delete_bytes (abfd
, sec
, erel
->r_offset
-nbytes
, nbytes
, next_alignment
,
1964 erel
->r_offset
== sec
->size
);
1970 if (irel
->r_addend
& RX_RELAXA_ELIGN
)
1973 insn
= contents
+ irel
->r_offset
;
1975 nrelocs
= irel
->r_addend
& RX_RELAXA_RNUM
;
1977 /* At this point, we have an insn that is a candidate for linker
1978 relaxation. There are NRELOCS relocs following that may be
1979 relaxed, although each reloc may be made of more than one
1980 reloc entry (such as gp-rel symbols). */
1982 /* Get the value of the symbol referred to by the reloc. Just
1983 in case this is the last reloc in the list, use the RL's
1984 addend to choose between this reloc (no addend) or the next
1985 (yes addend, which means at least one following reloc). */
1987 /* srel points to the "current" reloction for this insn -
1988 actually the last reloc for a given operand, which is the one
1989 we need to update. We check the relaxations in the same
1990 order that the relocations happen, so we'll just push it
1994 pc
= sec
->output_section
->vma
+ sec
->output_offset
1998 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
1999 pcrel = symval - pc + srel->r_addend; \
2002 #define SNIPNR(offset, nbytes) \
2003 elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0);
2004 #define SNIP(offset, nbytes, newtype) \
2005 SNIPNR (offset, nbytes); \
2006 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2008 /* The order of these bit tests must match the order that the
2009 relocs appear in. Since we sorted those by offset, we can
2012 /* Note that the numbers in, say, DSP6 are the bit offsets of
2013 the code fields that describe the operand. Bits number 0 for
2014 the MSB of insn[0]. */
2021 if (irel
->r_addend
& RX_RELAXA_DSP6
)
2026 if (code
== 2 && symval
/scale
<= 255)
2028 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2031 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2032 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2034 SNIP (3, 1, newrel
);
2039 else if (code
== 1 && symval
== 0)
2042 SNIP (2, 1, R_RX_NONE
);
2046 /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst. */
2047 else if (code
== 1 && symval
/scale
<= 31
2048 /* Decodable bits. */
2049 && (insn
[0] & 0xcc) == 0xcc
2051 && (insn
[0] & 0x30) != 3
2052 /* Register MSBs. */
2053 && (insn
[1] & 0x88) == 0x00)
2057 insn
[0] = 0x88 | (insn
[0] & 0x30);
2058 /* The register fields are in the right place already. */
2060 /* We can't relax this new opcode. */
2063 switch ((insn
[0] & 0x30) >> 4)
2066 newrel
= R_RX_RH_ABS5p5B
;
2069 newrel
= R_RX_RH_ABS5p5W
;
2072 newrel
= R_RX_RH_ABS5p5L
;
2076 move_reloc (irel
, srel
, -2);
2077 SNIP (2, 1, newrel
);
2080 /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst. */
2081 else if (code
== 1 && symval
/scale
<= 31
2082 /* Decodable bits. */
2083 && (insn
[0] & 0xf8) == 0x58
2084 /* Register MSBs. */
2085 && (insn
[1] & 0x88) == 0x00)
2089 insn
[0] = 0xb0 | ((insn
[0] & 0x04) << 1);
2090 /* The register fields are in the right place already. */
2092 /* We can't relax this new opcode. */
2095 switch ((insn
[0] & 0x08) >> 3)
2098 newrel
= R_RX_RH_ABS5p5B
;
2101 newrel
= R_RX_RH_ABS5p5W
;
2105 move_reloc (irel
, srel
, -2);
2106 SNIP (2, 1, newrel
);
2110 /* A DSP4 operand always follows a DSP6 operand, even if there's
2111 no relocation for it. We have to read the code out of the
2112 opcode to calculate the offset of the operand. */
2113 if (irel
->r_addend
& RX_RELAXA_DSP4
)
2115 int code6
, offset
= 0;
2119 code6
= insn
[0] & 0x03;
2122 case 0: offset
= 2; break;
2123 case 1: offset
= 3; break;
2124 case 2: offset
= 4; break;
2125 case 3: offset
= 2; break;
2128 code
= (insn
[0] & 0x0c) >> 2;
2130 if (code
== 2 && symval
/ scale
<= 255)
2132 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2136 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2137 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2139 SNIP (offset
+1, 1, newrel
);
2144 else if (code
== 1 && symval
== 0)
2147 SNIP (offset
, 1, R_RX_NONE
);
2150 /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2151 else if (code
== 1 && symval
/scale
<= 31
2152 /* Decodable bits. */
2153 && (insn
[0] & 0xc3) == 0xc3
2155 && (insn
[0] & 0x30) != 3
2156 /* Register MSBs. */
2157 && (insn
[1] & 0x88) == 0x00)
2161 insn
[0] = 0x80 | (insn
[0] & 0x30);
2162 /* The register fields are in the right place already. */
2164 /* We can't relax this new opcode. */
2167 switch ((insn
[0] & 0x30) >> 4)
2170 newrel
= R_RX_RH_ABS5p5B
;
2173 newrel
= R_RX_RH_ABS5p5W
;
2176 newrel
= R_RX_RH_ABS5p5L
;
2180 move_reloc (irel
, srel
, -2);
2181 SNIP (2, 1, newrel
);
2185 /* These always occur alone, but the offset depends on whether
2186 it's a MEMEX opcode (0x06) or not. */
2187 if (irel
->r_addend
& RX_RELAXA_DSP14
)
2192 if (insn
[0] == 0x06)
2199 if (code
== 2 && symval
/ scale
<= 255)
2201 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2205 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2206 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2208 SNIP (offset
, 1, newrel
);
2212 else if (code
== 1 && symval
== 0)
2215 SNIP (offset
, 1, R_RX_NONE
);
2226 /* These always occur alone. */
2227 if (irel
->r_addend
& RX_RELAXA_IMM6
)
2233 /* These relocations sign-extend, so we must do signed compares. */
2234 ssymval
= (long) symval
;
2236 code
= insn
[0] & 0x03;
2238 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2240 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2244 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2245 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2247 SNIP (2, 1, newrel
);
2252 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2254 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2258 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2259 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2261 SNIP (2, 1, newrel
);
2266 /* Special case UIMM8 format: CMP #uimm8,Rdst. */
2267 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2268 /* Decodable bits. */
2269 && (insn
[0] & 0xfc) == 0x74
2270 /* Decodable bits. */
2271 && ((insn
[1] & 0xf0) == 0x00))
2276 insn
[1] = 0x50 | (insn
[1] & 0x0f);
2278 /* We can't relax this new opcode. */
2281 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2282 newrel
= R_RX_ABS8U
;
2284 newrel
= R_RX_DIR8U
;
2286 SNIP (2, 1, newrel
);
2290 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2292 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2296 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2297 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2299 SNIP (2, 1, newrel
);
2304 /* Special case UIMM4 format: CMP, MUL, AND, OR. */
2305 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2306 /* Decodable bits and immediate type. */
2308 /* Decodable bits. */
2309 && (insn
[1] & 0xc0) == 0x00)
2311 static const int newop
[4] = { 1, 3, 4, 5 };
2313 insn
[0] = 0x60 | newop
[insn
[1] >> 4];
2314 /* The register number doesn't move. */
2316 /* We can't relax this new opcode. */
2319 move_reloc (irel
, srel
, -1);
2321 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2325 /* Special case UIMM4 format: ADD -> ADD/SUB. */
2326 else if (code
== 1 && ssymval
<= 15 && ssymval
>= -15
2327 /* Decodable bits and immediate type. */
2329 /* Same register for source and destination. */
2330 && ((insn
[1] >> 4) == (insn
[1] & 0x0f)))
2334 /* Note that we can't turn "add $0,Rs" into a NOP
2335 because the flags need to be set right. */
2339 insn
[0] = 0x60; /* Subtract. */
2340 newrel
= R_RX_RH_UNEG4p8
;
2344 insn
[0] = 0x62; /* Add. */
2345 newrel
= R_RX_RH_UIMM4p8
;
2348 /* The register number is in the right place. */
2350 /* We can't relax this new opcode. */
2353 move_reloc (irel
, srel
, -1);
2355 SNIP (2, 1, newrel
);
2360 /* These are either matched with a DSP6 (2-byte base) or an id24
2362 if (irel
->r_addend
& RX_RELAXA_IMM12
)
2364 int dspcode
, offset
= 0;
2369 if ((insn
[0] & 0xfc) == 0xfc)
2370 dspcode
= 1; /* Just something with one byte operand. */
2372 dspcode
= insn
[0] & 3;
2375 case 0: offset
= 2; break;
2376 case 1: offset
= 3; break;
2377 case 2: offset
= 4; break;
2378 case 3: offset
= 2; break;
2381 /* These relocations sign-extend, so we must do signed compares. */
2382 ssymval
= (long) symval
;
2384 code
= (insn
[1] >> 2) & 3;
2385 if (code
== 0 && ssymval
<= 8388607 && ssymval
>= -8388608)
2387 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2391 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2392 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2394 SNIP (offset
, 1, newrel
);
2399 else if (code
== 3 && ssymval
<= 32767 && ssymval
>= -32768)
2401 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2405 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2406 if (newrel
!= ELF32_R_TYPE (srel
->r_info
))
2408 SNIP (offset
, 1, newrel
);
2413 /* Special case UIMM8 format: MOV #uimm8,Rdst. */
2414 else if (code
== 2 && ssymval
<= 255 && ssymval
>= 16
2415 /* Decodable bits. */
2417 /* Decodable bits. */
2418 && ((insn
[1] & 0x03) == 0x02))
2423 insn
[1] = 0x40 | (insn
[1] >> 4);
2425 /* We can't relax this new opcode. */
2428 if (STACK_REL_P (ELF32_R_TYPE (srel
->r_info
)))
2429 newrel
= R_RX_ABS8U
;
2431 newrel
= R_RX_DIR8U
;
2433 SNIP (2, 1, newrel
);
2437 else if (code
== 2 && ssymval
<= 127 && ssymval
>= -128)
2439 unsigned int newrel
= ELF32_R_TYPE(srel
->r_info
);
2443 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2444 if (newrel
!= ELF32_R_TYPE(srel
->r_info
))
2446 SNIP (offset
, 1, newrel
);
2451 /* Special case UIMM4 format: MOV #uimm4,Rdst. */
2452 else if (code
== 1 && ssymval
<= 15 && ssymval
>= 0
2453 /* Decodable bits. */
2455 /* Decodable bits. */
2456 && ((insn
[1] & 0x03) == 0x02))
2459 insn
[1] = insn
[1] >> 4;
2461 /* We can't relax this new opcode. */
2464 move_reloc (irel
, srel
, -1);
2466 SNIP (2, 1, R_RX_RH_UIMM4p8
);
2471 if (irel
->r_addend
& RX_RELAXA_BRA
)
2473 unsigned int newrel
= ELF32_R_TYPE (srel
->r_info
);
2475 int alignment_glue
= 0;
2479 /* Branches over alignment chunks are problematic, as
2480 deleting bytes here makes the branch *further* away. We
2481 can be agressive with branches within this alignment
2482 block, but not branches outside it. */
2483 if ((prev_alignment
== NULL
2484 || symval
< (bfd_vma
)(sec_start
+ prev_alignment
->r_offset
))
2485 && (next_alignment
== NULL
2486 || symval
> (bfd_vma
)(sec_start
+ next_alignment
->r_offset
)))
2487 alignment_glue
= section_alignment_glue
;
2489 if (ELF32_R_TYPE(srel
[1].r_info
) == R_RX_RH_RELAX
2490 && srel
[1].r_addend
& RX_RELAXA_BRA
2491 && srel
[1].r_offset
< irel
->r_offset
+ pcrel
)
2494 newrel
= next_smaller_reloc (ELF32_R_TYPE (srel
->r_info
));
2496 /* The values we compare PCREL with are not what you'd
2497 expect; they're off by a little to compensate for (1)
2498 where the reloc is relative to the insn, and (2) how much
2499 the insn is going to change when we relax it. */
2501 /* These we have to decode. */
2504 case 0x04: /* BRA pcdsp:24 */
2505 if (-32768 + alignment_glue
<= pcrel
2506 && pcrel
<= 32765 - alignment_glue
)
2509 SNIP (3, 1, newrel
);
2514 case 0x38: /* BRA pcdsp:16 */
2515 if (-128 + alignment_glue
<= pcrel
2516 && pcrel
<= 127 - alignment_glue
)
2519 SNIP (2, 1, newrel
);
2524 case 0x2e: /* BRA pcdsp:8 */
2525 /* Note that there's a risk here of shortening things so
2526 much that we no longer fit this reloc; it *should*
2527 only happen when you branch across a branch, and that
2528 branch also devolves into BRA.S. "Real" code should
2530 if (max_pcrel3
+ alignment_glue
<= pcrel
2531 && pcrel
<= 10 - alignment_glue
2535 SNIP (1, 1, newrel
);
2536 move_reloc (irel
, srel
, -1);
2541 case 0x05: /* BSR pcdsp:24 */
2542 if (-32768 + alignment_glue
<= pcrel
2543 && pcrel
<= 32765 - alignment_glue
)
2546 SNIP (1, 1, newrel
);
2551 case 0x3a: /* BEQ.W pcdsp:16 */
2552 case 0x3b: /* BNE.W pcdsp:16 */
2553 if (-128 + alignment_glue
<= pcrel
2554 && pcrel
<= 127 - alignment_glue
)
2556 insn
[0] = 0x20 | (insn
[0] & 1);
2557 SNIP (1, 1, newrel
);
2562 case 0x20: /* BEQ.B pcdsp:8 */
2563 case 0x21: /* BNE.B pcdsp:8 */
2564 if (max_pcrel3
+ alignment_glue
<= pcrel
2565 && pcrel
- alignment_glue
<= 10
2568 insn
[0] = 0x10 | ((insn
[0] & 1) << 3);
2569 SNIP (1, 1, newrel
);
2570 move_reloc (irel
, srel
, -1);
2575 case 0x16: /* synthetic BNE dsp24 */
2576 case 0x1e: /* synthetic BEQ dsp24 */
2577 if (-32767 + alignment_glue
<= pcrel
2578 && pcrel
<= 32766 - alignment_glue
2581 if (insn
[0] == 0x16)
2585 /* We snip out the bytes at the end else the reloc
2586 will get moved too, and too much. */
2587 SNIP (3, 2, newrel
);
2588 move_reloc (irel
, srel
, -1);
2594 /* Special case - synthetic conditional branches, pcrel24.
2595 Note that EQ and NE have been handled above. */
2596 if ((insn
[0] & 0xf0) == 0x20
2599 && srel
->r_offset
!= irel
->r_offset
+ 1
2600 && -32767 + alignment_glue
<= pcrel
2601 && pcrel
<= 32766 - alignment_glue
)
2605 SNIP (5, 1, newrel
);
2609 /* Special case - synthetic conditional branches, pcrel16 */
2610 if ((insn
[0] & 0xf0) == 0x20
2613 && srel
->r_offset
!= irel
->r_offset
+ 1
2614 && -127 + alignment_glue
<= pcrel
2615 && pcrel
<= 126 - alignment_glue
)
2617 int cond
= (insn
[0] & 0x0f) ^ 0x01;
2619 insn
[0] = 0x20 | cond
;
2620 /* By moving the reloc first, we avoid having
2621 delete_bytes move it also. */
2622 move_reloc (irel
, srel
, -2);
2623 SNIP (2, 3, newrel
);
2628 BFD_ASSERT (nrelocs
== 0);
2630 /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2631 use MOV.bwl #uimm:8, dsp:5[r7] format. This is tricky
2632 because it may have one or two relocations. */
2633 if ((insn
[0] & 0xfc) == 0xf8
2634 && (insn
[1] & 0x80) == 0x00
2635 && (insn
[0] & 0x03) != 0x03)
2637 int dcode
, icode
, reg
, ioff
, dscale
, ilen
;
2638 bfd_vma disp_val
= 0;
2640 Elf_Internal_Rela
* disp_rel
= 0;
2641 Elf_Internal_Rela
* imm_rel
= 0;
2646 dcode
= insn
[0] & 0x03;
2647 icode
= (insn
[1] >> 2) & 0x03;
2648 reg
= (insn
[1] >> 4) & 0x0f;
2650 ioff
= dcode
== 1 ? 3 : dcode
== 2 ? 4 : 2;
2652 /* Figure out what the dispacement is. */
2653 if (dcode
== 1 || dcode
== 2)
2655 /* There's a displacement. See if there's a reloc for it. */
2656 if (srel
[1].r_offset
== irel
->r_offset
+ 2)
2668 #if RX_OPCODE_BIG_ENDIAN
2669 disp_val
= insn
[2] * 256 + insn
[3];
2671 disp_val
= insn
[2] + insn
[3] * 256;
2674 switch (insn
[1] & 3)
2690 /* Figure out what the immediate is. */
2691 if (srel
[1].r_offset
== irel
->r_offset
+ ioff
)
2694 imm_val
= (long) symval
;
2699 unsigned char * ip
= insn
+ ioff
;
2704 /* For byte writes, we don't sign extend. Makes the math easier later. */
2708 imm_val
= (char) ip
[0];
2711 #if RX_OPCODE_BIG_ENDIAN
2712 imm_val
= ((char) ip
[0] << 8) | ip
[1];
2714 imm_val
= ((char) ip
[1] << 8) | ip
[0];
2718 #if RX_OPCODE_BIG_ENDIAN
2719 imm_val
= ((char) ip
[0] << 16) | (ip
[1] << 8) | ip
[2];
2721 imm_val
= ((char) ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2725 #if RX_OPCODE_BIG_ENDIAN
2726 imm_val
= (ip
[0] << 24) | (ip
[1] << 16) | (ip
[2] << 8) | ip
[3];
2728 imm_val
= (ip
[3] << 24) | (ip
[2] << 16) | (ip
[1] << 8) | ip
[0];
2762 /* The shortcut happens when the immediate is 0..255,
2763 register r0 to r7, and displacement (scaled) 0..31. */
2765 if (0 <= imm_val
&& imm_val
<= 255
2766 && 0 <= reg
&& reg
<= 7
2767 && disp_val
/ dscale
<= 31)
2769 insn
[0] = 0x3c | (insn
[1] & 0x03);
2770 insn
[1] = (((disp_val
/ dscale
) << 3) & 0x80) | (reg
<< 4) | ((disp_val
/dscale
) & 0x0f);
2775 int newrel
= R_RX_NONE
;
2780 newrel
= R_RX_RH_ABS5p8B
;
2783 newrel
= R_RX_RH_ABS5p8W
;
2786 newrel
= R_RX_RH_ABS5p8L
;
2789 disp_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (disp_rel
->r_info
), newrel
);
2790 move_reloc (irel
, disp_rel
, -1);
2794 imm_rel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (imm_rel
->r_info
), R_RX_DIR8U
);
2795 move_reloc (disp_rel
? disp_rel
: irel
,
2797 irel
->r_offset
- imm_rel
->r_offset
+ 2);
2800 SNIPNR (3, ilen
- 3);
2803 /* We can't relax this new opcode. */
2809 /* We can't reliably relax branches to DIR3U_PCREL unless we know
2810 whatever they're branching over won't shrink any more. If we're
2811 basically done here, do one more pass just for branches - but
2812 don't request a pass after that one! */
2813 if (!*again
&& !allow_pcrel3
)
2815 bfd_boolean ignored
;
2817 elf32_rx_relax_section (abfd
, sec
, link_info
, &ignored
, TRUE
);
2823 if (free_relocs
!= NULL
)
2826 if (free_contents
!= NULL
)
2827 free (free_contents
);
2829 if (shndx_buf
!= NULL
)
2831 shndx_hdr
->contents
= NULL
;
2835 if (free_intsyms
!= NULL
)
2836 free (free_intsyms
);
2842 elf32_rx_relax_section_wrapper (bfd
* abfd
,
2844 struct bfd_link_info
* link_info
,
2845 bfd_boolean
* again
)
2847 return elf32_rx_relax_section (abfd
, sec
, link_info
, again
, FALSE
);
2850 /* Function to set the ELF flag bits. */
2853 rx_elf_set_private_flags (bfd
* abfd
, flagword flags
)
2855 elf_elfheader (abfd
)->e_flags
= flags
;
2856 elf_flags_init (abfd
) = TRUE
;
2860 static bfd_boolean no_warn_mismatch
= FALSE
;
2862 void bfd_elf32_rx_set_target_flags (bfd_boolean
);
2865 bfd_elf32_rx_set_target_flags (bfd_boolean user_no_warn_mismatch
)
2867 no_warn_mismatch
= user_no_warn_mismatch
;
2870 /* Merge backend specific data from an object file to the output
2871 object file when linking. */
2874 rx_elf_merge_private_bfd_data (bfd
* ibfd
, bfd
* obfd
)
2878 bfd_boolean error
= FALSE
;
2880 new_flags
= elf_elfheader (ibfd
)->e_flags
;
2881 old_flags
= elf_elfheader (obfd
)->e_flags
;
2883 if (!elf_flags_init (obfd
))
2885 /* First call, no flags set. */
2886 elf_flags_init (obfd
) = TRUE
;
2887 elf_elfheader (obfd
)->e_flags
= new_flags
;
2889 else if (old_flags
!= new_flags
)
2891 flagword known_flags
= E_FLAG_RX_64BIT_DOUBLES
| E_FLAG_RX_DSP
;
2893 if ((old_flags
^ new_flags
) & known_flags
)
2895 /* Only complain if flag bits we care about do not match.
2896 Other bits may be set, since older binaries did use some
2897 deprecated flags. */
2898 if (no_warn_mismatch
)
2900 elf_elfheader (obfd
)->e_flags
= (new_flags
| old_flags
) & known_flags
;
2904 (*_bfd_error_handler
)
2905 ("ELF header flags mismatch: old_flags = 0x%.8lx, new_flags = 0x%.8lx, filename = %s",
2906 old_flags
, new_flags
, bfd_get_filename (ibfd
));
2911 elf_elfheader (obfd
)->e_flags
= new_flags
& known_flags
;
2915 bfd_set_error (bfd_error_bad_value
);
2921 rx_elf_print_private_bfd_data (bfd
* abfd
, void * ptr
)
2923 FILE * file
= (FILE *) ptr
;
2926 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
2928 /* Print normal ELF private data. */
2929 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
2931 flags
= elf_elfheader (abfd
)->e_flags
;
2932 fprintf (file
, _("private flags = 0x%lx:"), (long) flags
);
2934 if (flags
& E_FLAG_RX_64BIT_DOUBLES
)
2935 fprintf (file
, _(" [64-bit doubles]"));
2936 if (flags
& E_FLAG_RX_DSP
)
2937 fprintf (file
, _(" [dsp]"));
2943 /* Return the MACH for an e_flags value. */
2946 elf32_rx_machine (bfd
* abfd
)
2948 if ((elf_elfheader (abfd
)->e_flags
& EF_RX_CPU_MASK
) == EF_RX_CPU_RX
)
2955 rx_elf_object_p (bfd
* abfd
)
2957 bfd_default_set_arch_mach (abfd
, bfd_arch_rx
,
2958 elf32_rx_machine (abfd
));
2965 dump_symtab (bfd
* abfd
, void * internal_syms
, void * external_syms
)
2968 Elf_Internal_Sym
* isymbuf
;
2969 Elf_Internal_Sym
* isymend
;
2970 Elf_Internal_Sym
* isym
;
2971 Elf_Internal_Shdr
* symtab_hdr
;
2972 bfd_boolean free_internal
= FALSE
, free_external
= FALSE
;
2974 char * st_info_stb_str
;
2975 char * st_other_str
;
2976 char * st_shndx_str
;
2978 if (! internal_syms
)
2980 internal_syms
= bfd_malloc (1000);
2983 if (! external_syms
)
2985 external_syms
= bfd_malloc (1000);
2989 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2990 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2992 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2993 symtab_hdr
->sh_info
, 0,
2994 internal_syms
, external_syms
, NULL
);
2996 isymbuf
= internal_syms
;
2997 isymend
= isymbuf
+ locsymcount
;
2999 for (isym
= isymbuf
; isym
< isymend
; isym
++)
3001 switch (ELF_ST_TYPE (isym
->st_info
))
3003 case STT_FUNC
: st_info_str
= "STT_FUNC";
3004 case STT_SECTION
: st_info_str
= "STT_SECTION";
3005 case STT_FILE
: st_info_str
= "STT_FILE";
3006 case STT_OBJECT
: st_info_str
= "STT_OBJECT";
3007 case STT_TLS
: st_info_str
= "STT_TLS";
3008 default: st_info_str
= "";
3010 switch (ELF_ST_BIND (isym
->st_info
))
3012 case STB_LOCAL
: st_info_stb_str
= "STB_LOCAL";
3013 case STB_GLOBAL
: st_info_stb_str
= "STB_GLOBAL";
3014 default: st_info_stb_str
= "";
3016 switch (ELF_ST_VISIBILITY (isym
->st_other
))
3018 case STV_DEFAULT
: st_other_str
= "STV_DEFAULT";
3019 case STV_INTERNAL
: st_other_str
= "STV_INTERNAL";
3020 case STV_PROTECTED
: st_other_str
= "STV_PROTECTED";
3021 default: st_other_str
= "";
3023 switch (isym
->st_shndx
)
3025 case SHN_ABS
: st_shndx_str
= "SHN_ABS";
3026 case SHN_COMMON
: st_shndx_str
= "SHN_COMMON";
3027 case SHN_UNDEF
: st_shndx_str
= "SHN_UNDEF";
3028 default: st_shndx_str
= "";
3031 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3032 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3034 (unsigned long) isym
->st_value
,
3035 (unsigned long) isym
->st_size
,
3037 bfd_elf_string_from_elf_section (abfd
, symtab_hdr
->sh_link
,
3039 isym
->st_info
, st_info_str
, st_info_stb_str
,
3040 isym
->st_other
, st_other_str
,
3041 isym
->st_shndx
, st_shndx_str
);
3044 free (internal_syms
);
3046 free (external_syms
);
3050 rx_get_reloc (long reloc
)
3052 if (0 <= reloc
&& reloc
< R_RX_max
)
3053 return rx_elf_howto_table
[reloc
].name
;
3059 /* We must take care to keep the on-disk copy of any code sections
3060 that are fully linked swapped if the target is big endian, to match
3061 the Renesas tools. */
3063 /* The rule is: big endian object that are final-link executables,
3064 have code sections stored with 32-bit words swapped relative to
3065 what you'd get by default. */
3068 rx_get_section_contents (bfd
* abfd
,
3072 bfd_size_type count
)
3074 int exec
= (abfd
->flags
& EXEC_P
) ? 1 : 0;
3075 int s_code
= (section
->flags
& SEC_CODE
) ? 1 : 0;
3079 fprintf (stderr
, "dj: get %ld %ld from %s %s e%d sc%d %08lx:%08lx\n",
3080 (long) offset
, (long) count
, section
->name
,
3081 bfd_big_endian(abfd
) ? "be" : "le",
3082 exec
, s_code
, (long unsigned) section
->filepos
,
3083 (long unsigned) offset
);
3086 if (exec
&& s_code
&& bfd_big_endian (abfd
))
3088 char * cloc
= (char *) location
;
3089 bfd_size_type cnt
, end_cnt
;
3093 /* Fetch and swap unaligned bytes at the beginning. */
3098 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3103 bfd_putb32 (bfd_getl32 (buf
), buf
);
3105 cnt
= 4 - (offset
% 4);
3109 memcpy (location
, buf
+ (offset
% 4), cnt
);
3116 end_cnt
= count
% 4;
3118 /* Fetch and swap the middle bytes. */
3121 rv
= _bfd_generic_get_section_contents (abfd
, section
, cloc
, offset
,
3126 for (cnt
= count
; cnt
>= 4; cnt
-= 4, cloc
+= 4)
3127 bfd_putb32 (bfd_getl32 (cloc
), cloc
);
3130 /* Fetch and swap the end bytes. */
3135 /* Fetch the end bytes. */
3136 rv
= _bfd_generic_get_section_contents (abfd
, section
, buf
,
3137 offset
+ count
- end_cnt
, 4);
3141 bfd_putb32 (bfd_getl32 (buf
), buf
);
3142 memcpy (cloc
, buf
, end_cnt
);
3146 rv
= _bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
);
3153 rx2_set_section_contents (bfd
* abfd
,
3155 const void * location
,
3157 bfd_size_type count
)
3161 fprintf (stderr
, " set sec %s %08x loc %p offset %#x count %#x\n",
3162 section
->name
, (unsigned) section
->vma
, location
, (int) offset
, (int) count
);
3163 for (i
= 0; i
< count
; i
++)
3165 if (i
% 16 == 0 && i
> 0)
3166 fprintf (stderr
, "\n");
3168 if (i
% 16 && i
% 4 == 0)
3169 fprintf (stderr
, " ");
3172 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3174 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3176 fprintf (stderr
, "\n");
3178 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3180 #define _bfd_elf_set_section_contents rx2_set_section_contents
3184 rx_set_section_contents (bfd
* abfd
,
3186 const void * location
,
3188 bfd_size_type count
)
3190 bfd_boolean exec
= (abfd
->flags
& EXEC_P
) ? TRUE
: FALSE
;
3191 bfd_boolean s_code
= (section
->flags
& SEC_CODE
) ? TRUE
: FALSE
;
3193 char * swapped_data
= NULL
;
3195 bfd_vma caddr
= section
->vma
+ offset
;
3197 bfd_size_type scount
;
3202 fprintf (stderr
, "\ndj: set %ld %ld to %s %s e%d sc%d\n",
3203 (long) offset
, (long) count
, section
->name
,
3204 bfd_big_endian (abfd
) ? "be" : "le",
3207 for (i
= 0; i
< count
; i
++)
3209 int a
= section
->vma
+ offset
+ i
;
3211 if (a
% 16 == 0 && a
> 0)
3212 fprintf (stderr
, "\n");
3214 if (a
% 16 && a
% 4 == 0)
3215 fprintf (stderr
, " ");
3217 if (a
% 16 == 0 || i
== 0)
3218 fprintf (stderr
, " %08x:", (int) (section
->vma
+ offset
+ i
));
3220 fprintf (stderr
, " %02x", ((unsigned char *) location
)[i
]);
3223 fprintf (stderr
, "\n");
3226 if (! exec
|| ! s_code
|| ! bfd_big_endian (abfd
))
3227 return _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
);
3229 while (count
> 0 && caddr
> 0 && caddr
% 4)
3233 case 0: faddr
= offset
+ 3; break;
3234 case 1: faddr
= offset
+ 1; break;
3235 case 2: faddr
= offset
- 1; break;
3236 case 3: faddr
= offset
- 3; break;
3239 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3249 scount
= (int)(count
/ 4) * 4;
3252 char * cloc
= (char *) location
;
3254 swapped_data
= (char *) bfd_alloc (abfd
, count
);
3256 for (i
= 0; i
< count
; i
+= 4)
3258 bfd_vma v
= bfd_getl32 (cloc
+ i
);
3259 bfd_putb32 (v
, swapped_data
+ i
);
3262 rv
= _bfd_elf_set_section_contents (abfd
, section
, swapped_data
, offset
, scount
);
3274 caddr
= section
->vma
+ offset
;
3279 case 0: faddr
= offset
+ 3; break;
3280 case 1: faddr
= offset
+ 1; break;
3281 case 2: faddr
= offset
- 1; break;
3282 case 3: faddr
= offset
- 3; break;
3284 rv
= _bfd_elf_set_section_contents (abfd
, section
, location
, faddr
, 1);
3299 rx_final_link (bfd
* abfd
, struct bfd_link_info
* info
)
3303 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3306 fprintf (stderr
, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3307 o
->name
, o
->flags
, o
->vma
, o
->lma
, o
->size
, o
->rawsize
);
3309 if (o
->flags
& SEC_CODE
3310 && bfd_big_endian (abfd
)
3311 && (o
->size
% 4 || o
->rawsize
% 4))
3314 fprintf (stderr
, "adjusting...\n");
3316 o
->size
+= 4 - (o
->size
% 4);
3317 o
->rawsize
+= 4 - (o
->rawsize
% 4);
3321 return bfd_elf_final_link (abfd
, info
);
3325 elf32_rx_modify_program_headers (bfd
* abfd ATTRIBUTE_UNUSED
,
3326 struct bfd_link_info
* info ATTRIBUTE_UNUSED
)
3328 const struct elf_backend_data
* bed
;
3329 struct elf_obj_tdata
* tdata
;
3330 Elf_Internal_Phdr
* phdr
;
3334 bed
= get_elf_backend_data (abfd
);
3335 tdata
= elf_tdata (abfd
);
3337 count
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
3339 for (i
= count
; i
-- != 0; )
3340 if (phdr
[i
].p_type
== PT_LOAD
)
3342 /* The Renesas tools expect p_paddr to be zero. However,
3343 there is no other way to store the writable data in ROM for
3344 startup initialization. So, we let the linker *think*
3345 we're using paddr and vaddr the "usual" way, but at the
3346 last minute we move the paddr into the vaddr (which is what
3347 the simulator uses) and zero out paddr. Note that this
3348 does not affect the section headers, just the program
3349 headers. We hope. */
3350 phdr
[i
].p_vaddr
= phdr
[i
].p_paddr
;
3351 /* If we zero out p_paddr, then the LMA in the section table
3353 /*phdr[i].p_paddr = 0;*/
3359 #define ELF_ARCH bfd_arch_rx
3360 #define ELF_MACHINE_CODE EM_RX
3361 #define ELF_MAXPAGESIZE 0x1000
3363 #define TARGET_BIG_SYM bfd_elf32_rx_be_vec
3364 #define TARGET_BIG_NAME "elf32-rx-be"
3366 #define TARGET_LITTLE_SYM bfd_elf32_rx_le_vec
3367 #define TARGET_LITTLE_NAME "elf32-rx-le"
3369 #define elf_info_to_howto_rel NULL
3370 #define elf_info_to_howto rx_info_to_howto_rela
3371 #define elf_backend_object_p rx_elf_object_p
3372 #define elf_backend_relocate_section rx_elf_relocate_section
3373 #define elf_symbol_leading_char ('_')
3374 #define elf_backend_can_gc_sections 1
3375 #define elf_backend_modify_program_headers elf32_rx_modify_program_headers
3377 #define bfd_elf32_bfd_reloc_type_lookup rx_reloc_type_lookup
3378 #define bfd_elf32_bfd_reloc_name_lookup rx_reloc_name_lookup
3379 #define bfd_elf32_bfd_set_private_flags rx_elf_set_private_flags
3380 #define bfd_elf32_bfd_merge_private_bfd_data rx_elf_merge_private_bfd_data
3381 #define bfd_elf32_bfd_print_private_bfd_data rx_elf_print_private_bfd_data
3382 #define bfd_elf32_get_section_contents rx_get_section_contents
3383 #define bfd_elf32_set_section_contents rx_set_section_contents
3384 #define bfd_elf32_bfd_final_link rx_final_link
3385 #define bfd_elf32_bfd_relax_section elf32_rx_relax_section_wrapper
3387 #include "elf32-target.h"