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