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