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