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