1 /* Renesas RL78 specific support for 32-bit ELF.
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. */
23 #include "bfd_stdint.h"
27 #include "libiberty.h"
29 #define valid_16bit_address(v) ((v) <= 0x0ffff || (v) >= 0xf0000)
31 #define RL78REL(n,sz,bit,shift,complain,pcrel) \
32 HOWTO (R_RL78_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
33 bfd_elf_generic_reloc, "R_RL78_" #n, FALSE, 0, ~0, FALSE)
35 /* Note that the relocations around 0x7f are internal to this file;
36 feel free to move them as needed to avoid conflicts with published
37 relocation numbers. */
39 static reloc_howto_type rl78_elf_howto_table
[] =
41 RL78REL (NONE
, 0, 0, 0, dont
, FALSE
),
42 RL78REL (DIR32
, 2, 32, 0, signed, FALSE
),
43 RL78REL (DIR24S
, 2, 24, 0, signed, FALSE
),
44 RL78REL (DIR16
, 1, 16, 0, dont
, FALSE
),
45 RL78REL (DIR16U
, 1, 16, 0, unsigned, FALSE
),
46 RL78REL (DIR16S
, 1, 16, 0, signed, FALSE
),
47 RL78REL (DIR8
, 0, 8, 0, dont
, FALSE
),
48 RL78REL (DIR8U
, 0, 8, 0, unsigned, FALSE
),
49 RL78REL (DIR8S
, 0, 8, 0, signed, FALSE
),
50 RL78REL (DIR24S_PCREL
, 2, 24, 0, signed, TRUE
),
51 RL78REL (DIR16S_PCREL
, 1, 16, 0, signed, TRUE
),
52 RL78REL (DIR8S_PCREL
, 0, 8, 0, signed, TRUE
),
53 RL78REL (DIR16UL
, 1, 16, 2, unsigned, FALSE
),
54 RL78REL (DIR16UW
, 1, 16, 1, unsigned, FALSE
),
55 RL78REL (DIR8UL
, 0, 8, 2, unsigned, FALSE
),
56 RL78REL (DIR8UW
, 0, 8, 1, unsigned, FALSE
),
57 RL78REL (DIR32_REV
, 1, 16, 0, dont
, FALSE
),
58 RL78REL (DIR16_REV
, 1, 16, 0, dont
, FALSE
),
59 RL78REL (DIR3U_PCREL
, 0, 3, 0, dont
, TRUE
),
88 RL78REL (RH_RELAX
, 0, 0, 0, dont
, FALSE
),
110 RL78REL (ABS32
, 2, 32, 0, dont
, FALSE
),
111 RL78REL (ABS24S
, 2, 24, 0, signed, FALSE
),
112 RL78REL (ABS16
, 1, 16, 0, dont
, FALSE
),
113 RL78REL (ABS16U
, 1, 16, 0, unsigned, FALSE
),
114 RL78REL (ABS16S
, 1, 16, 0, signed, FALSE
),
115 RL78REL (ABS8
, 0, 8, 0, dont
, FALSE
),
116 RL78REL (ABS8U
, 0, 8, 0, unsigned, FALSE
),
117 RL78REL (ABS8S
, 0, 8, 0, signed, FALSE
),
118 RL78REL (ABS24S_PCREL
, 2, 24, 0, signed, TRUE
),
119 RL78REL (ABS16S_PCREL
, 1, 16, 0, signed, TRUE
),
120 RL78REL (ABS8S_PCREL
, 0, 8, 0, signed, TRUE
),
121 RL78REL (ABS16UL
, 1, 16, 0, unsigned, FALSE
),
122 RL78REL (ABS16UW
, 1, 16, 0, unsigned, FALSE
),
123 RL78REL (ABS8UL
, 0, 8, 0, unsigned, FALSE
),
124 RL78REL (ABS8UW
, 0, 8, 0, unsigned, FALSE
),
125 RL78REL (ABS32_REV
, 2, 32, 0, dont
, FALSE
),
126 RL78REL (ABS16_REV
, 1, 16, 0, dont
, FALSE
),
128 #define STACK_REL_P(x) ((x) <= R_RL78_ABS16_REV && (x) >= R_RL78_ABS32)
178 RL78REL (SYM
, 2, 32, 0, dont
, FALSE
),
179 RL78REL (OPneg
, 2, 32, 0, dont
, FALSE
),
180 RL78REL (OPadd
, 2, 32, 0, dont
, FALSE
),
181 RL78REL (OPsub
, 2, 32, 0, dont
, FALSE
),
182 RL78REL (OPmul
, 2, 32, 0, dont
, FALSE
),
183 RL78REL (OPdiv
, 2, 32, 0, dont
, FALSE
),
184 RL78REL (OPshla
, 2, 32, 0, dont
, FALSE
),
185 RL78REL (OPshra
, 2, 32, 0, dont
, FALSE
),
186 RL78REL (OPsctsize
, 2, 32, 0, dont
, FALSE
),
191 RL78REL (OPscttop
, 2, 32, 0, dont
, FALSE
),
194 RL78REL (OPand
, 2, 32, 0, dont
, FALSE
),
195 RL78REL (OPor
, 2, 32, 0, dont
, FALSE
),
196 RL78REL (OPxor
, 2, 32, 0, dont
, FALSE
),
197 RL78REL (OPnot
, 2, 32, 0, dont
, FALSE
),
198 RL78REL (OPmod
, 2, 32, 0, dont
, FALSE
),
199 RL78REL (OPromtop
, 2, 32, 0, dont
, FALSE
),
200 RL78REL (OPramtop
, 2, 32, 0, dont
, FALSE
)
203 /* Map BFD reloc types to RL78 ELF reloc types. */
205 struct rl78_reloc_map
207 bfd_reloc_code_real_type bfd_reloc_val
;
208 unsigned int rl78_reloc_val
;
211 static const struct rl78_reloc_map rl78_reloc_map
[] =
213 { BFD_RELOC_NONE
, R_RL78_NONE
},
214 { BFD_RELOC_8
, R_RL78_DIR8S
},
215 { BFD_RELOC_16
, R_RL78_DIR16S
},
216 { BFD_RELOC_24
, R_RL78_DIR24S
},
217 { BFD_RELOC_32
, R_RL78_DIR32
},
218 { BFD_RELOC_RL78_16_OP
, R_RL78_DIR16
},
219 { BFD_RELOC_RL78_DIR3U_PCREL
, R_RL78_DIR3U_PCREL
},
220 { BFD_RELOC_8_PCREL
, R_RL78_DIR8S_PCREL
},
221 { BFD_RELOC_16_PCREL
, R_RL78_DIR16S_PCREL
},
222 { BFD_RELOC_24_PCREL
, R_RL78_DIR24S_PCREL
},
223 { BFD_RELOC_RL78_8U
, R_RL78_DIR8U
},
224 { BFD_RELOC_RL78_16U
, R_RL78_DIR16U
},
225 { BFD_RELOC_RL78_SYM
, R_RL78_SYM
},
226 { BFD_RELOC_RL78_OP_SUBTRACT
, R_RL78_OPsub
},
227 { BFD_RELOC_RL78_OP_NEG
, R_RL78_OPneg
},
228 { BFD_RELOC_RL78_OP_AND
, R_RL78_OPand
},
229 { BFD_RELOC_RL78_OP_SHRA
, R_RL78_OPshra
},
230 { BFD_RELOC_RL78_ABS8
, R_RL78_ABS8
},
231 { BFD_RELOC_RL78_ABS16
, R_RL78_ABS16
},
232 { BFD_RELOC_RL78_ABS16_REV
, R_RL78_ABS16_REV
},
233 { BFD_RELOC_RL78_ABS32
, R_RL78_ABS32
},
234 { BFD_RELOC_RL78_ABS32_REV
, R_RL78_ABS32_REV
},
235 { BFD_RELOC_RL78_ABS16UL
, R_RL78_ABS16UL
},
236 { BFD_RELOC_RL78_ABS16UW
, R_RL78_ABS16UW
},
237 { BFD_RELOC_RL78_ABS16U
, R_RL78_ABS16U
},
238 { BFD_RELOC_RL78_RELAX
, R_RL78_RH_RELAX
}
241 static reloc_howto_type
*
242 rl78_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
243 bfd_reloc_code_real_type code
)
247 if (code
== BFD_RELOC_RL78_32_OP
)
248 return rl78_elf_howto_table
+ R_RL78_DIR32
;
250 for (i
= ARRAY_SIZE (rl78_reloc_map
); --i
;)
251 if (rl78_reloc_map
[i
].bfd_reloc_val
== code
)
252 return rl78_elf_howto_table
+ rl78_reloc_map
[i
].rl78_reloc_val
;
257 static reloc_howto_type
*
258 rl78_reloc_name_lookup (bfd
* abfd ATTRIBUTE_UNUSED
, const char * r_name
)
262 for (i
= 0; i
< ARRAY_SIZE (rl78_elf_howto_table
); i
++)
263 if (rl78_elf_howto_table
[i
].name
!= NULL
264 && strcasecmp (rl78_elf_howto_table
[i
].name
, r_name
) == 0)
265 return rl78_elf_howto_table
+ i
;
270 /* Set the howto pointer for an RL78 ELF reloc. */
273 rl78_info_to_howto_rela (bfd
* abfd ATTRIBUTE_UNUSED
,
275 Elf_Internal_Rela
* dst
)
279 r_type
= ELF32_R_TYPE (dst
->r_info
);
280 BFD_ASSERT (r_type
< (unsigned int) R_RL78_max
);
281 cache_ptr
->howto
= rl78_elf_howto_table
+ r_type
;
285 get_symbol_value (const char * name
,
286 bfd_reloc_status_type
* status
,
287 struct bfd_link_info
* info
,
289 asection
* input_section
,
293 struct bfd_link_hash_entry
* h
;
295 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
298 || (h
->type
!= bfd_link_hash_defined
299 && h
->type
!= bfd_link_hash_defweak
))
300 * status
= info
->callbacks
->undefined_symbol
301 (info
, name
, input_bfd
, input_section
, offset
, TRUE
);
303 value
= (h
->u
.def
.value
304 + h
->u
.def
.section
->output_section
->vma
305 + h
->u
.def
.section
->output_offset
);
311 get_romstart (bfd_reloc_status_type
* status
,
312 struct bfd_link_info
* info
,
317 static bfd_boolean cached
= FALSE
;
318 static bfd_vma cached_value
= 0;
322 cached_value
= get_symbol_value ("_start", status
, info
, abfd
, sec
, offset
);
329 get_ramstart (bfd_reloc_status_type
* status
,
330 struct bfd_link_info
* info
,
335 static bfd_boolean cached
= FALSE
;
336 static bfd_vma cached_value
= 0;
340 cached_value
= get_symbol_value ("__datastart", status
, info
, abfd
, sec
, offset
);
346 #define NUM_STACK_ENTRIES 16
347 static int32_t rl78_stack
[ NUM_STACK_ENTRIES
];
348 static unsigned int rl78_stack_top
;
350 #define RL78_STACK_PUSH(val) \
353 if (rl78_stack_top < NUM_STACK_ENTRIES) \
354 rl78_stack [rl78_stack_top ++] = (val); \
356 r = bfd_reloc_dangerous; \
360 #define RL78_STACK_POP(dest) \
363 if (rl78_stack_top > 0) \
364 (dest) = rl78_stack [-- rl78_stack_top]; \
366 (dest) = 0, r = bfd_reloc_dangerous; \
370 /* Relocate an RL78 ELF section.
371 There is some attempt to make this function usable for many architectures,
372 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
373 if only to serve as a learning tool.
375 The RELOCATE_SECTION function is called by the new ELF backend linker
376 to handle the relocations for a section.
378 The relocs are always passed as Rela structures; if the section
379 actually uses Rel structures, the r_addend field will always be
382 This function is responsible for adjusting the section contents as
383 necessary, and (if using Rela relocs and generating a relocatable
384 output file) adjusting the reloc addend as necessary.
386 This function does not have to worry about setting the reloc
387 address or the reloc symbol index.
389 LOCAL_SYMS is a pointer to the swapped in local symbols.
391 LOCAL_SECTIONS is an array giving the section in the input file
392 corresponding to the st_shndx field of each local symbol.
394 The global hash table entry for the global symbols can be found
395 via elf_sym_hashes (input_bfd).
397 When generating relocatable output, this function must handle
398 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
399 going to be the section symbol corresponding to the output
400 section, which means that the addend must be adjusted
404 rl78_elf_relocate_section
406 struct bfd_link_info
* info
,
408 asection
* input_section
,
410 Elf_Internal_Rela
* relocs
,
411 Elf_Internal_Sym
* local_syms
,
412 asection
** local_sections
)
414 Elf_Internal_Shdr
* symtab_hdr
;
415 struct elf_link_hash_entry
** sym_hashes
;
416 Elf_Internal_Rela
* rel
;
417 Elf_Internal_Rela
* relend
;
421 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
422 sym_hashes
= elf_sym_hashes (input_bfd
);
423 relend
= relocs
+ input_section
->reloc_count
;
425 dynobj
= elf_hash_table (info
)->dynobj
;
428 splt
= bfd_get_section_by_name (dynobj
, ".plt");
430 for (rel
= relocs
; rel
< relend
; rel
++)
432 reloc_howto_type
* howto
;
433 unsigned long r_symndx
;
434 Elf_Internal_Sym
* sym
;
436 struct elf_link_hash_entry
* h
;
438 bfd_reloc_status_type r
;
439 const char * name
= NULL
;
440 bfd_boolean unresolved_reloc
= TRUE
;
443 r_type
= ELF32_R_TYPE (rel
->r_info
);
444 r_symndx
= ELF32_R_SYM (rel
->r_info
);
446 howto
= rl78_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
452 if (r_symndx
< symtab_hdr
->sh_info
)
454 sym
= local_syms
+ r_symndx
;
455 sec
= local_sections
[r_symndx
];
456 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, & sec
, rel
);
458 name
= bfd_elf_string_from_elf_section
459 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
460 name
= (sym
->st_name
== 0) ? bfd_section_name (input_bfd
, sec
) : name
;
466 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
467 r_symndx
, symtab_hdr
, sym_hashes
, h
,
468 sec
, relocation
, unresolved_reloc
,
471 name
= h
->root
.root
.string
;
474 if (sec
!= NULL
&& elf_discarded_section (sec
))
475 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
476 rel
, relend
, howto
, contents
);
478 if (info
->relocatable
)
480 /* This is a relocatable link. We don't have to change
481 anything, unless the reloc is against a section symbol,
482 in which case we have to adjust according to where the
483 section symbol winds up in the output section. */
484 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
485 rel
->r_addend
+= sec
->output_offset
;
489 switch (ELF32_R_TYPE (rel
->r_info
))
496 plt_offset
= &h
->plt
.offset
;
498 plt_offset
= elf_local_got_offsets (input_bfd
) + r_symndx
;
500 if (! valid_16bit_address (relocation
))
502 /* If this is the first time we've processed this symbol,
503 fill in the plt entry with the correct symbol address. */
504 if ((*plt_offset
& 1) == 0)
508 x
= 0x000000ec; /* br !!abs24 */
509 x
|= (relocation
<< 8) & 0xffffff00;
510 bfd_put_32 (input_bfd
, x
, splt
->contents
+ *plt_offset
);
514 relocation
= (splt
->output_section
->vma
515 + splt
->output_offset
516 + (*plt_offset
& -2));
519 char *newname
= bfd_malloc (strlen(name
)+5);
520 strcpy (newname
, name
);
521 strcat(newname
, ".plt");
522 _bfd_generic_link_add_one_symbol (info
,
525 BSF_FUNCTION
| BSF_WEAK
,
538 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
539 /* If the symbol is undefined and weak
540 then the relocation resolves to zero. */
544 if (howto
->pc_relative
)
546 relocation
-= (input_section
->output_section
->vma
547 + input_section
->output_offset
549 relocation
-= bfd_get_reloc_size (howto
);
552 relocation
+= rel
->r_addend
;
557 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
558 #define ALIGN(m) if (relocation & m) r = bfd_reloc_other;
559 #define OP(i) (contents[rel->r_offset + (i)])
561 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
567 case R_RL78_RH_RELAX
:
570 case R_RL78_DIR8S_PCREL
:
585 case R_RL78_DIR16S_PCREL
:
586 RANGE (-32768, 32767);
588 OP (1) = relocation
>> 8;
592 if ((relocation
& 0xf0000) == 0xf0000)
593 relocation
&= 0xffff;
594 RANGE (-32768, 65535);
596 OP (1) = relocation
>> 8;
602 OP (1) = relocation
>> 8;
606 RANGE (-32768, 65536);
608 OP (1) = relocation
>> 8;
611 case R_RL78_DIR16_REV
:
612 RANGE (-32768, 65536);
614 OP (0) = relocation
>> 8;
617 case R_RL78_DIR3U_PCREL
:
620 OP (0) |= relocation
& 0x07;
623 case R_RL78_DIR24S_PCREL
:
624 RANGE (-0x800000, 0x7fffff);
626 OP (1) = relocation
>> 8;
627 OP (2) = relocation
>> 16;
631 RANGE (-0x800000, 0x7fffff);
633 OP (1) = relocation
>> 8;
634 OP (2) = relocation
>> 16;
639 OP (1) = relocation
>> 8;
640 OP (2) = relocation
>> 16;
641 OP (3) = relocation
>> 24;
644 case R_RL78_DIR32_REV
:
646 OP (2) = relocation
>> 8;
647 OP (1) = relocation
>> 16;
648 OP (0) = relocation
>> 24;
652 RANGE (0xfff00, 0xfffff);
653 OP (0) = relocation
& 0xff;
656 case R_RL78_RH_SADDR
:
657 RANGE (0xffe20, 0xfff1f);
658 OP (0) = relocation
& 0xff;
661 /* Complex reloc handling: */
664 RL78_STACK_POP (relocation
);
666 OP (1) = relocation
>> 8;
667 OP (2) = relocation
>> 16;
668 OP (3) = relocation
>> 24;
671 case R_RL78_ABS32_REV
:
672 RL78_STACK_POP (relocation
);
674 OP (2) = relocation
>> 8;
675 OP (1) = relocation
>> 16;
676 OP (0) = relocation
>> 24;
679 case R_RL78_ABS24S_PCREL
:
681 RL78_STACK_POP (relocation
);
682 RANGE (-0x800000, 0x7fffff);
684 OP (1) = relocation
>> 8;
685 OP (2) = relocation
>> 16;
689 RL78_STACK_POP (relocation
);
690 RANGE (-32768, 65535);
692 OP (1) = relocation
>> 8;
695 case R_RL78_ABS16_REV
:
696 RL78_STACK_POP (relocation
);
697 RANGE (-32768, 65535);
699 OP (0) = relocation
>> 8;
702 case R_RL78_ABS16S_PCREL
:
704 RL78_STACK_POP (relocation
);
705 RANGE (-32768, 32767);
707 OP (1) = relocation
>> 8;
711 RL78_STACK_POP (relocation
);
714 OP (1) = relocation
>> 8;
718 RL78_STACK_POP (relocation
);
722 OP (1) = relocation
>> 8;
726 RL78_STACK_POP (relocation
);
730 OP (1) = relocation
>> 8;
734 RL78_STACK_POP (relocation
);
740 RL78_STACK_POP (relocation
);
746 RL78_STACK_POP (relocation
);
753 RL78_STACK_POP (relocation
);
759 case R_RL78_ABS8S_PCREL
:
761 RL78_STACK_POP (relocation
);
767 if (r_symndx
< symtab_hdr
->sh_info
)
768 RL78_STACK_PUSH (sec
->output_section
->vma
775 && (h
->root
.type
== bfd_link_hash_defined
776 || h
->root
.type
== bfd_link_hash_defweak
))
777 RL78_STACK_PUSH (h
->root
.u
.def
.value
778 + sec
->output_section
->vma
782 _bfd_error_handler (_("Warning: RL78_SYM reloc with an unknown symbol"));
790 RL78_STACK_POP (tmp
);
792 RL78_STACK_PUSH (tmp
);
800 RL78_STACK_POP (tmp2
);
801 RL78_STACK_POP (tmp1
);
803 RL78_STACK_PUSH (tmp1
);
811 RL78_STACK_POP (tmp2
);
812 RL78_STACK_POP (tmp1
);
814 RL78_STACK_PUSH (tmp2
);
822 RL78_STACK_POP (tmp2
);
823 RL78_STACK_POP (tmp1
);
825 RL78_STACK_PUSH (tmp1
);
833 RL78_STACK_POP (tmp2
);
834 RL78_STACK_POP (tmp1
);
836 RL78_STACK_PUSH (tmp1
);
844 RL78_STACK_POP (tmp2
);
845 RL78_STACK_POP (tmp1
);
847 RL78_STACK_PUSH (tmp1
);
855 RL78_STACK_POP (tmp2
);
856 RL78_STACK_POP (tmp1
);
858 RL78_STACK_PUSH (tmp1
);
862 case R_RL78_OPsctsize
:
863 RL78_STACK_PUSH (input_section
->size
);
866 case R_RL78_OPscttop
:
867 RL78_STACK_PUSH (input_section
->output_section
->vma
);
874 RL78_STACK_POP (tmp2
);
875 RL78_STACK_POP (tmp1
);
877 RL78_STACK_PUSH (tmp1
);
885 RL78_STACK_POP (tmp2
);
886 RL78_STACK_POP (tmp1
);
888 RL78_STACK_PUSH (tmp1
);
896 RL78_STACK_POP (tmp2
);
897 RL78_STACK_POP (tmp1
);
899 RL78_STACK_PUSH (tmp1
);
907 RL78_STACK_POP (tmp
);
909 RL78_STACK_PUSH (tmp
);
917 RL78_STACK_POP (tmp2
);
918 RL78_STACK_POP (tmp1
);
920 RL78_STACK_PUSH (tmp1
);
924 case R_RL78_OPromtop
:
925 RL78_STACK_PUSH (get_romstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
928 case R_RL78_OPramtop
:
929 RL78_STACK_PUSH (get_ramstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
933 r
= bfd_reloc_notsupported
;
937 if (r
!= bfd_reloc_ok
)
939 const char * msg
= NULL
;
943 case bfd_reloc_overflow
:
944 /* Catch the case of a missing function declaration
945 and emit a more helpful error message. */
946 if (r_type
== R_RL78_DIR24S_PCREL
)
947 msg
= _("%B(%A): error: call to undefined function '%s'");
949 r
= info
->callbacks
->reloc_overflow
950 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
951 input_bfd
, input_section
, rel
->r_offset
);
954 case bfd_reloc_undefined
:
955 r
= info
->callbacks
->undefined_symbol
956 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
960 case bfd_reloc_other
:
961 msg
= _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
964 case bfd_reloc_outofrange
:
965 msg
= _("%B(%A): internal error: out of range error");
968 case bfd_reloc_notsupported
:
969 msg
= _("%B(%A): internal error: unsupported relocation error");
972 case bfd_reloc_dangerous
:
973 msg
= _("%B(%A): internal error: dangerous relocation");
977 msg
= _("%B(%A): internal error: unknown error");
982 _bfd_error_handler (msg
, input_bfd
, input_section
, name
);
992 /* Function to set the ELF flag bits. */
995 rl78_elf_set_private_flags (bfd
* abfd
, flagword flags
)
997 elf_elfheader (abfd
)->e_flags
= flags
;
998 elf_flags_init (abfd
) = TRUE
;
1002 static bfd_boolean no_warn_mismatch
= FALSE
;
1004 void bfd_elf32_rl78_set_target_flags (bfd_boolean
);
1007 bfd_elf32_rl78_set_target_flags (bfd_boolean user_no_warn_mismatch
)
1009 no_warn_mismatch
= user_no_warn_mismatch
;
1012 /* Merge backend specific data from an object file to the output
1013 object file when linking. */
1016 rl78_elf_merge_private_bfd_data (bfd
* ibfd
, bfd
* obfd
)
1019 bfd_boolean error
= FALSE
;
1021 new_flags
= elf_elfheader (ibfd
)->e_flags
;
1023 if (!elf_flags_init (obfd
))
1025 /* First call, no flags set. */
1026 elf_flags_init (obfd
) = TRUE
;
1027 elf_elfheader (obfd
)->e_flags
= new_flags
;
1034 rl78_elf_print_private_bfd_data (bfd
* abfd
, void * ptr
)
1036 FILE * file
= (FILE *) ptr
;
1039 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
1041 /* Print normal ELF private data. */
1042 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
1044 flags
= elf_elfheader (abfd
)->e_flags
;
1045 fprintf (file
, _("private flags = 0x%lx:"), (long) flags
);
1051 /* Return the MACH for an e_flags value. */
1054 elf32_rl78_machine (bfd
* abfd
)
1056 if ((elf_elfheader (abfd
)->e_flags
& EF_RL78_CPU_MASK
) == EF_RL78_CPU_RL78
)
1057 return bfd_mach_rl78
;
1063 rl78_elf_object_p (bfd
* abfd
)
1065 bfd_default_set_arch_mach (abfd
, bfd_arch_rl78
,
1066 elf32_rl78_machine (abfd
));
1072 rl78_dump_symtab (bfd
* abfd
, void * internal_syms
, void * external_syms
)
1075 Elf_Internal_Sym
* isymbuf
;
1076 Elf_Internal_Sym
* isymend
;
1077 Elf_Internal_Sym
* isym
;
1078 Elf_Internal_Shdr
* symtab_hdr
;
1079 bfd_boolean free_internal
= FALSE
, free_external
= FALSE
;
1081 char * st_info_stb_str
;
1082 char * st_other_str
;
1083 char * st_shndx_str
;
1085 if (! internal_syms
)
1087 internal_syms
= bfd_malloc (1000);
1090 if (! external_syms
)
1092 external_syms
= bfd_malloc (1000);
1096 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1097 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
1099 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1100 symtab_hdr
->sh_info
, 0,
1101 internal_syms
, external_syms
, NULL
);
1103 isymbuf
= internal_syms
;
1104 isymend
= isymbuf
+ locsymcount
;
1106 for (isym
= isymbuf
; isym
< isymend
; isym
++)
1108 switch (ELF_ST_TYPE (isym
->st_info
))
1110 case STT_FUNC
: st_info_str
= "STT_FUNC";
1111 case STT_SECTION
: st_info_str
= "STT_SECTION";
1112 case STT_FILE
: st_info_str
= "STT_FILE";
1113 case STT_OBJECT
: st_info_str
= "STT_OBJECT";
1114 case STT_TLS
: st_info_str
= "STT_TLS";
1115 default: st_info_str
= "";
1117 switch (ELF_ST_BIND (isym
->st_info
))
1119 case STB_LOCAL
: st_info_stb_str
= "STB_LOCAL";
1120 case STB_GLOBAL
: st_info_stb_str
= "STB_GLOBAL";
1121 default: st_info_stb_str
= "";
1123 switch (ELF_ST_VISIBILITY (isym
->st_other
))
1125 case STV_DEFAULT
: st_other_str
= "STV_DEFAULT";
1126 case STV_INTERNAL
: st_other_str
= "STV_INTERNAL";
1127 case STV_PROTECTED
: st_other_str
= "STV_PROTECTED";
1128 default: st_other_str
= "";
1130 switch (isym
->st_shndx
)
1132 case SHN_ABS
: st_shndx_str
= "SHN_ABS";
1133 case SHN_COMMON
: st_shndx_str
= "SHN_COMMON";
1134 case SHN_UNDEF
: st_shndx_str
= "SHN_UNDEF";
1135 default: st_shndx_str
= "";
1139 free (internal_syms
);
1141 free (external_syms
);
1145 rl78_get_reloc (long reloc
)
1147 if (0 <= reloc
&& reloc
< R_RL78_max
)
1148 return rl78_elf_howto_table
[reloc
].name
;
1154 /* support PLT for 16-bit references to 24-bit functions. */
1156 /* We support 16-bit pointers to code above 64k by generating a thunk
1157 below 64k containing a JMP instruction to the final address. */
1160 rl78_elf_check_relocs
1162 struct bfd_link_info
* info
,
1164 const Elf_Internal_Rela
* relocs
)
1166 Elf_Internal_Shdr
* symtab_hdr
;
1167 struct elf_link_hash_entry
** sym_hashes
;
1168 const Elf_Internal_Rela
* rel
;
1169 const Elf_Internal_Rela
* rel_end
;
1170 bfd_vma
*local_plt_offsets
;
1174 if (info
->relocatable
)
1177 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1178 sym_hashes
= elf_sym_hashes (abfd
);
1179 local_plt_offsets
= elf_local_got_offsets (abfd
);
1181 dynobj
= elf_hash_table(info
)->dynobj
;
1183 rel_end
= relocs
+ sec
->reloc_count
;
1184 for (rel
= relocs
; rel
< rel_end
; rel
++)
1186 struct elf_link_hash_entry
*h
;
1187 unsigned long r_symndx
;
1190 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1191 if (r_symndx
< symtab_hdr
->sh_info
)
1195 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1196 while (h
->root
.type
== bfd_link_hash_indirect
1197 || h
->root
.type
== bfd_link_hash_warning
)
1198 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1201 switch (ELF32_R_TYPE (rel
->r_info
))
1203 /* This relocation describes a 16-bit pointer to a function.
1204 We may need to allocate a thunk in low memory; reserve memory
1208 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
1211 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1214 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1215 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
1216 | SEC_READONLY
| SEC_CODE
);
1217 splt
= bfd_make_section_with_flags (dynobj
, ".plt", flags
);
1219 || ! bfd_set_section_alignment (dynobj
, splt
, 1))
1225 offset
= &h
->plt
.offset
;
1228 if (local_plt_offsets
== NULL
)
1233 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
1234 local_plt_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
1235 if (local_plt_offsets
== NULL
)
1237 elf_local_got_offsets (abfd
) = local_plt_offsets
;
1239 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
1240 local_plt_offsets
[i
] = (bfd_vma
) -1;
1242 offset
= &local_plt_offsets
[r_symndx
];
1245 if (*offset
== (bfd_vma
) -1)
1247 *offset
= splt
->size
;
1257 /* This must exist if dynobj is ever set. */
1260 rl78_elf_finish_dynamic_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
1261 struct bfd_link_info
*info
)
1266 /* As an extra sanity check, verify that all plt entries have been
1267 filled in. However, relaxing might have changed the relocs so
1268 that some plt entries don't get filled in, so we have to skip
1269 this check if we're relaxing. Unfortunately, check_relocs is
1270 called before relaxation. */
1272 if (info
->relax_trip
> 0)
1274 if ((dynobj
= elf_hash_table (info
)->dynobj
) != NULL
1275 && (splt
= bfd_get_section_by_name (dynobj
, ".plt")) != NULL
)
1277 bfd_byte
*contents
= splt
->contents
;
1278 unsigned int i
, size
= splt
->size
;
1279 for (i
= 0; i
< size
; i
+= 4)
1281 unsigned int x
= bfd_get_32 (dynobj
, contents
+ i
);
1282 BFD_ASSERT (x
!= 0);
1291 rl78_elf_always_size_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1292 struct bfd_link_info
*info
)
1297 if (info
->relocatable
)
1300 dynobj
= elf_hash_table (info
)->dynobj
;
1304 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1305 BFD_ASSERT (splt
!= NULL
);
1307 splt
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, splt
->size
);
1308 if (splt
->contents
== NULL
)
1316 /* Handle relaxing. */
1318 /* A subroutine of rl78_elf_relax_section. If the global symbol H
1319 is within the low 64k, remove any entry for it in the plt. */
1321 struct relax_plt_data
1328 rl78_relax_plt_check (struct elf_link_hash_entry
*h
,
1331 struct relax_plt_data
*data
= (struct relax_plt_data
*) xdata
;
1333 if (h
->plt
.offset
!= (bfd_vma
) -1)
1337 if (h
->root
.type
== bfd_link_hash_undefined
1338 || h
->root
.type
== bfd_link_hash_undefweak
)
1341 address
= (h
->root
.u
.def
.section
->output_section
->vma
1342 + h
->root
.u
.def
.section
->output_offset
1343 + h
->root
.u
.def
.value
);
1345 if (valid_16bit_address (address
))
1348 data
->splt
->size
-= 4;
1349 *data
->again
= TRUE
;
1356 /* A subroutine of rl78_elf_relax_section. If the global symbol H
1357 previously had a plt entry, give it a new entry offset. */
1360 rl78_relax_plt_realloc (struct elf_link_hash_entry
*h
,
1363 bfd_vma
*entry
= (bfd_vma
*) xdata
;
1365 if (h
->plt
.offset
!= (bfd_vma
) -1)
1367 h
->plt
.offset
= *entry
;
1375 rl78_elf_relax_plt_section (bfd
*dynobj
,
1377 struct bfd_link_info
*info
,
1380 struct relax_plt_data relax_plt_data
;
1383 /* Assume nothing changes. */
1386 if (info
->relocatable
)
1389 /* We only relax the .plt section at the moment. */
1390 if (dynobj
!= elf_hash_table (info
)->dynobj
1391 || strcmp (splt
->name
, ".plt") != 0)
1394 /* Quick check for an empty plt. */
1395 if (splt
->size
== 0)
1398 /* Map across all global symbols; see which ones happen to
1399 fall in the low 64k. */
1400 relax_plt_data
.splt
= splt
;
1401 relax_plt_data
.again
= again
;
1402 elf_link_hash_traverse (elf_hash_table (info
), rl78_relax_plt_check
,
1405 /* Likewise for local symbols, though that's somewhat less convenient
1406 as we have to walk the list of input bfds and swap in symbol data. */
1407 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link_next
)
1409 bfd_vma
*local_plt_offsets
= elf_local_got_offsets (ibfd
);
1410 Elf_Internal_Shdr
*symtab_hdr
;
1411 Elf_Internal_Sym
*isymbuf
= NULL
;
1414 if (! local_plt_offsets
)
1417 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1418 if (symtab_hdr
->sh_info
!= 0)
1420 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1421 if (isymbuf
== NULL
)
1422 isymbuf
= bfd_elf_get_elf_syms (ibfd
, symtab_hdr
,
1423 symtab_hdr
->sh_info
, 0,
1425 if (isymbuf
== NULL
)
1429 for (idx
= 0; idx
< symtab_hdr
->sh_info
; ++idx
)
1431 Elf_Internal_Sym
*isym
;
1435 if (local_plt_offsets
[idx
] == (bfd_vma
) -1)
1438 isym
= &isymbuf
[idx
];
1439 if (isym
->st_shndx
== SHN_UNDEF
)
1441 else if (isym
->st_shndx
== SHN_ABS
)
1442 tsec
= bfd_abs_section_ptr
;
1443 else if (isym
->st_shndx
== SHN_COMMON
)
1444 tsec
= bfd_com_section_ptr
;
1446 tsec
= bfd_section_from_elf_index (ibfd
, isym
->st_shndx
);
1448 address
= (tsec
->output_section
->vma
1449 + tsec
->output_offset
1451 if (valid_16bit_address (address
))
1453 local_plt_offsets
[idx
] = -1;
1460 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1462 if (! info
->keep_memory
)
1466 /* Cache the symbols for elf_link_input_bfd. */
1467 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1472 /* If we changed anything, walk the symbols again to reallocate
1473 .plt entry addresses. */
1474 if (*again
&& splt
->size
> 0)
1478 elf_link_hash_traverse (elf_hash_table (info
),
1479 rl78_relax_plt_realloc
, &entry
);
1481 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link_next
)
1483 bfd_vma
*local_plt_offsets
= elf_local_got_offsets (ibfd
);
1484 unsigned int nlocals
= elf_tdata (ibfd
)->symtab_hdr
.sh_info
;
1487 if (! local_plt_offsets
)
1490 for (idx
= 0; idx
< nlocals
; ++idx
)
1491 if (local_plt_offsets
[idx
] != (bfd_vma
) -1)
1493 local_plt_offsets
[idx
] = entry
;
1502 /* Delete some bytes from a section while relaxing. */
1505 elf32_rl78_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, int count
,
1506 Elf_Internal_Rela
*alignment_rel
, int force_snip
)
1508 Elf_Internal_Shdr
* symtab_hdr
;
1509 unsigned int sec_shndx
;
1510 bfd_byte
* contents
;
1511 Elf_Internal_Rela
* irel
;
1512 Elf_Internal_Rela
* irelend
;
1513 Elf_Internal_Sym
* isym
;
1514 Elf_Internal_Sym
* isymend
;
1516 unsigned int symcount
;
1517 struct elf_link_hash_entry
** sym_hashes
;
1518 struct elf_link_hash_entry
** end_hashes
;
1523 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1525 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1527 /* The deletion must stop at the next alignment boundary, if
1528 ALIGNMENT_REL is non-NULL. */
1531 toaddr
= alignment_rel
->r_offset
;
1533 irel
= elf_section_data (sec
)->relocs
;
1534 irelend
= irel
+ sec
->reloc_count
;
1536 /* Actually delete the bytes. */
1537 memmove (contents
+ addr
, contents
+ addr
+ count
,
1538 (size_t) (toaddr
- addr
- count
));
1540 /* If we don't have an alignment marker to worry about, we can just
1541 shrink the section. Otherwise, we have to fill in the newly
1542 created gap with NOP insns (0x03). */
1546 memset (contents
+ toaddr
- count
, 0x03, count
);
1548 /* Adjust all the relocs. */
1549 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1551 /* Get the new reloc address. */
1552 if (irel
->r_offset
> addr
1553 && (irel
->r_offset
< toaddr
1554 || (force_snip
&& irel
->r_offset
== toaddr
)))
1555 irel
->r_offset
-= count
;
1557 /* If we see an ALIGN marker at the end of the gap, we move it
1558 to the beginning of the gap, since marking these gaps is what
1560 if (irel
->r_offset
== toaddr
1561 && ELF32_R_TYPE (irel
->r_info
) == R_RL78_RH_RELAX
1562 && irel
->r_addend
& RL78_RELAXA_ALIGN
)
1563 irel
->r_offset
-= count
;
1566 /* Adjust the local symbols defined in this section. */
1567 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1568 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1569 isymend
= isym
+ symtab_hdr
->sh_info
;
1571 for (; isym
< isymend
; isym
++)
1573 /* If the symbol is in the range of memory we just moved, we
1574 have to adjust its value. */
1575 if (isym
->st_shndx
== sec_shndx
1576 && isym
->st_value
> addr
1577 && isym
->st_value
< toaddr
)
1578 isym
->st_value
-= count
;
1580 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1581 *end* is in the moved bytes but it's *start* isn't), then we
1582 must adjust its size. */
1583 if (isym
->st_shndx
== sec_shndx
1584 && isym
->st_value
< addr
1585 && isym
->st_value
+ isym
->st_size
> addr
1586 && isym
->st_value
+ isym
->st_size
< toaddr
)
1587 isym
->st_size
-= count
;
1590 /* Now adjust the global symbols defined in this section. */
1591 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1592 - symtab_hdr
->sh_info
);
1593 sym_hashes
= elf_sym_hashes (abfd
);
1594 end_hashes
= sym_hashes
+ symcount
;
1596 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1598 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1600 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1601 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1602 && sym_hash
->root
.u
.def
.section
== sec
)
1604 /* As above, adjust the value if needed. */
1605 if (sym_hash
->root
.u
.def
.value
> addr
1606 && sym_hash
->root
.u
.def
.value
< toaddr
)
1607 sym_hash
->root
.u
.def
.value
-= count
;
1609 /* As above, adjust the size if needed. */
1610 if (sym_hash
->root
.u
.def
.value
< addr
1611 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
1612 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
1613 sym_hash
->size
-= count
;
1620 /* Used to sort relocs by address. If relocs have the same address,
1621 we maintain their relative order, except that R_RL78_RH_RELAX
1622 alignment relocs must be the first reloc for any given address. */
1625 reloc_bubblesort (Elf_Internal_Rela
* r
, int count
)
1629 bfd_boolean swappit
;
1631 /* This is almost a classic bubblesort. It's the slowest sort, but
1632 we're taking advantage of the fact that the relocations are
1633 mostly in order already (the assembler emits them that way) and
1634 we need relocs with the same address to remain in the same
1640 for (i
= 0; i
< count
- 1; i
++)
1642 if (r
[i
].r_offset
> r
[i
+ 1].r_offset
)
1644 else if (r
[i
].r_offset
< r
[i
+ 1].r_offset
)
1646 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RL78_RH_RELAX
1647 && (r
[i
+ 1].r_addend
& RL78_RELAXA_ALIGN
))
1649 else if (ELF32_R_TYPE (r
[i
+ 1].r_info
) == R_RL78_RH_RELAX
1650 && (r
[i
+ 1].r_addend
& RL78_RELAXA_ELIGN
)
1651 && !(ELF32_R_TYPE (r
[i
].r_info
) == R_RL78_RH_RELAX
1652 && (r
[i
].r_addend
& RL78_RELAXA_ALIGN
)))
1659 Elf_Internal_Rela tmp
;
1664 /* If we do move a reloc back, re-scan to see if it
1665 needs to be moved even further back. This avoids
1666 most of the O(n^2) behavior for our cases. */
1676 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1677 rl78_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1678 lrel, abfd, sec, link_info, scale)
1681 rl78_offset_for_reloc (bfd
* abfd
,
1682 Elf_Internal_Rela
* rel
,
1683 Elf_Internal_Shdr
* symtab_hdr
,
1684 Elf_External_Sym_Shndx
* shndx_buf ATTRIBUTE_UNUSED
,
1685 Elf_Internal_Sym
* intsyms
,
1686 Elf_Internal_Rela
** lrel
,
1688 asection
* input_section
,
1689 struct bfd_link_info
* info
,
1693 bfd_reloc_status_type r
;
1697 /* REL is the first of 1..N relocations. We compute the symbol
1698 value for each relocation, then combine them if needed. LREL
1699 gets a pointer to the last relocation used. */
1704 /* Get the value of the symbol referred to by the reloc. */
1705 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1707 /* A local symbol. */
1708 Elf_Internal_Sym
*isym
;
1711 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1713 if (isym
->st_shndx
== SHN_UNDEF
)
1714 ssec
= bfd_und_section_ptr
;
1715 else if (isym
->st_shndx
== SHN_ABS
)
1716 ssec
= bfd_abs_section_ptr
;
1717 else if (isym
->st_shndx
== SHN_COMMON
)
1718 ssec
= bfd_com_section_ptr
;
1720 ssec
= bfd_section_from_elf_index (abfd
,
1723 /* Initial symbol value. */
1724 symval
= isym
->st_value
;
1726 /* GAS may have made this symbol relative to a section, in
1727 which case, we have to add the addend to find the
1729 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
1730 symval
+= rel
->r_addend
;
1734 if ((ssec
->flags
& SEC_MERGE
)
1735 && ssec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
1736 symval
= _bfd_merged_section_offset (abfd
, & ssec
,
1737 elf_section_data (ssec
)->sec_info
,
1741 /* Now make the offset relative to where the linker is putting it. */
1744 ssec
->output_section
->vma
+ ssec
->output_offset
;
1746 symval
+= rel
->r_addend
;
1751 struct elf_link_hash_entry
* h
;
1753 /* An external symbol. */
1754 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1755 h
= elf_sym_hashes (abfd
)[indx
];
1756 BFD_ASSERT (h
!= NULL
);
1758 if (h
->root
.type
!= bfd_link_hash_defined
1759 && h
->root
.type
!= bfd_link_hash_defweak
)
1761 /* This appears to be a reference to an undefined
1762 symbol. Just ignore it--it will be caught by the
1763 regular reloc processing. */
1769 symval
= (h
->root
.u
.def
.value
1770 + h
->root
.u
.def
.section
->output_section
->vma
1771 + h
->root
.u
.def
.section
->output_offset
);
1773 symval
+= rel
->r_addend
;
1776 switch (ELF32_R_TYPE (rel
->r_info
))
1779 RL78_STACK_PUSH (symval
);
1783 RL78_STACK_POP (tmp1
);
1785 RL78_STACK_PUSH (tmp1
);
1789 RL78_STACK_POP (tmp1
);
1790 RL78_STACK_POP (tmp2
);
1792 RL78_STACK_PUSH (tmp1
);
1796 RL78_STACK_POP (tmp1
);
1797 RL78_STACK_POP (tmp2
);
1799 RL78_STACK_PUSH (tmp2
);
1803 RL78_STACK_POP (tmp1
);
1804 RL78_STACK_POP (tmp2
);
1806 RL78_STACK_PUSH (tmp1
);
1810 RL78_STACK_POP (tmp1
);
1811 RL78_STACK_POP (tmp2
);
1813 RL78_STACK_PUSH (tmp1
);
1817 RL78_STACK_POP (tmp1
);
1818 RL78_STACK_POP (tmp2
);
1820 RL78_STACK_PUSH (tmp1
);
1824 RL78_STACK_POP (tmp1
);
1825 RL78_STACK_POP (tmp2
);
1827 RL78_STACK_PUSH (tmp1
);
1830 case R_RL78_OPsctsize
:
1831 RL78_STACK_PUSH (input_section
->size
);
1834 case R_RL78_OPscttop
:
1835 RL78_STACK_PUSH (input_section
->output_section
->vma
);
1839 RL78_STACK_POP (tmp1
);
1840 RL78_STACK_POP (tmp2
);
1842 RL78_STACK_PUSH (tmp1
);
1846 RL78_STACK_POP (tmp1
);
1847 RL78_STACK_POP (tmp2
);
1849 RL78_STACK_PUSH (tmp1
);
1853 RL78_STACK_POP (tmp1
);
1854 RL78_STACK_POP (tmp2
);
1856 RL78_STACK_PUSH (tmp1
);
1860 RL78_STACK_POP (tmp1
);
1862 RL78_STACK_PUSH (tmp1
);
1866 RL78_STACK_POP (tmp1
);
1867 RL78_STACK_POP (tmp2
);
1869 RL78_STACK_PUSH (tmp1
);
1872 case R_RL78_OPromtop
:
1873 RL78_STACK_PUSH (get_romstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1876 case R_RL78_OPramtop
:
1877 RL78_STACK_PUSH (get_ramstart (&r
, info
, input_bfd
, input_section
, rel
->r_offset
));
1880 case R_RL78_DIR16UL
:
1882 case R_RL78_ABS16UL
:
1885 RL78_STACK_POP (symval
);
1891 case R_RL78_DIR16UW
:
1893 case R_RL78_ABS16UW
:
1896 RL78_STACK_POP (symval
);
1904 RL78_STACK_POP (symval
);
1915 int prefix
; /* or -1 for "no prefix" */
1916 int insn
; /* or -1 for "end of list" */
1917 int insn_for_saddr
; /* or -1 for "no alternative" */
1918 int insn_for_sfr
; /* or -1 for "no alternative" */
1919 } relax_addr16
[] = {
1920 { -1, 0x02, 0x06, -1 }, /* ADDW AX, !addr16 */
1921 { -1, 0x22, 0x26, -1 }, /* SUBW AX, !addr16 */
1922 { -1, 0x42, 0x46, -1 }, /* CMPW AX, !addr16 */
1923 { -1, 0x40, 0x4a, -1 }, /* CMP !addr16, #byte */
1925 { -1, 0x0f, 0x0b, -1 }, /* ADD A, !addr16 */
1926 { -1, 0x1f, 0x1b, -1 }, /* ADDC A, !addr16 */
1927 { -1, 0x2f, 0x2b, -1 }, /* SUB A, !addr16 */
1928 { -1, 0x3f, 0x3b, -1 }, /* SUBC A, !addr16 */
1929 { -1, 0x4f, 0x4b, -1 }, /* CMP A, !addr16 */
1930 { -1, 0x5f, 0x5b, -1 }, /* AND A, !addr16 */
1931 { -1, 0x6f, 0x6b, -1 }, /* OR A, !addr16 */
1932 { -1, 0x7f, 0x7b, -1 }, /* XOR A, !addr16 */
1934 { -1, 0x8f, 0x8d, 0x8e }, /* MOV A, !addr16 */
1935 { -1, 0x9f, 0x9d, 0x9e }, /* MOV !addr16, A */
1936 { -1, 0xaf, 0xad, 0xae }, /* MOVW AX, !addr16 */
1937 { -1, 0xbf, 0xbd, 0xbe }, /* MOVW !addr16, AX */
1938 { -1, 0xcf, 0xcd, 0xce }, /* MOVW !addr16, #word */
1940 { -1, 0xa0, 0xa4, -1 }, /* INC !addr16 */
1941 { -1, 0xa2, 0xa6, -1 }, /* INCW !addr16 */
1942 { -1, 0xb0, 0xb4, -1 }, /* DEC !addr16 */
1943 { -1, 0xb2, 0xb6, -1 }, /* DECW !addr16 */
1945 { -1, 0xd5, 0xd4, -1 }, /* CMP0 !addr16 */
1946 { -1, 0xe5, 0xe4, -1 }, /* ONEB !addr16 */
1947 { -1, 0xf5, 0xf4, -1 }, /* CLRB !addr16 */
1949 { -1, 0xd9, 0xd8, -1 }, /* MOV X, !addr16 */
1950 { -1, 0xe9, 0xe8, -1 }, /* MOV B, !addr16 */
1951 { -1, 0xf9, 0xf8, -1 }, /* MOV C, !addr16 */
1952 { -1, 0xdb, 0xda, -1 }, /* MOVW BC, !addr16 */
1953 { -1, 0xeb, 0xea, -1 }, /* MOVW DE, !addr16 */
1954 { -1, 0xfb, 0xfa, -1 }, /* MOVW HL, !addr16 */
1956 { 0x61, 0xaa, 0xa8, -1 }, /* XCH A, !addr16 */
1958 { 0x71, 0x00, 0x02, 0x0a }, /* SET1 !addr16.0 */
1959 { 0x71, 0x10, 0x12, 0x1a }, /* SET1 !addr16.0 */
1960 { 0x71, 0x20, 0x22, 0x2a }, /* SET1 !addr16.0 */
1961 { 0x71, 0x30, 0x32, 0x3a }, /* SET1 !addr16.0 */
1962 { 0x71, 0x40, 0x42, 0x4a }, /* SET1 !addr16.0 */
1963 { 0x71, 0x50, 0x52, 0x5a }, /* SET1 !addr16.0 */
1964 { 0x71, 0x60, 0x62, 0x6a }, /* SET1 !addr16.0 */
1965 { 0x71, 0x70, 0x72, 0x7a }, /* SET1 !addr16.0 */
1967 { 0x71, 0x08, 0x03, 0x0b }, /* CLR1 !addr16.0 */
1968 { 0x71, 0x18, 0x13, 0x1b }, /* CLR1 !addr16.0 */
1969 { 0x71, 0x28, 0x23, 0x2b }, /* CLR1 !addr16.0 */
1970 { 0x71, 0x38, 0x33, 0x3b }, /* CLR1 !addr16.0 */
1971 { 0x71, 0x48, 0x43, 0x4b }, /* CLR1 !addr16.0 */
1972 { 0x71, 0x58, 0x53, 0x5b }, /* CLR1 !addr16.0 */
1973 { 0x71, 0x68, 0x63, 0x6b }, /* CLR1 !addr16.0 */
1974 { 0x71, 0x78, 0x73, 0x7b }, /* CLR1 !addr16.0 */
1979 /* Relax one section. */
1982 rl78_elf_relax_section
1985 struct bfd_link_info
* link_info
,
1986 bfd_boolean
* again
)
1988 Elf_Internal_Shdr
* symtab_hdr
;
1989 Elf_Internal_Shdr
* shndx_hdr
;
1990 Elf_Internal_Rela
* internal_relocs
;
1991 Elf_Internal_Rela
* free_relocs
= NULL
;
1992 Elf_Internal_Rela
* irel
;
1993 Elf_Internal_Rela
* srel
;
1994 Elf_Internal_Rela
* irelend
;
1995 Elf_Internal_Rela
* next_alignment
;
1996 bfd_byte
* contents
= NULL
;
1997 bfd_byte
* free_contents
= NULL
;
1998 Elf_Internal_Sym
* intsyms
= NULL
;
1999 Elf_Internal_Sym
* free_intsyms
= NULL
;
2000 Elf_External_Sym_Shndx
* shndx_buf
= NULL
;
2002 bfd_vma symval ATTRIBUTE_UNUSED
= 0;
2003 int pcrel ATTRIBUTE_UNUSED
= 0;
2004 int code ATTRIBUTE_UNUSED
= 0;
2005 int section_alignment_glue
;
2008 if (abfd
== elf_hash_table (link_info
)->dynobj
2009 && strcmp (sec
->name
, ".plt") == 0)
2010 return rl78_elf_relax_plt_section (abfd
, sec
, link_info
, again
);
2012 /* Assume nothing changes. */
2015 /* We don't have to do anything for a relocatable link, if
2016 this section does not have relocs, or if this is not a
2018 if (link_info
->relocatable
2019 || (sec
->flags
& SEC_RELOC
) == 0
2020 || sec
->reloc_count
== 0
2021 || (sec
->flags
& SEC_CODE
) == 0)
2024 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2025 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2027 /* Get the section contents. */
2028 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2029 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2030 /* Go get them off disk. */
2033 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2035 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2038 /* Read this BFD's symbols. */
2039 /* Get cached copy if it exists. */
2040 if (symtab_hdr
->contents
!= NULL
)
2041 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2044 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
2045 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
2048 if (shndx_hdr
->sh_size
!= 0)
2052 amt
= symtab_hdr
->sh_info
;
2053 amt
*= sizeof (Elf_External_Sym_Shndx
);
2054 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
2055 if (shndx_buf
== NULL
)
2057 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
2058 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
2060 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
2063 /* Get a copy of the native relocations. */
2064 internal_relocs
= (_bfd_elf_link_read_relocs
2065 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
2066 link_info
->keep_memory
));
2067 if (internal_relocs
== NULL
)
2069 if (! link_info
->keep_memory
)
2070 free_relocs
= internal_relocs
;
2072 /* The RL_ relocs must be just before the operand relocs they go
2073 with, so we must sort them to guarantee this. We use bubblesort
2074 instead of qsort so we can guarantee that relocs with the same
2075 address remain in the same relative order. */
2076 reloc_bubblesort (internal_relocs
, sec
->reloc_count
);
2078 /* Walk through them looking for relaxing opportunities. */
2079 irelend
= internal_relocs
+ sec
->reloc_count
;
2082 /* This will either be NULL or a pointer to the next alignment
2084 next_alignment
= internal_relocs
;
2086 /* We calculate worst case shrinkage caused by alignment directives.
2087 No fool-proof, but better than either ignoring the problem or
2088 doing heavy duty analysis of all the alignment markers in all
2090 section_alignment_glue
= 0;
2091 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2092 if (ELF32_R_TYPE (irel
->r_info
) == R_RL78_RH_RELAX
2093 && irel
->r_addend
& RL78_RELAXA_ALIGN
)
2095 int this_glue
= 1 << (irel
->r_addend
& RL78_RELAXA_ANUM
);
2097 if (section_alignment_glue
< this_glue
)
2098 section_alignment_glue
= this_glue
;
2100 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2102 section_alignment_glue
*= 2;
2104 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2106 unsigned char *insn
;
2109 /* The insns we care about are all marked with one of these. */
2110 if (ELF32_R_TYPE (irel
->r_info
) != R_RL78_RH_RELAX
)
2113 if (irel
->r_addend
& RL78_RELAXA_ALIGN
2114 || next_alignment
== internal_relocs
)
2116 /* When we delete bytes, we need to maintain all the alignments
2117 indicated. In addition, we need to be careful about relaxing
2118 jumps across alignment boundaries - these displacements
2119 *grow* when we delete bytes. For now, don't shrink
2120 displacements across an alignment boundary, just in case.
2121 Note that this only affects relocations to the same
2123 next_alignment
+= 2;
2124 while (next_alignment
< irelend
2125 && (ELF32_R_TYPE (next_alignment
->r_info
) != R_RL78_RH_RELAX
2126 || !(next_alignment
->r_addend
& RL78_RELAXA_ELIGN
)))
2128 if (next_alignment
>= irelend
|| next_alignment
->r_offset
== 0)
2129 next_alignment
= NULL
;
2132 /* When we hit alignment markers, see if we've shrunk enough
2133 before them to reduce the gap without violating the alignment
2135 if (irel
->r_addend
& RL78_RELAXA_ALIGN
)
2137 /* At this point, the next relocation *should* be the ELIGN
2139 Elf_Internal_Rela
*erel
= irel
+ 1;
2140 unsigned int alignment
, nbytes
;
2142 if (ELF32_R_TYPE (erel
->r_info
) != R_RL78_RH_RELAX
)
2144 if (!(erel
->r_addend
& RL78_RELAXA_ELIGN
))
2147 alignment
= 1 << (irel
->r_addend
& RL78_RELAXA_ANUM
);
2149 if (erel
->r_offset
- irel
->r_offset
< alignment
)
2152 nbytes
= erel
->r_offset
- irel
->r_offset
;
2153 nbytes
/= alignment
;
2154 nbytes
*= alignment
;
2156 elf32_rl78_relax_delete_bytes (abfd
, sec
, erel
->r_offset
-nbytes
, nbytes
, next_alignment
,
2157 erel
->r_offset
== sec
->size
);
2163 if (irel
->r_addend
& RL78_RELAXA_ELIGN
)
2166 insn
= contents
+ irel
->r_offset
;
2168 nrelocs
= irel
->r_addend
& RL78_RELAXA_RNUM
;
2170 /* At this point, we have an insn that is a candidate for linker
2171 relaxation. There are NRELOCS relocs following that may be
2172 relaxed, although each reloc may be made of more than one
2173 reloc entry (such as gp-rel symbols). */
2175 /* Get the value of the symbol referred to by the reloc. Just
2176 in case this is the last reloc in the list, use the RL's
2177 addend to choose between this reloc (no addend) or the next
2178 (yes addend, which means at least one following reloc). */
2180 /* srel points to the "current" reloction for this insn -
2181 actually the last reloc for a given operand, which is the one
2182 we need to update. We check the relaxations in the same
2183 order that the relocations happen, so we'll just push it
2187 pc
= sec
->output_section
->vma
+ sec
->output_offset
2191 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2192 pcrel = symval - pc + srel->r_addend; \
2195 #define SNIPNR(offset, nbytes) \
2196 elf32_rl78_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0);
2197 #define SNIP(offset, nbytes, newtype) \
2198 SNIPNR (offset, nbytes); \
2199 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2201 /* The order of these bit tests must match the order that the
2202 relocs appear in. Since we sorted those by offset, we can
2205 /*----------------------------------------------------------------------*/
2206 /* EF ad BR $rel8 pcrel
2207 ED al ah BR !abs16 abs
2208 EE al ah BR $!rel16 pcrel
2209 EC al ah as BR !!abs20 abs
2211 FD al ah CALL !abs16 abs
2212 FE al ah CALL $!rel16 pcrel
2213 FC al ah as CALL !!abs20 abs
2221 61 C8 EF ad SKC ; BR $rel8
2222 61 D8 EF ad SKNC ; BR $rel8
2223 61 E8 EF ad SKZ ; BR $rel8
2224 61 F8 EF ad SKNZ ; BR $rel8
2225 61 E3 EF ad SKH ; BR $rel8
2226 61 F3 EF ad SKNH ; BR $rel8
2229 if (irel
->r_addend
& RL78_RELAXA_BRA
)
2235 case 0xec: /* BR !!abs20 */
2242 SNIP (2, 2, R_RL78_DIR8S_PCREL
);
2245 else if (symval
< 65536)
2248 insn
[1] = symval
& 0xff;
2249 insn
[2] = symval
>> 8;
2250 SNIP (2, 1, R_RL78_DIR16S
);
2253 else if (pcrel
< 32767
2257 insn
[1] = pcrel
& 0xff;
2258 insn
[2] = pcrel
>> 8;
2259 SNIP (2, 1, R_RL78_DIR16S_PCREL
);
2264 case 0xee: /* BR $!pcrel16 */
2265 case 0xed: /* BR $!abs16 */
2271 SNIP (2, 1, R_RL78_DIR8S_PCREL
);
2276 case 0xfc: /* CALL !!abs20 */
2280 insn
[1] = symval
& 0xff;
2281 insn
[2] = symval
>> 8;
2282 SNIP (2, 1, R_RL78_DIR16S
);
2285 else if (pcrel
< 32767
2289 insn
[1] = pcrel
& 0xff;
2290 insn
[2] = pcrel
>> 8;
2291 SNIP (2, 1, R_RL78_DIR16S_PCREL
);
2296 case 0x61: /* PREFIX */
2297 /* For SKIP/BR, we change the BR opcode and delete the
2298 SKIP. That way, we don't have to find and change the
2299 relocation for the BR. */
2302 case 0xc8: /* SKC */
2303 if (insn
[2] == 0xef)
2305 insn
[2] = 0xde; /* BNC */
2310 case 0xd8: /* SKNC */
2311 if (insn
[2] == 0xef)
2313 insn
[2] = 0xdc; /* BC */
2318 case 0xe8: /* SKZ */
2319 if (insn
[2] == 0xef)
2321 insn
[2] = 0xdf; /* BNZ */
2326 case 0xf8: /* SKNZ */
2327 if (insn
[2] == 0xef)
2329 insn
[2] = 0xdd; /* BZ */
2334 case 0xe3: /* SKH */
2335 if (insn
[2] == 0xef)
2337 insn
[2] = 0xd3; /* BNH */
2338 SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */
2342 case 0xf3: /* SKNH */
2343 if (insn
[2] == 0xef)
2345 insn
[2] = 0xc3; /* BH */
2346 SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */
2355 if (irel
->r_addend
& RL78_RELAXA_ADDR16
)
2357 /*----------------------------------------------------------------------*/
2358 /* Some insns have both a 16-bit address operand and an 8-bit
2359 variant if the address is within a special range:
2361 Address 16-bit operand SADDR range SFR range
2362 FFF00-FFFFF 0xff00-0xffff 0x00-0xff
2363 FFE20-FFF1F 0xfe20-0xff1f 0x00-0xff
2365 The RELAX_ADDR16[] array has the insn encodings for the
2366 16-bit operand version, as well as the SFR and SADDR
2367 variants. We only need to replace the encodings and
2370 Note: we intentionally do not attempt to decode and skip
2371 any ES: prefix, as adding ES: means the addr16 (likely)
2372 no longer points to saddr/sfr space.
2382 if (0xffe20 <= symval
&& symval
<= 0xfffff)
2385 is_saddr
= (0xffe20 <= symval
&& symval
<= 0xfff1f);
2386 is_sfr
= (0xfff00 <= symval
&& symval
<= 0xfffff);
2388 for (idx
= 0; relax_addr16
[idx
].insn
!= -1; idx
++)
2390 if (relax_addr16
[idx
].prefix
!= -1
2391 && insn
[0] == relax_addr16
[idx
].prefix
2392 && insn
[1] == relax_addr16
[idx
].insn
)
2396 else if (relax_addr16
[idx
].prefix
== -1
2397 && insn
[0] == relax_addr16
[idx
].insn
)
2404 /* We have a matched insn, and poff is 0 or 1 depending
2405 on the base pattern size. */
2407 if (is_sfr
&& relax_addr16
[idx
].insn_for_sfr
!= -1)
2409 insn
[poff
] = relax_addr16
[idx
].insn_for_sfr
;
2410 SNIP (poff
+2, 1, R_RL78_RH_SFR
);
2413 else if (is_saddr
&& relax_addr16
[idx
].insn_for_saddr
!= -1)
2415 insn
[poff
] = relax_addr16
[idx
].insn_for_saddr
;
2416 SNIP (poff
+2, 1, R_RL78_RH_SADDR
);
2423 /*----------------------------------------------------------------------*/
2430 if (free_relocs
!= NULL
)
2433 if (free_contents
!= NULL
)
2434 free (free_contents
);
2436 if (shndx_buf
!= NULL
)
2438 shndx_hdr
->contents
= NULL
;
2442 if (free_intsyms
!= NULL
)
2443 free (free_intsyms
);
2450 #define ELF_ARCH bfd_arch_rl78
2451 #define ELF_MACHINE_CODE EM_RL78
2452 #define ELF_MAXPAGESIZE 0x1000
2454 #define TARGET_LITTLE_SYM bfd_elf32_rl78_vec
2455 #define TARGET_LITTLE_NAME "elf32-rl78"
2457 #define elf_info_to_howto_rel NULL
2458 #define elf_info_to_howto rl78_info_to_howto_rela
2459 #define elf_backend_object_p rl78_elf_object_p
2460 #define elf_backend_relocate_section rl78_elf_relocate_section
2461 #define elf_symbol_leading_char ('_')
2462 #define elf_backend_can_gc_sections 1
2464 #define bfd_elf32_bfd_reloc_type_lookup rl78_reloc_type_lookup
2465 #define bfd_elf32_bfd_reloc_name_lookup rl78_reloc_name_lookup
2466 #define bfd_elf32_bfd_set_private_flags rl78_elf_set_private_flags
2467 #define bfd_elf32_bfd_merge_private_bfd_data rl78_elf_merge_private_bfd_data
2468 #define bfd_elf32_bfd_print_private_bfd_data rl78_elf_print_private_bfd_data
2470 #define bfd_elf32_bfd_relax_section rl78_elf_relax_section
2471 #define elf_backend_check_relocs rl78_elf_check_relocs
2472 #define elf_backend_always_size_sections \
2473 rl78_elf_always_size_sections
2474 #define elf_backend_finish_dynamic_sections \
2475 rl78_elf_finish_dynamic_sections
2477 #include "elf32-target.h"