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 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
501 relocation, *plt_offset);*/
502 if (! valid_16bit_address (relocation))
504 /* If this is the first time we've processed this symbol,
505 fill in the plt entry with the correct symbol address. */
506 if ((*plt_offset & 1) == 0)
510 x = 0x000000ec; /* br !!abs24 */
511 x |= (relocation << 8) & 0xffffff00;
512 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
516 relocation = (splt->output_section->vma
517 + splt->output_offset
518 + (*plt_offset & -2));
521 char *newname = bfd_malloc (strlen(name)+5);
522 strcpy (newname, name);
523 strcat(newname, ".plt");
524 _bfd_generic_link_add_one_symbol (info,
527 BSF_FUNCTION | BSF_WEAK,
540 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
541 /* If the symbol is undefined and weak
542 then the relocation resolves to zero. */
546 if (howto->pc_relative)
548 relocation -= (input_section->output_section->vma
549 + input_section->output_offset
551 relocation -= bfd_get_reloc_size (howto);
554 relocation += rel->r_addend;
559 #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow
560 #define ALIGN(m) if (relocation & m) r = bfd_reloc_other;
561 #define OP(i) (contents[rel->r_offset + (i)])
563 /* Opcode relocs are always big endian. Data relocs are bi-endian. */
569 case R_RL78_RH_RELAX:
572 case R_RL78_DIR8S_PCREL:
587 case R_RL78_DIR16S_PCREL:
588 RANGE (-32768, 32767);
590 OP (1) = relocation >> 8;
594 if ((relocation & 0xf0000) == 0xf0000)
595 relocation &= 0xffff;
596 RANGE (-32768, 65535);
598 OP (1) = relocation >> 8;
604 OP (1) = relocation >> 8;
608 RANGE (-32768, 65536);
610 OP (1) = relocation >> 8;
613 case R_RL78_DIR16_REV:
614 RANGE (-32768, 65536);
616 OP (0) = relocation >> 8;
619 case R_RL78_DIR3U_PCREL:
622 OP (0) |= relocation & 0x07;
625 case R_RL78_DIR24S_PCREL:
626 RANGE (-0x800000, 0x7fffff);
628 OP (1) = relocation >> 8;
629 OP (2) = relocation >> 16;
633 RANGE (-0x800000, 0x7fffff);
635 OP (1) = relocation >> 8;
636 OP (2) = relocation >> 16;
641 OP (1) = relocation >> 8;
642 OP (2) = relocation >> 16;
643 OP (3) = relocation >> 24;
646 case R_RL78_DIR32_REV:
648 OP (2) = relocation >> 8;
649 OP (1) = relocation >> 16;
650 OP (0) = relocation >> 24;
654 printf("SFR 0x%lx\n", relocation);
655 RANGE (0xfff00, 0xfffff);
656 OP (0) = relocation & 0xff;
659 case R_RL78_RH_SADDR:
660 printf("SADDR 0x%lx\n", relocation);
661 RANGE (0xffe20, 0xfff1f);
662 OP (0) = relocation & 0xff;
666 /* Complex reloc handling: */
669 RL78_STACK_POP (relocation);
671 OP (1) = relocation >> 8;
672 OP (2) = relocation >> 16;
673 OP (3) = relocation >> 24;
676 case R_RL78_ABS32_REV:
677 RL78_STACK_POP (relocation);
679 OP (2) = relocation >> 8;
680 OP (1) = relocation >> 16;
681 OP (0) = relocation >> 24;
684 case R_RL78_ABS24S_PCREL:
686 RL78_STACK_POP (relocation);
687 RANGE (-0x800000, 0x7fffff);
689 OP (1) = relocation >> 8;
690 OP (2) = relocation >> 16;
694 RL78_STACK_POP (relocation);
695 RANGE (-32768, 65535);
697 OP (1) = relocation >> 8;
700 case R_RL78_ABS16_REV:
701 RL78_STACK_POP (relocation);
702 RANGE (-32768, 65535);
704 OP (0) = relocation >> 8;
707 case R_RL78_ABS16S_PCREL:
709 RL78_STACK_POP (relocation);
710 RANGE (-32768, 32767);
712 OP (1) = relocation >> 8;
716 RL78_STACK_POP (relocation);
719 OP (1) = relocation >> 8;
723 RL78_STACK_POP (relocation);
727 OP (1) = relocation >> 8;
731 RL78_STACK_POP (relocation);
735 OP (1) = relocation >> 8;
739 RL78_STACK_POP (relocation);
745 RL78_STACK_POP (relocation);
751 RL78_STACK_POP (relocation);
758 RL78_STACK_POP (relocation);
764 case R_RL78_ABS8S_PCREL:
766 RL78_STACK_POP (relocation);
772 if (r_symndx < symtab_hdr->sh_info)
773 RL78_STACK_PUSH (sec->output_section->vma
780 && (h->root.type == bfd_link_hash_defined
781 || h->root.type == bfd_link_hash_defweak))
782 RL78_STACK_PUSH (h->root.u.def.value
783 + sec->output_section->vma
787 _bfd_error_handler (_("Warning: RL78_SYM reloc with an unknown symbol"));
795 RL78_STACK_POP (tmp);
797 RL78_STACK_PUSH (tmp);
805 RL78_STACK_POP (tmp2);
806 RL78_STACK_POP (tmp1);
808 RL78_STACK_PUSH (tmp1);
816 RL78_STACK_POP (tmp2);
817 RL78_STACK_POP (tmp1);
819 RL78_STACK_PUSH (tmp2);
827 RL78_STACK_POP (tmp2);
828 RL78_STACK_POP (tmp1);
830 RL78_STACK_PUSH (tmp1);
838 RL78_STACK_POP (tmp2);
839 RL78_STACK_POP (tmp1);
841 RL78_STACK_PUSH (tmp1);
849 RL78_STACK_POP (tmp2);
850 RL78_STACK_POP (tmp1);
852 RL78_STACK_PUSH (tmp1);
860 RL78_STACK_POP (tmp2);
861 RL78_STACK_POP (tmp1);
863 RL78_STACK_PUSH (tmp1);
867 case R_RL78_OPsctsize:
868 RL78_STACK_PUSH (input_section->size);
871 case R_RL78_OPscttop:
872 RL78_STACK_PUSH (input_section->output_section->vma);
879 RL78_STACK_POP (tmp2);
880 RL78_STACK_POP (tmp1);
882 RL78_STACK_PUSH (tmp1);
890 RL78_STACK_POP (tmp2);
891 RL78_STACK_POP (tmp1);
893 RL78_STACK_PUSH (tmp1);
901 RL78_STACK_POP (tmp2);
902 RL78_STACK_POP (tmp1);
904 RL78_STACK_PUSH (tmp1);
912 RL78_STACK_POP (tmp);
914 RL78_STACK_PUSH (tmp);
922 RL78_STACK_POP (tmp2);
923 RL78_STACK_POP (tmp1);
925 RL78_STACK_PUSH (tmp1);
929 case R_RL78_OPromtop:
930 RL78_STACK_PUSH (get_romstart (&r, info, input_bfd, input_section, rel->r_offset));
933 case R_RL78_OPramtop:
934 RL78_STACK_PUSH (get_ramstart (&r, info, input_bfd, input_section, rel->r_offset));
938 r = bfd_reloc_notsupported;
942 if (r != bfd_reloc_ok)
944 const char * msg = NULL;
948 case bfd_reloc_overflow:
949 /* Catch the case of a missing function declaration
950 and emit a more helpful error message. */
951 if (r_type == R_RL78_DIR24S_PCREL)
952 msg = _("%B(%A): error: call to undefined function '%s'");
954 r = info->callbacks->reloc_overflow
955 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
956 input_bfd, input_section, rel->r_offset);
959 case bfd_reloc_undefined:
960 r = info->callbacks->undefined_symbol
961 (info, name, input_bfd, input_section, rel->r_offset,
965 case bfd_reloc_other:
966 msg = _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
969 case bfd_reloc_outofrange:
970 msg = _("%B(%A): internal error: out of range error");
973 case bfd_reloc_notsupported:
974 msg = _("%B(%A): internal error: unsupported relocation error");
977 case bfd_reloc_dangerous:
978 msg = _("%B(%A): internal error: dangerous relocation");
982 msg = _("%B(%A): internal error: unknown error");
987 _bfd_error_handler (msg, input_bfd, input_section, name);
997 /* Function to set the ELF flag bits. */
1000 rl78_elf_set_private_flags (bfd * abfd, flagword flags)
1002 elf_elfheader (abfd)->e_flags = flags;
1003 elf_flags_init (abfd) = TRUE;
1007 static bfd_boolean no_warn_mismatch = FALSE;
1009 void bfd_elf32_rl78_set_target_flags (bfd_boolean);
1012 bfd_elf32_rl78_set_target_flags (bfd_boolean user_no_warn_mismatch)
1014 no_warn_mismatch = user_no_warn_mismatch;
1017 /* Merge backend specific data from an object file to the output
1018 object file when linking. */
1021 rl78_elf_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
1024 bfd_boolean error = FALSE;
1026 new_flags = elf_elfheader (ibfd)->e_flags;
1028 if (!elf_flags_init (obfd))
1030 /* First call, no flags set. */
1031 elf_flags_init (obfd) = TRUE;
1032 elf_elfheader (obfd)->e_flags = new_flags;
1039 rl78_elf_print_private_bfd_data (bfd * abfd, void * ptr)
1041 FILE * file = (FILE *) ptr;
1044 BFD_ASSERT (abfd != NULL && ptr != NULL);
1046 /* Print normal ELF private data. */
1047 _bfd_elf_print_private_bfd_data (abfd, ptr);
1049 flags = elf_elfheader (abfd)->e_flags;
1050 fprintf (file, _("private flags = 0x%lx:"), (long) flags);
1056 /* Return the MACH for an e_flags value. */
1059 elf32_rl78_machine (bfd * abfd)
1061 if ((elf_elfheader (abfd)->e_flags & EF_RL78_CPU_MASK) == EF_RL78_CPU_RL78)
1062 return bfd_mach_rl78;
1068 rl78_elf_object_p (bfd * abfd)
1070 bfd_default_set_arch_mach (abfd, bfd_arch_rl78,
1071 elf32_rl78_machine (abfd));
1077 rl78_dump_symtab (bfd * abfd, void * internal_syms, void * external_syms)
1080 Elf_Internal_Sym * isymbuf;
1081 Elf_Internal_Sym * isymend;
1082 Elf_Internal_Sym * isym;
1083 Elf_Internal_Shdr * symtab_hdr;
1084 bfd_boolean free_internal = FALSE, free_external = FALSE;
1086 char * st_info_stb_str;
1087 char * st_other_str;
1088 char * st_shndx_str;
1090 if (! internal_syms)
1092 internal_syms = bfd_malloc (1000);
1095 if (! external_syms)
1097 external_syms = bfd_malloc (1000);
1101 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1102 locsymcount = symtab_hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
1104 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1105 symtab_hdr->sh_info, 0,
1106 internal_syms, external_syms, NULL);
1108 isymbuf = internal_syms;
1109 isymend = isymbuf + locsymcount;
1111 for (isym = isymbuf ; isym < isymend ; isym++)
1113 switch (ELF_ST_TYPE (isym->st_info))
1115 case STT_FUNC: st_info_str = "STT_FUNC";
1116 case STT_SECTION: st_info_str = "STT_SECTION";
1117 case STT_FILE: st_info_str = "STT_FILE";
1118 case STT_OBJECT: st_info_str = "STT_OBJECT";
1119 case STT_TLS: st_info_str = "STT_TLS";
1120 default: st_info_str = "";
1122 switch (ELF_ST_BIND (isym->st_info))
1124 case STB_LOCAL: st_info_stb_str = "STB_LOCAL";
1125 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL";
1126 default: st_info_stb_str = "";
1128 switch (ELF_ST_VISIBILITY (isym->st_other))
1130 case STV_DEFAULT: st_other_str = "STV_DEFAULT";
1131 case STV_INTERNAL: st_other_str = "STV_INTERNAL";
1132 case STV_PROTECTED: st_other_str = "STV_PROTECTED";
1133 default: st_other_str = "";
1135 switch (isym->st_shndx)
1137 case SHN_ABS: st_shndx_str = "SHN_ABS";
1138 case SHN_COMMON: st_shndx_str = "SHN_COMMON";
1139 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF";
1140 default: st_shndx_str = "";
1143 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
1144 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1146 (unsigned long) isym->st_value,
1147 (unsigned long) isym->st_size,
1149 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
1151 isym->st_info, st_info_str, st_info_stb_str,
1152 isym->st_other, st_other_str,
1153 isym->st_shndx, st_shndx_str);
1156 free (internal_syms);
1158 free (external_syms);
1162 rl78_get_reloc (long reloc)
1164 if (0 <= reloc && reloc < R_RL78_max)
1165 return rl78_elf_howto_table[reloc].name;
1171 /* support PLT for 16-bit references to 24-bit functions. */
1173 /* We support 16-bit pointers to code above 64k by generating a thunk
1174 below 64k containing a JMP instruction to the final address. */
1177 rl78_elf_check_relocs
1179 struct bfd_link_info * info,
1181 const Elf_Internal_Rela * relocs)
1183 Elf_Internal_Shdr * symtab_hdr;
1184 struct elf_link_hash_entry ** sym_hashes;
1185 const Elf_Internal_Rela * rel;
1186 const Elf_Internal_Rela * rel_end;
1187 bfd_vma *local_plt_offsets;
1191 if (info->relocatable)
1194 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1195 sym_hashes = elf_sym_hashes (abfd);
1196 local_plt_offsets = elf_local_got_offsets (abfd);
1198 dynobj = elf_hash_table(info)->dynobj;
1200 rel_end = relocs + sec->reloc_count;
1201 for (rel = relocs; rel < rel_end; rel++)
1203 struct elf_link_hash_entry *h;
1204 unsigned long r_symndx;
1207 r_symndx = ELF32_R_SYM (rel->r_info);
1208 if (r_symndx < symtab_hdr->sh_info)
1212 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1213 while (h->root.type == bfd_link_hash_indirect
1214 || h->root.type == bfd_link_hash_warning)
1215 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1218 switch (ELF32_R_TYPE (rel->r_info))
1220 /* This relocation describes a 16-bit pointer to a function.
1221 We may need to allocate a thunk in low memory; reserve memory
1225 elf_hash_table (info)->dynobj = dynobj = abfd;
1228 splt = bfd_get_section_by_name (dynobj, ".plt");
1231 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1232 | SEC_IN_MEMORY | SEC_LINKER_CREATED
1233 | SEC_READONLY | SEC_CODE);
1234 splt = bfd_make_section_with_flags (dynobj, ".plt", flags);
1236 || ! bfd_set_section_alignment (dynobj, splt, 1))
1242 offset = &h->plt.offset;
1245 if (local_plt_offsets == NULL)
1250 size = symtab_hdr->sh_info * sizeof (bfd_vma);
1251 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
1252 if (local_plt_offsets == NULL)
1254 elf_local_got_offsets (abfd) = local_plt_offsets;
1256 for (i = 0; i < symtab_hdr->sh_info; i++)
1257 local_plt_offsets[i] = (bfd_vma) -1;
1259 offset = &local_plt_offsets[r_symndx];
1262 if (*offset == (bfd_vma) -1)
1264 *offset = splt->size;
1274 /* This must exist if dynobj is ever set. */
1277 rl78_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
1278 struct bfd_link_info *info)
1283 /* As an extra sanity check, verify that all plt entries have been
1284 filled in. However, relaxing might have changed the relocs so
1285 that some plt entries don't get filled in, so we have to skip
1286 this check if we're relaxing. Unfortunately, check_relocs is
1287 called before relaxation. */
1289 if (info->relax_trip > 0)
1291 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
1292 && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL)
1294 bfd_byte *contents = splt->contents;
1295 unsigned int i, size = splt->size;
1296 for (i = 0; i < size; i += 4)
1298 unsigned int x = bfd_get_32 (dynobj, contents + i);
1299 BFD_ASSERT (x != 0);
1308 rl78_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1309 struct bfd_link_info *info)
1314 if (info->relocatable)
1317 dynobj = elf_hash_table (info)->dynobj;
1321 splt = bfd_get_section_by_name (dynobj, ".plt");
1322 BFD_ASSERT (splt != NULL);
1324 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
1325 if (splt->contents == NULL)
1333 /* Handle relaxing. */
1335 /* A subroutine of rl78_elf_relax_section. If the global symbol H
1336 is within the low 64k, remove any entry for it in the plt. */
1338 struct relax_plt_data
1345 rl78_relax_plt_check (struct elf_link_hash_entry *h,
1348 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1350 if (h->plt.offset != (bfd_vma) -1)
1354 if (h->root.type == bfd_link_hash_undefined
1355 || h->root.type == bfd_link_hash_undefweak)
1358 address = (h->root.u.def.section->output_section->vma
1359 + h->root.u.def.section->output_offset
1360 + h->root.u.def.value);
1362 if (valid_16bit_address (address))
1365 data->splt->size -= 4;
1366 *data->again = TRUE;
1373 /* A subroutine of rl78_elf_relax_section. If the global symbol H
1374 previously had a plt entry, give it a new entry offset. */
1377 rl78_relax_plt_realloc (struct elf_link_hash_entry *h,
1380 bfd_vma *entry = (bfd_vma *) xdata;
1382 if (h->plt.offset != (bfd_vma) -1)
1384 h->plt.offset = *entry;
1392 rl78_elf_relax_plt_section (bfd *dynobj,
1394 struct bfd_link_info *info,
1397 struct relax_plt_data relax_plt_data;
1400 /* Assume nothing changes. */
1403 if (info->relocatable)
1406 /* We only relax the .plt section at the moment. */
1407 if (dynobj != elf_hash_table (info)->dynobj
1408 || strcmp (splt->name, ".plt") != 0)
1411 /* Quick check for an empty plt. */
1412 if (splt->size == 0)
1415 /* Map across all global symbols; see which ones happen to
1416 fall in the low 64k. */
1417 relax_plt_data.splt = splt;
1418 relax_plt_data.again = again;
1419 elf_link_hash_traverse (elf_hash_table (info), rl78_relax_plt_check,
1422 /* Likewise for local symbols, though that's somewhat less convenient
1423 as we have to walk the list of input bfds and swap in symbol data. */
1424 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1426 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1427 Elf_Internal_Shdr *symtab_hdr;
1428 Elf_Internal_Sym *isymbuf = NULL;
1431 if (! local_plt_offsets)
1434 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1435 if (symtab_hdr->sh_info != 0)
1437 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1438 if (isymbuf == NULL)
1439 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1440 symtab_hdr->sh_info, 0,
1442 if (isymbuf == NULL)
1446 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1448 Elf_Internal_Sym *isym;
1452 if (local_plt_offsets[idx] == (bfd_vma) -1)
1455 isym = &isymbuf[idx];
1456 if (isym->st_shndx == SHN_UNDEF)
1458 else if (isym->st_shndx == SHN_ABS)
1459 tsec = bfd_abs_section_ptr;
1460 else if (isym->st_shndx == SHN_COMMON)
1461 tsec = bfd_com_section_ptr;
1463 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1465 address = (tsec->output_section->vma
1466 + tsec->output_offset
1468 if (valid_16bit_address (address))
1470 local_plt_offsets[idx] = -1;
1477 && symtab_hdr->contents != (unsigned char *) isymbuf)
1479 if (! info->keep_memory)
1483 /* Cache the symbols for elf_link_input_bfd. */
1484 symtab_hdr->contents = (unsigned char *) isymbuf;
1489 /* If we changed anything, walk the symbols again to reallocate
1490 .plt entry addresses. */
1491 if (*again && splt->size > 0)
1495 elf_link_hash_traverse (elf_hash_table (info),
1496 rl78_relax_plt_realloc, &entry);
1498 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1500 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1501 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1504 if (! local_plt_offsets)
1507 for (idx = 0; idx < nlocals; ++idx)
1508 if (local_plt_offsets[idx] != (bfd_vma) -1)
1510 local_plt_offsets[idx] = entry;
1519 /* Delete some bytes from a section while relaxing. */
1522 elf32_rl78_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
1523 Elf_Internal_Rela *alignment_rel, int force_snip)
1525 Elf_Internal_Shdr * symtab_hdr;
1526 unsigned int sec_shndx;
1527 bfd_byte * contents;
1528 Elf_Internal_Rela * irel;
1529 Elf_Internal_Rela * irelend;
1530 Elf_Internal_Sym * isym;
1531 Elf_Internal_Sym * isymend;
1533 unsigned int symcount;
1534 struct elf_link_hash_entry ** sym_hashes;
1535 struct elf_link_hash_entry ** end_hashes;
1540 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1542 contents = elf_section_data (sec)->this_hdr.contents;
1544 /* The deletion must stop at the next alignment boundary, if
1545 ALIGNMENT_REL is non-NULL. */
1548 toaddr = alignment_rel->r_offset;
1550 irel = elf_section_data (sec)->relocs;
1551 irelend = irel + sec->reloc_count;
1553 /* Actually delete the bytes. */
1554 memmove (contents + addr, contents + addr + count,
1555 (size_t) (toaddr - addr - count));
1557 /* If we don't have an alignment marker to worry about, we can just
1558 shrink the section. Otherwise, we have to fill in the newly
1559 created gap with NOP insns (0x03). */
1563 memset (contents + toaddr - count, 0x03, count);
1565 /* Adjust all the relocs. */
1566 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1568 /* Get the new reloc address. */
1569 if (irel->r_offset > addr
1570 && (irel->r_offset < toaddr
1571 || (force_snip && irel->r_offset == toaddr)))
1572 irel->r_offset -= count;
1574 /* If we see an ALIGN marker at the end of the gap, we move it
1575 to the beginning of the gap, since marking these gaps is what
1577 if (irel->r_offset == toaddr
1578 && ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX
1579 && irel->r_addend & RL78_RELAXA_ALIGN)
1580 irel->r_offset -= count;
1583 /* Adjust the local symbols defined in this section. */
1584 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1585 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1586 isymend = isym + symtab_hdr->sh_info;
1588 for (; isym < isymend; isym++)
1590 /* If the symbol is in the range of memory we just moved, we
1591 have to adjust its value. */
1592 if (isym->st_shndx == sec_shndx
1593 && isym->st_value > addr
1594 && isym->st_value < toaddr)
1595 isym->st_value -= count;
1597 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1598 *end* is in the moved bytes but it's *start* isn't), then we
1599 must adjust its size. */
1600 if (isym->st_shndx == sec_shndx
1601 && isym->st_value < addr
1602 && isym->st_value + isym->st_size > addr
1603 && isym->st_value + isym->st_size < toaddr)
1604 isym->st_size -= count;
1607 /* Now adjust the global symbols defined in this section. */
1608 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1609 - symtab_hdr->sh_info);
1610 sym_hashes = elf_sym_hashes (abfd);
1611 end_hashes = sym_hashes + symcount;
1613 for (; sym_hashes < end_hashes; sym_hashes++)
1615 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1617 if ((sym_hash->root.type == bfd_link_hash_defined
1618 || sym_hash->root.type == bfd_link_hash_defweak)
1619 && sym_hash->root.u.def.section == sec)
1621 /* As above, adjust the value if needed. */
1622 if (sym_hash->root.u.def.value > addr
1623 && sym_hash->root.u.def.value < toaddr)
1624 sym_hash->root.u.def.value -= count;
1626 /* As above, adjust the size if needed. */
1627 if (sym_hash->root.u.def.value < addr
1628 && sym_hash->root.u.def.value + sym_hash->size > addr
1629 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1630 sym_hash->size -= count;
1637 /* Used to sort relocs by address. If relocs have the same address,
1638 we maintain their relative order, except that R_RL78_RH_RELAX
1639 alignment relocs must be the first reloc for any given address. */
1642 reloc_bubblesort (Elf_Internal_Rela * r, int count)
1646 bfd_boolean swappit;
1648 /* This is almost a classic bubblesort. It's the slowest sort, but
1649 we're taking advantage of the fact that the relocations are
1650 mostly in order already (the assembler emits them that way) and
1651 we need relocs with the same address to remain in the same
1657 for (i = 0; i < count - 1; i ++)
1659 if (r[i].r_offset > r[i + 1].r_offset)
1661 else if (r[i].r_offset < r[i + 1].r_offset)
1663 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX
1664 && (r[i + 1].r_addend & RL78_RELAXA_ALIGN))
1666 else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX
1667 && (r[i + 1].r_addend & RL78_RELAXA_ELIGN)
1668 && !(ELF32_R_TYPE (r[i].r_info) == R_RL78_RH_RELAX
1669 && (r[i].r_addend & RL78_RELAXA_ALIGN)))
1676 Elf_Internal_Rela tmp;
1681 /* If we do move a reloc back, re-scan to see if it
1682 needs to be moved even further back. This avoids
1683 most of the O(n^2) behavior for our cases. */
1693 #define OFFSET_FOR_RELOC(rel, lrel, scale) \
1694 rl78_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1695 lrel, abfd, sec, link_info, scale)
1698 rl78_offset_for_reloc (bfd * abfd,
1699 Elf_Internal_Rela * rel,
1700 Elf_Internal_Shdr * symtab_hdr,
1701 Elf_External_Sym_Shndx * shndx_buf ATTRIBUTE_UNUSED,
1702 Elf_Internal_Sym * intsyms,
1703 Elf_Internal_Rela ** lrel,
1705 asection * input_section,
1706 struct bfd_link_info * info,
1710 bfd_reloc_status_type r;
1714 /* REL is the first of 1..N relocations. We compute the symbol
1715 value for each relocation, then combine them if needed. LREL
1716 gets a pointer to the last relocation used. */
1721 /* Get the value of the symbol referred to by the reloc. */
1722 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1724 /* A local symbol. */
1725 Elf_Internal_Sym *isym;
1728 isym = intsyms + ELF32_R_SYM (rel->r_info);
1730 if (isym->st_shndx == SHN_UNDEF)
1731 ssec = bfd_und_section_ptr;
1732 else if (isym->st_shndx == SHN_ABS)
1733 ssec = bfd_abs_section_ptr;
1734 else if (isym->st_shndx == SHN_COMMON)
1735 ssec = bfd_com_section_ptr;
1737 ssec = bfd_section_from_elf_index (abfd,
1740 /* Initial symbol value. */
1741 symval = isym->st_value;
1743 /* GAS may have made this symbol relative to a section, in
1744 which case, we have to add the addend to find the
1746 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
1747 symval += rel->r_addend;
1751 if ((ssec->flags & SEC_MERGE)
1752 && ssec->sec_info_type == ELF_INFO_TYPE_MERGE)
1753 symval = _bfd_merged_section_offset (abfd, & ssec,
1754 elf_section_data (ssec)->sec_info,
1758 /* Now make the offset relative to where the linker is putting it. */
1761 ssec->output_section->vma + ssec->output_offset;
1763 symval += rel->r_addend;
1768 struct elf_link_hash_entry * h;
1770 /* An external symbol. */
1771 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1772 h = elf_sym_hashes (abfd)[indx];
1773 BFD_ASSERT (h != NULL);
1775 if (h->root.type != bfd_link_hash_defined
1776 && h->root.type != bfd_link_hash_defweak)
1778 /* This appears to be a reference to an undefined
1779 symbol. Just ignore it--it will be caught by the
1780 regular reloc processing. */
1786 symval = (h->root.u.def.value
1787 + h->root.u.def.section->output_section->vma
1788 + h->root.u.def.section->output_offset);
1790 symval += rel->r_addend;
1793 switch (ELF32_R_TYPE (rel->r_info))
1796 RL78_STACK_PUSH (symval);
1800 RL78_STACK_POP (tmp1);
1802 RL78_STACK_PUSH (tmp1);
1806 RL78_STACK_POP (tmp1);
1807 RL78_STACK_POP (tmp2);
1809 RL78_STACK_PUSH (tmp1);
1813 RL78_STACK_POP (tmp1);
1814 RL78_STACK_POP (tmp2);
1816 RL78_STACK_PUSH (tmp2);
1820 RL78_STACK_POP (tmp1);
1821 RL78_STACK_POP (tmp2);
1823 RL78_STACK_PUSH (tmp1);
1827 RL78_STACK_POP (tmp1);
1828 RL78_STACK_POP (tmp2);
1830 RL78_STACK_PUSH (tmp1);
1834 RL78_STACK_POP (tmp1);
1835 RL78_STACK_POP (tmp2);
1837 RL78_STACK_PUSH (tmp1);
1841 RL78_STACK_POP (tmp1);
1842 RL78_STACK_POP (tmp2);
1844 RL78_STACK_PUSH (tmp1);
1847 case R_RL78_OPsctsize:
1848 RL78_STACK_PUSH (input_section->size);
1851 case R_RL78_OPscttop:
1852 RL78_STACK_PUSH (input_section->output_section->vma);
1856 RL78_STACK_POP (tmp1);
1857 RL78_STACK_POP (tmp2);
1859 RL78_STACK_PUSH (tmp1);
1863 RL78_STACK_POP (tmp1);
1864 RL78_STACK_POP (tmp2);
1866 RL78_STACK_PUSH (tmp1);
1870 RL78_STACK_POP (tmp1);
1871 RL78_STACK_POP (tmp2);
1873 RL78_STACK_PUSH (tmp1);
1877 RL78_STACK_POP (tmp1);
1879 RL78_STACK_PUSH (tmp1);
1883 RL78_STACK_POP (tmp1);
1884 RL78_STACK_POP (tmp2);
1886 RL78_STACK_PUSH (tmp1);
1889 case R_RL78_OPromtop:
1890 RL78_STACK_PUSH (get_romstart (&r, info, input_bfd, input_section, rel->r_offset));
1893 case R_RL78_OPramtop:
1894 RL78_STACK_PUSH (get_ramstart (&r, info, input_bfd, input_section, rel->r_offset));
1897 case R_RL78_DIR16UL:
1899 case R_RL78_ABS16UL:
1902 RL78_STACK_POP (symval);
1908 case R_RL78_DIR16UW:
1910 case R_RL78_ABS16UW:
1913 RL78_STACK_POP (symval);
1921 RL78_STACK_POP (symval);
1932 int prefix; /* or -1 for "no prefix" */
1933 int insn; /* or -1 for "end of list" */
1934 int insn_for_saddr; /* or -1 for "no alternative" */
1935 int insn_for_sfr; /* or -1 for "no alternative" */
1936 } relax_addr16[] = {
1937 { -1, 0x02, 0x06, -1 }, /* ADDW AX, !addr16 */
1938 { -1, 0x22, 0x26, -1 }, /* SUBW AX, !addr16 */
1939 { -1, 0x42, 0x46, -1 }, /* CMPW AX, !addr16 */
1940 { -1, 0x40, 0x4a, -1 }, /* CMP !addr16, #byte */
1942 { -1, 0x0f, 0x0b, -1 }, /* ADD A, !addr16 */
1943 { -1, 0x1f, 0x1b, -1 }, /* ADDC A, !addr16 */
1944 { -1, 0x2f, 0x2b, -1 }, /* SUB A, !addr16 */
1945 { -1, 0x3f, 0x3b, -1 }, /* SUBC A, !addr16 */
1946 { -1, 0x4f, 0x4b, -1 }, /* CMP A, !addr16 */
1947 { -1, 0x5f, 0x5b, -1 }, /* AND A, !addr16 */
1948 { -1, 0x6f, 0x6b, -1 }, /* OR A, !addr16 */
1949 { -1, 0x7f, 0x7b, -1 }, /* XOR A, !addr16 */
1951 { -1, 0x8f, 0x8d, 0x8e }, /* MOV A, !addr16 */
1952 { -1, 0x9f, 0x9d, 0x9e }, /* MOV !addr16, A */
1953 { -1, 0xaf, 0xad, 0xae }, /* MOVW AX, !addr16 */
1954 { -1, 0xbf, 0xbd, 0xbe }, /* MOVW !addr16, AX */
1955 { -1, 0xcf, 0xcd, 0xce }, /* MOVW !addr16, #word */
1957 { -1, 0xa0, 0xa4, -1 }, /* INC !addr16 */
1958 { -1, 0xa2, 0xa6, -1 }, /* INCW !addr16 */
1959 { -1, 0xb0, 0xb4, -1 }, /* DEC !addr16 */
1960 { -1, 0xb2, 0xb6, -1 }, /* DECW !addr16 */
1962 { -1, 0xd5, 0xd4, -1 }, /* CMP0 !addr16 */
1963 { -1, 0xe5, 0xe4, -1 }, /* ONEB !addr16 */
1964 { -1, 0xf5, 0xf4, -1 }, /* CLRB !addr16 */
1966 { -1, 0xd9, 0xd8, -1 }, /* MOV X, !addr16 */
1967 { -1, 0xe9, 0xe8, -1 }, /* MOV B, !addr16 */
1968 { -1, 0xf9, 0xf8, -1 }, /* MOV C, !addr16 */
1969 { -1, 0xdb, 0xda, -1 }, /* MOVW BC, !addr16 */
1970 { -1, 0xeb, 0xea, -1 }, /* MOVW DE, !addr16 */
1971 { -1, 0xfb, 0xfa, -1 }, /* MOVW HL, !addr16 */
1973 { 0x61, 0xaa, 0xa8, -1 }, /* XCH A, !addr16 */
1975 { 0x71, 0x00, 0x02, 0x0a }, /* SET1 !addr16.0 */
1976 { 0x71, 0x10, 0x12, 0x1a }, /* SET1 !addr16.0 */
1977 { 0x71, 0x20, 0x22, 0x2a }, /* SET1 !addr16.0 */
1978 { 0x71, 0x30, 0x32, 0x3a }, /* SET1 !addr16.0 */
1979 { 0x71, 0x40, 0x42, 0x4a }, /* SET1 !addr16.0 */
1980 { 0x71, 0x50, 0x52, 0x5a }, /* SET1 !addr16.0 */
1981 { 0x71, 0x60, 0x62, 0x6a }, /* SET1 !addr16.0 */
1982 { 0x71, 0x70, 0x72, 0x7a }, /* SET1 !addr16.0 */
1984 { 0x71, 0x08, 0x03, 0x0b }, /* CLR1 !addr16.0 */
1985 { 0x71, 0x18, 0x13, 0x1b }, /* CLR1 !addr16.0 */
1986 { 0x71, 0x28, 0x23, 0x2b }, /* CLR1 !addr16.0 */
1987 { 0x71, 0x38, 0x33, 0x3b }, /* CLR1 !addr16.0 */
1988 { 0x71, 0x48, 0x43, 0x4b }, /* CLR1 !addr16.0 */
1989 { 0x71, 0x58, 0x53, 0x5b }, /* CLR1 !addr16.0 */
1990 { 0x71, 0x68, 0x63, 0x6b }, /* CLR1 !addr16.0 */
1991 { 0x71, 0x78, 0x73, 0x7b }, /* CLR1 !addr16.0 */
1996 /* Relax one section. */
1999 rl78_elf_relax_section
2002 struct bfd_link_info * link_info,
2003 bfd_boolean * again)
2005 Elf_Internal_Shdr * symtab_hdr;
2006 Elf_Internal_Shdr * shndx_hdr;
2007 Elf_Internal_Rela * internal_relocs;
2008 Elf_Internal_Rela * free_relocs = NULL;
2009 Elf_Internal_Rela * irel;
2010 Elf_Internal_Rela * srel;
2011 Elf_Internal_Rela * irelend;
2012 Elf_Internal_Rela * next_alignment;
2013 Elf_Internal_Rela * prev_alignment;
2014 bfd_byte * contents = NULL;
2015 bfd_byte * free_contents = NULL;
2016 Elf_Internal_Sym * intsyms = NULL;
2017 Elf_Internal_Sym * free_intsyms = NULL;
2018 Elf_External_Sym_Shndx * shndx_buf = NULL;
2021 bfd_vma symval ATTRIBUTE_UNUSED = 0;
2022 int pcrel ATTRIBUTE_UNUSED = 0;
2023 int code ATTRIBUTE_UNUSED = 0;
2024 int section_alignment_glue;
2027 if (abfd == elf_hash_table (link_info)->dynobj
2028 && strcmp (sec->name, ".plt") == 0)
2029 return rl78_elf_relax_plt_section (abfd, sec, link_info, again);
2031 /* Assume nothing changes. */
2034 /* We don't have to do anything for a relocatable link, if
2035 this section does not have relocs, or if this is not a
2037 if (link_info->relocatable
2038 || (sec->flags & SEC_RELOC) == 0
2039 || sec->reloc_count == 0
2040 || (sec->flags & SEC_CODE) == 0)
2043 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2044 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2046 sec_start = sec->output_section->vma + sec->output_offset;
2048 /* Get the section contents. */
2049 if (elf_section_data (sec)->this_hdr.contents != NULL)
2050 contents = elf_section_data (sec)->this_hdr.contents;
2051 /* Go get them off disk. */
2054 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2056 elf_section_data (sec)->this_hdr.contents = contents;
2059 /* Read this BFD's symbols. */
2060 /* Get cached copy if it exists. */
2061 if (symtab_hdr->contents != NULL)
2062 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2065 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
2066 symtab_hdr->contents = (bfd_byte *) intsyms;
2069 if (shndx_hdr->sh_size != 0)
2073 amt = symtab_hdr->sh_info;
2074 amt *= sizeof (Elf_External_Sym_Shndx);
2075 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2076 if (shndx_buf == NULL)
2078 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2079 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
2081 shndx_hdr->contents = (bfd_byte *) shndx_buf;
2084 /* Get a copy of the native relocations. */
2085 internal_relocs = (_bfd_elf_link_read_relocs
2086 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2087 link_info->keep_memory));
2088 if (internal_relocs == NULL)
2090 if (! link_info->keep_memory)
2091 free_relocs = internal_relocs;
2093 /* The RL_ relocs must be just before the operand relocs they go
2094 with, so we must sort them to guarantee this. We use bubblesort
2095 instead of qsort so we can guarantee that relocs with the same
2096 address remain in the same relative order. */
2097 reloc_bubblesort (internal_relocs, sec->reloc_count);
2099 /* Walk through them looking for relaxing opportunities. */
2100 irelend = internal_relocs + sec->reloc_count;
2103 /* This will either be NULL or a pointer to the next alignment
2105 next_alignment = internal_relocs;
2106 /* This will be the previous alignment, although at first it points
2107 to the first real relocation. */
2108 prev_alignment = internal_relocs;
2110 /* We calculate worst case shrinkage caused by alignment directives.
2111 No fool-proof, but better than either ignoring the problem or
2112 doing heavy duty analysis of all the alignment markers in all
2114 section_alignment_glue = 0;
2115 for (irel = internal_relocs; irel < irelend; irel++)
2116 if (ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX
2117 && irel->r_addend & RL78_RELAXA_ALIGN)
2119 int this_glue = 1 << (irel->r_addend & RL78_RELAXA_ANUM);
2121 if (section_alignment_glue < this_glue)
2122 section_alignment_glue = this_glue;
2124 /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2126 section_alignment_glue *= 2;
2128 for (irel = internal_relocs; irel < irelend; irel++)
2130 unsigned char *insn;
2133 /* The insns we care about are all marked with one of these. */
2134 if (ELF32_R_TYPE (irel->r_info) != R_RL78_RH_RELAX)
2137 if (irel->r_addend & RL78_RELAXA_ALIGN
2138 || next_alignment == internal_relocs)
2140 /* When we delete bytes, we need to maintain all the alignments
2141 indicated. In addition, we need to be careful about relaxing
2142 jumps across alignment boundaries - these displacements
2143 *grow* when we delete bytes. For now, don't shrink
2144 displacements across an alignment boundary, just in case.
2145 Note that this only affects relocations to the same
2147 prev_alignment = next_alignment;
2148 next_alignment += 2;
2149 while (next_alignment < irelend
2150 && (ELF32_R_TYPE (next_alignment->r_info) != R_RL78_RH_RELAX
2151 || !(next_alignment->r_addend & RL78_RELAXA_ELIGN)))
2153 if (next_alignment >= irelend || next_alignment->r_offset == 0)
2154 next_alignment = NULL;
2157 /* When we hit alignment markers, see if we've shrunk enough
2158 before them to reduce the gap without violating the alignment
2160 if (irel->r_addend & RL78_RELAXA_ALIGN)
2162 /* At this point, the next relocation *should* be the ELIGN
2164 Elf_Internal_Rela *erel = irel + 1;
2165 unsigned int alignment, nbytes;
2167 if (ELF32_R_TYPE (erel->r_info) != R_RL78_RH_RELAX)
2169 if (!(erel->r_addend & RL78_RELAXA_ELIGN))
2172 alignment = 1 << (irel->r_addend & RL78_RELAXA_ANUM);
2174 if (erel->r_offset - irel->r_offset < alignment)
2177 nbytes = erel->r_offset - irel->r_offset;
2178 nbytes /= alignment;
2179 nbytes *= alignment;
2181 elf32_rl78_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment,
2182 erel->r_offset == sec->size);
2188 if (irel->r_addend & RL78_RELAXA_ELIGN)
2191 insn = contents + irel->r_offset;
2193 nrelocs = irel->r_addend & RL78_RELAXA_RNUM;
2195 /* At this point, we have an insn that is a candidate for linker
2196 relaxation. There are NRELOCS relocs following that may be
2197 relaxed, although each reloc may be made of more than one
2198 reloc entry (such as gp-rel symbols). */
2200 /* Get the value of the symbol referred to by the reloc. Just
2201 in case this is the last reloc in the list, use the RL's
2202 addend to choose between this reloc (no addend) or the next
2203 (yes addend, which means at least one following reloc). */
2205 /* srel points to the "current" reloction for this insn -
2206 actually the last reloc for a given operand, which is the one
2207 we need to update. We check the relaxations in the same
2208 order that the relocations happen, so we'll just push it
2212 pc = sec->output_section->vma + sec->output_offset
2216 symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2217 pcrel = symval - pc + srel->r_addend; \
2220 #define SNIPNR(offset, nbytes) \
2221 elf32_rl78_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0);
2222 #define SNIP(offset, nbytes, newtype) \
2223 SNIPNR (offset, nbytes); \
2224 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2226 /* The order of these bit tests must match the order that the
2227 relocs appear in. Since we sorted those by offset, we can
2230 /*----------------------------------------------------------------------*/
2231 /* EF ad BR $rel8 pcrel
2232 ED al ah BR !abs16 abs
2233 EE al ah BR $!rel16 pcrel
2234 EC al ah as BR !!abs20 abs
2236 FD al ah CALL !abs16 abs
2237 FE al ah CALL $!rel16 pcrel
2238 FC al ah as CALL !!abs20 abs
2246 61 C8 EF ad SKC ; BR $rel8
2247 61 D8 EF ad SKNC ; BR $rel8
2248 61 E8 EF ad SKZ ; BR $rel8
2249 61 F8 EF ad SKNZ ; BR $rel8
2250 61 E3 EF ad SKH ; BR $rel8
2251 61 F3 EF ad SKNH ; BR $rel8
2254 if (irel->r_addend & RL78_RELAXA_BRA)
2260 case 0xec: /* BR !!abs20 */
2267 SNIP (2, 2, R_RL78_DIR8S_PCREL);
2270 else if (symval < 65536)
2273 insn[1] = symval & 0xff;
2274 insn[2] = symval >> 8;
2275 SNIP (2, 1, R_RL78_DIR16S);
2278 else if (pcrel < 32767
2282 insn[1] = pcrel & 0xff;
2283 insn[2] = pcrel >> 8;
2284 SNIP (2, 1, R_RL78_DIR16S_PCREL);
2289 case 0xee: /* BR $!pcrel16 */
2290 case 0xed: /* BR $!abs16 */
2296 SNIP (2, 1, R_RL78_DIR8S_PCREL);
2301 case 0xfc: /* CALL !!abs20 */
2305 insn[1] = symval & 0xff;
2306 insn[2] = symval >> 8;
2307 SNIP (2, 1, R_RL78_DIR16S);
2310 else if (pcrel < 32767
2314 insn[1] = pcrel & 0xff;
2315 insn[2] = pcrel >> 8;
2316 SNIP (2, 1, R_RL78_DIR16S_PCREL);
2321 case 0x61: /* PREFIX */
2322 /* For SKIP/BR, we change the BR opcode and delete the
2323 SKIP. That way, we don't have to find and change the
2324 relocation for the BR. */
2327 case 0xc8: /* SKC */
2328 if (insn[2] == 0xef)
2330 insn[2] = 0xde; /* BNC */
2335 case 0xd8: /* SKNC */
2336 if (insn[2] == 0xef)
2338 insn[2] = 0xdc; /* BC */
2343 case 0xe8: /* SKZ */
2344 if (insn[2] == 0xef)
2346 insn[2] = 0xdf; /* BNZ */
2351 case 0xf8: /* SKNZ */
2352 if (insn[2] == 0xef)
2354 insn[2] = 0xdd; /* BZ */
2359 case 0xe3: /* SKH */
2360 if (insn[2] == 0xef)
2362 insn[2] = 0xd3; /* BNH */
2363 SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */
2367 case 0xf3: /* SKNH */
2368 if (insn[2] == 0xef)
2370 insn[2] = 0xc3; /* BH */
2371 SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */
2380 if (irel->r_addend & RL78_RELAXA_ADDR16)
2382 /*----------------------------------------------------------------------*/
2383 /* Some insns have both a 16-bit address operand and an 8-bit
2384 variant if the address is within a special range:
2386 Address 16-bit operand SADDR range SFR range
2387 FFF00-FFFFF 0xff00-0xffff 0x00-0xff
2388 FFE20-FFF1F 0xfe20-0xff1f 0x00-0xff
2390 The RELAX_ADDR16[] array has the insn encodings for the
2391 16-bit operand version, as well as the SFR and SADDR
2392 variants. We only need to replace the encodings and
2395 Note: we intentionally do not attempt to decode and skip
2396 any ES: prefix, as adding ES: means the addr16 (likely)
2397 no longer points to saddr/sfr space.
2407 printf("relax_addr16 detected, symval 0x%lx %02x %02x\n", symval, insn[0], insn[1]);
2409 if (0xffe20 <= symval && symval <= 0xfffff)
2412 is_saddr = (0xffe20 <= symval && symval <= 0xfff1f);
2413 is_sfr = (0xfff00 <= symval && symval <= 0xfffff);
2415 for (idx = 0; relax_addr16[idx].insn != -1; idx ++)
2417 if (relax_addr16[idx].prefix != -1
2418 && insn[0] == relax_addr16[idx].prefix
2419 && insn[1] == relax_addr16[idx].insn)
2423 else if (relax_addr16[idx].prefix == -1
2424 && insn[0] == relax_addr16[idx].insn)
2431 /* We have a matched insn, and poff is 0 or 1 depending
2432 on the base pattern size. */
2434 if (is_sfr && relax_addr16[idx].insn_for_sfr != -1)
2436 insn[poff] = relax_addr16[idx].insn_for_sfr;
2437 SNIP (poff+2, 1, R_RL78_RH_SFR);
2438 printf(" - replaced by SFR\n");
2441 else if (is_saddr && relax_addr16[idx].insn_for_saddr != -1)
2443 insn[poff] = relax_addr16[idx].insn_for_saddr;
2444 SNIP (poff+2, 1, R_RL78_RH_SADDR);
2445 printf(" - replaced by SADDR\n");
2452 /*----------------------------------------------------------------------*/
2459 if (free_relocs != NULL)
2462 if (free_contents != NULL)
2463 free (free_contents);
2465 if (shndx_buf != NULL)
2467 shndx_hdr->contents = NULL;
2471 if (free_intsyms != NULL)
2472 free (free_intsyms);
2479 #define ELF_ARCH bfd_arch_rl78
2480 #define ELF_MACHINE_CODE EM_RL78
2481 #define ELF_MAXPAGESIZE 0x1000
2483 #define TARGET_LITTLE_SYM bfd_elf32_rl78_vec
2484 #define TARGET_LITTLE_NAME "elf32-rl78"
2486 #define elf_info_to_howto_rel NULL
2487 #define elf_info_to_howto rl78_info_to_howto_rela
2488 #define elf_backend_object_p rl78_elf_object_p
2489 #define elf_backend_relocate_section rl78_elf_relocate_section
2490 #define elf_symbol_leading_char ('_')
2491 #define elf_backend_can_gc_sections 1
2493 #define bfd_elf32_bfd_reloc_type_lookup rl78_reloc_type_lookup
2494 #define bfd_elf32_bfd_reloc_name_lookup rl78_reloc_name_lookup
2495 #define bfd_elf32_bfd_set_private_flags rl78_elf_set_private_flags
2496 #define bfd_elf32_bfd_merge_private_bfd_data rl78_elf_merge_private_bfd_data
2497 #define bfd_elf32_bfd_print_private_bfd_data rl78_elf_print_private_bfd_data
2499 #define bfd_elf32_bfd_relax_section rl78_elf_relax_section
2500 #define elf_backend_check_relocs rl78_elf_check_relocs
2501 #define elf_backend_always_size_sections \
2502 rl78_elf_always_size_sections
2503 #define elf_backend_finish_dynamic_sections \
2504 rl78_elf_finish_dynamic_sections
2506 #include "elf32-target.h"