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