* elfcode.h (elf_checksum_contents): Free contents.
[external/binutils.git] / bfd / elf32-m68hc1x.c
1 /* Motorola 68HC11/HC12-specific support for 32-bit ELF
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3    2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4    Contributed by Stephane Carrez (stcarrez@nerim.fr)
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 #include "sysdep.h"
24 #include "alloca-conf.h"
25 #include "bfd.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf32-m68hc1x.h"
30 #include "elf/m68hc11.h"
31 #include "opcode/m68hc11.h"
32
33
34 #define m68hc12_stub_hash_lookup(table, string, create, copy) \
35   ((struct elf32_m68hc11_stub_hash_entry *) \
36    bfd_hash_lookup ((table), (string), (create), (copy)))
37
38 static struct elf32_m68hc11_stub_hash_entry* m68hc12_add_stub
39   (const char *stub_name,
40    asection *section,
41    struct m68hc11_elf_link_hash_table *htab);
42
43 static struct bfd_hash_entry *stub_hash_newfunc
44   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
45
46 static void m68hc11_elf_set_symbol (bfd* abfd, struct bfd_link_info *info,
47                                     const char* name, bfd_vma value,
48                                     asection* sec);
49
50 static bfd_boolean m68hc11_elf_export_one_stub
51   (struct bfd_hash_entry *gen_entry, void *in_arg);
52
53 static void scan_sections_for_abi (bfd*, asection*, void *);
54
55 struct m68hc11_scan_param
56 {
57    struct m68hc11_page_info* pinfo;
58    bfd_boolean use_memory_banks;
59 };
60
61
62 /* Create a 68HC11/68HC12 ELF linker hash table.  */
63
64 struct m68hc11_elf_link_hash_table*
65 m68hc11_elf_hash_table_create (bfd *abfd)
66 {
67   struct m68hc11_elf_link_hash_table *ret;
68   bfd_size_type amt = sizeof (struct m68hc11_elf_link_hash_table);
69
70   ret = (struct m68hc11_elf_link_hash_table *) bfd_zmalloc (amt);
71   if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
72     return NULL;
73
74   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
75                                       _bfd_elf_link_hash_newfunc,
76                                       sizeof (struct elf_link_hash_entry),
77                                       M68HC11_ELF_DATA))
78     {
79       free (ret);
80       return NULL;
81     }
82
83   /* Init the stub hash table too.  */
84   amt = sizeof (struct bfd_hash_table);
85   ret->stub_hash_table = (struct bfd_hash_table*) bfd_malloc (amt);
86   if (ret->stub_hash_table == NULL)
87     {
88       free (ret);
89       return NULL;
90     }
91   if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
92                             sizeof (struct elf32_m68hc11_stub_hash_entry)))
93     return NULL;
94
95   return ret;
96 }
97
98 /* Free the derived linker hash table.  */
99
100 void
101 m68hc11_elf_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
102 {
103   struct m68hc11_elf_link_hash_table *ret
104     = (struct m68hc11_elf_link_hash_table *) hash;
105
106   bfd_hash_table_free (ret->stub_hash_table);
107   free (ret->stub_hash_table);
108   _bfd_elf_link_hash_table_free (hash);
109 }
110
111 /* Assorted hash table functions.  */
112
113 /* Initialize an entry in the stub hash table.  */
114
115 static struct bfd_hash_entry *
116 stub_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
117                    const char *string)
118 {
119   /* Allocate the structure if it has not already been allocated by a
120      subclass.  */
121   if (entry == NULL)
122     {
123       entry = bfd_hash_allocate (table,
124                                  sizeof (struct elf32_m68hc11_stub_hash_entry));
125       if (entry == NULL)
126         return entry;
127     }
128
129   /* Call the allocation method of the superclass.  */
130   entry = bfd_hash_newfunc (entry, table, string);
131   if (entry != NULL)
132     {
133       struct elf32_m68hc11_stub_hash_entry *eh;
134
135       /* Initialize the local fields.  */
136       eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
137       eh->stub_sec = NULL;
138       eh->stub_offset = 0;
139       eh->target_value = 0;
140       eh->target_section = NULL;
141     }
142
143   return entry;
144 }
145
146 /* Add a new stub entry to the stub hash.  Not all fields of the new
147    stub entry are initialised.  */
148
149 static struct elf32_m68hc11_stub_hash_entry *
150 m68hc12_add_stub (const char *stub_name, asection *section,
151                   struct m68hc11_elf_link_hash_table *htab)
152 {
153   struct elf32_m68hc11_stub_hash_entry *stub_entry;
154
155   /* Enter this entry into the linker stub hash table.  */
156   stub_entry = m68hc12_stub_hash_lookup (htab->stub_hash_table, stub_name,
157                                          TRUE, FALSE);
158   if (stub_entry == NULL)
159     {
160       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
161                              section->owner, stub_name);
162       return NULL;
163     }
164
165   if (htab->stub_section == 0)
166     {
167       htab->stub_section = (*htab->add_stub_section) (".tramp",
168                                                       htab->tramp_section);
169     }
170
171   stub_entry->stub_sec = htab->stub_section;
172   stub_entry->stub_offset = 0;
173   return stub_entry;
174 }
175
176 /* Hook called by the linker routine which adds symbols from an object
177    file.  We use it for identify far symbols and force a loading of
178    the trampoline handler.  */
179
180 bfd_boolean
181 elf32_m68hc11_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
182                                Elf_Internal_Sym *sym,
183                                const char **namep ATTRIBUTE_UNUSED,
184                                flagword *flagsp ATTRIBUTE_UNUSED,
185                                asection **secp ATTRIBUTE_UNUSED,
186                                bfd_vma *valp ATTRIBUTE_UNUSED)
187 {
188   if (sym->st_other & STO_M68HC12_FAR)
189     {
190       struct elf_link_hash_entry *h;
191
192       h = (struct elf_link_hash_entry *)
193         bfd_link_hash_lookup (info->hash, "__far_trampoline",
194                               FALSE, FALSE, FALSE);
195       if (h == NULL)
196         {
197           struct bfd_link_hash_entry* entry = NULL;
198
199           _bfd_generic_link_add_one_symbol (info, abfd,
200                                             "__far_trampoline",
201                                             BSF_GLOBAL,
202                                             bfd_und_section_ptr,
203                                             (bfd_vma) 0, (const char*) NULL,
204                                             FALSE, FALSE, &entry);
205         }
206
207     }
208   return TRUE;
209 }
210
211 /* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
212    STO_M68HC12_INTERRUPT.  */
213
214 void
215 elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry *h,
216                                       const Elf_Internal_Sym *isym,
217                                       bfd_boolean definition,
218                                       bfd_boolean dynamic ATTRIBUTE_UNUSED)
219 {
220   if (definition)
221     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
222                 | ELF_ST_VISIBILITY (h->other));
223 }
224
225 /* External entry points for sizing and building linker stubs.  */
226
227 /* Set up various things so that we can make a list of input sections
228    for each output section included in the link.  Returns -1 on error,
229    0 when no stubs will be needed, and 1 on success.  */
230
231 int
232 elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
233 {
234   bfd *input_bfd;
235   unsigned int bfd_count;
236   int top_id, top_index;
237   asection *section;
238   asection **input_list, **list;
239   bfd_size_type amt;
240   asection *text_section;
241   struct m68hc11_elf_link_hash_table *htab;
242
243   htab = m68hc11_elf_hash_table (info);
244   if (htab == NULL)
245     return -1;
246
247   if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
248     return 0;
249
250   /* Count the number of input BFDs and find the top input section id.
251      Also search for an existing ".tramp" section so that we know
252      where generated trampolines must go.  Default to ".text" if we
253      can't find it.  */
254   htab->tramp_section = 0;
255   text_section = 0;
256   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
257        input_bfd != NULL;
258        input_bfd = input_bfd->link_next)
259     {
260       bfd_count += 1;
261       for (section = input_bfd->sections;
262            section != NULL;
263            section = section->next)
264         {
265           const char* name = bfd_get_section_name (input_bfd, section);
266
267           if (!strcmp (name, ".tramp"))
268             htab->tramp_section = section;
269
270           if (!strcmp (name, ".text"))
271             text_section = section;
272
273           if (top_id < section->id)
274             top_id = section->id;
275         }
276     }
277   htab->bfd_count = bfd_count;
278   if (htab->tramp_section == 0)
279     htab->tramp_section = text_section;
280
281   /* We can't use output_bfd->section_count here to find the top output
282      section index as some sections may have been removed, and
283      strip_excluded_output_sections doesn't renumber the indices.  */
284   for (section = output_bfd->sections, top_index = 0;
285        section != NULL;
286        section = section->next)
287     {
288       if (top_index < section->index)
289         top_index = section->index;
290     }
291
292   htab->top_index = top_index;
293   amt = sizeof (asection *) * (top_index + 1);
294   input_list = (asection **) bfd_malloc (amt);
295   htab->input_list = input_list;
296   if (input_list == NULL)
297     return -1;
298
299   /* For sections we aren't interested in, mark their entries with a
300      value we can check later.  */
301   list = input_list + top_index;
302   do
303     *list = bfd_abs_section_ptr;
304   while (list-- != input_list);
305
306   for (section = output_bfd->sections;
307        section != NULL;
308        section = section->next)
309     {
310       if ((section->flags & SEC_CODE) != 0)
311         input_list[section->index] = NULL;
312     }
313
314   return 1;
315 }
316
317 /* Determine and set the size of the stub section for a final link.
318
319    The basic idea here is to examine all the relocations looking for
320    PC-relative calls to a target that is unreachable with a "bl"
321    instruction.  */
322
323 bfd_boolean
324 elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
325                           struct bfd_link_info *info,
326                           asection * (*add_stub_section) (const char*, asection*))
327 {
328   bfd *input_bfd;
329   asection *section;
330   Elf_Internal_Sym *local_syms, **all_local_syms;
331   unsigned int bfd_indx, bfd_count;
332   bfd_size_type amt;
333   asection *stub_sec;
334   struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
335
336   if (htab == NULL)
337     return FALSE;
338
339   /* Stash our params away.  */
340   htab->stub_bfd = stub_bfd;
341   htab->add_stub_section = add_stub_section;
342
343   /* Count the number of input BFDs and find the top input section id.  */
344   for (input_bfd = info->input_bfds, bfd_count = 0;
345        input_bfd != NULL;
346        input_bfd = input_bfd->link_next)
347     bfd_count += 1;
348
349   /* We want to read in symbol extension records only once.  To do this
350      we need to read in the local symbols in parallel and save them for
351      later use; so hold pointers to the local symbols in an array.  */
352   amt = sizeof (Elf_Internal_Sym *) * bfd_count;
353   all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
354   if (all_local_syms == NULL)
355     return FALSE;
356
357   /* Walk over all the input BFDs, swapping in local symbols.  */
358   for (input_bfd = info->input_bfds, bfd_indx = 0;
359        input_bfd != NULL;
360        input_bfd = input_bfd->link_next, bfd_indx++)
361     {
362       Elf_Internal_Shdr *symtab_hdr;
363
364       /* We'll need the symbol table in a second.  */
365       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
366       if (symtab_hdr->sh_info == 0)
367         continue;
368
369       /* We need an array of the local symbols attached to the input bfd.  */
370       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
371       if (local_syms == NULL)
372         {
373           local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
374                                              symtab_hdr->sh_info, 0,
375                                              NULL, NULL, NULL);
376           /* Cache them for elf_link_input_bfd.  */
377           symtab_hdr->contents = (unsigned char *) local_syms;
378         }
379       if (local_syms == NULL)
380         {
381           free (all_local_syms);
382           return FALSE;
383         }
384
385       all_local_syms[bfd_indx] = local_syms;
386     }
387
388   for (input_bfd = info->input_bfds, bfd_indx = 0;
389        input_bfd != NULL;
390        input_bfd = input_bfd->link_next, bfd_indx++)
391     {
392       Elf_Internal_Shdr *symtab_hdr;
393       struct elf_link_hash_entry ** sym_hashes;
394
395       sym_hashes = elf_sym_hashes (input_bfd);
396
397       /* We'll need the symbol table in a second.  */
398       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
399       if (symtab_hdr->sh_info == 0)
400         continue;
401
402       local_syms = all_local_syms[bfd_indx];
403
404       /* Walk over each section attached to the input bfd.  */
405       for (section = input_bfd->sections;
406            section != NULL;
407            section = section->next)
408         {
409           Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
410
411           /* If there aren't any relocs, then there's nothing more
412              to do.  */
413           if ((section->flags & SEC_RELOC) == 0
414               || section->reloc_count == 0)
415             continue;
416
417           /* If this section is a link-once section that will be
418              discarded, then don't create any stubs.  */
419           if (section->output_section == NULL
420               || section->output_section->owner != output_bfd)
421             continue;
422
423           /* Get the relocs.  */
424           internal_relocs
425             = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
426                                          (Elf_Internal_Rela *) NULL,
427                                          info->keep_memory);
428           if (internal_relocs == NULL)
429             goto error_ret_free_local;
430
431           /* Now examine each relocation.  */
432           irela = internal_relocs;
433           irelaend = irela + section->reloc_count;
434           for (; irela < irelaend; irela++)
435             {
436               unsigned int r_type, r_indx;
437               struct elf32_m68hc11_stub_hash_entry *stub_entry;
438               asection *sym_sec;
439               bfd_vma sym_value;
440               struct elf_link_hash_entry *hash;
441               const char *stub_name;
442               Elf_Internal_Sym *sym;
443
444               r_type = ELF32_R_TYPE (irela->r_info);
445
446               /* Only look at 16-bit relocs.  */
447               if (r_type != (unsigned int) R_M68HC11_16)
448                 continue;
449
450               /* Now determine the call target, its name, value,
451                  section.  */
452               r_indx = ELF32_R_SYM (irela->r_info);
453               if (r_indx < symtab_hdr->sh_info)
454                 {
455                   /* It's a local symbol.  */
456                   Elf_Internal_Shdr *hdr;
457                   bfd_boolean is_far;
458
459                   sym = local_syms + r_indx;
460                   is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
461                   if (!is_far)
462                     continue;
463
464                   if (sym->st_shndx >= elf_numsections (input_bfd))
465                     sym_sec = NULL;
466                   else
467                     {
468                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
469                       sym_sec = hdr->bfd_section;
470                     }
471                   stub_name = (bfd_elf_string_from_elf_section
472                                (input_bfd, symtab_hdr->sh_link,
473                                 sym->st_name));
474                   sym_value = sym->st_value;
475                   hash = NULL;
476                 }
477               else
478                 {
479                   /* It's an external symbol.  */
480                   int e_indx;
481
482                   e_indx = r_indx - symtab_hdr->sh_info;
483                   hash = (struct elf_link_hash_entry *)
484                     (sym_hashes[e_indx]);
485
486                   while (hash->root.type == bfd_link_hash_indirect
487                          || hash->root.type == bfd_link_hash_warning)
488                     hash = ((struct elf_link_hash_entry *)
489                             hash->root.u.i.link);
490
491                   if (hash->root.type == bfd_link_hash_defined
492                       || hash->root.type == bfd_link_hash_defweak
493                       || hash->root.type == bfd_link_hash_new)
494                     {
495                       if (!(hash->other & STO_M68HC12_FAR))
496                         continue;
497                     }
498                   else if (hash->root.type == bfd_link_hash_undefweak)
499                     {
500                       continue;
501                     }
502                   else if (hash->root.type == bfd_link_hash_undefined)
503                     {
504                       continue;
505                     }
506                   else
507                     {
508                       bfd_set_error (bfd_error_bad_value);
509                       goto error_ret_free_internal;
510                     }
511                   sym_sec = hash->root.u.def.section;
512                   sym_value = hash->root.u.def.value;
513                   stub_name = hash->root.root.string;
514                 }
515
516               if (!stub_name)
517                 goto error_ret_free_internal;
518
519               stub_entry = m68hc12_stub_hash_lookup
520                 (htab->stub_hash_table,
521                  stub_name,
522                  FALSE, FALSE);
523               if (stub_entry == NULL)
524                 {
525                   if (add_stub_section == 0)
526                     continue;
527
528                   stub_entry = m68hc12_add_stub (stub_name, section, htab);
529                   if (stub_entry == NULL)
530                     {
531                     error_ret_free_internal:
532                       if (elf_section_data (section)->relocs == NULL)
533                         free (internal_relocs);
534                       goto error_ret_free_local;
535                     }
536                 }
537
538               stub_entry->target_value = sym_value;
539               stub_entry->target_section = sym_sec;
540             }
541
542           /* We're done with the internal relocs, free them.  */
543           if (elf_section_data (section)->relocs == NULL)
544             free (internal_relocs);
545         }
546     }
547
548   if (add_stub_section)
549     {
550       /* OK, we've added some stubs.  Find out the new size of the
551          stub sections.  */
552       for (stub_sec = htab->stub_bfd->sections;
553            stub_sec != NULL;
554            stub_sec = stub_sec->next)
555         {
556           stub_sec->size = 0;
557         }
558
559       bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
560     }
561   free (all_local_syms);
562   return TRUE;
563
564  error_ret_free_local:
565   free (all_local_syms);
566   return FALSE;
567 }
568
569 /* Export the trampoline addresses in the symbol table.  */
570 static bfd_boolean
571 m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
572 {
573   struct bfd_link_info *info;
574   struct m68hc11_elf_link_hash_table *htab;
575   struct elf32_m68hc11_stub_hash_entry *stub_entry;
576   char* name;
577   bfd_boolean result;
578
579   info = (struct bfd_link_info *) in_arg;
580   htab = m68hc11_elf_hash_table (info);
581   if (htab == NULL)
582     return FALSE;
583
584   /* Massage our args to the form they really have.  */
585   stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
586
587   /* Generate the trampoline according to HC11 or HC12.  */
588   result = (* htab->build_one_stub) (gen_entry, in_arg);
589
590   /* Make a printable name that does not conflict with the real function.  */
591   name = alloca (strlen (stub_entry->root.string) + 16);
592   sprintf (name, "tramp.%s", stub_entry->root.string);
593
594   /* Export the symbol for debugging/disassembling.  */
595   m68hc11_elf_set_symbol (htab->stub_bfd, info, name,
596                           stub_entry->stub_offset,
597                           stub_entry->stub_sec);
598   return result;
599 }
600
601 /* Export a symbol or set its value and section.  */
602 static void
603 m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
604                         const char *name, bfd_vma value, asection *sec)
605 {
606   struct elf_link_hash_entry *h;
607
608   h = (struct elf_link_hash_entry *)
609     bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
610   if (h == NULL)
611     {
612       _bfd_generic_link_add_one_symbol (info, abfd,
613                                         name,
614                                         BSF_GLOBAL,
615                                         sec,
616                                         value,
617                                         (const char*) NULL,
618                                         TRUE, FALSE, NULL);
619     }
620   else
621     {
622       h->root.type = bfd_link_hash_defined;
623       h->root.u.def.value = value;
624       h->root.u.def.section = sec;
625     }
626 }
627
628
629 /* Build all the stubs associated with the current output file.  The
630    stubs are kept in a hash table attached to the main linker hash
631    table.  This function is called via m68hc12elf_finish in the
632    linker.  */
633
634 bfd_boolean
635 elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
636 {
637   asection *stub_sec;
638   struct bfd_hash_table *table;
639   struct m68hc11_elf_link_hash_table *htab;
640   struct m68hc11_scan_param param;
641
642   m68hc11_elf_get_bank_parameters (info);
643   htab = m68hc11_elf_hash_table (info);
644   if (htab == NULL)
645     return FALSE;
646
647   for (stub_sec = htab->stub_bfd->sections;
648        stub_sec != NULL;
649        stub_sec = stub_sec->next)
650     {
651       bfd_size_type size;
652
653       /* Allocate memory to hold the linker stubs.  */
654       size = stub_sec->size;
655       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
656       if (stub_sec->contents == NULL && size != 0)
657         return FALSE;
658       stub_sec->size = 0;
659     }
660
661   /* Build the stubs as directed by the stub hash table.  */
662   table = htab->stub_hash_table;
663   bfd_hash_traverse (table, m68hc11_elf_export_one_stub, info);
664
665   /* Scan the output sections to see if we use the memory banks.
666      If so, export the symbols that define how the memory banks
667      are mapped.  This is used by gdb and the simulator to obtain
668      the information.  It can be used by programs to burn the eprom
669      at the good addresses.  */
670   param.use_memory_banks = FALSE;
671   param.pinfo = &htab->pinfo;
672   bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
673   if (param.use_memory_banks)
674     {
675       m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_START_NAME,
676                               htab->pinfo.bank_physical,
677                               bfd_abs_section_ptr);
678       m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_VIRTUAL_NAME,
679                               htab->pinfo.bank_virtual,
680                               bfd_abs_section_ptr);
681       m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_SIZE_NAME,
682                               htab->pinfo.bank_size,
683                               bfd_abs_section_ptr);
684     }
685
686   return TRUE;
687 }
688
689 void
690 m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
691 {
692   unsigned i;
693   struct m68hc11_page_info *pinfo;
694   struct bfd_link_hash_entry *h;
695   struct m68hc11_elf_link_hash_table *htab;
696
697   htab = m68hc11_elf_hash_table (info);
698   if (htab == NULL)
699     return;
700
701   pinfo = & htab->pinfo;
702   if (pinfo->bank_param_initialized)
703     return;
704
705   pinfo->bank_virtual = M68HC12_BANK_VIRT;
706   pinfo->bank_mask = M68HC12_BANK_MASK;
707   pinfo->bank_physical = M68HC12_BANK_BASE;
708   pinfo->bank_shift = M68HC12_BANK_SHIFT;
709   pinfo->bank_size = 1 << M68HC12_BANK_SHIFT;
710
711   h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
712                             FALSE, FALSE, TRUE);
713   if (h != (struct bfd_link_hash_entry*) NULL
714       && h->type == bfd_link_hash_defined)
715     pinfo->bank_physical = (h->u.def.value
716                             + h->u.def.section->output_section->vma
717                             + h->u.def.section->output_offset);
718
719   h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
720                             FALSE, FALSE, TRUE);
721   if (h != (struct bfd_link_hash_entry*) NULL
722       && h->type == bfd_link_hash_defined)
723     pinfo->bank_virtual = (h->u.def.value
724                            + h->u.def.section->output_section->vma
725                            + h->u.def.section->output_offset);
726
727   h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_SIZE_NAME,
728                             FALSE, FALSE, TRUE);
729   if (h != (struct bfd_link_hash_entry*) NULL
730       && h->type == bfd_link_hash_defined)
731     pinfo->bank_size = (h->u.def.value
732                         + h->u.def.section->output_section->vma
733                         + h->u.def.section->output_offset);
734
735   pinfo->bank_shift = 0;
736   for (i = pinfo->bank_size; i != 0; i >>= 1)
737     pinfo->bank_shift++;
738   pinfo->bank_shift--;
739   pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
740   pinfo->bank_physical_end = pinfo->bank_physical + pinfo->bank_size;
741   pinfo->bank_param_initialized = 1;
742
743   h = bfd_link_hash_lookup (info->hash, "__far_trampoline", FALSE,
744                             FALSE, TRUE);
745   if (h != (struct bfd_link_hash_entry*) NULL
746       && h->type == bfd_link_hash_defined)
747     pinfo->trampoline_addr = (h->u.def.value
748                               + h->u.def.section->output_section->vma
749                               + h->u.def.section->output_offset);
750 }
751
752 /* Return 1 if the address is in banked memory.
753    This can be applied to a virtual address and to a physical address.  */
754 int
755 m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
756 {
757   if (addr >= pinfo->bank_virtual)
758     return 1;
759
760   if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
761     return 1;
762
763   return 0;
764 }
765
766 /* Return the physical address seen by the processor, taking
767    into account banked memory.  */
768 bfd_vma
769 m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
770 {
771   if (addr < pinfo->bank_virtual)
772     return addr;
773
774   /* Map the address to the memory bank.  */
775   addr -= pinfo->bank_virtual;
776   addr &= pinfo->bank_mask;
777   addr += pinfo->bank_physical;
778   return addr;
779 }
780
781 /* Return the page number corresponding to an address in banked memory.  */
782 bfd_vma
783 m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
784 {
785   if (addr < pinfo->bank_virtual)
786     return 0;
787
788   /* Map the address to the memory bank.  */
789   addr -= pinfo->bank_virtual;
790   addr >>= pinfo->bank_shift;
791   addr &= 0x0ff;
792   return addr;
793 }
794
795 /* This function is used for relocs which are only used for relaxing,
796    which the linker should otherwise ignore.  */
797
798 bfd_reloc_status_type
799 m68hc11_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
800                           arelent *reloc_entry,
801                           asymbol *symbol ATTRIBUTE_UNUSED,
802                           void *data ATTRIBUTE_UNUSED,
803                           asection *input_section,
804                           bfd *output_bfd,
805                           char **error_message ATTRIBUTE_UNUSED)
806 {
807   if (output_bfd != NULL)
808     reloc_entry->address += input_section->output_offset;
809   return bfd_reloc_ok;
810 }
811
812 bfd_reloc_status_type
813 m68hc11_elf_special_reloc (bfd *abfd ATTRIBUTE_UNUSED,
814                            arelent *reloc_entry,
815                            asymbol *symbol,
816                            void *data ATTRIBUTE_UNUSED,
817                            asection *input_section,
818                            bfd *output_bfd,
819                            char **error_message ATTRIBUTE_UNUSED)
820 {
821   if (output_bfd != (bfd *) NULL
822       && (symbol->flags & BSF_SECTION_SYM) == 0
823       && (! reloc_entry->howto->partial_inplace
824           || reloc_entry->addend == 0))
825     {
826       reloc_entry->address += input_section->output_offset;
827       return bfd_reloc_ok;
828     }
829
830   if (output_bfd != NULL)
831     return bfd_reloc_continue;
832
833   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
834     return bfd_reloc_outofrange;
835
836   abort();
837 }
838
839 /* Look through the relocs for a section during the first phase.
840    Since we don't do .gots or .plts, we just need to consider the
841    virtual table relocs for gc.  */
842
843 bfd_boolean
844 elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
845                             asection *sec, const Elf_Internal_Rela *relocs)
846 {
847   Elf_Internal_Shdr *           symtab_hdr;
848   struct elf_link_hash_entry ** sym_hashes;
849   const Elf_Internal_Rela *     rel;
850   const Elf_Internal_Rela *     rel_end;
851
852   if (info->relocatable)
853     return TRUE;
854
855   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
856   sym_hashes = elf_sym_hashes (abfd);
857   rel_end = relocs + sec->reloc_count;
858
859   for (rel = relocs; rel < rel_end; rel++)
860     {
861       struct elf_link_hash_entry * h;
862       unsigned long r_symndx;
863
864       r_symndx = ELF32_R_SYM (rel->r_info);
865
866       if (r_symndx < symtab_hdr->sh_info)
867         h = NULL;
868       else
869         {
870           h = sym_hashes [r_symndx - symtab_hdr->sh_info];
871           while (h->root.type == bfd_link_hash_indirect
872                  || h->root.type == bfd_link_hash_warning)
873             h = (struct elf_link_hash_entry *) h->root.u.i.link;
874         }
875
876       switch (ELF32_R_TYPE (rel->r_info))
877         {
878         /* This relocation describes the C++ object vtable hierarchy.
879            Reconstruct it for later use during GC.  */
880         case R_M68HC11_GNU_VTINHERIT:
881           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
882             return FALSE;
883           break;
884
885         /* This relocation describes which C++ vtable entries are actually
886            used.  Record for later use during GC.  */
887         case R_M68HC11_GNU_VTENTRY:
888           BFD_ASSERT (h != NULL);
889           if (h != NULL
890               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
891             return FALSE;
892           break;
893         }
894     }
895
896   return TRUE;
897 }
898
899 /* Relocate a 68hc11/68hc12 ELF section.  */
900 bfd_boolean
901 elf32_m68hc11_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
902                                 struct bfd_link_info *info,
903                                 bfd *input_bfd, asection *input_section,
904                                 bfd_byte *contents, Elf_Internal_Rela *relocs,
905                                 Elf_Internal_Sym *local_syms,
906                                 asection **local_sections)
907 {
908   Elf_Internal_Shdr *symtab_hdr;
909   struct elf_link_hash_entry **sym_hashes;
910   Elf_Internal_Rela *rel, *relend;
911   const char *name = NULL;
912   struct m68hc11_page_info *pinfo;
913   const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
914   struct m68hc11_elf_link_hash_table *htab;
915   unsigned long e_flags;
916
917   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
918   sym_hashes = elf_sym_hashes (input_bfd);
919   e_flags = elf_elfheader (input_bfd)->e_flags;
920
921   htab = m68hc11_elf_hash_table (info);
922   if (htab == NULL)
923     return FALSE;
924
925   /* Get memory bank parameters.  */
926   m68hc11_elf_get_bank_parameters (info);
927
928   pinfo = & htab->pinfo;
929   rel = relocs;
930   relend = relocs + input_section->reloc_count;
931
932   for (; rel < relend; rel++)
933     {
934       int r_type;
935       arelent arel;
936       reloc_howto_type *howto;
937       unsigned long r_symndx;
938       Elf_Internal_Sym *sym;
939       asection *sec;
940       bfd_vma relocation = 0;
941       bfd_reloc_status_type r = bfd_reloc_undefined;
942       bfd_vma phys_page;
943       bfd_vma phys_addr;
944       bfd_vma insn_addr;
945       bfd_vma insn_page;
946       bfd_boolean is_far = FALSE;
947       bfd_boolean is_xgate_symbol = FALSE;
948       bfd_boolean is_section_symbol = FALSE;
949       struct elf_link_hash_entry *h;
950       bfd_vma val;
951
952       r_symndx = ELF32_R_SYM (rel->r_info);
953       r_type = ELF32_R_TYPE (rel->r_info);
954
955       if (r_type == R_M68HC11_GNU_VTENTRY
956           || r_type == R_M68HC11_GNU_VTINHERIT)
957         continue;
958
959       (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
960       howto = arel.howto;
961
962       h = NULL;
963       sym = NULL;
964       sec = NULL;
965       if (r_symndx < symtab_hdr->sh_info)
966         {
967           sym = local_syms + r_symndx;
968           sec = local_sections[r_symndx];
969           relocation = (sec->output_section->vma
970                         + sec->output_offset
971                         + sym->st_value);
972           is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
973           is_xgate_symbol = (sym && (sym->st_target_internal));
974           is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
975         }
976       else
977         {
978           bfd_boolean unresolved_reloc, warned;
979
980           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
981                                    r_symndx, symtab_hdr, sym_hashes,
982                                    h, sec, relocation, unresolved_reloc,
983                                    warned);
984
985           is_far = (h && (h->other & STO_M68HC12_FAR));
986           is_xgate_symbol = (h && (h->target_internal));
987         }
988
989       if (sec != NULL && discarded_section (sec))
990         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
991                                          rel, 1, relend, howto, 0, contents);
992
993       if (info->relocatable)
994         {
995           /* This is a relocatable link.  We don't have to change
996              anything, unless the reloc is against a section symbol,
997              in which case we have to adjust according to where the
998              section symbol winds up in the output section.  */
999           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1000             rel->r_addend += sec->output_offset;
1001           continue;
1002         }
1003
1004       if (h != NULL)
1005         name = h->root.root.string;
1006       else
1007         {
1008           name = (bfd_elf_string_from_elf_section
1009                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
1010           if (name == NULL || *name == '\0')
1011             name = bfd_section_name (input_bfd, sec);
1012         }
1013
1014       if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1015         {
1016           struct elf32_m68hc11_stub_hash_entry* stub;
1017
1018           stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1019                                            name, FALSE, FALSE);
1020           if (stub)
1021             {
1022               relocation = stub->stub_offset
1023                 + stub->stub_sec->output_section->vma
1024                 + stub->stub_sec->output_offset;
1025               is_far = FALSE;
1026             }
1027         }
1028
1029       /* Do the memory bank mapping.  */
1030       phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1031       phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1032       switch (r_type)
1033         {
1034         case R_M68HC12_LO8XG:
1035           /* This relocation is specific to XGATE IMM16 calls and will precede
1036              a HI8. tc-m68hc11 only generates them in pairs.
1037              Leave the relocation to the HI8XG step.  */
1038           r = bfd_reloc_ok;
1039           r_type = R_M68HC11_NONE;
1040           break;
1041
1042         case R_M68HC12_HI8XG:
1043           /* This relocation is specific to XGATE IMM16 calls and must follow
1044              a LO8XG. Does not actually check that it was a LO8XG.
1045              Adjusts high and low bytes.  */
1046           relocation = phys_addr;
1047           if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
1048               && (relocation >= 0x2000))
1049             relocation += 0xc000; /* HARDCODED RAM offset for XGATE.  */
1050
1051           /* Fetch 16 bit value including low byte in previous insn.  */
1052           val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
1053             | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
1054
1055           /* Add on value to preserve carry, then write zero to high byte.  */
1056           relocation += val;
1057
1058           /* Write out top byte.  */
1059           bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1060                      (bfd_byte*) contents + rel->r_offset);
1061
1062           /* Write out low byte to previous instruction.  */
1063           bfd_put_8 (input_bfd, relocation & 0xff,
1064                      (bfd_byte*) contents + rel->r_offset - 2);
1065
1066           /* Mark as relocation completed.  */
1067           r = bfd_reloc_ok;
1068           r_type = R_M68HC11_NONE;
1069           break;
1070
1071         /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1072            assembler directives. %hi does not support carry.  */
1073         case R_M68HC11_HI8:
1074         case R_M68HC11_LO8:
1075           relocation = phys_addr;
1076           break;
1077
1078         case R_M68HC11_24:
1079           /* Reloc used by 68HC12 call instruction.  */
1080           bfd_put_16 (input_bfd, phys_addr,
1081                       (bfd_byte*) contents + rel->r_offset);
1082           bfd_put_8 (input_bfd, phys_page,
1083                      (bfd_byte*) contents + rel->r_offset + 2);
1084           r = bfd_reloc_ok;
1085           r_type = R_M68HC11_NONE;
1086           break;
1087
1088         case R_M68HC11_NONE:
1089           r = bfd_reloc_ok;
1090           break;
1091
1092         case R_M68HC11_LO16:
1093           /* Reloc generated by %addr(expr) gas to obtain the
1094              address as mapped in the memory bank window.  */
1095           relocation = phys_addr;
1096           break;
1097
1098         case R_M68HC11_PAGE:
1099           /* Reloc generated by %page(expr) gas to obtain the
1100              page number associated with the address.  */
1101           relocation = phys_page;
1102           break;
1103
1104         case R_M68HC11_16:
1105           /* Get virtual address of instruction having the relocation.  */
1106           if (is_far)
1107             {
1108               const char* msg;
1109               char* buf;
1110               msg = _("Reference to the far symbol `%s' using a wrong "
1111                       "relocation may result in incorrect execution");
1112               buf = alloca (strlen (msg) + strlen (name) + 10);
1113               sprintf (buf, msg, name);
1114
1115               (* info->callbacks->warning)
1116                 (info, buf, name, input_bfd, NULL, rel->r_offset);
1117             }
1118
1119           /* Get virtual address of instruction having the relocation.  */
1120           insn_addr = input_section->output_section->vma
1121             + input_section->output_offset
1122             + rel->r_offset;
1123
1124           insn_page = m68hc11_phys_page (pinfo, insn_addr);
1125
1126          /* If we are linking an S12 instruction against an XGATE symbol, we
1127             need to change the offset of the symbol value so that it's correct
1128             from the S12's perspective.  */
1129           if (is_xgate_symbol)
1130             {
1131               /* The ram in the global space is mapped to 0x2000 in the 16-bit
1132                  address space for S12 and 0xE000 in the 16-bit address space
1133                  for XGATE.  */
1134               if (relocation >= 0xE000)
1135                 {
1136                   /* We offset the address by the difference
1137                      between these two mappings.  */
1138                   relocation -= 0xC000;
1139                   break;
1140                 }
1141               else
1142                 {
1143                   const char * msg;
1144                   char * buf;
1145
1146                   msg = _("XGATE address (%lx) is not within shared RAM"
1147                           "(0xE000-0xFFFF), therefore you must manually offset "
1148                           "the address, and possibly manage the page, in your "
1149                           "code.");
1150                   buf = alloca (strlen (msg) + 128);
1151                   sprintf (buf, msg, phys_addr);
1152                   if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1153                                                      input_section, insn_addr)))
1154                     return FALSE;
1155                   break;
1156                 }
1157             }
1158
1159           if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1160               && m68hc11_addr_is_banked (pinfo, insn_addr)
1161               && phys_page != insn_page && !(e_flags & E_M68HC11_NO_BANK_WARNING))
1162             {
1163               const char * msg;
1164               char * buf;
1165
1166               msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1167                       "as current banked address [%lx:%04lx] (%lx)");
1168
1169               buf = alloca (strlen (msg) + 128);
1170               sprintf (buf, msg, phys_page, phys_addr,
1171                        (long) (relocation + rel->r_addend),
1172                        insn_page, m68hc11_phys_addr (pinfo, insn_addr),
1173                        (long) (insn_addr));
1174               if (!((*info->callbacks->warning)
1175                     (info, buf, name, input_bfd, input_section,
1176                      rel->r_offset)))
1177                 return FALSE;
1178               break;
1179             }
1180
1181           if (phys_page != 0 && insn_page == 0)
1182             {
1183               const char * msg;
1184               char * buf;
1185
1186               msg = _("reference to a banked address [%lx:%04lx] in the "
1187                       "normal address space at %04lx");
1188
1189               buf = alloca (strlen (msg) + 128);
1190               sprintf (buf, msg, phys_page, phys_addr, insn_addr);
1191               if (!((*info->callbacks->warning)
1192                     (info, buf, name, input_bfd, input_section,
1193                      insn_addr)))
1194                 return FALSE;
1195
1196               relocation = phys_addr;
1197               break;
1198             }
1199
1200           /* If this is a banked address use the phys_addr so that
1201              we stay in the banked window.  */
1202           if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1203             relocation = phys_addr;
1204           break;
1205         }
1206
1207       /* If we are linking an XGATE instruction against an S12 symbol, we
1208          need to change the offset of the symbol value so that it's correct
1209          from the XGATE's perspective.  */
1210       if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
1211           || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
1212         {
1213           /* We can only offset S12 addresses that lie within the non-paged
1214              area of RAM.  */
1215           if (!is_xgate_symbol && !is_section_symbol)
1216             {
1217               /* The ram in the global space is mapped to 0x2000 and stops at
1218                  0x4000 in the 16-bit address space for S12 and 0xE000 in the
1219                  16-bit address space for XGATE.  */
1220               if (relocation >= 0x2000 && relocation < 0x4000)
1221                  /* We offset the address by the difference
1222                    between these two mappings.  */
1223                 relocation += 0xC000;
1224               else
1225                 {
1226                   const char * msg;
1227                   char * buf;
1228
1229                   /* Get virtual address of instruction having the relocation.  */
1230                   insn_addr = input_section->output_section->vma
1231                       + input_section->output_offset + rel->r_offset;
1232
1233                   msg = _("S12 address (%lx) is not within shared RAM"
1234                       "(0x2000-0x4000), therefore you must manually "
1235                       "offset the address in your code");
1236                   buf = alloca (strlen (msg) + 128);
1237                   sprintf (buf, msg, phys_addr);
1238                   if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1239                                                      input_section, insn_addr)))
1240                     return FALSE;
1241                   break;
1242                 }
1243             }
1244         }
1245
1246       if (r_type != R_M68HC11_NONE)
1247         {
1248           if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
1249             r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1250                                       contents, rel->r_offset,
1251                                       relocation - 2, rel->r_addend);
1252           else
1253             r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1254                                           contents, rel->r_offset,
1255                                           relocation, rel->r_addend);
1256         }
1257
1258       if (r != bfd_reloc_ok)
1259         {
1260           const char * msg = (const char *) 0;
1261
1262           switch (r)
1263             {
1264             case bfd_reloc_overflow:
1265               if (!((*info->callbacks->reloc_overflow)
1266                     (info, NULL, name, howto->name, (bfd_vma) 0,
1267                      input_bfd, input_section, rel->r_offset)))
1268                 return FALSE;
1269               break;
1270
1271             case bfd_reloc_undefined:
1272               if (!((*info->callbacks->undefined_symbol)
1273                     (info, name, input_bfd, input_section,
1274                      rel->r_offset, TRUE)))
1275                 return FALSE;
1276               break;
1277
1278             case bfd_reloc_outofrange:
1279               msg = _ ("internal error: out of range error");
1280               goto common_error;
1281
1282             case bfd_reloc_notsupported:
1283               msg = _ ("internal error: unsupported relocation error");
1284               goto common_error;
1285
1286             case bfd_reloc_dangerous:
1287               msg = _ ("internal error: dangerous error");
1288               goto common_error;
1289
1290             default:
1291               msg = _ ("internal error: unknown error");
1292               /* fall through */
1293
1294             common_error:
1295               if (!((*info->callbacks->warning)
1296                     (info, msg, name, input_bfd, input_section,
1297                      rel->r_offset)))
1298                 return FALSE;
1299               break;
1300             }
1301         }
1302     }
1303
1304   return TRUE;
1305 }
1306
1307
1308 \f
1309 /* Set and control ELF flags in ELF header.  */
1310
1311 bfd_boolean
1312 _bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
1313 {
1314   BFD_ASSERT (!elf_flags_init (abfd)
1315               || elf_elfheader (abfd)->e_flags == flags);
1316
1317   elf_elfheader (abfd)->e_flags = flags;
1318   elf_flags_init (abfd) = TRUE;
1319   return TRUE;
1320 }
1321
1322 /* Merge backend specific data from an object file to the output
1323    object file when linking.  */
1324
1325 bfd_boolean
1326 _bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1327 {
1328   flagword old_flags;
1329   flagword new_flags;
1330   bfd_boolean ok = TRUE;
1331
1332   /* Check if we have the same endianness */
1333   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
1334     return FALSE;
1335
1336   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1337       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1338     return TRUE;
1339
1340   new_flags = elf_elfheader (ibfd)->e_flags;
1341   elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1342   old_flags = elf_elfheader (obfd)->e_flags;
1343
1344   if (! elf_flags_init (obfd))
1345     {
1346       elf_flags_init (obfd) = TRUE;
1347       elf_elfheader (obfd)->e_flags = new_flags;
1348       elf_elfheader (obfd)->e_ident[EI_CLASS]
1349         = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1350
1351       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1352           && bfd_get_arch_info (obfd)->the_default)
1353         {
1354           if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1355                                    bfd_get_mach (ibfd)))
1356             return FALSE;
1357         }
1358
1359       return TRUE;
1360     }
1361
1362   /* Check ABI compatibility.  */
1363   if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1364     {
1365       (*_bfd_error_handler)
1366         (_("%B: linking files compiled for 16-bit integers (-mshort) "
1367            "and others for 32-bit integers"), ibfd);
1368       ok = FALSE;
1369     }
1370   if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1371     {
1372       (*_bfd_error_handler)
1373         (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1374            "and others for 64-bit double"), ibfd);
1375       ok = FALSE;
1376     }
1377
1378   /* Processor compatibility.  */
1379   if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1380     {
1381       (*_bfd_error_handler)
1382         (_("%B: linking files compiled for HCS12 with "
1383            "others compiled for HC12"), ibfd);
1384       ok = FALSE;
1385     }
1386   new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1387                | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1388
1389   elf_elfheader (obfd)->e_flags = new_flags;
1390
1391   new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1392   old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1393
1394   /* Warn about any other mismatches */
1395   if (new_flags != old_flags)
1396     {
1397       (*_bfd_error_handler)
1398         (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1399          ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
1400       ok = FALSE;
1401     }
1402
1403   if (! ok)
1404     {
1405       bfd_set_error (bfd_error_bad_value);
1406       return FALSE;
1407     }
1408
1409   return TRUE;
1410 }
1411
1412 bfd_boolean
1413 _bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
1414 {
1415   FILE *file = (FILE *) ptr;
1416
1417   BFD_ASSERT (abfd != NULL && ptr != NULL);
1418
1419   /* Print normal ELF private data.  */
1420   _bfd_elf_print_private_bfd_data (abfd, ptr);
1421
1422   /* xgettext:c-format */
1423   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1424
1425   if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1426     fprintf (file, _("[abi=32-bit int, "));
1427   else
1428     fprintf (file, _("[abi=16-bit int, "));
1429
1430   if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1431     fprintf (file, _("64-bit double, "));
1432   else
1433     fprintf (file, _("32-bit double, "));
1434
1435   if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1436     fprintf (file, _("cpu=HC11]"));
1437   else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1438     fprintf (file, _("cpu=HCS12]"));
1439   else
1440     fprintf (file, _("cpu=HC12]"));
1441
1442   if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1443     fprintf (file, _(" [memory=bank-model]"));
1444   else
1445     fprintf (file, _(" [memory=flat]"));
1446
1447   if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1448     fprintf (file, _(" [XGATE RAM offsetting]"));
1449
1450   fputc ('\n', file);
1451
1452   return TRUE;
1453 }
1454
1455 static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1456                                    asection *asect, void *arg)
1457 {
1458   struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1459
1460   if (asect->vma >= p->pinfo->bank_virtual)
1461     p->use_memory_banks = TRUE;
1462 }
1463
1464 /* Tweak the OSABI field of the elf header.  */
1465
1466 void
1467 elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
1468 {
1469   struct m68hc11_scan_param param;
1470   struct m68hc11_elf_link_hash_table *htab;
1471
1472   if (link_info == NULL)
1473     return;
1474
1475   htab = m68hc11_elf_hash_table (link_info);
1476   if (htab == NULL)
1477     return;
1478
1479   m68hc11_elf_get_bank_parameters (link_info);
1480
1481   param.use_memory_banks = FALSE;
1482   param.pinfo = & htab->pinfo;
1483
1484   bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
1485
1486   if (param.use_memory_banks)
1487     {
1488       Elf_Internal_Ehdr * i_ehdrp;
1489
1490       i_ehdrp = elf_elfheader (abfd);
1491       i_ehdrp->e_flags |= E_M68HC12_BANKS;
1492     }
1493 }