Home | History | Annotate | Line # | Download | only in bfd
elf32-h8300.c revision 1.4.8.1
      1      1.1  christos /* BFD back-end for Renesas H8/300 ELF binaries.
      2  1.4.8.1  pgoyette    Copyright (C) 1993-2016 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      5      1.1  christos 
      6      1.1  christos    This program is free software; you can redistribute it and/or modify
      7      1.1  christos    it under the terms of the GNU General Public License as published by
      8      1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9      1.1  christos    (at your option) any later version.
     10      1.1  christos 
     11      1.1  christos    This program is distributed in the hope that it will be useful,
     12      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14      1.1  christos    GNU General Public License for more details.
     15      1.1  christos 
     16      1.1  christos    You should have received a copy of the GNU General Public License
     17      1.1  christos    along with this program; if not, write to the Free Software
     18      1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19      1.1  christos    MA 02110-1301, USA.  */
     20      1.1  christos 
     21      1.1  christos #include "sysdep.h"
     22      1.1  christos #include "bfd.h"
     23      1.1  christos #include "libbfd.h"
     24      1.1  christos #include "elf-bfd.h"
     25      1.1  christos #include "elf/h8.h"
     26      1.1  christos 
     27      1.1  christos static reloc_howto_type *elf32_h8_reloc_type_lookup
     28      1.1  christos   (bfd *abfd, bfd_reloc_code_real_type code);
     29      1.1  christos static void elf32_h8_info_to_howto
     30      1.1  christos   (bfd *, arelent *, Elf_Internal_Rela *);
     31      1.1  christos static void elf32_h8_info_to_howto_rel
     32      1.1  christos   (bfd *, arelent *, Elf_Internal_Rela *);
     33      1.1  christos static unsigned long elf32_h8_mach (flagword);
     34      1.1  christos static void elf32_h8_final_write_processing (bfd *, bfd_boolean);
     35      1.1  christos static bfd_boolean elf32_h8_object_p (bfd *);
     36      1.1  christos static bfd_boolean elf32_h8_merge_private_bfd_data (bfd *, bfd *);
     37      1.1  christos static bfd_boolean elf32_h8_relax_section
     38      1.1  christos   (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
     39      1.1  christos static bfd_boolean elf32_h8_relax_delete_bytes
     40      1.1  christos   (bfd *, asection *, bfd_vma, int);
     41      1.1  christos static bfd_boolean elf32_h8_symbol_address_p (bfd *, asection *, bfd_vma);
     42      1.1  christos static bfd_byte *elf32_h8_get_relocated_section_contents
     43      1.1  christos   (bfd *, struct bfd_link_info *, struct bfd_link_order *,
     44      1.1  christos    bfd_byte *, bfd_boolean, asymbol **);
     45      1.1  christos static bfd_reloc_status_type elf32_h8_final_link_relocate
     46      1.1  christos   (unsigned long, bfd *, bfd *, asection *,
     47      1.1  christos    bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
     48      1.1  christos    struct bfd_link_info *, asection *, int);
     49      1.1  christos static bfd_boolean elf32_h8_relocate_section
     50      1.1  christos   (bfd *, struct bfd_link_info *, bfd *, asection *,
     51      1.1  christos    bfd_byte *, Elf_Internal_Rela *,
     52      1.1  christos    Elf_Internal_Sym *, asection **);
     53      1.1  christos static bfd_reloc_status_type special
     54      1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     55      1.1  christos 
     56      1.1  christos /* This does not include any relocation information, but should be
     57      1.1  christos    good enough for GDB or objdump to read the file.  */
     58      1.1  christos 
     59      1.1  christos static reloc_howto_type h8_elf_howto_table[] =
     60      1.1  christos {
     61      1.1  christos #define R_H8_NONE_X 0
     62      1.1  christos   HOWTO (R_H8_NONE,		/* type */
     63      1.1  christos 	 0,			/* rightshift */
     64      1.3  christos 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
     65      1.1  christos 	 0,			/* bitsize */
     66      1.1  christos 	 FALSE,			/* pc_relative */
     67      1.1  christos 	 0,			/* bitpos */
     68      1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
     69      1.1  christos 	 special,		/* special_function */
     70      1.1  christos 	 "R_H8_NONE",		/* name */
     71      1.1  christos 	 FALSE,			/* partial_inplace */
     72      1.1  christos 	 0,			/* src_mask */
     73      1.1  christos 	 0,			/* dst_mask */
     74      1.1  christos 	 FALSE),		/* pcrel_offset */
     75      1.1  christos #define R_H8_DIR32_X (R_H8_NONE_X + 1)
     76      1.1  christos   HOWTO (R_H8_DIR32,		/* type */
     77      1.1  christos 	 0,			/* rightshift */
     78      1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
     79      1.1  christos 	 32,			/* bitsize */
     80      1.1  christos 	 FALSE,			/* pc_relative */
     81      1.1  christos 	 0,			/* bitpos */
     82      1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
     83      1.1  christos 	 special,		/* special_function */
     84      1.1  christos 	 "R_H8_DIR32",		/* name */
     85      1.1  christos 	 FALSE,			/* partial_inplace */
     86      1.1  christos 	 0,			/* src_mask */
     87      1.1  christos 	 0xffffffff,		/* dst_mask */
     88      1.1  christos 	 FALSE),		/* pcrel_offset */
     89      1.1  christos #define R_H8_DIR16_X (R_H8_DIR32_X + 1)
     90      1.1  christos   HOWTO (R_H8_DIR16,		/* type */
     91      1.1  christos 	 0,			/* rightshift */
     92      1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
     93      1.1  christos 	 16,			/* bitsize */
     94      1.1  christos 	 FALSE,			/* pc_relative */
     95      1.1  christos 	 0,			/* bitpos */
     96      1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
     97      1.1  christos 	 special,		/* special_function */
     98      1.1  christos 	 "R_H8_DIR16",		/* name */
     99      1.1  christos 	 FALSE,			/* partial_inplace */
    100      1.1  christos 	 0,			/* src_mask */
    101      1.1  christos 	 0x0000ffff,		/* dst_mask */
    102      1.1  christos 	 FALSE),		/* pcrel_offset */
    103      1.1  christos #define R_H8_DIR8_X (R_H8_DIR16_X + 1)
    104      1.1  christos   HOWTO (R_H8_DIR8,		/* type */
    105      1.1  christos 	 0,			/* rightshift */
    106      1.1  christos 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    107      1.1  christos 	 8,			/* bitsize */
    108      1.1  christos 	 FALSE,			/* pc_relative */
    109      1.1  christos 	 0,			/* bitpos */
    110      1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
    111      1.1  christos 	 special,		/* special_function */
    112      1.1  christos 	 "R_H8_DIR8",		/* name */
    113      1.1  christos 	 FALSE,			/* partial_inplace */
    114      1.1  christos 	 0,			/* src_mask */
    115      1.1  christos 	 0x000000ff,		/* dst_mask */
    116      1.1  christos 	 FALSE),		/* pcrel_offset */
    117      1.1  christos #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1)
    118      1.1  christos   HOWTO (R_H8_DIR16A8,		/* type */
    119      1.1  christos 	 0,			/* rightshift */
    120      1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    121      1.1  christos 	 16,			/* bitsize */
    122      1.1  christos 	 FALSE,			/* pc_relative */
    123      1.1  christos 	 0,			/* bitpos */
    124      1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    125      1.1  christos 	 special,		/* special_function */
    126      1.1  christos 	 "R_H8_DIR16A8",	/* name */
    127      1.1  christos 	 FALSE,			/* partial_inplace */
    128      1.1  christos 	 0,			/* src_mask */
    129      1.1  christos 	 0x0000ffff,		/* dst_mask */
    130      1.1  christos 	 FALSE),		/* pcrel_offset */
    131      1.1  christos #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1)
    132      1.1  christos   HOWTO (R_H8_DIR16R8,		/* type */
    133      1.1  christos 	 0,			/* rightshift */
    134      1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    135      1.1  christos 	 16,			/* bitsize */
    136      1.1  christos 	 FALSE,			/* pc_relative */
    137      1.1  christos 	 0,			/* bitpos */
    138      1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    139      1.1  christos 	 special,		/* special_function */
    140      1.1  christos 	 "R_H8_DIR16R8",	/* name */
    141      1.1  christos 	 FALSE,			/* partial_inplace */
    142      1.1  christos 	 0,			/* src_mask */
    143      1.1  christos 	 0x0000ffff,		/* dst_mask */
    144      1.1  christos 	 FALSE),		/* pcrel_offset */
    145      1.1  christos #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1)
    146      1.1  christos   HOWTO (R_H8_DIR24A8,		/* type */
    147      1.1  christos 	 0,			/* rightshift */
    148      1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    149      1.1  christos 	 24,			/* bitsize */
    150      1.1  christos 	 FALSE,			/* pc_relative */
    151      1.1  christos 	 0,			/* bitpos */
    152      1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    153      1.1  christos 	 special,		/* special_function */
    154      1.1  christos 	 "R_H8_DIR24A8",	/* name */
    155      1.1  christos 	 TRUE,			/* partial_inplace */
    156      1.1  christos 	 0xff000000,		/* src_mask */
    157      1.1  christos 	 0x00ffffff,		/* dst_mask */
    158      1.1  christos 	 FALSE),		/* pcrel_offset */
    159      1.1  christos #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1)
    160      1.1  christos   HOWTO (R_H8_DIR24R8,		/* type */
    161      1.1  christos 	 0,			/* rightshift */
    162      1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    163      1.1  christos 	 24,			/* bitsize */
    164      1.1  christos 	 FALSE,			/* pc_relative */
    165      1.1  christos 	 0,			/* bitpos */
    166      1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    167      1.1  christos 	 special,		/* special_function */
    168      1.1  christos 	 "R_H8_DIR24R8",	/* name */
    169      1.1  christos 	 TRUE,			/* partial_inplace */
    170      1.1  christos 	 0xff000000,		/* src_mask */
    171      1.1  christos 	 0x00ffffff,		/* dst_mask */
    172      1.1  christos 	 FALSE),		/* pcrel_offset */
    173      1.1  christos #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1)
    174      1.1  christos   HOWTO (R_H8_DIR32A16,		/* type */
    175      1.1  christos 	 0,			/* rightshift */
    176      1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    177      1.1  christos 	 32,			/* bitsize */
    178      1.1  christos 	 FALSE,			/* pc_relative */
    179      1.1  christos 	 0,			/* bitpos */
    180      1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
    181      1.1  christos 	 special,		/* special_function */
    182      1.1  christos 	 "R_H8_DIR32A16",	/* name */
    183      1.1  christos 	 FALSE,			/* partial_inplace */
    184      1.1  christos 	 0,			/* src_mask */
    185      1.1  christos 	 0xffffffff,		/* dst_mask */
    186      1.1  christos 	 FALSE),		/* pcrel_offset */
    187      1.3  christos #define R_H8_DISP32A16_X (R_H8_DIR32A16_X + 1)
    188      1.3  christos   HOWTO (R_H8_DISP32A16,	/* type */
    189      1.3  christos 	 0,			/* rightshift */
    190      1.3  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    191      1.3  christos 	 32,			/* bitsize */
    192      1.3  christos 	 FALSE,			/* pc_relative */
    193      1.3  christos 	 0,			/* bitpos */
    194      1.3  christos 	 complain_overflow_dont,/* complain_on_overflow */
    195      1.3  christos 	 special,		/* special_function */
    196      1.3  christos 	 "R_H8_DISP32A16",	/* name */
    197      1.3  christos 	 FALSE,			/* partial_inplace */
    198      1.3  christos 	 0,			/* src_mask */
    199      1.3  christos 	 0xffffffff,		/* dst_mask */
    200      1.3  christos 	 FALSE),		/* pcrel_offset */
    201      1.3  christos #define R_H8_PCREL16_X (R_H8_DISP32A16_X + 1)
    202      1.1  christos   HOWTO (R_H8_PCREL16,		/* type */
    203      1.1  christos 	 0,			/* rightshift */
    204      1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    205      1.1  christos 	 16,			/* bitsize */
    206      1.1  christos 	 TRUE,			/* pc_relative */
    207      1.1  christos 	 0,			/* bitpos */
    208      1.1  christos 	 complain_overflow_signed,/* complain_on_overflow */
    209      1.1  christos 	 special,		/* special_function */
    210      1.1  christos 	 "R_H8_PCREL16",	/* name */
    211      1.1  christos 	 FALSE,			/* partial_inplace */
    212      1.1  christos 	 0xffff,		/* src_mask */
    213      1.1  christos 	 0xffff,		/* dst_mask */
    214      1.1  christos 	 TRUE),			/* pcrel_offset */
    215      1.1  christos #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1)
    216      1.1  christos   HOWTO (R_H8_PCREL8,		/* type */
    217      1.1  christos 	 0,			/* rightshift */
    218      1.1  christos 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    219      1.1  christos 	 8,			/* bitsize */
    220      1.1  christos 	 TRUE,			/* pc_relative */
    221      1.1  christos 	 0,			/* bitpos */
    222      1.1  christos 	 complain_overflow_signed,/* complain_on_overflow */
    223      1.1  christos 	 special,		/* special_function */
    224      1.1  christos 	 "R_H8_PCREL8",		/* name */
    225      1.1  christos 	 FALSE,			/* partial_inplace */
    226      1.1  christos 	 0xff,			/* src_mask */
    227      1.1  christos 	 0xff,			/* dst_mask */
    228      1.1  christos 	 TRUE),			/* pcrel_offset */
    229      1.1  christos };
    230      1.1  christos 
    231      1.1  christos /* This structure is used to map BFD reloc codes to H8 ELF relocs.  */
    232      1.1  christos 
    233      1.1  christos struct elf_reloc_map {
    234      1.1  christos   bfd_reloc_code_real_type bfd_reloc_val;
    235      1.1  christos   unsigned char howto_index;
    236      1.1  christos };
    237      1.1  christos 
    238      1.1  christos /* An array mapping BFD reloc codes to H8 ELF relocs.  */
    239      1.1  christos 
    240      1.1  christos static const struct elf_reloc_map h8_reloc_map[] = {
    241      1.1  christos   { BFD_RELOC_NONE, R_H8_NONE_X },
    242      1.1  christos   { BFD_RELOC_32, R_H8_DIR32_X },
    243      1.1  christos   { BFD_RELOC_16, R_H8_DIR16_X },
    244      1.1  christos   { BFD_RELOC_8, R_H8_DIR8_X },
    245      1.1  christos   { BFD_RELOC_H8_DIR16A8, R_H8_DIR16A8_X },
    246      1.1  christos   { BFD_RELOC_H8_DIR16R8, R_H8_DIR16R8_X },
    247      1.1  christos   { BFD_RELOC_H8_DIR24A8, R_H8_DIR24A8_X },
    248      1.1  christos   { BFD_RELOC_H8_DIR24R8, R_H8_DIR24R8_X },
    249      1.1  christos   { BFD_RELOC_H8_DIR32A16, R_H8_DIR32A16_X },
    250      1.3  christos   { BFD_RELOC_H8_DISP32A16, R_H8_DISP32A16_X },
    251      1.1  christos   { BFD_RELOC_16_PCREL, R_H8_PCREL16_X },
    252      1.1  christos   { BFD_RELOC_8_PCREL, R_H8_PCREL8_X },
    253      1.1  christos };
    254      1.1  christos 
    255      1.1  christos 
    256      1.1  christos static reloc_howto_type *
    257      1.1  christos elf32_h8_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    258      1.1  christos 			    bfd_reloc_code_real_type code)
    259      1.1  christos {
    260      1.1  christos   unsigned int i;
    261      1.1  christos 
    262      1.1  christos   for (i = 0; i < sizeof (h8_reloc_map) / sizeof (struct elf_reloc_map); i++)
    263      1.1  christos     {
    264      1.1  christos       if (h8_reloc_map[i].bfd_reloc_val == code)
    265      1.1  christos 	return &h8_elf_howto_table[(int) h8_reloc_map[i].howto_index];
    266      1.1  christos     }
    267      1.1  christos   return NULL;
    268      1.1  christos }
    269      1.1  christos 
    270      1.1  christos static reloc_howto_type *
    271      1.1  christos elf32_h8_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    272      1.1  christos 			    const char *r_name)
    273      1.1  christos {
    274      1.1  christos   unsigned int i;
    275      1.1  christos 
    276      1.1  christos   for (i = 0;
    277      1.1  christos        i < sizeof (h8_elf_howto_table) / sizeof (h8_elf_howto_table[0]);
    278      1.1  christos        i++)
    279      1.1  christos     if (h8_elf_howto_table[i].name != NULL
    280      1.1  christos 	&& strcasecmp (h8_elf_howto_table[i].name, r_name) == 0)
    281      1.1  christos       return &h8_elf_howto_table[i];
    282      1.1  christos 
    283      1.1  christos   return NULL;
    284      1.1  christos }
    285      1.1  christos 
    286      1.1  christos static void
    287      1.1  christos elf32_h8_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
    288      1.1  christos 			Elf_Internal_Rela *elf_reloc)
    289      1.1  christos {
    290      1.1  christos   unsigned int r;
    291      1.1  christos   unsigned int i;
    292      1.1  christos 
    293      1.1  christos   r = ELF32_R_TYPE (elf_reloc->r_info);
    294      1.1  christos   for (i = 0; i < sizeof (h8_elf_howto_table) / sizeof (reloc_howto_type); i++)
    295      1.1  christos     if (h8_elf_howto_table[i].type == r)
    296      1.1  christos       {
    297      1.1  christos 	bfd_reloc->howto = &h8_elf_howto_table[i];
    298      1.1  christos 	return;
    299      1.1  christos       }
    300      1.1  christos   abort ();
    301      1.1  christos }
    302      1.1  christos 
    303      1.1  christos static void
    304      1.1  christos elf32_h8_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
    305      1.1  christos 			    Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED)
    306      1.1  christos {
    307      1.1  christos   unsigned int r;
    308      1.1  christos 
    309      1.1  christos   abort ();
    310      1.1  christos   r = ELF32_R_TYPE (elf_reloc->r_info);
    311      1.1  christos   bfd_reloc->howto = &h8_elf_howto_table[r];
    312      1.1  christos }
    313      1.1  christos 
    314      1.1  christos /* Special handling for H8/300 relocs.
    315      1.1  christos    We only come here for pcrel stuff and return normally if not an -r link.
    316      1.1  christos    When doing -r, we can't do any arithmetic for the pcrel stuff, because
    317      1.1  christos    we support relaxing on the H8/300 series chips.  */
    318      1.1  christos static bfd_reloc_status_type
    319      1.1  christos special (bfd *abfd ATTRIBUTE_UNUSED,
    320      1.1  christos 	 arelent *reloc_entry ATTRIBUTE_UNUSED,
    321      1.1  christos 	 asymbol *symbol ATTRIBUTE_UNUSED,
    322      1.1  christos 	 void * data ATTRIBUTE_UNUSED,
    323      1.1  christos 	 asection *input_section ATTRIBUTE_UNUSED,
    324      1.1  christos 	 bfd *output_bfd,
    325      1.1  christos 	 char **error_message ATTRIBUTE_UNUSED)
    326      1.1  christos {
    327      1.1  christos   if (output_bfd == (bfd *) NULL)
    328      1.1  christos     return bfd_reloc_continue;
    329      1.1  christos 
    330      1.1  christos   /* Adjust the reloc address to that in the output section.  */
    331      1.1  christos   reloc_entry->address += input_section->output_offset;
    332      1.1  christos   return bfd_reloc_ok;
    333      1.1  christos }
    334      1.1  christos 
    335      1.1  christos /* Perform a relocation as part of a final link.  */
    336      1.1  christos static bfd_reloc_status_type
    337      1.1  christos elf32_h8_final_link_relocate (unsigned long r_type, bfd *input_bfd,
    338      1.1  christos 			      bfd *output_bfd ATTRIBUTE_UNUSED,
    339      1.1  christos 			      asection *input_section ATTRIBUTE_UNUSED,
    340      1.1  christos 			      bfd_byte *contents, bfd_vma offset,
    341      1.1  christos 			      bfd_vma value, bfd_vma addend,
    342      1.1  christos 			      struct bfd_link_info *info ATTRIBUTE_UNUSED,
    343      1.1  christos 			      asection *sym_sec ATTRIBUTE_UNUSED,
    344      1.1  christos 			      int is_local ATTRIBUTE_UNUSED)
    345      1.1  christos {
    346      1.1  christos   bfd_byte *hit_data = contents + offset;
    347      1.1  christos 
    348      1.1  christos   switch (r_type)
    349      1.1  christos     {
    350      1.1  christos     case R_H8_NONE:
    351      1.1  christos       return bfd_reloc_ok;
    352      1.1  christos 
    353      1.1  christos     case R_H8_DIR32:
    354      1.1  christos     case R_H8_DIR32A16:
    355      1.3  christos     case R_H8_DISP32A16:
    356      1.1  christos     case R_H8_DIR24A8:
    357      1.1  christos       value += addend;
    358      1.1  christos       bfd_put_32 (input_bfd, value, hit_data);
    359      1.1  christos       return bfd_reloc_ok;
    360      1.1  christos 
    361      1.1  christos     case R_H8_DIR16:
    362      1.1  christos     case R_H8_DIR16A8:
    363      1.1  christos     case R_H8_DIR16R8:
    364      1.1  christos       value += addend;
    365      1.1  christos       bfd_put_16 (input_bfd, value, hit_data);
    366      1.1  christos       return bfd_reloc_ok;
    367      1.1  christos 
    368      1.1  christos     /* AKA R_RELBYTE */
    369      1.1  christos     case R_H8_DIR8:
    370      1.1  christos       value += addend;
    371      1.1  christos 
    372      1.1  christos       bfd_put_8 (input_bfd, value, hit_data);
    373      1.1  christos       return bfd_reloc_ok;
    374      1.1  christos 
    375      1.1  christos     case R_H8_DIR24R8:
    376      1.1  christos       value += addend;
    377      1.1  christos 
    378      1.1  christos       /* HIT_DATA is the address for the first byte for the relocated
    379      1.1  christos 	 value.  Subtract 1 so that we can manipulate the data in 32-bit
    380      1.1  christos 	 hunks.  */
    381      1.1  christos       hit_data--;
    382      1.1  christos 
    383      1.1  christos       /* Clear out the top byte in value.  */
    384      1.1  christos       value &= 0xffffff;
    385      1.1  christos 
    386      1.1  christos       /* Retrieve the type byte for value from the section contents.  */
    387      1.1  christos       value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
    388      1.1  christos 
    389      1.1  christos       /* Now scribble it out in one 32-bit hunk.  */
    390      1.1  christos       bfd_put_32 (input_bfd, value, hit_data);
    391      1.1  christos       return bfd_reloc_ok;
    392      1.1  christos 
    393      1.1  christos     case R_H8_PCREL16:
    394      1.1  christos       value -= (input_section->output_section->vma
    395      1.1  christos 		+ input_section->output_offset);
    396      1.1  christos       value -= offset;
    397      1.1  christos       value += addend;
    398      1.1  christos 
    399      1.1  christos       /* The value is relative to the start of the instruction,
    400      1.1  christos 	 not the relocation offset.  Subtract 2 to account for
    401      1.1  christos 	 this minor issue.  */
    402      1.1  christos       value -= 2;
    403      1.1  christos 
    404      1.1  christos       bfd_put_16 (input_bfd, value, hit_data);
    405      1.1  christos       return bfd_reloc_ok;
    406      1.1  christos 
    407      1.1  christos     case R_H8_PCREL8:
    408      1.1  christos       value -= (input_section->output_section->vma
    409      1.1  christos 		+ input_section->output_offset);
    410      1.1  christos       value -= offset;
    411      1.1  christos       value += addend;
    412      1.1  christos 
    413      1.1  christos       /* The value is relative to the start of the instruction,
    414      1.1  christos 	 not the relocation offset.  Subtract 1 to account for
    415      1.1  christos 	 this minor issue.  */
    416      1.1  christos       value -= 1;
    417      1.1  christos 
    418      1.1  christos       bfd_put_8 (input_bfd, value, hit_data);
    419      1.1  christos       return bfd_reloc_ok;
    420      1.1  christos 
    421      1.1  christos     default:
    422      1.1  christos       return bfd_reloc_notsupported;
    423      1.1  christos     }
    424      1.1  christos }
    425      1.1  christos 
    426      1.1  christos /* Relocate an H8 ELF section.  */
    428      1.1  christos static bfd_boolean
    429      1.1  christos elf32_h8_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
    430      1.1  christos 			   bfd *input_bfd, asection *input_section,
    431      1.1  christos 			   bfd_byte *contents, Elf_Internal_Rela *relocs,
    432      1.1  christos 			   Elf_Internal_Sym *local_syms,
    433      1.1  christos 			   asection **local_sections)
    434      1.1  christos {
    435      1.1  christos   Elf_Internal_Shdr *symtab_hdr;
    436      1.1  christos   struct elf_link_hash_entry **sym_hashes;
    437      1.1  christos   Elf_Internal_Rela *rel, *relend;
    438      1.1  christos 
    439      1.1  christos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
    440      1.1  christos   sym_hashes = elf_sym_hashes (input_bfd);
    441      1.1  christos 
    442      1.1  christos   rel = relocs;
    443      1.1  christos   relend = relocs + input_section->reloc_count;
    444      1.1  christos   for (; rel < relend; rel++)
    445      1.1  christos     {
    446      1.1  christos       unsigned int r_type;
    447      1.1  christos       unsigned long r_symndx;
    448      1.1  christos       Elf_Internal_Sym *sym;
    449      1.1  christos       asection *sec;
    450      1.1  christos       struct elf_link_hash_entry *h;
    451      1.1  christos       bfd_vma relocation;
    452      1.1  christos       bfd_reloc_status_type r;
    453      1.1  christos       arelent bfd_reloc;
    454      1.1  christos       reloc_howto_type *howto;
    455      1.1  christos 
    456      1.1  christos       elf32_h8_info_to_howto (input_bfd, &bfd_reloc, rel);
    457      1.1  christos       howto = bfd_reloc.howto;
    458      1.1  christos 
    459      1.1  christos       r_symndx = ELF32_R_SYM (rel->r_info);
    460      1.1  christos       r_type = ELF32_R_TYPE (rel->r_info);
    461      1.1  christos       h = NULL;
    462      1.1  christos       sym = NULL;
    463      1.1  christos       sec = NULL;
    464      1.1  christos       if (r_symndx < symtab_hdr->sh_info)
    465      1.1  christos 	{
    466      1.1  christos 	  sym = local_syms + r_symndx;
    467      1.1  christos 	  sec = local_sections[r_symndx];
    468      1.1  christos 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
    469      1.1  christos 	}
    470      1.1  christos       else
    471      1.3  christos 	{
    472      1.1  christos 	  bfd_boolean unresolved_reloc, warned, ignored;
    473      1.1  christos 
    474      1.1  christos 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
    475      1.1  christos 				   r_symndx, symtab_hdr, sym_hashes,
    476      1.3  christos 				   h, sec, relocation,
    477      1.1  christos 				   unresolved_reloc, warned, ignored);
    478      1.1  christos 	}
    479      1.1  christos 
    480      1.1  christos       if (sec != NULL && discarded_section (sec))
    481      1.1  christos 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
    482      1.1  christos 					 rel, 1, relend, howto, 0, contents);
    483      1.3  christos 
    484      1.1  christos       if (bfd_link_relocatable (info))
    485      1.1  christos 	continue;
    486      1.1  christos 
    487      1.1  christos       r = elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd,
    488      1.1  christos 					input_section,
    489      1.1  christos 					contents, rel->r_offset,
    490      1.1  christos 					relocation, rel->r_addend,
    491      1.1  christos 					info, sec, h == NULL);
    492      1.1  christos 
    493      1.1  christos       if (r != bfd_reloc_ok)
    494      1.1  christos 	{
    495      1.1  christos 	  const char *name;
    496      1.1  christos 	  const char *msg = (const char *) 0;
    497      1.1  christos 
    498      1.1  christos 	  if (h != NULL)
    499      1.1  christos 	    name = h->root.root.string;
    500      1.1  christos 	  else
    501      1.1  christos 	    {
    502      1.1  christos 	      name = (bfd_elf_string_from_elf_section
    503      1.1  christos 		      (input_bfd, symtab_hdr->sh_link, sym->st_name));
    504      1.1  christos 	      if (name == NULL || *name == '\0')
    505      1.1  christos 		name = bfd_section_name (input_bfd, sec);
    506      1.1  christos 	    }
    507      1.1  christos 
    508      1.1  christos 	  switch (r)
    509      1.1  christos 	    {
    510  1.4.8.1  pgoyette 	    case bfd_reloc_overflow:
    511  1.4.8.1  pgoyette 	      (*info->callbacks->reloc_overflow)
    512  1.4.8.1  pgoyette 		(info, (h ? &h->root : NULL), name, howto->name,
    513      1.1  christos 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
    514      1.1  christos 	      break;
    515      1.1  christos 
    516  1.4.8.1  pgoyette 	    case bfd_reloc_undefined:
    517  1.4.8.1  pgoyette 	      (*info->callbacks->undefined_symbol)
    518      1.1  christos 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
    519      1.1  christos 	      break;
    520      1.1  christos 
    521      1.1  christos 	    case bfd_reloc_outofrange:
    522      1.1  christos 	      msg = _("internal error: out of range error");
    523      1.1  christos 	      goto common_error;
    524      1.1  christos 
    525      1.1  christos 	    case bfd_reloc_notsupported:
    526      1.1  christos 	      msg = _("internal error: unsupported relocation error");
    527      1.1  christos 	      goto common_error;
    528      1.1  christos 
    529      1.1  christos 	    case bfd_reloc_dangerous:
    530      1.1  christos 	      msg = _("internal error: dangerous error");
    531      1.1  christos 	      goto common_error;
    532      1.1  christos 
    533      1.1  christos 	    default:
    534      1.1  christos 	      msg = _("internal error: unknown error");
    535      1.1  christos 	      /* fall through */
    536      1.1  christos 
    537  1.4.8.1  pgoyette 	    common_error:
    538  1.4.8.1  pgoyette 	      (*info->callbacks->warning) (info, msg, name, input_bfd,
    539      1.1  christos 					   input_section, rel->r_offset);
    540      1.1  christos 	      break;
    541      1.1  christos 	    }
    542      1.1  christos 	}
    543      1.1  christos     }
    544      1.1  christos 
    545      1.1  christos   return TRUE;
    546      1.1  christos }
    547      1.1  christos 
    548      1.1  christos /* Object files encode the specific H8 model they were compiled
    549      1.1  christos    for in the ELF flags field.
    550      1.1  christos 
    551      1.1  christos    Examine that field and return the proper BFD machine type for
    552      1.1  christos    the object file.  */
    553      1.1  christos static unsigned long
    554      1.1  christos elf32_h8_mach (flagword flags)
    555      1.1  christos {
    556      1.1  christos   switch (flags & EF_H8_MACH)
    557      1.1  christos     {
    558      1.1  christos     case E_H8_MACH_H8300:
    559      1.1  christos     default:
    560      1.1  christos       return bfd_mach_h8300;
    561      1.1  christos 
    562      1.1  christos     case E_H8_MACH_H8300H:
    563      1.1  christos       return bfd_mach_h8300h;
    564      1.1  christos 
    565      1.1  christos     case E_H8_MACH_H8300S:
    566      1.1  christos       return bfd_mach_h8300s;
    567      1.1  christos 
    568      1.1  christos     case E_H8_MACH_H8300HN:
    569      1.1  christos       return bfd_mach_h8300hn;
    570      1.1  christos 
    571      1.1  christos     case E_H8_MACH_H8300SN:
    572      1.1  christos       return bfd_mach_h8300sn;
    573      1.1  christos 
    574      1.1  christos     case E_H8_MACH_H8300SX:
    575      1.1  christos       return bfd_mach_h8300sx;
    576      1.1  christos 
    577      1.1  christos     case E_H8_MACH_H8300SXN:
    578      1.1  christos       return bfd_mach_h8300sxn;
    579      1.1  christos     }
    580      1.1  christos }
    581      1.1  christos 
    582      1.1  christos /* The final processing done just before writing out a H8 ELF object
    583      1.1  christos    file.  We use this opportunity to encode the BFD machine type
    584      1.1  christos    into the flags field in the object file.  */
    585      1.1  christos 
    586      1.1  christos static void
    587      1.1  christos elf32_h8_final_write_processing (bfd *abfd,
    588      1.1  christos 				 bfd_boolean linker ATTRIBUTE_UNUSED)
    589      1.1  christos {
    590      1.1  christos   unsigned long val;
    591      1.1  christos 
    592      1.1  christos   switch (bfd_get_mach (abfd))
    593      1.1  christos     {
    594      1.1  christos     default:
    595      1.1  christos     case bfd_mach_h8300:
    596      1.1  christos       val = E_H8_MACH_H8300;
    597      1.1  christos       break;
    598      1.1  christos 
    599      1.1  christos     case bfd_mach_h8300h:
    600      1.1  christos       val = E_H8_MACH_H8300H;
    601      1.1  christos       break;
    602      1.1  christos 
    603      1.1  christos     case bfd_mach_h8300s:
    604      1.1  christos       val = E_H8_MACH_H8300S;
    605      1.1  christos       break;
    606      1.1  christos 
    607      1.1  christos     case bfd_mach_h8300hn:
    608      1.1  christos       val = E_H8_MACH_H8300HN;
    609      1.1  christos       break;
    610      1.1  christos 
    611      1.1  christos     case bfd_mach_h8300sn:
    612      1.1  christos       val = E_H8_MACH_H8300SN;
    613      1.1  christos       break;
    614      1.1  christos 
    615      1.1  christos     case bfd_mach_h8300sx:
    616      1.1  christos       val = E_H8_MACH_H8300SX;
    617      1.1  christos       break;
    618      1.1  christos 
    619      1.1  christos     case bfd_mach_h8300sxn:
    620      1.1  christos       val = E_H8_MACH_H8300SXN;
    621      1.1  christos       break;
    622      1.1  christos     }
    623      1.1  christos 
    624      1.1  christos   elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH);
    625      1.1  christos   elf_elfheader (abfd)->e_flags |= val;
    626      1.1  christos }
    627      1.1  christos 
    628      1.1  christos /* Return nonzero if ABFD represents a valid H8 ELF object file; also
    629      1.1  christos    record the encoded machine type found in the ELF flags.  */
    630      1.1  christos 
    631      1.1  christos static bfd_boolean
    632      1.1  christos elf32_h8_object_p (bfd *abfd)
    633      1.1  christos {
    634      1.1  christos   bfd_default_set_arch_mach (abfd, bfd_arch_h8300,
    635      1.1  christos 			     elf32_h8_mach (elf_elfheader (abfd)->e_flags));
    636      1.1  christos   return TRUE;
    637      1.1  christos }
    638      1.1  christos 
    639      1.1  christos /* Merge backend specific data from an object file to the output
    640      1.1  christos    object file when linking.  The only data we need to copy at this
    641      1.1  christos    time is the architecture/machine information.  */
    642      1.1  christos 
    643      1.1  christos static bfd_boolean
    644      1.1  christos elf32_h8_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
    645      1.1  christos {
    646      1.1  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
    647      1.1  christos       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
    648      1.1  christos     return TRUE;
    649      1.1  christos 
    650      1.1  christos   if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
    651      1.1  christos       && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
    652      1.1  christos     {
    653      1.1  christos       if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
    654      1.1  christos 			       bfd_get_mach (ibfd)))
    655      1.1  christos 	return FALSE;
    656      1.1  christos     }
    657      1.1  christos 
    658      1.1  christos   return TRUE;
    659      1.1  christos }
    660      1.1  christos 
    661      1.1  christos /* This function handles relaxing for the H8..
    662      1.1  christos 
    663      1.1  christos    There are a few relaxing opportunities available on the H8:
    664      1.1  christos 
    665      1.1  christos      jmp/jsr:24    ->    bra/bsr:8		2 bytes
    666      1.1  christos      The jmp may be completely eliminated if the previous insn is a
    667      1.1  christos      conditional branch to the insn after the jump.  In that case
    668      1.1  christos      we invert the branch and delete the jump and save 4 bytes.
    669      1.1  christos 
    670      1.1  christos      bCC:16          ->    bCC:8                  2 bytes
    671      1.1  christos      bsr:16          ->    bsr:8                  2 bytes
    672      1.1  christos 
    673      1.1  christos      bset:16	     ->    bset:8                 2 bytes
    674      1.1  christos      bset:24/32	     ->    bset:8                 4 bytes
    675      1.1  christos      (also applicable to other bit manipulation instructions)
    676      1.1  christos 
    677      1.1  christos      mov.b:16	     ->    mov.b:8                2 bytes
    678      1.1  christos      mov.b:24/32     ->    mov.b:8                4 bytes
    679      1.1  christos 
    680      1.1  christos      bset:24/32	     ->    bset:16                2 bytes
    681      1.1  christos      (also applicable to other bit manipulation instructions)
    682      1.3  christos 
    683      1.3  christos      mov.[bwl]:24/32 ->    mov.[bwl]:16           2 bytes
    684      1.3  christos 
    685      1.1  christos      mov.[bwl] @(displ:24/32+ERx) -> mov.[bwl] @(displ:16+ERx)  4 bytes.  */
    686      1.1  christos 
    687      1.1  christos static bfd_boolean
    688      1.1  christos elf32_h8_relax_section (bfd *abfd, asection *sec,
    689      1.1  christos 			struct bfd_link_info *link_info, bfd_boolean *again)
    690      1.1  christos {
    691      1.1  christos   Elf_Internal_Shdr *symtab_hdr;
    692      1.1  christos   Elf_Internal_Rela *internal_relocs;
    693      1.1  christos   Elf_Internal_Rela *irel, *irelend;
    694      1.1  christos   bfd_byte *contents = NULL;
    695      1.1  christos   Elf_Internal_Sym *isymbuf = NULL;
    696      1.1  christos   static asection *last_input_section = NULL;
    697      1.1  christos   static Elf_Internal_Rela *last_reloc = NULL;
    698      1.1  christos 
    699      1.1  christos   /* Assume nothing changes.  */
    700      1.1  christos   *again = FALSE;
    701      1.1  christos 
    702      1.1  christos   /* We don't have to do anything for a relocatable link, if
    703      1.1  christos      this section does not have relocs, or if this is not a
    704      1.3  christos      code section.  */
    705      1.1  christos   if (bfd_link_relocatable (link_info)
    706      1.1  christos       || (sec->flags & SEC_RELOC) == 0
    707      1.1  christos       || sec->reloc_count == 0
    708      1.1  christos       || (sec->flags & SEC_CODE) == 0)
    709      1.1  christos     return TRUE;
    710      1.1  christos 
    711      1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    712      1.1  christos 
    713      1.1  christos   /* Get a copy of the native relocations.  */
    714      1.1  christos   internal_relocs = (_bfd_elf_link_read_relocs
    715      1.1  christos 		     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
    716      1.1  christos 		      link_info->keep_memory));
    717      1.1  christos   if (internal_relocs == NULL)
    718      1.1  christos     goto error_return;
    719      1.1  christos 
    720      1.1  christos   if (sec != last_input_section)
    721      1.1  christos     last_reloc = NULL;
    722      1.1  christos 
    723      1.1  christos   last_input_section = sec;
    724      1.1  christos 
    725      1.1  christos   /* Walk through the relocs looking for relaxing opportunities.  */
    726      1.1  christos   irelend = internal_relocs + sec->reloc_count;
    727      1.1  christos   for (irel = internal_relocs; irel < irelend; irel++)
    728      1.1  christos     {
    729      1.1  christos       bfd_vma symval;
    730      1.1  christos 
    731      1.1  christos       {
    732      1.1  christos 	arelent bfd_reloc;
    733      1.1  christos 
    734      1.1  christos 	elf32_h8_info_to_howto (abfd, &bfd_reloc, irel);
    735      1.1  christos       }
    736      1.1  christos       /* Keep track of the previous reloc so that we can delete
    737      1.1  christos 	 some long jumps created by the compiler.  */
    738      1.1  christos       if (irel != internal_relocs)
    739      1.1  christos 	last_reloc = irel - 1;
    740      1.3  christos 
    741      1.3  christos       switch(ELF32_R_TYPE (irel->r_info))
    742      1.3  christos 	{
    743      1.3  christos 	case R_H8_DIR24R8:
    744      1.3  christos 	case R_H8_PCREL16:
    745      1.3  christos 	case R_H8_DIR16A8:
    746      1.3  christos 	case R_H8_DIR24A8:
    747      1.3  christos 	case R_H8_DIR32A16:
    748      1.3  christos 	case R_H8_DISP32A16:
    749      1.3  christos 	  break;
    750      1.3  christos 	default:
    751      1.3  christos 	  continue;
    752      1.1  christos 	}
    753      1.1  christos 
    754      1.1  christos       /* Get the section contents if we haven't done so already.  */
    755      1.1  christos       if (contents == NULL)
    756      1.1  christos 	{
    757      1.1  christos 	  /* Get cached copy if it exists.  */
    758      1.1  christos 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
    759      1.1  christos 	    contents = elf_section_data (sec)->this_hdr.contents;
    760      1.1  christos 	  else
    761      1.1  christos 	    {
    762      1.1  christos 	      /* Go get them off disk.  */
    763      1.1  christos 	      if (!bfd_malloc_and_get_section (abfd, sec, &contents))
    764      1.1  christos 		goto error_return;
    765      1.1  christos 	    }
    766      1.1  christos 	}
    767      1.1  christos 
    768      1.1  christos       /* Read this BFD's local symbols if we haven't done so already.  */
    769      1.1  christos       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
    770      1.1  christos 	{
    771      1.1  christos 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
    772      1.1  christos 	  if (isymbuf == NULL)
    773      1.1  christos 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
    774      1.1  christos 					    symtab_hdr->sh_info, 0,
    775      1.1  christos 					    NULL, NULL, NULL);
    776      1.1  christos 	  if (isymbuf == NULL)
    777      1.1  christos 	    goto error_return;
    778      1.1  christos 	}
    779      1.1  christos 
    780      1.1  christos       /* Get the value of the symbol referred to by the reloc.  */
    781      1.1  christos       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
    782      1.1  christos 	{
    783      1.1  christos 	  /* A local symbol.  */
    784      1.1  christos 	  Elf_Internal_Sym *isym;
    785      1.1  christos 	  asection *sym_sec;
    786      1.1  christos 
    787      1.1  christos 	  isym = isymbuf + ELF32_R_SYM (irel->r_info);
    788      1.1  christos 	  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
    789      1.1  christos 	  symval = isym->st_value;
    790      1.1  christos 	  /* If the reloc is absolute, it will not have
    791      1.1  christos 	     a symbol or section associated with it.  */
    792      1.1  christos 	  if (sym_sec)
    793      1.1  christos 	    symval += sym_sec->output_section->vma
    794      1.1  christos 	      + sym_sec->output_offset;
    795      1.1  christos 	}
    796      1.1  christos       else
    797      1.1  christos 	{
    798      1.1  christos 	  unsigned long indx;
    799      1.1  christos 	  struct elf_link_hash_entry *h;
    800      1.1  christos 
    801      1.1  christos 	  /* An external symbol.  */
    802      1.1  christos 	  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
    803      1.1  christos 	  h = elf_sym_hashes (abfd)[indx];
    804      1.1  christos 	  BFD_ASSERT (h != NULL);
    805      1.1  christos 	  if (h->root.type != bfd_link_hash_defined
    806      1.1  christos 	      && h->root.type != bfd_link_hash_defweak)
    807      1.1  christos 	    {
    808      1.1  christos 	      /* This appears to be a reference to an undefined
    809      1.1  christos                  symbol.  Just ignore it--it will be caught by the
    810      1.1  christos                  regular reloc processing.  */
    811      1.1  christos 	      continue;
    812      1.1  christos 	    }
    813      1.1  christos 
    814      1.1  christos 	  symval = (h->root.u.def.value
    815      1.1  christos 		    + h->root.u.def.section->output_section->vma
    816      1.1  christos 		    + h->root.u.def.section->output_offset);
    817      1.1  christos 	}
    818      1.1  christos 
    819      1.1  christos       /* For simplicity of coding, we are going to modify the section
    820      1.1  christos 	 contents, the section relocs, and the BFD symbol table.  We
    821      1.1  christos 	 must tell the rest of the code not to free up this
    822      1.1  christos 	 information.  It would be possible to instead create a table
    823      1.1  christos 	 of changes which have to be made, as is done in coff-mips.c;
    824      1.1  christos 	 that would be more work, but would require less memory when
    825      1.1  christos 	 the linker is run.  */
    826      1.1  christos       switch (ELF32_R_TYPE (irel->r_info))
    827      1.3  christos 	{
    828      1.3  christos 	  /* Try to turn a 24-bit absolute branch/call into an 8-bit
    829      1.1  christos 	     pc-relative branch/call.  */
    830      1.1  christos 	case R_H8_DIR24R8:
    831      1.1  christos 	  {
    832      1.1  christos 	    bfd_vma value = symval + irel->r_addend;
    833      1.1  christos 	    bfd_vma dot, gap;
    834      1.1  christos 
    835      1.1  christos 	    /* Get the address of this instruction.  */
    836      1.1  christos 	    dot = (sec->output_section->vma
    837      1.1  christos 		   + sec->output_offset + irel->r_offset - 1);
    838      1.1  christos 
    839      1.1  christos 	    /* Compute the distance from this insn to the branch target.  */
    840      1.1  christos 	    gap = value - dot;
    841      1.1  christos 
    842      1.1  christos 	    /* If the distance is within -126..+130 inclusive, then we can
    843      1.1  christos 	       relax this jump.  +130 is valid since the target will move
    844      1.1  christos 	       two bytes closer if we do relax this branch.  */
    845      1.1  christos 	    if ((int) gap >= -126 && (int) gap <= 130)
    846      1.1  christos 	      {
    847      1.1  christos 		unsigned char code;
    848      1.1  christos 
    849      1.1  christos 		/* Note that we've changed the relocs, section contents,
    850      1.1  christos 		   etc.  */
    851      1.1  christos 		elf_section_data (sec)->relocs = internal_relocs;
    852      1.1  christos 		elf_section_data (sec)->this_hdr.contents = contents;
    853      1.1  christos 		symtab_hdr->contents = (unsigned char *) isymbuf;
    854      1.1  christos 
    855      1.1  christos 		/* Get the instruction code being relaxed.  */
    856      1.1  christos 		code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
    857      1.1  christos 
    858      1.1  christos 		/* If the previous instruction conditionally jumped around
    859      1.1  christos 		   this instruction, we may be able to reverse the condition
    860      1.1  christos 		   and redirect the previous instruction to the target of
    861      1.1  christos 		   this instruction.
    862      1.1  christos 
    863      1.1  christos 		   Such sequences are used by the compiler to deal with
    864      1.1  christos 		   long conditional branches.
    865      1.1  christos 
    866      1.1  christos 		   Only perform this optimisation for jumps (code 0x5a) not
    867      1.1  christos 		   subroutine calls, as otherwise it could transform:
    868      1.3  christos 
    869      1.3  christos 		   mov.w   r0,r0
    870      1.3  christos 		   beq     .L1
    871      1.3  christos 		   jsr     @_bar
    872      1.3  christos 		   .L1:   rts
    873      1.1  christos 		   _bar:  rts
    874      1.3  christos 		   into:
    875      1.3  christos 		   mov.w   r0,r0
    876      1.3  christos 		   bne     _bar
    877      1.3  christos 		   rts
    878      1.1  christos 		   _bar:  rts
    879      1.1  christos 
    880      1.3  christos 		   which changes the call (jsr) into a branch (bne).  */
    881      1.1  christos 		if (code == 0x5a	/* jmp24.  */
    882      1.1  christos 		    && (int) gap <= 130
    883      1.1  christos 		    && (int) gap >= -128
    884      1.1  christos 		    && last_reloc
    885      1.1  christos 		    && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8
    886      1.1  christos 		    && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info)
    887      1.1  christos 		  {
    888      1.1  christos 		    bfd_vma last_value;
    889      1.1  christos 		    asection *last_sym_sec;
    890      1.1  christos 		    Elf_Internal_Sym *last_sym;
    891      1.1  christos 
    892      1.1  christos 		    /* We will need to examine the symbol used by the
    893      1.1  christos 		       previous relocation.  */
    894      1.1  christos 
    895      1.1  christos 		    last_sym = isymbuf + ELF32_R_SYM (last_reloc->r_info);
    896      1.1  christos 		    last_sym_sec
    897      1.1  christos 		      = bfd_section_from_elf_index (abfd, last_sym->st_shndx);
    898      1.1  christos 		    last_value = (last_sym->st_value
    899      1.1  christos 				  + last_sym_sec->output_section->vma
    900      1.1  christos 				  + last_sym_sec->output_offset);
    901      1.1  christos 
    902      1.1  christos 		    /* Verify that the previous relocation was for a
    903      1.1  christos 		       branch around this instruction and that no symbol
    904      1.1  christos 		       exists at the current location.  */
    905      1.1  christos 		    if (last_value == dot + 4
    906      1.1  christos 			&& last_reloc->r_offset + 2 == irel->r_offset
    907      1.1  christos 			&& ! elf32_h8_symbol_address_p (abfd, sec, dot))
    908      1.1  christos 		      {
    909      1.1  christos 			/* We can eliminate this jump.  Twiddle the
    910      1.1  christos 			   previous relocation as necessary.  */
    911      1.1  christos 			irel->r_info
    912      1.1  christos 			  = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
    913      1.1  christos 					  ELF32_R_TYPE (R_H8_NONE));
    914      1.1  christos 
    915      1.1  christos 			last_reloc->r_info
    916      1.1  christos 			  = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
    917      1.1  christos 					  ELF32_R_TYPE (R_H8_PCREL8));
    918      1.1  christos 			last_reloc->r_addend = irel->r_addend;
    919      1.1  christos 
    920      1.1  christos 			code = bfd_get_8 (abfd,
    921      1.1  christos 					  contents + last_reloc->r_offset - 1);
    922      1.1  christos 			code ^= 1;
    923      1.1  christos 			bfd_put_8 (abfd,
    924      1.3  christos 				   code,
    925      1.1  christos 				   contents + last_reloc->r_offset - 1);
    926      1.1  christos 
    927      1.1  christos 			/* Delete four bytes of data.  */
    928      1.1  christos 			if (!elf32_h8_relax_delete_bytes (abfd, sec,
    929      1.1  christos 							  irel->r_offset - 1,
    930      1.1  christos 							  4))
    931      1.1  christos 			  goto error_return;
    932      1.1  christos 
    933      1.1  christos 			*again = TRUE;
    934      1.1  christos 			break;
    935      1.1  christos 		      }
    936      1.1  christos 		  }
    937      1.1  christos 
    938      1.3  christos 		if (code == 0x5e)
    939      1.3  christos 		  /* This is jsr24  */
    940      1.1  christos 		  bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1);	/* bsr8. */
    941      1.3  christos 		else if (code == 0x5a)
    942      1.3  christos 		  /* This is jmp24  */
    943      1.1  christos 		  bfd_put_8 (abfd, 0x40, contents + irel->r_offset - 1);	/* bra8. */
    944      1.1  christos 		else
    945      1.1  christos 		  abort ();
    946      1.1  christos 
    947      1.1  christos 		/* Fix the relocation's type.  */
    948      1.1  christos 		irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
    949      1.1  christos 					     R_H8_PCREL8);
    950      1.1  christos 
    951      1.1  christos 		/* Delete two bytes of data.  */
    952      1.1  christos 		if (!elf32_h8_relax_delete_bytes (abfd, sec,
    953      1.1  christos 						  irel->r_offset + 1, 2))
    954      1.1  christos 		  goto error_return;
    955      1.1  christos 
    956      1.1  christos 		/* That will change things, so, we should relax again.
    957      1.1  christos 		   Note that this is not required, and it may be slow.  */
    958      1.1  christos 		*again = TRUE;
    959      1.1  christos 	      }
    960      1.1  christos 	    break;
    961      1.1  christos 	  }
    962      1.3  christos 
    963      1.3  christos 	  /* Try to turn a 16-bit pc-relative branch into a 8-bit pc-relative
    964      1.1  christos 	     branch.  */
    965      1.1  christos 	case R_H8_PCREL16:
    966      1.1  christos 	  {
    967      1.1  christos 	    bfd_vma value = symval + irel->r_addend;
    968      1.1  christos 	    bfd_vma dot;
    969      1.1  christos 	    bfd_vma gap;
    970      1.1  christos 
    971      1.1  christos 	    /* Get the address of this instruction.  */
    972      1.1  christos 	    dot = (sec->output_section->vma
    973      1.1  christos 		   + sec->output_offset
    974      1.1  christos 		   + irel->r_offset - 2);
    975      1.1  christos 
    976      1.1  christos 	    gap = value - dot;
    977      1.1  christos 
    978      1.1  christos 	    /* If the distance is within -126..+130 inclusive, then we can
    979      1.1  christos 	       relax this jump.  +130 is valid since the target will move
    980      1.1  christos 	       two bytes closer if we do relax this branch.  */
    981      1.1  christos 	    if ((int) gap >= -126 && (int) gap <= 130)
    982      1.1  christos 	      {
    983      1.1  christos 		unsigned char code;
    984      1.1  christos 
    985      1.1  christos 		/* Note that we've changed the relocs, section contents,
    986      1.1  christos 		   etc.  */
    987      1.1  christos 		elf_section_data (sec)->relocs = internal_relocs;
    988      1.1  christos 		elf_section_data (sec)->this_hdr.contents = contents;
    989      1.1  christos 		symtab_hdr->contents = (unsigned char *) isymbuf;
    990      1.1  christos 
    991      1.1  christos 		/* Get the opcode.  */
    992      1.1  christos 		code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
    993      1.1  christos 
    994      1.1  christos 		if (code == 0x58)
    995      1.1  christos 		  {
    996      1.1  christos 		    /* bCC:16 -> bCC:8 */
    997      1.1  christos 		    /* Get the second byte of the original insn, which
    998      1.1  christos 		       contains the condition code.  */
    999      1.1  christos 		    code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   1000      1.3  christos 
   1001      1.1  christos 		    /* Compute the first byte of the relaxed
   1002      1.1  christos 		       instruction.  The original sequence 0x58 0xX0
   1003      1.1  christos 		       is relaxed to 0x4X, where X represents the
   1004      1.1  christos 		       condition code.  */
   1005      1.1  christos 		    code &= 0xf0;
   1006      1.1  christos 		    code >>= 4;
   1007      1.3  christos 		    code |= 0x40;
   1008      1.1  christos 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 2); /* bCC:8.  */
   1009      1.3  christos 		  }
   1010      1.1  christos 		else if (code == 0x5c)	/* bsr16.  */
   1011      1.3  christos 		  /* This is bsr.  */
   1012      1.1  christos 		  bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2);  /* bsr8.  */
   1013      1.1  christos 		else
   1014      1.1  christos 		  /* Might be MOVSD.  */
   1015      1.1  christos 		  break;
   1016      1.1  christos 
   1017      1.1  christos 		/* Fix the relocation's type.  */
   1018      1.1  christos 		irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   1019      1.1  christos 					     R_H8_PCREL8);
   1020      1.1  christos 		irel->r_offset--;
   1021      1.1  christos 
   1022      1.1  christos 		/* Delete two bytes of data.  */
   1023      1.1  christos 		if (!elf32_h8_relax_delete_bytes (abfd, sec,
   1024      1.1  christos 						  irel->r_offset + 1, 2))
   1025      1.1  christos 		  goto error_return;
   1026      1.1  christos 
   1027      1.1  christos 		/* That will change things, so, we should relax again.
   1028      1.1  christos 		   Note that this is not required, and it may be slow.  */
   1029      1.1  christos 		*again = TRUE;
   1030      1.1  christos 	      }
   1031      1.1  christos 	    break;
   1032      1.1  christos 	  }
   1033      1.3  christos 
   1034      1.3  christos 	  /* This is a 16-bit absolute address in one of the following
   1035      1.1  christos 	     instructions:
   1036      1.1  christos 
   1037      1.1  christos 	     "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
   1038      1.1  christos 	     "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
   1039      1.1  christos 	     "mov.b"
   1040      1.3  christos 
   1041      1.3  christos 	     We may relax this into an 8-bit absolute address if it's in
   1042      1.1  christos 	     the right range.  */
   1043      1.1  christos 	case R_H8_DIR16A8:
   1044      1.1  christos 	  {
   1045      1.1  christos 	    bfd_vma value;
   1046      1.1  christos 
   1047      1.1  christos 	    value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
   1048      1.1  christos 	    if (value >= 0xffffff00u)
   1049      1.1  christos 	      {
   1050      1.1  christos 		unsigned char code;
   1051      1.1  christos 		unsigned char temp_code;
   1052      1.1  christos 
   1053      1.1  christos 		/* Note that we've changed the relocs, section contents,
   1054      1.1  christos 		   etc.  */
   1055      1.1  christos 		elf_section_data (sec)->relocs = internal_relocs;
   1056      1.1  christos 		elf_section_data (sec)->this_hdr.contents = contents;
   1057      1.1  christos 		symtab_hdr->contents = (unsigned char *) isymbuf;
   1058      1.1  christos 
   1059      1.1  christos 		/* Get the opcode.  */
   1060      1.1  christos 		code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
   1061      1.1  christos 
   1062      1.1  christos 		/* All instructions with R_H8_DIR16A8 start with
   1063      1.1  christos 		   0x6a.  */
   1064      1.1  christos 		if (code != 0x6a)
   1065      1.1  christos 		  abort ();
   1066      1.1  christos 
   1067      1.1  christos 		temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   1068      1.1  christos 		/* If this is a mov.b instruction, clear the lower
   1069      1.1  christos 		   nibble, which contains the source/destination
   1070      1.1  christos 		   register number.  */
   1071      1.1  christos 		if ((temp_code & 0x10) != 0x10)
   1072      1.1  christos 		  temp_code &= 0xf0;
   1073      1.1  christos 
   1074      1.1  christos 		switch (temp_code)
   1075      1.1  christos 		  {
   1076      1.1  christos 		  case 0x00:
   1077      1.1  christos 		    /* This is mov.b @aa:16,Rd.  */
   1078      1.1  christos 		    bfd_put_8 (abfd, (code & 0xf) | 0x20,
   1079      1.1  christos 			       contents + irel->r_offset - 2);
   1080      1.1  christos 		    break;
   1081      1.1  christos 		  case 0x80:
   1082      1.1  christos 		    /* This is mov.b Rs,@aa:16.  */
   1083      1.1  christos 		    bfd_put_8 (abfd, (code & 0xf) | 0x30,
   1084      1.1  christos 			       contents + irel->r_offset - 2);
   1085      1.1  christos 		    break;
   1086      1.1  christos 		  case 0x18:
   1087      1.1  christos 		    /* This is a bit-maniputation instruction that
   1088      1.1  christos 		       stores one bit into memory, one of "bclr",
   1089      1.1  christos 		       "bist", "bnot", "bset", and "bst".  */
   1090      1.1  christos 		    bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2);
   1091      1.1  christos 		    break;
   1092      1.1  christos 		  case 0x10:
   1093      1.1  christos 		    /* This is a bit-maniputation instruction that
   1094      1.1  christos 		       loads one bit from memory, one of "band",
   1095      1.1  christos 		       "biand", "bild", "bior", "bixor", "bld", "bor",
   1096      1.1  christos 		       "btst", and "bxor".  */
   1097      1.1  christos 		    bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2);
   1098      1.1  christos 		    break;
   1099      1.1  christos 		  default:
   1100      1.1  christos 		    abort ();
   1101      1.1  christos 		  }
   1102      1.1  christos 
   1103      1.1  christos 		/* Fix the relocation's type.  */
   1104      1.1  christos 		irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   1105      1.1  christos 					     R_H8_DIR8);
   1106      1.1  christos 
   1107      1.1  christos 		/* Move the relocation.  */
   1108      1.1  christos 		irel->r_offset--;
   1109      1.1  christos 
   1110      1.1  christos 		/* Delete two bytes of data.  */
   1111      1.1  christos 		if (!elf32_h8_relax_delete_bytes (abfd, sec,
   1112      1.1  christos 						  irel->r_offset + 1, 2))
   1113      1.1  christos 		  goto error_return;
   1114      1.1  christos 
   1115      1.1  christos 		/* That will change things, so, we should relax again.
   1116      1.1  christos 		   Note that this is not required, and it may be slow.  */
   1117      1.1  christos 		*again = TRUE;
   1118      1.1  christos 	      }
   1119      1.1  christos 	    break;
   1120      1.1  christos 	  }
   1121      1.3  christos 
   1122      1.3  christos 	  /* This is a 24-bit absolute address in one of the following
   1123      1.1  christos 	     instructions:
   1124      1.1  christos 
   1125      1.1  christos 	     "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
   1126      1.1  christos 	     "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
   1127      1.1  christos 	     "mov.b"
   1128      1.3  christos 
   1129      1.3  christos 	     We may relax this into an 8-bit absolute address if it's in
   1130      1.1  christos 	     the right range.  */
   1131      1.1  christos 	case R_H8_DIR24A8:
   1132      1.1  christos 	  {
   1133      1.1  christos 	    bfd_vma value;
   1134      1.1  christos 
   1135      1.1  christos 	    value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
   1136      1.1  christos 	    if (value >= 0xffffff00u)
   1137      1.1  christos 	      {
   1138      1.1  christos 		unsigned char code;
   1139      1.1  christos 		unsigned char temp_code;
   1140      1.1  christos 
   1141      1.1  christos 		/* Note that we've changed the relocs, section contents,
   1142      1.1  christos 		   etc.  */
   1143      1.1  christos 		elf_section_data (sec)->relocs = internal_relocs;
   1144      1.1  christos 		elf_section_data (sec)->this_hdr.contents = contents;
   1145      1.1  christos 		symtab_hdr->contents = (unsigned char *) isymbuf;
   1146      1.1  christos 
   1147      1.1  christos 		/* Get the opcode.  */
   1148      1.1  christos 		code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
   1149      1.1  christos 
   1150      1.1  christos 		/* All instructions with R_H8_DIR24A8 start with
   1151      1.1  christos 		   0x6a.  */
   1152      1.1  christos 		if (code != 0x6a)
   1153      1.1  christos 		  abort ();
   1154      1.1  christos 
   1155      1.1  christos 		temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   1156      1.1  christos 
   1157      1.1  christos 		/* If this is a mov.b instruction, clear the lower
   1158      1.1  christos 		   nibble, which contains the source/destination
   1159      1.1  christos 		   register number.  */
   1160      1.1  christos 		if ((temp_code & 0x30) != 0x30)
   1161      1.1  christos 		  temp_code &= 0xf0;
   1162      1.1  christos 
   1163      1.1  christos 		switch (temp_code)
   1164      1.1  christos 		  {
   1165      1.1  christos 		  case 0x20:
   1166      1.1  christos 		    /* This is mov.b @aa:24/32,Rd.  */
   1167      1.1  christos 		    bfd_put_8 (abfd, (code & 0xf) | 0x20,
   1168      1.1  christos 			       contents + irel->r_offset - 2);
   1169      1.1  christos 		    break;
   1170      1.1  christos 		  case 0xa0:
   1171      1.1  christos 		    /* This is mov.b Rs,@aa:24/32.  */
   1172      1.1  christos 		    bfd_put_8 (abfd, (code & 0xf) | 0x30,
   1173      1.1  christos 			       contents + irel->r_offset - 2);
   1174      1.1  christos 		    break;
   1175      1.1  christos 		  case 0x38:
   1176      1.1  christos 		    /* This is a bit-maniputation instruction that
   1177      1.1  christos 		       stores one bit into memory, one of "bclr",
   1178      1.1  christos 		       "bist", "bnot", "bset", and "bst".  */
   1179      1.1  christos 		    bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2);
   1180      1.1  christos 		    break;
   1181      1.1  christos 		  case 0x30:
   1182      1.1  christos 		    /* This is a bit-maniputation instruction that
   1183      1.1  christos 		       loads one bit from memory, one of "band",
   1184      1.1  christos 		       "biand", "bild", "bior", "bixor", "bld", "bor",
   1185      1.1  christos 		       "btst", and "bxor".  */
   1186      1.1  christos 		    bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2);
   1187      1.1  christos 		    break;
   1188      1.1  christos 		  default:
   1189      1.1  christos 		    abort();
   1190      1.1  christos 		  }
   1191      1.1  christos 
   1192      1.1  christos 		/* Fix the relocation's type.  */
   1193      1.1  christos 		irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   1194      1.1  christos 					     R_H8_DIR8);
   1195      1.1  christos 		irel->r_offset--;
   1196      1.3  christos 
   1197      1.1  christos 		/* Delete four bytes of data.  */
   1198      1.1  christos 		if (!elf32_h8_relax_delete_bytes (abfd, sec,
   1199      1.1  christos 						  irel->r_offset + 1, 4))
   1200      1.1  christos 		  goto error_return;
   1201      1.1  christos 
   1202      1.1  christos 		/* That will change things, so, we should relax again.
   1203      1.1  christos 		   Note that this is not required, and it may be slow.  */
   1204      1.1  christos 		*again = TRUE;
   1205      1.1  christos 		break;
   1206      1.1  christos 	      }
   1207      1.1  christos 	  }
   1208      1.1  christos 
   1209      1.1  christos 	  /* Fall through.  */
   1210      1.1  christos 
   1211      1.1  christos 	  /* This is a 24-/32-bit absolute address in one of the
   1212      1.1  christos 	     following instructions:
   1213      1.3  christos 
   1214      1.3  christos 	     "band", "bclr", "biand", "bild", "bior", "bist",
   1215      1.3  christos 	     "bixor", "bld", "bnot", "bor", "bset", "bst", "btst",
   1216      1.1  christos 	     "bxor", "ldc.w", "stc.w" and "mov.[bwl]"
   1217      1.1  christos 
   1218      1.1  christos 	     We may relax this into an 16-bit absolute address if it's
   1219      1.1  christos 	     in the right range.  */
   1220      1.1  christos 	case R_H8_DIR32A16:
   1221      1.1  christos 	  {
   1222      1.1  christos 	    bfd_vma value;
   1223      1.1  christos 
   1224      1.1  christos 	    value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
   1225      1.1  christos 	    if (value <= 0x7fff || value >= 0xffff8000u)
   1226      1.1  christos 	      {
   1227      1.1  christos 		unsigned char code;
   1228      1.1  christos 		unsigned char op0, op1, op2, op3;
   1229      1.1  christos 		unsigned char *op_ptr;
   1230      1.1  christos 
   1231      1.1  christos 		/* Note that we've changed the relocs, section contents,
   1232      1.1  christos 		   etc.  */
   1233      1.1  christos 		elf_section_data (sec)->relocs = internal_relocs;
   1234      1.1  christos 		elf_section_data (sec)->this_hdr.contents = contents;
   1235      1.1  christos 		symtab_hdr->contents = (unsigned char *) isymbuf;
   1236      1.1  christos 
   1237      1.1  christos 		if (irel->r_offset >= 4)
   1238      1.3  christos 		  {
   1239      1.1  christos 		    /* Check for 4-byte MOVA relaxation (SH-specific).  */
   1240      1.1  christos 		    int second_reloc = 0;
   1241      1.1  christos 
   1242      1.1  christos 		    op_ptr = contents + irel->r_offset - 4;
   1243      1.1  christos 
   1244      1.1  christos 		    if (last_reloc)
   1245      1.1  christos 		      {
   1246      1.1  christos 			arelent bfd_reloc;
   1247      1.1  christos 			reloc_howto_type *h;
   1248      1.1  christos 			bfd_vma last_reloc_size;
   1249      1.1  christos 
   1250      1.1  christos 			elf32_h8_info_to_howto (abfd, &bfd_reloc, last_reloc);
   1251      1.1  christos 			h = bfd_reloc.howto;
   1252      1.1  christos 			last_reloc_size = 1 << h->size;
   1253      1.1  christos 			if (last_reloc->r_offset + last_reloc_size
   1254      1.1  christos 			    == irel->r_offset)
   1255      1.1  christos 			  {
   1256      1.1  christos 			    op_ptr -= last_reloc_size;
   1257      1.1  christos 			    second_reloc = 1;
   1258      1.1  christos 			  }
   1259      1.3  christos 		      }
   1260      1.3  christos 
   1261      1.1  christos 		    if (irel + 1 < irelend)
   1262      1.1  christos 		      {
   1263      1.1  christos 			Elf_Internal_Rela *next_reloc = irel + 1;
   1264      1.1  christos 			arelent bfd_reloc;
   1265      1.1  christos 			reloc_howto_type *h;
   1266      1.1  christos 			bfd_vma next_reloc_size;
   1267      1.1  christos 
   1268      1.1  christos 			elf32_h8_info_to_howto (abfd, &bfd_reloc, next_reloc);
   1269      1.1  christos 			h = bfd_reloc.howto;
   1270      1.1  christos 			next_reloc_size = 1 << h->size;
   1271      1.1  christos 			if (next_reloc->r_offset + next_reloc_size
   1272      1.1  christos 			    == irel->r_offset)
   1273      1.1  christos 			  {
   1274      1.1  christos 			    op_ptr -= next_reloc_size;
   1275      1.1  christos 			    second_reloc = 1;
   1276      1.1  christos 			  }
   1277      1.1  christos 		      }
   1278      1.1  christos 
   1279      1.1  christos 		    op0 = bfd_get_8 (abfd, op_ptr + 0);
   1280      1.1  christos 		    op1 = bfd_get_8 (abfd, op_ptr + 1);
   1281      1.1  christos 		    op2 = bfd_get_8 (abfd, op_ptr + 2);
   1282      1.1  christos 		    op3 = bfd_get_8 (abfd, op_ptr + 3);
   1283      1.1  christos 
   1284      1.1  christos 		    if (op0 == 0x01
   1285      1.1  christos 			&& (op1 & 0xdf) == 0x5f
   1286      1.1  christos 			&& (op2 & 0x40) == 0x40
   1287      1.1  christos 			&& (op3 & 0x80) == 0x80)
   1288      1.1  christos 		      {
   1289      1.1  christos 			if ((op2 & 0x08) == 0)
   1290      1.1  christos 			  second_reloc = 1;
   1291      1.1  christos 
   1292      1.1  christos 			if (second_reloc)
   1293      1.1  christos 			  {
   1294      1.1  christos 			    op3 &= ~0x08;
   1295      1.1  christos 			    bfd_put_8 (abfd, op3, op_ptr + 3);
   1296      1.1  christos 			  }
   1297      1.1  christos 			else
   1298      1.1  christos 			  {
   1299      1.1  christos 			    op2 &= ~0x08;
   1300      1.1  christos 			    bfd_put_8 (abfd, op2, op_ptr + 2);
   1301      1.1  christos 			  }
   1302      1.1  christos 			goto r_h8_dir32a16_common;
   1303      1.1  christos 		      }
   1304      1.1  christos 		  }
   1305      1.3  christos 
   1306      1.1  christos 		/* Now check for short version of MOVA.  (SH-specific) */
   1307      1.1  christos 		op_ptr = contents + irel->r_offset - 2;
   1308      1.1  christos 		op0 = bfd_get_8 (abfd, op_ptr + 0);
   1309      1.1  christos 		op1 = bfd_get_8 (abfd, op_ptr + 1);
   1310      1.1  christos 
   1311      1.1  christos 		if (op0 == 0x7a
   1312      1.1  christos 		    && (op1 & 0x88) == 0x80)
   1313      1.1  christos 		  {
   1314      1.1  christos 		    op1 |= 0x08;
   1315      1.1  christos 		    bfd_put_8 (abfd, op1, op_ptr + 1);
   1316      1.1  christos 		    goto r_h8_dir32a16_common;
   1317      1.1  christos 		  }
   1318      1.1  christos 
   1319      1.1  christos 		/* Get the opcode.  */
   1320      1.1  christos 		code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   1321      1.1  christos 
   1322      1.1  christos 		/* Fix the opcode.  For all the instructions that
   1323      1.1  christos 		   belong to this relaxation, we simply need to turn
   1324      1.1  christos 		   off bit 0x20 in the previous byte.  */
   1325      1.1  christos 		code &= ~0x20;
   1326      1.1  christos 
   1327      1.1  christos 		bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
   1328      1.1  christos 
   1329      1.1  christos 	      r_h8_dir32a16_common:
   1330      1.1  christos 		/* Fix the relocation's type.  */
   1331      1.1  christos 		irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   1332      1.1  christos 					     R_H8_DIR16);
   1333      1.1  christos 
   1334      1.1  christos 		/* Delete two bytes of data.  */
   1335      1.1  christos 		if (!elf32_h8_relax_delete_bytes (abfd, sec,
   1336      1.1  christos 						  irel->r_offset + 1, 2))
   1337      1.1  christos 		  goto error_return;
   1338      1.1  christos 
   1339      1.1  christos 		/* That will change things, so, we should relax again.
   1340      1.1  christos 		   Note that this is not required, and it may be slow.  */
   1341      1.1  christos 		*again = TRUE;
   1342      1.3  christos 	      }
   1343      1.3  christos 	    break;	/* case R_H8_DIR32A16 */
   1344      1.3  christos 	  }
   1345      1.3  christos 
   1346      1.3  christos 	case R_H8_DISP32A16:
   1347      1.3  christos 	  /* mov.[bwl] @(displ:24/32+ERx) -> mov.[bwl] @(displ:16+ERx)  4 bytes
   1348      1.3  christos 	     It is assured that instruction uses at least 4 bytes opcode before
   1349      1.3  christos 	     reloc entry addressing mode "register indirect with displacement"
   1350      1.3  christos 	     relaxing options (all saving 4 bytes):
   1351      1.3  christos 	     0x78 0sss0000 0x6A 0010dddd disp:32  mov.b @(d:32,ERs),Rd  ->
   1352      1.3  christos 	     0x6E 0sssdddd disp:16  mov.b @(d:16,ERs),Rd
   1353      1.3  christos 	     0x78 0sss0000 0x6B 0010dddd disp:32  mov.w @(d:32,ERs),Rd  ->
   1354      1.3  christos 	     0x6F 0sssdddd disp:16  mov.w @(d:16,ERs),Rd
   1355      1.3  christos 	     0x01 0x00 0x78 0sss0000 0x6B 00100ddd disp:32  mov.l @(d:32,ERs),ERd ->
   1356      1.3  christos 	     0x01 0x00 0x6F 0sss0ddd disp:16  mov.l @(d:16,ERs),ERd
   1357      1.3  christos 
   1358      1.3  christos 	     0x78 0ddd0000 0x6A 1010ssss disp:32  mov.b Rs,@(d:32,ERd)  ->
   1359      1.3  christos 	     0x6E 1dddssss disp:16  mov.b Rs,@(d:16,ERd)
   1360      1.3  christos 	     0x78 0ddd0000 0x6B 1010ssss disp:32  mov.w Rs,@(d:32,ERd)  ->
   1361      1.3  christos 	     0x6F 1dddssss disp:16  mov.w Rs,@(d:16,ERd)
   1362      1.3  christos 	     0x01 0x00 0x78 xddd0000 0x6B 10100sss disp:32  mov.l ERs,@(d:32,ERd) ->
   1363      1.3  christos 	     0x01 0x00 0x6F 1ddd0sss disp:16  mov.l ERs,@(d:16,ERd)
   1364      1.3  christos 	     mov.l prefix 0x01 0x00 can be left as is and mov.l handled same
   1365      1.3  christos 	     as mov.w/  */
   1366      1.3  christos 	  {
   1367      1.3  christos 	    bfd_vma value;
   1368      1.3  christos 
   1369      1.3  christos 	    value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
   1370      1.3  christos 	    if (value <= 0x7fff || value >= 0xffff8000u)
   1371      1.3  christos 	      {
   1372      1.3  christos 		unsigned char op0, op1, op2, op3, op0n, op1n;
   1373      1.3  christos 		int relax = 0;
   1374      1.3  christos 
   1375      1.3  christos 		/* Note that we've changed the relocs, section contents,
   1376      1.3  christos 		   etc.  */
   1377      1.3  christos 		elf_section_data (sec)->relocs = internal_relocs;
   1378      1.3  christos 		elf_section_data (sec)->this_hdr.contents = contents;
   1379      1.3  christos 		symtab_hdr->contents = (unsigned char *) isymbuf;
   1380      1.3  christos 
   1381      1.3  christos 		if (irel->r_offset >= 4)
   1382      1.3  christos 		  {
   1383      1.3  christos 		    op0 = bfd_get_8 (abfd, contents + irel->r_offset - 4);
   1384      1.3  christos 		    op1 = bfd_get_8 (abfd, contents + irel->r_offset - 3);
   1385      1.3  christos 		    op2 = bfd_get_8 (abfd, contents + irel->r_offset - 2);
   1386      1.3  christos 		    op3 = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   1387      1.3  christos 
   1388      1.3  christos 		    if (op0 == 0x78)
   1389      1.3  christos 		      {
   1390      1.3  christos 			switch(op2)
   1391      1.3  christos 			  {
   1392      1.3  christos 			  case 0x6A:
   1393      1.3  christos 			    if ((op1 & 0x8F) == 0x00 && (op3 & 0x70) == 0x20)
   1394      1.3  christos 			      {
   1395      1.3  christos 				/* mov.b.  */
   1396      1.3  christos 				op0n = 0x6E;
   1397      1.3  christos 				relax = 1;
   1398      1.3  christos 			      }
   1399      1.3  christos 			    break;
   1400      1.3  christos 			  case 0x6B:
   1401      1.3  christos 			    if ((op1 & 0x0F) == 0x00 && (op3 & 0x70) == 0x20)
   1402      1.3  christos 			      {
   1403      1.3  christos 				/* mov.w/l.  */
   1404      1.3  christos 				op0n = 0x6F;
   1405      1.3  christos 				relax = 1;
   1406      1.3  christos 			      }
   1407      1.3  christos 			    break;
   1408      1.3  christos 			  default:
   1409      1.3  christos 			    break;
   1410      1.3  christos 			  }
   1411      1.3  christos 		      }
   1412      1.3  christos 		  }
   1413      1.3  christos 
   1414      1.3  christos 		if (relax)
   1415      1.3  christos 		  {
   1416      1.3  christos 		    op1n = (op3 & 0x8F) | (op1 & 0x70);
   1417      1.3  christos 		    bfd_put_8 (abfd, op0n, contents + irel->r_offset - 4);
   1418      1.3  christos 		    bfd_put_8 (abfd, op1n, contents + irel->r_offset - 3);
   1419      1.3  christos 
   1420      1.3  christos 		    /* Fix the relocation's type.  */
   1421      1.3  christos 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_H8_DIR16);
   1422      1.3  christos 		    irel->r_offset -= 2;
   1423      1.3  christos 
   1424      1.3  christos 		    /* Delete four bytes of data.  */
   1425      1.3  christos 		    if (!elf32_h8_relax_delete_bytes (abfd, sec, irel->r_offset + 2, 4))
   1426      1.3  christos 		      goto error_return;
   1427      1.3  christos 
   1428      1.3  christos 		    /* That will change things, so, we should relax again.
   1429      1.3  christos 		       Note that this is not required, and it may be slow.  */
   1430      1.3  christos 		    *again = TRUE;
   1431      1.3  christos 		  }
   1432      1.1  christos 	      }
   1433      1.3  christos 	  }
   1434      1.1  christos 	  break;
   1435      1.1  christos 
   1436      1.1  christos 	default:
   1437      1.1  christos 	  break;
   1438      1.1  christos 	}
   1439      1.1  christos     }
   1440      1.1  christos 
   1441      1.1  christos   if (isymbuf != NULL
   1442      1.1  christos       && symtab_hdr->contents != (unsigned char *) isymbuf)
   1443      1.1  christos     {
   1444      1.1  christos       if (! link_info->keep_memory)
   1445      1.1  christos 	free (isymbuf);
   1446      1.1  christos       else
   1447      1.1  christos 	symtab_hdr->contents = (unsigned char *) isymbuf;
   1448      1.1  christos     }
   1449      1.1  christos 
   1450      1.1  christos   if (contents != NULL
   1451      1.1  christos       && elf_section_data (sec)->this_hdr.contents != contents)
   1452      1.1  christos     {
   1453      1.1  christos       if (! link_info->keep_memory)
   1454      1.1  christos 	free (contents);
   1455      1.1  christos       else
   1456      1.1  christos 	{
   1457      1.1  christos 	  /* Cache the section contents for elf_link_input_bfd.  */
   1458      1.1  christos 	  elf_section_data (sec)->this_hdr.contents = contents;
   1459      1.1  christos 	}
   1460      1.1  christos     }
   1461      1.1  christos 
   1462      1.1  christos   if (internal_relocs != NULL
   1463      1.1  christos       && elf_section_data (sec)->relocs != internal_relocs)
   1464      1.1  christos     free (internal_relocs);
   1465      1.1  christos 
   1466      1.1  christos   return TRUE;
   1467      1.1  christos 
   1468      1.1  christos  error_return:
   1469      1.1  christos   if (isymbuf != NULL
   1470      1.1  christos       && symtab_hdr->contents != (unsigned char *) isymbuf)
   1471      1.1  christos     free (isymbuf);
   1472      1.1  christos   if (contents != NULL
   1473      1.1  christos       && elf_section_data (sec)->this_hdr.contents != contents)
   1474      1.1  christos     free (contents);
   1475      1.1  christos   if (internal_relocs != NULL
   1476      1.1  christos       && elf_section_data (sec)->relocs != internal_relocs)
   1477      1.1  christos     free (internal_relocs);
   1478      1.1  christos   return FALSE;
   1479      1.1  christos }
   1480      1.1  christos 
   1481      1.1  christos /* Delete some bytes from a section while relaxing.  */
   1482      1.1  christos 
   1483      1.1  christos static bfd_boolean
   1484      1.1  christos elf32_h8_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count)
   1485      1.1  christos {
   1486      1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1487      1.1  christos   unsigned int sec_shndx;
   1488      1.1  christos   bfd_byte *contents;
   1489      1.1  christos   Elf_Internal_Rela *irel, *irelend;
   1490      1.1  christos   Elf_Internal_Sym *isym;
   1491      1.1  christos   Elf_Internal_Sym *isymend;
   1492      1.1  christos   bfd_vma toaddr;
   1493      1.1  christos   struct elf_link_hash_entry **sym_hashes;
   1494      1.1  christos   struct elf_link_hash_entry **end_hashes;
   1495      1.1  christos   unsigned int symcount;
   1496      1.1  christos 
   1497      1.1  christos   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   1498      1.1  christos 
   1499      1.1  christos   contents = elf_section_data (sec)->this_hdr.contents;
   1500      1.1  christos 
   1501      1.1  christos   toaddr = sec->size;
   1502      1.1  christos 
   1503      1.1  christos   irel = elf_section_data (sec)->relocs;
   1504      1.1  christos   irelend = irel + sec->reloc_count;
   1505      1.1  christos 
   1506      1.1  christos   /* Actually delete the bytes.  */
   1507      1.1  christos   memmove (contents + addr, contents + addr + count,
   1508      1.1  christos 	   (size_t) (toaddr - addr - count));
   1509      1.1  christos   sec->size -= count;
   1510      1.1  christos 
   1511      1.1  christos   /* Adjust all the relocs.  */
   1512      1.1  christos   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
   1513      1.1  christos     {
   1514      1.1  christos       /* Get the new reloc address.  */
   1515      1.3  christos       if ((irel->r_offset > addr
   1516      1.1  christos 	   && irel->r_offset <= toaddr))
   1517      1.1  christos 	irel->r_offset -= count;
   1518      1.1  christos     }
   1519      1.1  christos 
   1520      1.1  christos   /* Adjust the local symbols defined in this section.  */
   1521      1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1522      1.1  christos   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   1523      1.1  christos   isymend = isym + symtab_hdr->sh_info;
   1524      1.1  christos   for (; isym < isymend; isym++)
   1525      1.1  christos     {
   1526      1.1  christos       if (isym->st_shndx == sec_shndx
   1527      1.3  christos 	  && isym->st_value > addr
   1528      1.1  christos 	  && isym->st_value <= toaddr)
   1529      1.1  christos 	isym->st_value -= count;
   1530      1.1  christos     }
   1531      1.1  christos 
   1532      1.1  christos   /* Now adjust the global symbols defined in this section.  */
   1533      1.1  christos   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   1534      1.1  christos 	      - symtab_hdr->sh_info);
   1535      1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   1536      1.1  christos   end_hashes = sym_hashes + symcount;
   1537      1.1  christos   for (; sym_hashes < end_hashes; sym_hashes++)
   1538      1.1  christos     {
   1539      1.3  christos       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   1540      1.1  christos 
   1541      1.1  christos       if ((sym_hash->root.type == bfd_link_hash_defined
   1542      1.1  christos 	   || sym_hash->root.type == bfd_link_hash_defweak)
   1543      1.1  christos 	  && sym_hash->root.u.def.section == sec
   1544      1.3  christos 	  && sym_hash->root.u.def.value > addr
   1545      1.3  christos 	  && sym_hash->root.u.def.value <= toaddr)
   1546      1.1  christos 	sym_hash->root.u.def.value -= count;
   1547      1.1  christos     }
   1548      1.1  christos 
   1549      1.1  christos   return TRUE;
   1550      1.1  christos }
   1551      1.1  christos 
   1552      1.1  christos /* Return TRUE if a symbol exists at the given address, else return
   1553      1.1  christos    FALSE.  */
   1554      1.1  christos static bfd_boolean
   1555      1.1  christos elf32_h8_symbol_address_p (bfd *abfd, asection *sec, bfd_vma addr)
   1556      1.1  christos {
   1557      1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1558      1.1  christos   unsigned int sec_shndx;
   1559      1.1  christos   Elf_Internal_Sym *isym;
   1560      1.1  christos   Elf_Internal_Sym *isymend;
   1561      1.1  christos   struct elf_link_hash_entry **sym_hashes;
   1562      1.1  christos   struct elf_link_hash_entry **end_hashes;
   1563      1.1  christos   unsigned int symcount;
   1564      1.1  christos 
   1565      1.1  christos   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   1566      1.1  christos 
   1567      1.1  christos   /* Examine all the symbols.  */
   1568      1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1569      1.1  christos   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   1570      1.1  christos   isymend = isym + symtab_hdr->sh_info;
   1571      1.1  christos   for (; isym < isymend; isym++)
   1572      1.1  christos     {
   1573      1.1  christos       if (isym->st_shndx == sec_shndx
   1574      1.1  christos 	  && isym->st_value == addr)
   1575      1.1  christos 	return TRUE;
   1576      1.1  christos     }
   1577      1.1  christos 
   1578      1.1  christos   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   1579      1.1  christos 	      - symtab_hdr->sh_info);
   1580      1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   1581      1.1  christos   end_hashes = sym_hashes + symcount;
   1582      1.1  christos   for (; sym_hashes < end_hashes; sym_hashes++)
   1583      1.1  christos     {
   1584      1.1  christos       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   1585      1.1  christos       if ((sym_hash->root.type == bfd_link_hash_defined
   1586      1.1  christos 	   || sym_hash->root.type == bfd_link_hash_defweak)
   1587      1.1  christos 	  && sym_hash->root.u.def.section == sec
   1588      1.1  christos 	  && sym_hash->root.u.def.value == addr)
   1589      1.1  christos 	return TRUE;
   1590      1.1  christos     }
   1591      1.1  christos 
   1592      1.1  christos   return FALSE;
   1593      1.1  christos }
   1594      1.1  christos 
   1595      1.1  christos /* This is a version of bfd_generic_get_relocated_section_contents
   1596      1.1  christos    which uses elf32_h8_relocate_section.  */
   1597      1.1  christos 
   1598      1.1  christos static bfd_byte *
   1599      1.1  christos elf32_h8_get_relocated_section_contents (bfd *output_bfd,
   1600      1.1  christos 					 struct bfd_link_info *link_info,
   1601      1.1  christos 					 struct bfd_link_order *link_order,
   1602      1.1  christos 					 bfd_byte *data,
   1603      1.1  christos 					 bfd_boolean relocatable,
   1604      1.1  christos 					 asymbol **symbols)
   1605      1.1  christos {
   1606      1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1607      1.1  christos   asection *input_section = link_order->u.indirect.section;
   1608      1.1  christos   bfd *input_bfd = input_section->owner;
   1609      1.1  christos   asection **sections = NULL;
   1610      1.1  christos   Elf_Internal_Rela *internal_relocs = NULL;
   1611      1.1  christos   Elf_Internal_Sym *isymbuf = NULL;
   1612      1.1  christos 
   1613      1.1  christos   /* We only need to handle the case of relaxing, or of having a
   1614      1.1  christos      particular set of section contents, specially.  */
   1615      1.1  christos   if (relocatable
   1616      1.1  christos       || elf_section_data (input_section)->this_hdr.contents == NULL)
   1617      1.1  christos     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
   1618      1.1  christos 						       link_order, data,
   1619      1.1  christos 						       relocatable,
   1620      1.1  christos 						       symbols);
   1621      1.1  christos 
   1622      1.1  christos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   1623      1.1  christos 
   1624      1.1  christos   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
   1625      1.1  christos 	  (size_t) input_section->size);
   1626      1.1  christos 
   1627      1.1  christos   if ((input_section->flags & SEC_RELOC) != 0
   1628      1.1  christos       && input_section->reloc_count > 0)
   1629      1.1  christos     {
   1630      1.1  christos       asection **secpp;
   1631      1.1  christos       Elf_Internal_Sym *isym, *isymend;
   1632      1.1  christos       bfd_size_type amt;
   1633      1.1  christos 
   1634      1.1  christos       internal_relocs = (_bfd_elf_link_read_relocs
   1635      1.1  christos 			 (input_bfd, input_section, NULL,
   1636      1.1  christos 			  (Elf_Internal_Rela *) NULL, FALSE));
   1637      1.1  christos       if (internal_relocs == NULL)
   1638      1.1  christos 	goto error_return;
   1639      1.1  christos 
   1640      1.1  christos       if (symtab_hdr->sh_info != 0)
   1641      1.1  christos 	{
   1642      1.1  christos 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   1643      1.1  christos 	  if (isymbuf == NULL)
   1644      1.1  christos 	    isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   1645      1.1  christos 					    symtab_hdr->sh_info, 0,
   1646      1.1  christos 					    NULL, NULL, NULL);
   1647      1.1  christos 	  if (isymbuf == NULL)
   1648      1.1  christos 	    goto error_return;
   1649      1.1  christos 	}
   1650      1.1  christos 
   1651      1.1  christos       amt = symtab_hdr->sh_info;
   1652      1.1  christos       amt *= sizeof (asection *);
   1653      1.1  christos       sections = (asection **) bfd_malloc (amt);
   1654      1.1  christos       if (sections == NULL && amt != 0)
   1655      1.1  christos 	goto error_return;
   1656      1.1  christos 
   1657      1.1  christos       isymend = isymbuf + symtab_hdr->sh_info;
   1658      1.1  christos       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
   1659      1.1  christos 	{
   1660      1.1  christos 	  asection *isec;
   1661      1.1  christos 
   1662      1.1  christos 	  if (isym->st_shndx == SHN_UNDEF)
   1663      1.1  christos 	    isec = bfd_und_section_ptr;
   1664      1.1  christos 	  else if (isym->st_shndx == SHN_ABS)
   1665      1.1  christos 	    isec = bfd_abs_section_ptr;
   1666      1.1  christos 	  else if (isym->st_shndx == SHN_COMMON)
   1667      1.1  christos 	    isec = bfd_com_section_ptr;
   1668      1.1  christos 	  else
   1669      1.1  christos 	    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   1670      1.1  christos 
   1671      1.1  christos 	  *secpp = isec;
   1672      1.1  christos 	}
   1673      1.1  christos 
   1674      1.1  christos       if (! elf32_h8_relocate_section (output_bfd, link_info, input_bfd,
   1675      1.1  christos 				       input_section, data, internal_relocs,
   1676      1.1  christos 				       isymbuf, sections))
   1677      1.1  christos 	goto error_return;
   1678      1.1  christos 
   1679      1.1  christos       if (sections != NULL)
   1680      1.1  christos 	free (sections);
   1681      1.1  christos       if (isymbuf != NULL
   1682      1.1  christos 	  && symtab_hdr->contents != (unsigned char *) isymbuf)
   1683      1.1  christos 	free (isymbuf);
   1684      1.1  christos       if (elf_section_data (input_section)->relocs != internal_relocs)
   1685      1.1  christos 	free (internal_relocs);
   1686      1.1  christos     }
   1687      1.1  christos 
   1688      1.1  christos   return data;
   1689      1.1  christos 
   1690      1.1  christos  error_return:
   1691      1.1  christos   if (sections != NULL)
   1692      1.1  christos     free (sections);
   1693      1.1  christos   if (isymbuf != NULL
   1694      1.1  christos       && symtab_hdr->contents != (unsigned char *) isymbuf)
   1695      1.1  christos     free (isymbuf);
   1696      1.1  christos   if (internal_relocs != NULL
   1697      1.1  christos       && elf_section_data (input_section)->relocs != internal_relocs)
   1698      1.1  christos     free (internal_relocs);
   1699      1.1  christos   return NULL;
   1700      1.1  christos }
   1701      1.1  christos 
   1702      1.3  christos 
   1703      1.1  christos #define TARGET_BIG_SYM			h8300_elf32_vec
   1704      1.1  christos #define TARGET_BIG_NAME			"elf32-h8300"
   1705      1.1  christos #define ELF_ARCH			bfd_arch_h8300
   1706      1.1  christos #define ELF_MACHINE_CODE		EM_H8_300
   1707      1.1  christos #define ELF_MAXPAGESIZE			0x1
   1708      1.1  christos #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
   1709      1.1  christos #define bfd_elf32_bfd_reloc_name_lookup elf32_h8_reloc_name_lookup
   1710      1.1  christos #define elf_info_to_howto		elf32_h8_info_to_howto
   1711      1.1  christos #define elf_info_to_howto_rel		elf32_h8_info_to_howto_rel
   1712      1.1  christos 
   1713      1.1  christos /* So we can set/examine bits in e_flags to get the specific
   1714      1.1  christos    H8 architecture in use.  */
   1715      1.1  christos #define elf_backend_final_write_processing \
   1716      1.1  christos   elf32_h8_final_write_processing
   1717      1.1  christos #define elf_backend_object_p \
   1718      1.1  christos   elf32_h8_object_p
   1719      1.1  christos #define bfd_elf32_bfd_merge_private_bfd_data \
   1720      1.1  christos   elf32_h8_merge_private_bfd_data
   1721      1.1  christos 
   1722      1.1  christos /* ??? when elf_backend_relocate_section is not defined, elf32-target.h
   1723      1.1  christos    defaults to using _bfd_generic_link_hash_table_create, but
   1724      1.1  christos    bfd_elf_size_dynamic_sections uses
   1725      1.1  christos    dynobj = elf_hash_table (info)->dynobj;
   1726      1.1  christos    and thus requires an elf hash table.  */
   1727      1.1  christos #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
   1728      1.1  christos 
   1729      1.1  christos /* Use an H8 specific linker, not the ELF generic linker.  */
   1730      1.1  christos #define elf_backend_relocate_section elf32_h8_relocate_section
   1731      1.1  christos #define elf_backend_rela_normal		1
   1732      1.1  christos #define elf_backend_can_gc_sections	1
   1733      1.1  christos 
   1734      1.1  christos /* And relaxing stuff.  */
   1735      1.1  christos #define bfd_elf32_bfd_relax_section     elf32_h8_relax_section
   1736      1.1  christos #define bfd_elf32_bfd_get_relocated_section_contents \
   1737      1.1  christos                                 elf32_h8_get_relocated_section_contents
   1738      1.1  christos 
   1739      1.1  christos #define elf_symbol_leading_char '_'
   1740      1.1  christos 
   1741      1.3  christos #include "elf32-target.h"
   1742      1.3  christos 
   1743      1.3  christos #undef  TARGET_BIG_SYM
   1744      1.3  christos #define TARGET_BIG_SYM			h8300_elf32_linux_vec
   1745      1.3  christos #undef  TARGET_BIG_NAME
   1746      1.3  christos #define TARGET_BIG_NAME			"elf32-h8300-linux"
   1747      1.3  christos #undef  elf_symbol_leading_char
   1748      1.3  christos #define elf32_bed			elf32_h8300_linux_bed
   1749      1.3  christos 
   1750                    #include "elf32-target.h"
   1751