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