1 /* Renesas RL78 specific support for 32-bit ELF.
2 Copyright (C) 2011-2015 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "bfd_stdint.h"
26 #include "libiberty.h"
28 #define valid_16bit_address(v) ((v) <= 0x0ffff || (v) >= 0xf0000)
30 #define RL78REL(n,sz,bit,shift,complain,pcrel) \
31 HOWTO (R_RL78_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
32 bfd_elf_generic_reloc, "R_RL78_" #n, FALSE, 0, ~0, FALSE)
34 /* Note that the relocations around 0x7f are internal to this file;
35 feel free to move them as needed to avoid conflicts with published
36 relocation numbers. */
38 static reloc_howto_type rl78_elf_howto_table [] =
40 RL78REL (NONE, 3, 0, 0, dont, FALSE),
41 RL78REL (DIR32, 2, 32, 0, signed, FALSE),
42 RL78REL (DIR24S, 2, 24, 0, signed, FALSE),
43 RL78REL (DIR16, 1, 16, 0, dont, FALSE),
44 RL78REL (DIR16U, 1, 16, 0, unsigned, FALSE),
45 RL78REL (DIR16S, 1, 16, 0, signed, FALSE),
46 RL78REL (DIR8, 0, 8, 0, dont, FALSE),
47 RL78REL (DIR8U, 0, 8, 0, unsigned, FALSE),
48 RL78REL (DIR8S, 0, 8, 0, signed, FALSE),
49 RL78REL (DIR24S_PCREL, 2, 24, 0, signed, TRUE),
50 RL78REL (DIR16S_PCREL, 1, 16, 0, signed, TRUE),
51 RL78REL (DIR8S_PCREL, 0, 8, 0, signed, TRUE),
52 RL78REL (DIR16UL, 1, 16, 2, unsigned, FALSE),
53 RL78REL (DIR16UW, 1, 16, 1, unsigned, FALSE),
54 RL78REL (DIR8UL, 0, 8, 2, unsigned, FALSE),
55 RL78REL (DIR8UW, 0, 8, 1, unsigned, FALSE),
56 RL78REL (DIR32_REV, 1, 16, 0, dont, FALSE),
57 RL78REL (DIR16_REV, 1, 16, 0, dont, FALSE),
58 RL78REL (DIR3U_PCREL, 0, 3, 0, dont, TRUE),
87 RL78REL (RH_RELAX, 0, 0, 0, dont, FALSE),
109 RL78REL (ABS32, 2, 32, 0, dont, FALSE),
110 RL78REL (ABS24S, 2, 24, 0, signed, FALSE),
111 RL78REL (ABS16, 1, 16, 0, dont, FALSE),
112 RL78REL (ABS16U, 1, 16, 0, unsigned, FALSE),
113 RL78REL (ABS16S, 1, 16, 0, signed, FALSE),
114 RL78REL (ABS8, 0, 8, 0, dont, FALSE),
115 RL78REL (ABS8U, 0, 8, 0, unsigned, FALSE),
116 RL78REL (ABS8S, 0, 8, 0, signed, FALSE),
117 RL78REL (ABS24S_PCREL, 2, 24, 0, signed, TRUE),
118 RL78REL (ABS16S_PCREL, 1, 16, 0, signed, TRUE),
119 RL78REL (ABS8S_PCREL, 0, 8, 0, signed, TRUE),
120 RL78REL (ABS16UL, 1, 16, 0, unsigned, FALSE),
121 RL78REL (ABS16UW, 1, 16, 0, unsigned, FALSE),
122 RL78REL (ABS8UL, 0, 8, 0, unsigned, FALSE),
123 RL78REL (ABS8UW, 0, 8, 0, unsigned, FALSE),
124 RL78REL (ABS32_REV, 2, 32, 0, dont, FALSE),
125 RL78REL (ABS16_REV, 1, 16, 0, dont, FALSE),
127 #define STACK_REL_P(x) ((x) <= R_RL78_ABS16_REV && (x) >= R_RL78_ABS32)
177 RL78REL (SYM, 2, 32, 0, dont, FALSE),
178 RL78REL (OPneg, 2, 32, 0, dont, FALSE),
179 RL78REL (OPadd, 2, 32, 0, dont, FALSE),
180 RL78REL (OPsub, 2, 32, 0, dont, FALSE),
181 RL78REL (OPmul, 2, 32, 0, dont, FALSE),
182 RL78REL (OPdiv, 2, 32, 0, dont, FALSE),
183 RL78REL (OPshla, 2, 32, 0, dont, FALSE),
184 RL78REL (OPshra, 2, 32, 0, dont, FALSE),
185 RL78REL (OPsctsize, 2, 32, 0, dont, FALSE),
190 RL78REL (OPscttop, 2, 32, 0, dont, FALSE),
193 RL78REL (OPand, 2, 32, 0, dont, FALSE),
194 RL78REL (OPor, 2, 32, 0, dont, FALSE),
195 RL78REL (OPxor, 2, 32, 0, dont, FALSE),
196 RL78REL (OPnot, 2, 32, 0, dont, FALSE),
197 RL78REL (OPmod, 2, 32, 0, dont, FALSE),
198 RL78REL (OPromtop, 2, 32, 0, dont, FALSE),
199 RL78REL (OPramtop, 2, 32, 0, dont, FALSE)
202 /* Map BFD reloc types to RL78 ELF reloc types. */
204 struct rl78_reloc_map
206 bfd_reloc_code_real_type bfd_reloc_val;
207 unsigned int rl78_reloc_val;
210 static const struct rl78_reloc_map rl78_reloc_map [] =
212 { BFD_RELOC_NONE, R_RL78_NONE },
213 { BFD_RELOC_8, R_RL78_DIR8S },
214 { BFD_RELOC_16, R_RL78_DIR16S },
215 { BFD_RELOC_24, R_RL78_DIR24S },
216 { BFD_RELOC_32, R_RL78_DIR32 },
217 { BFD_RELOC_RL78_16_OP, R_RL78_DIR16 },
218 { BFD_RELOC_RL78_DIR3U_PCREL, R_RL78_DIR3U_PCREL },
219 { BFD_RELOC_8_PCREL, R_RL78_DIR8S_PCREL },
220 { BFD_RELOC_16_PCREL, R_RL78_DIR16S_PCREL },
221 { BFD_RELOC_24_PCREL, R_RL78_DIR24S_PCREL },
222 { BFD_RELOC_RL78_8U, R_RL78_DIR8U },
223 { BFD_RELOC_RL78_16U, R_RL78_DIR16U },
224 { BFD_RELOC_RL78_SYM, R_RL78_SYM },
225 { BFD_RELOC_RL78_OP_SUBTRACT, R_RL78_OPsub },
226 { BFD_RELOC_RL78_OP_NEG, R_RL78_OPneg },
227 { BFD_RELOC_RL78_OP_AND, R_RL78_OPand },
228 { BFD_RELOC_RL78_OP_SHRA, R_RL78_OPshra },
229 { BFD_RELOC_RL78_ABS8, R_RL78_ABS8 },
230 { BFD_RELOC_RL78_ABS16, R_RL78_ABS16 },
231 { BFD_RELOC_RL78_ABS16_REV, R_RL78_ABS16_REV },
232 { BFD_RELOC_RL78_ABS32, R_RL78_ABS32 },
233 { BFD_RELOC_RL78_ABS32_REV, R_RL78_ABS32_REV },
234 { BFD_RELOC_RL78_ABS16UL, R_RL78_ABS16UL },
235 { BFD_RELOC_RL78_ABS16UW, R_RL78_ABS16UW },
236 { BFD_RELOC_RL78_ABS16U, R_RL78_ABS16U },
237 { BFD_RELOC_RL78_RELAX, R_RL78_RH_RELAX }
240 static reloc_howto_type *
241 rl78_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
242 bfd_reloc_code_real_type code)
246 if (code == BFD_RELOC_RL78_32_OP)
247 return rl78_elf_howto_table + R_RL78_DIR32;
249 for (i = ARRAY_SIZE (rl78_reloc_map); i--;)
250 if (rl78_reloc_map [i].bfd_reloc_val == code)
251 return rl78_elf_howto_table + rl78_reloc_map[i].rl78_reloc_val;
256 static reloc_howto_type *
257 rl78_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, const char * r_name)
261 for (i = 0; i < ARRAY_SIZE (rl78_elf_howto_table); i++)
262 if (rl78_elf_howto_table[i].name != NULL
263 && strcasecmp (rl78_elf_howto_table[i].name, r_name) == 0)
264 return rl78_elf_howto_table + i;
269 /* Set the howto pointer for an RL78 ELF reloc. */
272 rl78_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
274 Elf_Internal_Rela * dst)
278 r_type = ELF32_R_TYPE (dst->r_info);
279 if (r_type >= (unsigned int) R_RL78_max)
281 _bfd_error_handler (_("%B: invalid RL78 reloc number: %d"), abfd, r_type);
284 cache_ptr->howto = rl78_elf_howto_table + r_type;
288 get_symbol_value (const char * name,
289 bfd_reloc_status_type * status,
290 struct bfd_link_info * info,
292 asection * input_section,
296 struct bfd_link_hash_entry * h;
298 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
301 || (h->type != bfd_link_hash_defined
302 && h->type != bfd_link_hash_defweak))
303 * status = info->callbacks->undefined_symbol
304 (info, name, input_bfd, input_section, offset, TRUE);
306 value = (h->u.def.value
307 + h->u.def.section->output_section->vma
308 + h->u.def.section->output_offset);
314 get_romstart (bfd_reloc_status_type * status,
315 struct bfd_link_info * info,
320 static bfd_boolean cached = FALSE;
321 static bfd_vma cached_value = 0;
325 cached_value = get_symbol_value ("_start", status, info, abfd, sec, offset);
332 get_ramstart (bfd_reloc_status_type * status,
333 struct bfd_link_info * info,
338 static bfd_boolean cached = FALSE;
339 static bfd_vma cached_value = 0;
343 cached_value = get_symbol_value ("__datastart", status, info, abfd, sec, offset);
349 #define NUM_STACK_ENTRIES 16
350 static int32_t rl78_stack [ NUM_STACK_ENTRIES ];
351 static unsigned int rl78_stack_top;
353 #define RL78_STACK_PUSH(val) \
356 if (rl78_stack_top < NUM_STACK_ENTRIES) \
357 rl78_stack [rl78_stack_top ++] = (val); \
359 r = bfd_reloc_dangerous; \
363 #define RL78_STACK_POP(dest) \
366 if (rl78_stack_top > 0) \
367 (dest) = rl78_stack [-- rl78_stack_top]; \
369 (dest) = 0, r = bfd_reloc_dangerous; \
373 /* Relocate an RL78 ELF section.
374 There is some attempt to make this function usable for many architectures,
375 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
376 if only to serve as a learning tool.
378 The RELOCATE_SECTION function is called by the new ELF backend linker
379 to handle the relocations for a section.
381 The relocs are always passed as Rela structures; if the section
382 actually uses Rel structures, the r_addend field will always be
385 This function is responsible for adjusting the section contents as
386 necessary, and (if using Rela relocs and generating a relocatable
387 output file) adjusting the reloc addend as necessary.
389 This function does not have to worry about setting the reloc
390 address or the reloc symbol index.
392 LOCAL_SYMS is a pointer to the swapped in local symbols.
394 LOCAL_SECTIONS is an array giving the section in the input file
395 corresponding to the st_shndx field of each local symbol.
397 The global hash table entry for the global symbols can be found
398 via elf_sym_hashes (input_bfd).
400 When generating relocatable output, this function must handle
401 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
402 going to be the section symbol corresponding to the output
403 section, which means that the addend must be adjusted
407 rl78_elf_relocate_section
409 struct bfd_link_info * info,
411 asection * input_section,
413 Elf_Internal_Rela * relocs,
414 Elf_Internal_Sym * local_syms,
415 asection ** local_sections)
417 Elf_Internal_Shdr * symtab_hdr;
418 struct elf_link_hash_entry ** sym_hashes;
419 Elf_Internal_Rela * rel;
420 Elf_Internal_Rela * relend;
424 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
425 sym_hashes = elf_sym_hashes (input_bfd);
426 relend = relocs + input_section->reloc_count;
428 dynobj = elf_hash_table (info)->dynobj;
431 splt = bfd_get_linker_section (dynobj, ".plt");
433 for (rel = relocs; rel < relend; rel ++)
435 reloc_howto_type * howto;
436 unsigned long r_symndx;
437 Elf_Internal_Sym * sym;
439 struct elf_link_hash_entry * h;
441 bfd_reloc_status_type r;
442 const char * name = NULL;
443 bfd_boolean unresolved_reloc = TRUE;
446 r_type = ELF32_R_TYPE (rel->r_info);
447 r_symndx = ELF32_R_SYM (rel->r_info);
449 howto = rl78_elf_howto_table + ELF32_R_TYPE (rel->r_info);
455 if (r_symndx < symtab_hdr->sh_info)
457 sym = local_syms + r_symndx;
458 sec = local_sections [r_symndx];
459 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel);
461 name = bfd_elf_string_from_elf_section
462 (input_bfd, symtab_hdr->sh_link, sym->st_name);
463 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
467 bfd_boolean warned ATTRIBUTE_UNUSED;
468 bfd_boolean ignored ATTRIBUTE_UNUSED;
470 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
471 r_symndx, symtab_hdr, sym_hashes, h,
472 sec, relocation, unresolved_reloc,
475 name = h->root.root.string;
478 if (sec != NULL && discarded_section (sec))
479 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
480 rel, 1, relend, howto, 0, contents);
482 if (info->relocatable)
484 /* This is a relocatable link. We don't have to change
485 anything, unless the reloc is against a section symbol,
486 in which case we have to adjust according to where the
487 section symbol winds up in the output section. */
488 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
489 rel->r_addend += sec->output_offset;
493 switch (ELF32_R_TYPE (rel->r_info))
500 plt_offset = &h->plt.offset;
502 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
504 if (! valid_16bit_address (relocation))
506 /* If this is the first time we've processed this symbol,
507 fill in the plt entry with the correct symbol address. */
508 if ((*plt_offset & 1) == 0)
512 x = 0x000000ec; /* br !!abs24 */
513 x |= (relocation << 8) & 0xffffff00;
514 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
518 relocation = (splt->output_section->vma
519 + splt->output_offset
520 + (*plt_offset & -2));
523 char *newname = bfd_malloc (strlen(name)+5);
524 strcpy (newname, name);
525 strcat(newname, ".plt");
526 _bfd_generic_link_add_one_symbol (info,
529 BSF_FUNCTION | BSF_WEAK,
542 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
543 /* If the symbol is undefined and weak
544 then the relocation resolves to zero. */
548 if (howto->pc_relative)
550 relocation -= (input_section->output_section->vma
551 + input_section->output_offset
553 relocation -= bfd_get_reloc_size (howto);
556 relocation += rel->r_addend;
561 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
562 #define ALIGN(m) if (relocation & m) r = bfd_reloc_other;
563 #define OP(i) (contents[rel->r_offset + (i)])
565 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
571 case R_RL78_RH_RELAX:
574 case R_RL78_DIR8S_PCREL:
589 case R_RL78_DIR16S_PCREL:
590 RANGE (-32768, 32767);
592 OP (1) = relocation >> 8;
596 if ((relocation & 0xf0000) == 0xf0000)
597 relocation &= 0xffff;
598 RANGE (-32768, 65535);
600 OP (1) = relocation >> 8;
606 OP (1) = relocation >> 8;
610 RANGE (-32768, 65536);
612 OP (1) = relocation >> 8;
615 case R_RL78_DIR16_REV:
616 RANGE (-32768, 65536);
618 OP (0) = relocation >> 8;
621 case R_RL78_DIR3U_PCREL:
624 OP (0) |= relocation & 0x07;
627 case R_RL78_DIR24S_PCREL:
628 RANGE (-0x800000, 0x7fffff);
630 OP (1) = relocation >> 8;
631 OP (2) = relocation >> 16;
635 RANGE (-0x800000, 0x7fffff);
637 OP (1) = relocation >> 8;
638 OP (2) = relocation >> 16;
643 OP (1) = relocation >> 8;
644 OP (2) = relocation >> 16;
645 OP (3) = relocation >> 24;
648 case R_RL78_DIR32_REV:
650 OP (2) = relocation >> 8;
651 OP (1) = relocation >> 16;
652 OP (0) = relocation >> 24;
656 RANGE (0xfff00, 0xfffff);
657 OP (0) = relocation & 0xff;
660 case R_RL78_RH_SADDR:
661 RANGE (0xffe20, 0xfff1f);
662 OP (0) = relocation & 0xff;
665 /* Complex reloc handling: */
668 RL78_STACK_POP (relocation);
670 OP (1) = relocation >> 8;
671 OP (2) = relocation >> 16;
672 OP (3) = relocation >> 24;
675 case R_RL78_ABS32_REV:
676 RL78_STACK_POP (relocation);
678 OP (2) = relocation >> 8;
679 OP (1) = relocation >> 16;
680 OP (0) = relocation >> 24;
683 case R_RL78_ABS24S_PCREL:
685 RL78_STACK_POP (relocation);
686 RANGE (-0x800000, 0x7fffff);
688 OP (1) = relocation >> 8;
689 OP (2) = relocation >> 16;
693 RL78_STACK_POP (relocation);
694 RANGE (-32768, 65535);
696 OP (1) = relocation >> 8;
699 case R_RL78_ABS16_REV:
700 RL78_STACK_POP (relocation);
701 RANGE (-32768, 65535);
703 OP (0) = relocation >> 8;
706 case R_RL78_ABS16S_PCREL:
708 RL78_STACK_POP (relocation);
709 RANGE (-32768, 32767);
711 OP (1) = relocation >> 8;
715 RL78_STACK_POP (relocation);
718 OP (1) = relocation >> 8;
722 RL78_STACK_POP (relocation);
726 OP (1) = relocation >> 8;
730 RL78_STACK_POP (relocation);
734 OP (1) = relocation >> 8;
738 RL78_STACK_POP (relocation);
744 RL78_STACK_POP (relocation);
750 RL78_STACK_POP (relocation);
757 RL78_STACK_POP (relocation);
763 case R_RL78_ABS8S_PCREL:
765 RL78_STACK_POP (relocation);
771 if (r_symndx < symtab_hdr->sh_info)
772 RL78_STACK_PUSH (sec->output_section->vma
779 && (h->root.type == bfd_link_hash_defined
780 || h->root.type == bfd_link_hash_defweak))
781 RL78_STACK_PUSH (h->root.u.def.value
782 + sec->output_section->vma
785 else if (h->root.type == bfd_link_hash_undefweak)
788 _bfd_error_handler (_("Warning: RL78_SYM reloc with an unknown symbol"));
796 RL78_STACK_POP (tmp);
798 RL78_STACK_PUSH (tmp);
806 RL78_STACK_POP (tmp2);
807 RL78_STACK_POP (tmp1);
809 RL78_STACK_PUSH (tmp1);
817 /* For the expression "A - B", the assembler pushes A,
818 then B, then OPSUB. So the first op we pop is B, not
820 RL78_STACK_POP (tmp2); /* B */
821 RL78_STACK_POP (tmp1); /* A */
822 tmp1 -= tmp2; /* A - B */
823 RL78_STACK_PUSH (tmp1);
831 RL78_STACK_POP (tmp2);
832 RL78_STACK_POP (tmp1);
834 RL78_STACK_PUSH (tmp1);
842 RL78_STACK_POP (tmp2);
843 RL78_STACK_POP (tmp1);
845 RL78_STACK_PUSH (tmp1);
853 RL78_STACK_POP (tmp2);
854 RL78_STACK_POP (tmp1);
856 RL78_STACK_PUSH (tmp1);
864 RL78_STACK_POP (tmp2);
865 RL78_STACK_POP (tmp1);
867 RL78_STACK_PUSH (tmp1);
871 case R_RL78_OPsctsize:
872 RL78_STACK_PUSH (input_section->size);
875 case R_RL78_OPscttop:
876 RL78_STACK_PUSH (input_section->output_section->vma);
883 RL78_STACK_POP (tmp2);
884 RL78_STACK_POP (tmp1);
886 RL78_STACK_PUSH (tmp1);
894 RL78_STACK_POP (tmp2);
895 RL78_STACK_POP (tmp1);
897 RL78_STACK_PUSH (tmp1);
905 RL78_STACK_POP (tmp2);
906 RL78_STACK_POP (tmp1);
908 RL78_STACK_PUSH (tmp1);
916 RL78_STACK_POP (tmp);
918 RL78_STACK_PUSH (tmp);
926 RL78_STACK_POP (tmp2);
927 RL78_STACK_POP (tmp1);
929 RL78_STACK_PUSH (tmp1);
933 case R_RL78_OPromtop:
934 RL78_STACK_PUSH (get_romstart (&r, info, input_bfd, input_section, rel->r_offset));
937 case R_RL78_OPramtop:
938 RL78_STACK_PUSH (get_ramstart (&r, info, input_bfd, input_section, rel->r_offset));
942 r = bfd_reloc_notsupported;
946 if (r != bfd_reloc_ok)
948 const char * msg = NULL;
952 case bfd_reloc_overflow:
953 /* Catch the case of a missing function declaration
954 and emit a more helpful error message. */
955 if (r_type == R_RL78_DIR24S_PCREL)
956 msg = _("%B(%A): error: call to undefined function '%s'");
958 r = info->callbacks->reloc_overflow
959 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
960 input_bfd, input_section, rel->r_offset);
963 case bfd_reloc_undefined:
964 r = info->callbacks->undefined_symbol
965 (info, name, input_bfd, input_section, rel->r_offset,
969 case bfd_reloc_other:
970 msg = _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
973 case bfd_reloc_outofrange:
974 msg = _("%B(%A): internal error: out of range error");
977 case bfd_reloc_notsupported:
978 msg = _("%B(%A): internal error: unsupported relocation error");
981 case bfd_reloc_dangerous:
982 msg = _("%B(%A): internal error: dangerous relocation");
986 msg = _("%B(%A): internal error: unknown error");
991 _bfd_error_handler (msg, input_bfd, input_section, name);
1001 /* Function to set the ELF flag bits. */
1004 rl78_elf_set_private_flags (bfd * abfd, flagword flags)
1006 elf_elfheader (abfd)->e_flags = flags;
1007 elf_flags_init (abfd) = TRUE;
1011 static bfd_boolean no_warn_mismatch = FALSE;
1013 void bfd_elf32_rl78_set_target_flags (bfd_boolean);
1016 bfd_elf32_rl78_set_target_flags (bfd_boolean user_no_warn_mismatch)
1018 no_warn_mismatch = user_no_warn_mismatch;
1022 rl78_cpu_name (flagword flags)
1024 switch (flags & E_FLAG_RL78_CPU_MASK)
1027 case E_FLAG_RL78_G10: return "G10";
1028 case E_FLAG_RL78_G13: return "G13";
1029 case E_FLAG_RL78_G14: return "G14";
1033 /* Merge backend specific data from an object file to the output
1034 object file when linking. */
1037 rl78_elf_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
1041 bfd_boolean error = FALSE;
1043 new_flags = elf_elfheader (ibfd)->e_flags;
1044 old_flags = elf_elfheader (obfd)->e_flags;
1046 if (!elf_flags_init (obfd))
1048 /* First call, no flags set. */
1049 elf_flags_init (obfd) = TRUE;
1050 elf_elfheader (obfd)->e_flags = new_flags;
1052 else if (old_flags != new_flags)
1054 flagword changed_flags = old_flags ^ new_flags;
1056 if (changed_flags & E_FLAG_RL78_CPU_MASK)
1058 flagword out_cpu = old_flags & E_FLAG_RL78_CPU_MASK;
1059 flagword in_cpu = new_flags & E_FLAG_RL78_CPU_MASK;
1061 if (in_cpu == E_FLAG_RL78_ANY_CPU || in_cpu == out_cpu)
1062 /* It does not matter what new_cpu may have. */;
1063 else if (out_cpu == E_FLAG_RL78_ANY_CPU)
1065 if (in_cpu == E_FLAG_RL78_G10)
1067 /* G10 files can only be linked with other G10 files.
1068 If the output is set to "any" this means that it is
1069 a G14 file that does not use hardware multiply/divide,
1070 but that is still incompatible with the G10 ABI. */
1073 (*_bfd_error_handler)
1074 (_("RL78 ABI conflict: G10 file %s cannot be linked with %s file %s"),
1075 bfd_get_filename (ibfd),
1076 rl78_cpu_name (out_cpu), bfd_get_filename (obfd));
1080 old_flags &= ~ E_FLAG_RL78_CPU_MASK;
1081 old_flags |= in_cpu;
1082 elf_elfheader (obfd)->e_flags = old_flags;
1089 (*_bfd_error_handler)
1090 (_("RL78 ABI conflict: cannot link %s file %s with %s file %s"),
1091 rl78_cpu_name (in_cpu), bfd_get_filename (ibfd),
1092 rl78_cpu_name (out_cpu), bfd_get_filename (obfd));
1096 if (changed_flags & E_FLAG_RL78_64BIT_DOUBLES)
1098 (*_bfd_error_handler)
1099 (_("RL78 merge conflict: cannot link 32-bit and 64-bit objects together"));
1101 if (old_flags & E_FLAG_RL78_64BIT_DOUBLES)
1102 (*_bfd_error_handler) (_("- %s is 64-bit, %s is not"),
1103 bfd_get_filename (obfd), bfd_get_filename (ibfd));
1105 (*_bfd_error_handler) (_("- %s is 64-bit, %s is not"),
1106 bfd_get_filename (ibfd), bfd_get_filename (obfd));
1115 rl78_elf_print_private_bfd_data (bfd * abfd, void * ptr)
1117 FILE * file = (FILE *) ptr;
1120 BFD_ASSERT (abfd != NULL && ptr != NULL);
1122 /* Print normal ELF private data. */
1123 _bfd_elf_print_private_bfd_data (abfd, ptr);
1125 flags = elf_elfheader (abfd)->e_flags;
1126 fprintf (file, _("private flags = 0x%lx:"), (long) flags);
1128 if (flags & E_FLAG_RL78_CPU_MASK)
1129 fprintf (file, " [%s]", rl78_cpu_name (flags));
1131 if (flags & E_FLAG_RL78_64BIT_DOUBLES)
1132 fprintf (file, _(" [64-bit doubles]"));
1138 /* Return the MACH for an e_flags value. */
1141 elf32_rl78_machine (bfd * abfd ATTRIBUTE_UNUSED)
1143 return bfd_mach_rl78;
1147 rl78_elf_object_p (bfd * abfd)
1149 bfd_default_set_arch_mach (abfd, bfd_arch_rl78,
1150 elf32_rl78_machine (abfd));
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;
1200 /* PR15323, ref flags aren't set for references in the same
1202 h->root.non_ir_ref = 1;
1205 switch (ELF32_R_TYPE (rel->r_info))
1207 /* This relocation describes a 16-bit pointer to a function.
1208 We may need to allocate a thunk in low memory; reserve memory
1212 elf_hash_table (info)->dynobj = dynobj = abfd;
1215 splt = bfd_get_linker_section (dynobj, ".plt");
1218 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1219 | SEC_IN_MEMORY | SEC_LINKER_CREATED
1220 | SEC_READONLY | SEC_CODE);
1221 splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
1224 || ! bfd_set_section_alignment (dynobj, splt, 1))
1230 offset = &h->plt.offset;
1233 if (local_plt_offsets == NULL)
1238 size = symtab_hdr->sh_info * sizeof (bfd_vma);
1239 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
1240 if (local_plt_offsets == NULL)
1242 elf_local_got_offsets (abfd) = local_plt_offsets;
1244 for (i = 0; i < symtab_hdr->sh_info; i++)
1245 local_plt_offsets[i] = (bfd_vma) -1;
1247 offset = &local_plt_offsets[r_symndx];
1250 if (*offset == (bfd_vma) -1)
1252 *offset = splt->size;
1262 /* This must exist if dynobj is ever set. */
1265 rl78_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
1266 struct bfd_link_info *info)
1271 if (!elf_hash_table (info)->dynamic_sections_created)
1274 /* As an extra sanity check, verify that all plt entries have been
1275 filled in. However, relaxing might have changed the relocs so
1276 that some plt entries don't get filled in, so we have to skip
1277 this check if we're relaxing. Unfortunately, check_relocs is
1278 called before relaxation. */
1280 if (info->relax_trip > 0)
1283 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
1284 && (splt = bfd_get_linker_section (dynobj, ".plt")) != NULL)
1286 bfd_byte *contents = splt->contents;
1287 unsigned int i, size = splt->size;
1289 for (i = 0; i < size; i += 4)
1291 unsigned int x = bfd_get_32 (dynobj, contents + i);
1292 BFD_ASSERT (x != 0);
1300 rl78_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1301 struct bfd_link_info *info)
1306 if (info->relocatable)
1309 dynobj = elf_hash_table (info)->dynobj;
1313 splt = bfd_get_linker_section (dynobj, ".plt");
1314 BFD_ASSERT (splt != NULL);
1316 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
1317 if (splt->contents == NULL)
1325 /* Handle relaxing. */
1327 /* A subroutine of rl78_elf_relax_section. If the global symbol H
1328 is within the low 64k, remove any entry for it in the plt. */
1330 struct relax_plt_data
1337 rl78_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
1339 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1341 if (h->plt.offset != (bfd_vma) -1)
1345 if (h->root.type == bfd_link_hash_undefined
1346 || h->root.type == bfd_link_hash_undefweak)
1349 address = (h->root.u.def.section->output_section->vma
1350 + h->root.u.def.section->output_offset
1351 + h->root.u.def.value);
1353 if (valid_16bit_address (address))
1356 data->splt->size -= 4;
1357 *data->again = TRUE;
1364 /* A subroutine of rl78_elf_relax_section. If the global symbol H
1365 previously had a plt entry, give it a new entry offset. */
1368 rl78_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
1370 bfd_vma *entry = (bfd_vma *) xdata;
1372 if (h->plt.offset != (bfd_vma) -1)
1374 h->plt.offset = *entry;
1382 rl78_elf_relax_plt_section (bfd *dynobj,
1384 struct bfd_link_info *info,
1387 struct relax_plt_data relax_plt_data;
1390 /* Assume nothing changes. */
1393 if (info->relocatable)
1396 /* We only relax the .plt section at the moment. */
1397 if (dynobj != elf_hash_table (info)->dynobj
1398 || strcmp (splt->name, ".plt") != 0)
1401 /* Quick check for an empty plt. */
1402 if (splt->size == 0)
1405 /* Map across all global symbols; see which ones happen to
1406 fall in the low 64k. */
1407 relax_plt_data.splt = splt;
1408 relax_plt_data.again = again;
1409 elf_link_hash_traverse (elf_hash_table (info), rl78_relax_plt_check,
1412 /* Likewise for local symbols, though that's somewhat less convenient
1413 as we have to walk the list of input bfds and swap in symbol data. */
1414 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1416 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1417 Elf_Internal_Shdr *symtab_hdr;
1418 Elf_Internal_Sym *isymbuf = NULL;
1421 if (! local_plt_offsets)
1424 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1425 if (symtab_hdr->sh_info != 0)
1427 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1428 if (isymbuf == NULL)
1429 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1430 symtab_hdr->sh_info, 0,
1432 if (isymbuf == NULL)
1436 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1438 Elf_Internal_Sym *isym;
1442 if (local_plt_offsets[idx] == (bfd_vma) -1)
1445 isym = &isymbuf[idx];
1446 if (isym->st_shndx == SHN_UNDEF)
1448 else if (isym->st_shndx == SHN_ABS)
1449 tsec = bfd_abs_section_ptr;
1450 else if (isym->st_shndx == SHN_COMMON)
1451 tsec = bfd_com_section_ptr;
1453 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1455 address = (tsec->output_section->vma
1456 + tsec->output_offset
1458 if (valid_16bit_address (address))
1460 local_plt_offsets[idx] = -1;
1467 && symtab_hdr->contents != (unsigned char *) isymbuf)
1469 if (! info->keep_memory)
1473 /* Cache the symbols for elf_link_input_bfd. */
1474 symtab_hdr->contents = (unsigned char *) isymbuf;
1479 /* If we changed anything, walk the symbols again to reallocate
1480 .plt entry addresses. */
1481 if (*again && splt->size > 0)
1485 elf_link_hash_traverse (elf_hash_table (info),
1486 rl78_relax_plt_realloc, &entry);
1488 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1490 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1491 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1494 if (! local_plt_offsets)
1497 for (idx = 0; idx < nlocals; ++idx)
1498 if (local_plt_offsets[idx] != (bfd_vma) -1)
1500 local_plt_offsets[idx] = entry;
1509 /* Delete some bytes from a section while relaxing. */
1512 elf32_rl78_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
1513 Elf_Internal_Rela *alignment_rel, int force_snip)
1515 Elf_Internal_Shdr * symtab_hdr;
1516 unsigned int sec_shndx;
1517 bfd_byte * contents;
1518 Elf_Internal_Rela * irel;
1519 Elf_Internal_Rela * irelend;
1520 Elf_Internal_Sym * isym;
1521 Elf_Internal_Sym * isymend;
1523 unsigned int symcount;
1524 struct elf_link_hash_entry ** sym_hashes;
1525 struct elf_link_hash_entry ** end_hashes;
1530 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1532 contents = elf_section_data (sec)->this_hdr.contents;
1534 /* The deletion must stop at the next alignment boundary, if
1535 ALIGNMENT_REL is non-NULL. */
1538 toaddr = alignment_rel->r_offset;
1540 irel = elf_section_data (sec)->relocs;
1543 _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
1544 irel = elf_section_data (sec)->relocs;
1547 irelend = irel + sec->reloc_count;
1549 /* Actually delete the bytes. */
1550 memmove (contents + addr, contents + addr + count,
1551 (size_t) (toaddr - addr - count));
1553 /* If we don't have an alignment marker to worry about, we can just
1554 shrink the section. Otherwise, we have to fill in the newly
1555 created gap with NOP insns (0x03). */
1559 memset (contents + toaddr - count, 0x03, count);
1561 /* Adjust all the relocs. */
1562 for (; irel && irel < irelend; irel++)
1564 /* Get the new reloc address. */
1565 if (irel->r_offset > addr
1566 && (irel->r_offset < toaddr
1567 || (force_snip && irel->r_offset == toaddr)))
1568 irel->r_offset -= count;
1570 /* If we see an ALIGN marker at the end of the gap, we move it
1571 to the beginning of the gap, since marking these gaps is what
1573 if (irel->r_offset == toaddr
1574 && ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX
1575 && irel->r_addend & RL78_RELAXA_ALIGN)
1576 irel->r_offset -= count;
1579 /* Adjust the local symbols defined in this section. */
1580 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1581 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1582 isymend = isym + symtab_hdr->sh_info;
1584 for (; isym < isymend; isym++)
1586 /* If the symbol is in the range of memory we just moved, we
1587 have to adjust its value. */
1588 if (isym->st_shndx == sec_shndx
1589 && isym->st_value > addr
1590 && isym->st_value < toaddr)
1591 isym->st_value -= count;
1593 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1594 *end* is in the moved bytes but it's *start* isn't), then we
1595 must adjust its size. */
1596 if (isym->st_shndx == sec_shndx
1597 && isym->st_value < addr
1598 && isym->st_value + isym->st_size > addr
1599 && isym->st_value + isym->st_size < toaddr)
1600 isym->st_size -= count;
1603 /* Now adjust the global symbols defined in this section. */
1604 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1605 - symtab_hdr->sh_info);
1606 sym_hashes = elf_sym_hashes (abfd);
1607 end_hashes = sym_hashes + symcount;
1609 for (; sym_hashes < end_hashes; sym_hashes++)
1611 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1613 if ((sym_hash->root.type == bfd_link_hash_defined
1614 || sym_hash->root.type == bfd_link_hash_defweak)
1615 && sym_hash->root.u.def.section == sec)
1617 /* As above, adjust the value if needed. */
1618 if (sym_hash->root.u.def.value > addr
1619 && sym_hash->root.u.def.value < toaddr)
1620 sym_hash->root.u.def.value -= count;
1622 /* As above, adjust the size if needed. */
1623 if (sym_hash->root.u.def.value < addr
1624 && sym_hash->root.u.def.value + sym_hash->size > addr
1625 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1626 sym_hash->size -= count;
1633 /* Used to sort relocs by address. If relocs have the same address,
1634 we maintain their relative order, except that R_RL78_RH_RELAX
1635 alignment relocs must be the first reloc for any given address. */
1638 reloc_bubblesort (Elf_Internal_Rela * r, int count)
1642 bfd_boolean swappit;
1644 /* This is almost a classic bubblesort. It's the slowest sort, but
1645 we're taking advantage of the fact that the relocations are
1646 mostly in order already (the assembler emits them that way) and
1647 we need relocs with the same address to remain in the same
1653 for (i = 0; i < count - 1; i ++)
1655 if (r[i].r_offset > r[i + 1].r_offset)
1657 else if (r[i].r_offset < r[i + 1].r_offset)
1659 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX
1660 && (r[i + 1].r_addend & RL78_RELAXA_ALIGN))
1662 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX
1663 && (r[i + 1].r_addend & RL78_RELAXA_ELIGN)
1664 && !(ELF32_R_TYPE (r[i].r_info) == R_RL78_RH_RELAX
1665 && (r[i].r_addend & RL78_RELAXA_ALIGN)))
1672 Elf_Internal_Rela tmp;
1677 /* If we do move a reloc back, re-scan to see if it
1678 needs to be moved even further back. This avoids
1679 most of the O(n^2) behavior for our cases. */
1689 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1690 rl78_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1691 lrel, abfd, sec, link_info, scale)
1694 rl78_offset_for_reloc (bfd * abfd,
1695 Elf_Internal_Rela * rel,
1696 Elf_Internal_Shdr * symtab_hdr,
1697 Elf_External_Sym_Shndx * shndx_buf ATTRIBUTE_UNUSED,
1698 Elf_Internal_Sym * intsyms,
1699 Elf_Internal_Rela ** lrel,
1701 asection * input_section,
1702 struct bfd_link_info * info,
1706 bfd_reloc_status_type r;
1710 /* REL is the first of 1..N relocations. We compute the symbol
1711 value for each relocation, then combine them if needed. LREL
1712 gets a pointer to the last relocation used. */
1717 /* Get the value of the symbol referred to by the reloc. */
1718 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1720 /* A local symbol. */
1721 Elf_Internal_Sym *isym;
1724 isym = intsyms + ELF32_R_SYM (rel->r_info);
1726 if (isym->st_shndx == SHN_UNDEF)
1727 ssec = bfd_und_section_ptr;
1728 else if (isym->st_shndx == SHN_ABS)
1729 ssec = bfd_abs_section_ptr;
1730 else if (isym->st_shndx == SHN_COMMON)
1731 ssec = bfd_com_section_ptr;
1733 ssec = bfd_section_from_elf_index (abfd,
1736 /* Initial symbol value. */
1737 symval = isym->st_value;
1739 /* GAS may have made this symbol relative to a section, in
1740 which case, we have to add the addend to find the
1742 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
1743 symval += rel->r_addend;
1747 if ((ssec->flags & SEC_MERGE)
1748 && ssec->sec_info_type == SEC_INFO_TYPE_MERGE)
1749 symval = _bfd_merged_section_offset (abfd, & ssec,
1750 elf_section_data (ssec)->sec_info,
1754 /* Now make the offset relative to where the linker is putting it. */
1757 ssec->output_section->vma + ssec->output_offset;
1759 symval += rel->r_addend;
1764 struct elf_link_hash_entry * h;
1766 /* An external symbol. */
1767 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1768 h = elf_sym_hashes (abfd)[indx];
1769 BFD_ASSERT (h != NULL);
1771 if (h->root.type != bfd_link_hash_defined
1772 && h->root.type != bfd_link_hash_defweak)
1774 /* This appears to be a reference to an undefined
1775 symbol. Just ignore it--it will be caught by the
1776 regular reloc processing. */
1782 symval = (h->root.u.def.value
1783 + h->root.u.def.section->output_section->vma
1784 + h->root.u.def.section->output_offset);
1786 symval += rel->r_addend;
1789 switch (ELF32_R_TYPE (rel->r_info))
1792 RL78_STACK_PUSH (symval);
1796 RL78_STACK_POP (tmp1);
1798 RL78_STACK_PUSH (tmp1);
1802 RL78_STACK_POP (tmp1);
1803 RL78_STACK_POP (tmp2);
1805 RL78_STACK_PUSH (tmp1);
1809 RL78_STACK_POP (tmp1);
1810 RL78_STACK_POP (tmp2);
1812 RL78_STACK_PUSH (tmp2);
1816 RL78_STACK_POP (tmp1);
1817 RL78_STACK_POP (tmp2);
1819 RL78_STACK_PUSH (tmp1);
1823 RL78_STACK_POP (tmp1);
1824 RL78_STACK_POP (tmp2);
1826 RL78_STACK_PUSH (tmp1);
1830 RL78_STACK_POP (tmp1);
1831 RL78_STACK_POP (tmp2);
1833 RL78_STACK_PUSH (tmp1);
1837 RL78_STACK_POP (tmp1);
1838 RL78_STACK_POP (tmp2);
1840 RL78_STACK_PUSH (tmp1);
1843 case R_RL78_OPsctsize:
1844 RL78_STACK_PUSH (input_section->size);
1847 case R_RL78_OPscttop:
1848 RL78_STACK_PUSH (input_section->output_section->vma);
1852 RL78_STACK_POP (tmp1);
1853 RL78_STACK_POP (tmp2);
1855 RL78_STACK_PUSH (tmp1);
1859 RL78_STACK_POP (tmp1);
1860 RL78_STACK_POP (tmp2);
1862 RL78_STACK_PUSH (tmp1);
1866 RL78_STACK_POP (tmp1);
1867 RL78_STACK_POP (tmp2);
1869 RL78_STACK_PUSH (tmp1);
1873 RL78_STACK_POP (tmp1);
1875 RL78_STACK_PUSH (tmp1);
1879 RL78_STACK_POP (tmp1);
1880 RL78_STACK_POP (tmp2);
1882 RL78_STACK_PUSH (tmp1);
1885 case R_RL78_OPromtop:
1886 RL78_STACK_PUSH (get_romstart (&r, info, input_bfd, input_section, rel->r_offset));
1889 case R_RL78_OPramtop:
1890 RL78_STACK_PUSH (get_ramstart (&r, info, input_bfd, input_section, rel->r_offset));
1893 case R_RL78_DIR16UL:
1895 case R_RL78_ABS16UL:
1898 RL78_STACK_POP (symval);
1904 case R_RL78_DIR16UW:
1906 case R_RL78_ABS16UW:
1909 RL78_STACK_POP (symval);
1917 RL78_STACK_POP (symval);
1928 int prefix; /* or -1 for "no prefix" */
1929 int insn; /* or -1 for "end of list" */
1930 int insn_for_saddr; /* or -1 for "no alternative" */
1931 int insn_for_sfr; /* or -1 for "no alternative" */
1932 } relax_addr16[] = {
1933 { -1, 0x02, 0x06, -1 }, /* ADDW AX, !addr16 */
1934 { -1, 0x22, 0x26, -1 }, /* SUBW AX, !addr16 */
1935 { -1, 0x42, 0x46, -1 }, /* CMPW AX, !addr16 */
1936 { -1, 0x40, 0x4a, -1 }, /* CMP !addr16, #byte */
1938 { -1, 0x0f, 0x0b, -1 }, /* ADD A, !addr16 */
1939 { -1, 0x1f, 0x1b, -1 }, /* ADDC A, !addr16 */
1940 { -1, 0x2f, 0x2b, -1 }, /* SUB A, !addr16 */
1941 { -1, 0x3f, 0x3b, -1 }, /* SUBC A, !addr16 */
1942 { -1, 0x4f, 0x4b, -1 }, /* CMP A, !addr16 */
1943 { -1, 0x5f, 0x5b, -1 }, /* AND A, !addr16 */
1944 { -1, 0x6f, 0x6b, -1 }, /* OR A, !addr16 */
1945 { -1, 0x7f, 0x7b, -1 }, /* XOR A, !addr16 */
1947 { -1, 0x8f, 0x8d, 0x8e }, /* MOV A, !addr16 */
1948 { -1, 0x9f, 0x9d, 0x9e }, /* MOV !addr16, A */
1949 { -1, 0xaf, 0xad, 0xae }, /* MOVW AX, !addr16 */
1950 { -1, 0xbf, 0xbd, 0xbe }, /* MOVW !addr16, AX */
1951 { -1, 0xcf, 0xcd, 0xce }, /* MOVW !addr16, #word */
1953 { -1, 0xa0, 0xa4, -1 }, /* INC !addr16 */
1954 { -1, 0xa2, 0xa6, -1 }, /* INCW !addr16 */
1955 { -1, 0xb0, 0xb4, -1 }, /* DEC !addr16 */
1956 { -1, 0xb2, 0xb6, -1 }, /* DECW !addr16 */
1958 { -1, 0xd5, 0xd4, -1 }, /* CMP0 !addr16 */
1959 { -1, 0xe5, 0xe4, -1 }, /* ONEB !addr16 */
1960 { -1, 0xf5, 0xf4, -1 }, /* CLRB !addr16 */
1962 { -1, 0xd9, 0xd8, -1 }, /* MOV X, !addr16 */
1963 { -1, 0xe9, 0xe8, -1 }, /* MOV B, !addr16 */
1964 { -1, 0xf9, 0xf8, -1 }, /* MOV C, !addr16 */
1965 { -1, 0xdb, 0xda, -1 }, /* MOVW BC, !addr16 */
1966 { -1, 0xeb, 0xea, -1 }, /* MOVW DE, !addr16 */
1967 { -1, 0xfb, 0xfa, -1 }, /* MOVW HL, !addr16 */
1969 { 0x61, 0xaa, 0xa8, -1 }, /* XCH A, !addr16 */
1971 { 0x71, 0x00, 0x02, 0x0a }, /* SET1 !addr16.0 */
1972 { 0x71, 0x10, 0x12, 0x1a }, /* SET1 !addr16.0 */
1973 { 0x71, 0x20, 0x22, 0x2a }, /* SET1 !addr16.0 */
1974 { 0x71, 0x30, 0x32, 0x3a }, /* SET1 !addr16.0 */
1975 { 0x71, 0x40, 0x42, 0x4a }, /* SET1 !addr16.0 */
1976 { 0x71, 0x50, 0x52, 0x5a }, /* SET1 !addr16.0 */
1977 { 0x71, 0x60, 0x62, 0x6a }, /* SET1 !addr16.0 */
1978 { 0x71, 0x70, 0x72, 0x7a }, /* SET1 !addr16.0 */
1980 { 0x71, 0x08, 0x03, 0x0b }, /* CLR1 !addr16.0 */
1981 { 0x71, 0x18, 0x13, 0x1b }, /* CLR1 !addr16.0 */
1982 { 0x71, 0x28, 0x23, 0x2b }, /* CLR1 !addr16.0 */
1983 { 0x71, 0x38, 0x33, 0x3b }, /* CLR1 !addr16.0 */
1984 { 0x71, 0x48, 0x43, 0x4b }, /* CLR1 !addr16.0 */
1985 { 0x71, 0x58, 0x53, 0x5b }, /* CLR1 !addr16.0 */
1986 { 0x71, 0x68, 0x63, 0x6b }, /* CLR1 !addr16.0 */
1987 { 0x71, 0x78, 0x73, 0x7b }, /* CLR1 !addr16.0 */
1992 /* Relax one section. */
1995 rl78_elf_relax_section
1998 struct bfd_link_info * link_info,
1999 bfd_boolean * again)
2001 Elf_Internal_Shdr * symtab_hdr;
2002 Elf_Internal_Shdr * shndx_hdr;
2003 Elf_Internal_Rela * internal_relocs;
2004 Elf_Internal_Rela * free_relocs = NULL;
2005 Elf_Internal_Rela * irel;
2006 Elf_Internal_Rela * srel;
2007 Elf_Internal_Rela * irelend;
2008 Elf_Internal_Rela * next_alignment;
2009 bfd_byte * contents = NULL;
2010 bfd_byte * free_contents = NULL;
2011 Elf_Internal_Sym * intsyms = NULL;
2012 Elf_Internal_Sym * free_intsyms = NULL;
2013 Elf_External_Sym_Shndx * shndx_buf = NULL;
2015 bfd_vma symval ATTRIBUTE_UNUSED = 0;
2016 int pcrel ATTRIBUTE_UNUSED = 0;
2017 int code ATTRIBUTE_UNUSED = 0;
2018 int section_alignment_glue;
2021 if (abfd == elf_hash_table (link_info)->dynobj
2022 && strcmp (sec->name, ".plt") == 0)
2023 return rl78_elf_relax_plt_section (abfd, sec, link_info, again);
2025 /* Assume nothing changes. */
2028 /* We don't have to do anything for a relocatable link, if
2029 this section does not have relocs, or if this is not a
2031 if (link_info->relocatable
2032 || (sec->flags & SEC_RELOC) == 0
2033 || sec->reloc_count == 0
2034 || (sec->flags & SEC_CODE) == 0)
2037 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2038 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2040 /* Get the section contents. */
2041 if (elf_section_data (sec)->this_hdr.contents != NULL)
2042 contents = elf_section_data (sec)->this_hdr.contents;
2043 /* Go get them off disk. */
2046 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2048 elf_section_data (sec)->this_hdr.contents = contents;
2051 /* Read this BFD's symbols. */
2052 /* Get cached copy if it exists. */
2053 if (symtab_hdr->contents != NULL)
2054 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2057 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
2058 symtab_hdr->contents = (bfd_byte *) intsyms;
2061 if (shndx_hdr->sh_size != 0)
2065 amt = symtab_hdr->sh_info;
2066 amt *= sizeof (Elf_External_Sym_Shndx);
2067 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2068 if (shndx_buf == NULL)
2070 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2071 || bfd_bread (shndx_buf, amt, abfd) != amt)
2073 shndx_hdr->contents = (bfd_byte *) shndx_buf;
2076 /* Get a copy of the native relocations. */
2077 internal_relocs = (_bfd_elf_link_read_relocs
2078 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2079 link_info->keep_memory));
2080 if (internal_relocs == NULL)
2082 if (! link_info->keep_memory)
2083 free_relocs = internal_relocs;
2085 /* The RL_ relocs must be just before the operand relocs they go
2086 with, so we must sort them to guarantee this. We use bubblesort
2087 instead of qsort so we can guarantee that relocs with the same
2088 address remain in the same relative order. */
2089 reloc_bubblesort (internal_relocs, sec->reloc_count);
2091 /* Walk through them looking for relaxing opportunities. */
2092 irelend = internal_relocs + sec->reloc_count;
2095 /* This will either be NULL or a pointer to the next alignment
2097 next_alignment = internal_relocs;
2099 /* We calculate worst case shrinkage caused by alignment directives.
2100 No fool-proof, but better than either ignoring the problem or
2101 doing heavy duty analysis of all the alignment markers in all
2103 section_alignment_glue = 0;
2104 for (irel = internal_relocs; irel < irelend; irel++)
2105 if (ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX
2106 && irel->r_addend & RL78_RELAXA_ALIGN)
2108 int this_glue = 1 << (irel->r_addend & RL78_RELAXA_ANUM);
2110 if (section_alignment_glue < this_glue)
2111 section_alignment_glue = this_glue;
2113 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2115 section_alignment_glue *= 2;
2117 for (irel = internal_relocs; irel < irelend; irel++)
2119 unsigned char *insn;
2122 /* The insns we care about are all marked with one of these. */
2123 if (ELF32_R_TYPE (irel->r_info) != R_RL78_RH_RELAX)
2126 if (irel->r_addend & RL78_RELAXA_ALIGN
2127 || next_alignment == internal_relocs)
2129 /* When we delete bytes, we need to maintain all the alignments
2130 indicated. In addition, we need to be careful about relaxing
2131 jumps across alignment boundaries - these displacements
2132 *grow* when we delete bytes. For now, don't shrink
2133 displacements across an alignment boundary, just in case.
2134 Note that this only affects relocations to the same
2136 next_alignment += 2;
2137 while (next_alignment < irelend
2138 && (ELF32_R_TYPE (next_alignment->r_info) != R_RL78_RH_RELAX
2139 || !(next_alignment->r_addend & RL78_RELAXA_ELIGN)))
2141 if (next_alignment >= irelend || next_alignment->r_offset == 0)
2142 next_alignment = NULL;
2145 /* When we hit alignment markers, see if we've shrunk enough
2146 before them to reduce the gap without violating the alignment
2148 if (irel->r_addend & RL78_RELAXA_ALIGN)
2150 /* At this point, the next relocation *should* be the ELIGN
2152 Elf_Internal_Rela *erel = irel + 1;
2153 unsigned int alignment, nbytes;
2155 if (ELF32_R_TYPE (erel->r_info) != R_RL78_RH_RELAX)
2157 if (!(erel->r_addend & RL78_RELAXA_ELIGN))
2160 alignment = 1 << (irel->r_addend & RL78_RELAXA_ANUM);
2162 if (erel->r_offset - irel->r_offset < alignment)
2165 nbytes = erel->r_offset - irel->r_offset;
2166 nbytes /= alignment;
2167 nbytes *= alignment;
2169 elf32_rl78_relax_delete_bytes (abfd, sec, erel->r_offset - nbytes, nbytes,
2170 next_alignment, erel->r_offset == sec->size);
2176 if (irel->r_addend & RL78_RELAXA_ELIGN)
2179 insn = contents + irel->r_offset;
2181 nrelocs = irel->r_addend & RL78_RELAXA_RNUM;
2183 /* At this point, we have an insn that is a candidate for linker
2184 relaxation. There are NRELOCS relocs following that may be
2185 relaxed, although each reloc may be made of more than one
2186 reloc entry (such as gp-rel symbols). */
2188 /* Get the value of the symbol referred to by the reloc. Just
2189 in case this is the last reloc in the list, use the RL's
2190 addend to choose between this reloc (no addend) or the next
2191 (yes addend, which means at least one following reloc). */
2193 /* srel points to the "current" reloction for this insn -
2194 actually the last reloc for a given operand, which is the one
2195 we need to update. We check the relaxations in the same
2196 order that the relocations happen, so we'll just push it
2200 pc = sec->output_section->vma + sec->output_offset
2204 BFD_ASSERT (nrelocs > 0); \
2205 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2206 pcrel = symval - pc + srel->r_addend; \
2209 #define SNIPNR(offset, nbytes) \
2210 elf32_rl78_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0);
2212 #define SNIP(offset, nbytes, newtype) \
2213 SNIPNR (offset, nbytes); \
2214 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2216 /* The order of these bit tests must match the order that the
2217 relocs appear in. Since we sorted those by offset, we can
2220 /*----------------------------------------------------------------------*/
2221 /* EF ad BR $rel8 pcrel
2222 ED al ah BR !abs16 abs
2223 EE al ah BR $!rel16 pcrel
2224 EC al ah as BR !!abs20 abs
2226 FD al ah CALL !abs16 abs
2227 FE al ah CALL $!rel16 pcrel
2228 FC al ah as CALL !!abs20 abs
2236 61 C8 EF ad SKC ; BR $rel8
2237 61 D8 EF ad SKNC ; BR $rel8
2238 61 E8 EF ad SKZ ; BR $rel8
2239 61 F8 EF ad SKNZ ; BR $rel8
2240 61 E3 EF ad SKH ; BR $rel8
2241 61 F3 EF ad SKNH ; BR $rel8
2244 if ((irel->r_addend & RL78_RELAXA_MASK) == RL78_RELAXA_BRA)
2246 /* SKIP opcodes that skip non-branches will have a relax tag
2247 but no corresponding symbol to relax against; we just
2249 if (irel->r_addend & RL78_RELAXA_RNUM)
2256 case 0xec: /* BR !!abs20 */
2263 SNIP (2, 2, R_RL78_DIR8S_PCREL);
2266 else if (symval < 65536)
2269 insn[1] = symval & 0xff;
2270 insn[2] = symval >> 8;
2271 SNIP (2, 1, R_RL78_DIR16S);
2274 else if (pcrel < 32767
2278 insn[1] = pcrel & 0xff;
2279 insn[2] = pcrel >> 8;
2280 SNIP (2, 1, R_RL78_DIR16S_PCREL);
2285 case 0xee: /* BR $!pcrel16 */
2286 case 0xed: /* BR $!abs16 */
2292 SNIP (2, 1, R_RL78_DIR8S_PCREL);
2297 case 0xfc: /* CALL !!abs20 */
2301 insn[1] = symval & 0xff;
2302 insn[2] = symval >> 8;
2303 SNIP (2, 1, R_RL78_DIR16S);
2306 else if (pcrel < 32767
2310 insn[1] = pcrel & 0xff;
2311 insn[2] = pcrel >> 8;
2312 SNIP (2, 1, R_RL78_DIR16S_PCREL);
2317 case 0x61: /* PREFIX */
2318 /* For SKIP/BR, we change the BR opcode and delete the
2319 SKIP. That way, we don't have to find and change the
2320 relocation for the BR. */
2321 /* Note that, for the case where we're skipping some
2322 other insn, we have no "other" reloc but that's safe
2326 case 0xc8: /* SKC */
2327 if (insn[2] == 0xef)
2329 insn[2] = 0xde; /* BNC */
2334 case 0xd8: /* SKNC */
2335 if (insn[2] == 0xef)
2337 insn[2] = 0xdc; /* BC */
2342 case 0xe8: /* SKZ */
2343 if (insn[2] == 0xef)
2345 insn[2] = 0xdf; /* BNZ */
2350 case 0xf8: /* SKNZ */
2351 if (insn[2] == 0xef)
2353 insn[2] = 0xdd; /* BZ */
2358 case 0xe3: /* SKH */
2359 if (insn[2] == 0xef)
2361 insn[2] = 0xd3; /* BNH */
2362 SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */
2366 case 0xf3: /* SKNH */
2367 if (insn[2] == 0xef)
2369 insn[2] = 0xc3; /* BH */
2370 SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */
2378 if ((irel->r_addend & RL78_RELAXA_MASK) == RL78_RELAXA_ADDR16
2381 /*----------------------------------------------------------------------*/
2382 /* Some insns have both a 16-bit address operand and an 8-bit
2383 variant if the address is within a special range:
2385 Address 16-bit operand SADDR range SFR range
2386 FFF00-FFFFF 0xff00-0xffff 0x00-0xff
2387 FFE20-FFF1F 0xfe20-0xff1f 0x00-0xff
2389 The RELAX_ADDR16[] array has the insn encodings for the
2390 16-bit operand version, as well as the SFR and SADDR
2391 variants. We only need to replace the encodings and
2394 Note: we intentionally do not attempt to decode and skip
2395 any ES: prefix, as adding ES: means the addr16 (likely)
2396 no longer points to saddr/sfr space.
2406 if (0xffe20 <= symval && symval <= 0xfffff)
2409 is_saddr = (0xffe20 <= symval && symval <= 0xfff1f);
2410 is_sfr = (0xfff00 <= symval && symval <= 0xfffff);
2412 for (idx = 0; relax_addr16[idx].insn != -1; idx ++)
2414 if (relax_addr16[idx].prefix != -1
2415 && insn[0] == relax_addr16[idx].prefix
2416 && insn[1] == relax_addr16[idx].insn)
2420 else if (relax_addr16[idx].prefix == -1
2421 && insn[0] == relax_addr16[idx].insn)
2428 /* We have a matched insn, and poff is 0 or 1 depending
2429 on the base pattern size. */
2431 if (is_sfr && relax_addr16[idx].insn_for_sfr != -1)
2433 insn[poff] = relax_addr16[idx].insn_for_sfr;
2434 SNIP (poff+2, 1, R_RL78_RH_SFR);
2437 else if (is_saddr && relax_addr16[idx].insn_for_saddr != -1)
2439 insn[poff] = relax_addr16[idx].insn_for_saddr;
2440 SNIP (poff+2, 1, R_RL78_RH_SADDR);
2445 /*----------------------------------------------------------------------*/
2451 if (free_relocs != NULL)
2454 if (free_contents != NULL)
2455 free (free_contents);
2457 if (shndx_buf != NULL)
2459 shndx_hdr->contents = NULL;
2463 if (free_intsyms != NULL)
2464 free (free_intsyms);
2471 #define ELF_ARCH bfd_arch_rl78
2472 #define ELF_MACHINE_CODE EM_RL78
2473 #define ELF_MAXPAGESIZE 0x1000
2475 #define TARGET_LITTLE_SYM rl78_elf32_vec
2476 #define TARGET_LITTLE_NAME "elf32-rl78"
2478 #define elf_info_to_howto_rel NULL
2479 #define elf_info_to_howto rl78_info_to_howto_rela
2480 #define elf_backend_object_p rl78_elf_object_p
2481 #define elf_backend_relocate_section rl78_elf_relocate_section
2482 #define elf_symbol_leading_char ('_')
2483 #define elf_backend_can_gc_sections 1
2485 #define bfd_elf32_bfd_reloc_type_lookup rl78_reloc_type_lookup
2486 #define bfd_elf32_bfd_reloc_name_lookup rl78_reloc_name_lookup
2487 #define bfd_elf32_bfd_set_private_flags rl78_elf_set_private_flags
2488 #define bfd_elf32_bfd_merge_private_bfd_data rl78_elf_merge_private_bfd_data
2489 #define bfd_elf32_bfd_print_private_bfd_data rl78_elf_print_private_bfd_data
2491 #define bfd_elf32_bfd_relax_section rl78_elf_relax_section
2492 #define elf_backend_check_relocs rl78_elf_check_relocs
2493 #define elf_backend_always_size_sections \
2494 rl78_elf_always_size_sections
2495 #define elf_backend_finish_dynamic_sections \
2496 rl78_elf_finish_dynamic_sections
2498 #include "elf32-target.h"