Home | History | Annotate | Line # | Download | only in bfd
elf32-m68hc1x.c revision 1.1.1.1
      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 (at) 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 	  /* PR15323, ref flags aren't set for references in the same
    876 	     object.  */
    877 	  h->root.non_ir_ref = 1;
    878 	}
    879 
    880       switch (ELF32_R_TYPE (rel->r_info))
    881         {
    882         /* This relocation describes the C++ object vtable hierarchy.
    883            Reconstruct it for later use during GC.  */
    884         case R_M68HC11_GNU_VTINHERIT:
    885           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
    886             return FALSE;
    887           break;
    888 
    889         /* This relocation describes which C++ vtable entries are actually
    890            used.  Record for later use during GC.  */
    891         case R_M68HC11_GNU_VTENTRY:
    892           BFD_ASSERT (h != NULL);
    893           if (h != NULL
    894               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
    895             return FALSE;
    896           break;
    897         }
    898     }
    899 
    900   return TRUE;
    901 }
    902 
    903 /* Relocate a 68hc11/68hc12 ELF section.  */
    904 bfd_boolean
    905 elf32_m68hc11_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
    906                                 struct bfd_link_info *info,
    907                                 bfd *input_bfd, asection *input_section,
    908                                 bfd_byte *contents, Elf_Internal_Rela *relocs,
    909                                 Elf_Internal_Sym *local_syms,
    910                                 asection **local_sections)
    911 {
    912   Elf_Internal_Shdr *symtab_hdr;
    913   struct elf_link_hash_entry **sym_hashes;
    914   Elf_Internal_Rela *rel, *relend;
    915   const char *name = NULL;
    916   struct m68hc11_page_info *pinfo;
    917   const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
    918   struct m68hc11_elf_link_hash_table *htab;
    919   unsigned long e_flags;
    920 
    921   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
    922   sym_hashes = elf_sym_hashes (input_bfd);
    923   e_flags = elf_elfheader (input_bfd)->e_flags;
    924 
    925   htab = m68hc11_elf_hash_table (info);
    926   if (htab == NULL)
    927     return FALSE;
    928 
    929   /* Get memory bank parameters.  */
    930   m68hc11_elf_get_bank_parameters (info);
    931 
    932   pinfo = & htab->pinfo;
    933   rel = relocs;
    934   relend = relocs + input_section->reloc_count;
    935 
    936   for (; rel < relend; rel++)
    937     {
    938       int r_type;
    939       arelent arel;
    940       reloc_howto_type *howto;
    941       unsigned long r_symndx;
    942       Elf_Internal_Sym *sym;
    943       asection *sec;
    944       bfd_vma relocation = 0;
    945       bfd_reloc_status_type r = bfd_reloc_undefined;
    946       bfd_vma phys_page;
    947       bfd_vma phys_addr;
    948       bfd_vma insn_addr;
    949       bfd_vma insn_page;
    950       bfd_boolean is_far = FALSE;
    951       bfd_boolean is_xgate_symbol = FALSE;
    952       bfd_boolean is_section_symbol = FALSE;
    953       struct elf_link_hash_entry *h;
    954       bfd_vma val;
    955 
    956       r_symndx = ELF32_R_SYM (rel->r_info);
    957       r_type = ELF32_R_TYPE (rel->r_info);
    958 
    959       if (r_type == R_M68HC11_GNU_VTENTRY
    960           || r_type == R_M68HC11_GNU_VTINHERIT)
    961         continue;
    962 
    963       (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
    964       howto = arel.howto;
    965 
    966       h = NULL;
    967       sym = NULL;
    968       sec = NULL;
    969       if (r_symndx < symtab_hdr->sh_info)
    970 	{
    971 	  sym = local_syms + r_symndx;
    972 	  sec = local_sections[r_symndx];
    973 	  relocation = (sec->output_section->vma
    974 			+ sec->output_offset
    975 			+ sym->st_value);
    976 	  is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
    977 	  is_xgate_symbol = (sym && (sym->st_target_internal));
    978 	  is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
    979 	}
    980       else
    981 	{
    982 	  bfd_boolean unresolved_reloc, warned, ignored;
    983 
    984 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
    985 				   r_symndx, symtab_hdr, sym_hashes,
    986 				   h, sec, relocation, unresolved_reloc,
    987 				   warned, ignored);
    988 
    989 	  is_far = (h && (h->other & STO_M68HC12_FAR));
    990 	  is_xgate_symbol = (h && (h->target_internal));
    991 	}
    992 
    993       if (sec != NULL && discarded_section (sec))
    994 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
    995 					 rel, 1, relend, howto, 0, contents);
    996 
    997       if (info->relocatable)
    998 	{
    999 	  /* This is a relocatable link.  We don't have to change
   1000 	     anything, unless the reloc is against a section symbol,
   1001 	     in which case we have to adjust according to where the
   1002 	     section symbol winds up in the output section.  */
   1003 	  if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   1004 	    rel->r_addend += sec->output_offset;
   1005 	  continue;
   1006 	}
   1007 
   1008       if (h != NULL)
   1009 	name = h->root.root.string;
   1010       else
   1011 	{
   1012 	  name = (bfd_elf_string_from_elf_section
   1013 		  (input_bfd, symtab_hdr->sh_link, sym->st_name));
   1014 	  if (name == NULL || *name == '\0')
   1015 	    name = bfd_section_name (input_bfd, sec);
   1016 	}
   1017 
   1018       if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
   1019 	{
   1020 	  struct elf32_m68hc11_stub_hash_entry* stub;
   1021 
   1022 	  stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
   1023 					   name, FALSE, FALSE);
   1024 	  if (stub)
   1025 	    {
   1026 	      relocation = stub->stub_offset
   1027 		+ stub->stub_sec->output_section->vma
   1028 		+ stub->stub_sec->output_offset;
   1029 	      is_far = FALSE;
   1030 	    }
   1031 	}
   1032 
   1033       /* Do the memory bank mapping.  */
   1034       phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
   1035       phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
   1036       switch (r_type)
   1037         {
   1038         case R_M68HC12_LO8XG:
   1039           /* This relocation is specific to XGATE IMM16 calls and will precede
   1040 	     a HI8. tc-m68hc11 only generates them in pairs.
   1041 	     Leave the relocation to the HI8XG step.  */
   1042           r = bfd_reloc_ok;
   1043           r_type = R_M68HC11_NONE;
   1044           break;
   1045 
   1046         case R_M68HC12_HI8XG:
   1047           /* This relocation is specific to XGATE IMM16 calls and must follow
   1048              a LO8XG. Does not actually check that it was a LO8XG.
   1049 	     Adjusts high and low bytes.  */
   1050           relocation = phys_addr;
   1051           if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
   1052 	      && (relocation >= 0x2000))
   1053 	    relocation += 0xc000; /* HARDCODED RAM offset for XGATE.  */
   1054 
   1055           /* Fetch 16 bit value including low byte in previous insn.  */
   1056           val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
   1057 	    | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
   1058 
   1059           /* Add on value to preserve carry, then write zero to high byte.  */
   1060           relocation += val;
   1061 
   1062           /* Write out top byte.  */
   1063           bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
   1064 		     (bfd_byte*) contents + rel->r_offset);
   1065 
   1066           /* Write out low byte to previous instruction.  */
   1067           bfd_put_8 (input_bfd, relocation & 0xff,
   1068 		     (bfd_byte*) contents + rel->r_offset - 2);
   1069 
   1070           /* Mark as relocation completed.  */
   1071           r = bfd_reloc_ok;
   1072           r_type = R_M68HC11_NONE;
   1073           break;
   1074 
   1075         /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
   1076            assembler directives. %hi does not support carry.  */
   1077         case R_M68HC11_HI8:
   1078         case R_M68HC11_LO8:
   1079           relocation = phys_addr;
   1080           break;
   1081 
   1082         case R_M68HC11_24:
   1083           /* Reloc used by 68HC12 call instruction.  */
   1084           bfd_put_16 (input_bfd, phys_addr,
   1085                       (bfd_byte*) contents + rel->r_offset);
   1086           bfd_put_8 (input_bfd, phys_page,
   1087                      (bfd_byte*) contents + rel->r_offset + 2);
   1088           r = bfd_reloc_ok;
   1089           r_type = R_M68HC11_NONE;
   1090           break;
   1091 
   1092         case R_M68HC11_NONE:
   1093           r = bfd_reloc_ok;
   1094           break;
   1095 
   1096         case R_M68HC11_LO16:
   1097           /* Reloc generated by %addr(expr) gas to obtain the
   1098              address as mapped in the memory bank window.  */
   1099           relocation = phys_addr;
   1100           break;
   1101 
   1102         case R_M68HC11_PAGE:
   1103           /* Reloc generated by %page(expr) gas to obtain the
   1104              page number associated with the address.  */
   1105           relocation = phys_page;
   1106           break;
   1107 
   1108         case R_M68HC11_16:
   1109           /* Get virtual address of instruction having the relocation.  */
   1110           if (is_far)
   1111             {
   1112               const char* msg;
   1113               char* buf;
   1114               msg = _("Reference to the far symbol `%s' using a wrong "
   1115                       "relocation may result in incorrect execution");
   1116               buf = alloca (strlen (msg) + strlen (name) + 10);
   1117               sprintf (buf, msg, name);
   1118 
   1119               (* info->callbacks->warning)
   1120                 (info, buf, name, input_bfd, NULL, rel->r_offset);
   1121             }
   1122 
   1123           /* Get virtual address of instruction having the relocation.  */
   1124           insn_addr = input_section->output_section->vma
   1125             + input_section->output_offset
   1126             + rel->r_offset;
   1127 
   1128           insn_page = m68hc11_phys_page (pinfo, insn_addr);
   1129 
   1130          /* If we are linking an S12 instruction against an XGATE symbol, we
   1131             need to change the offset of the symbol value so that it's correct
   1132 	    from the S12's perspective.  */
   1133           if (is_xgate_symbol)
   1134 	    {
   1135 	      /* The ram in the global space is mapped to 0x2000 in the 16-bit
   1136 		 address space for S12 and 0xE000 in the 16-bit address space
   1137 		 for XGATE.  */
   1138 	      if (relocation >= 0xE000)
   1139 		{
   1140 		  /* We offset the address by the difference
   1141 		     between these two mappings.  */
   1142 		  relocation -= 0xC000;
   1143 		  break;
   1144 		}
   1145 	      else
   1146 		{
   1147 		  const char * msg;
   1148 		  char * buf;
   1149 
   1150 		  msg = _("XGATE address (%lx) is not within shared RAM"
   1151 			  "(0xE000-0xFFFF), therefore you must manually offset "
   1152 			  "the address, and possibly manage the page, in your "
   1153 			  "code.");
   1154 		  buf = alloca (strlen (msg) + 128);
   1155 		  sprintf (buf, msg, phys_addr);
   1156 		  if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
   1157 						     input_section, insn_addr)))
   1158 		    return FALSE;
   1159 		  break;
   1160 		}
   1161 	    }
   1162 
   1163           if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
   1164               && m68hc11_addr_is_banked (pinfo, insn_addr)
   1165               && phys_page != insn_page && !(e_flags & E_M68HC11_NO_BANK_WARNING))
   1166             {
   1167               const char * msg;
   1168               char * buf;
   1169 
   1170               msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
   1171                       "as current banked address [%lx:%04lx] (%lx)");
   1172 
   1173               buf = alloca (strlen (msg) + 128);
   1174               sprintf (buf, msg, phys_page, phys_addr,
   1175                        (long) (relocation + rel->r_addend),
   1176                        insn_page, m68hc11_phys_addr (pinfo, insn_addr),
   1177                        (long) (insn_addr));
   1178               if (!((*info->callbacks->warning)
   1179                     (info, buf, name, input_bfd, input_section,
   1180                      rel->r_offset)))
   1181                 return FALSE;
   1182               break;
   1183             }
   1184 
   1185           if (phys_page != 0 && insn_page == 0)
   1186             {
   1187               const char * msg;
   1188               char * buf;
   1189 
   1190               msg = _("reference to a banked address [%lx:%04lx] in the "
   1191                       "normal address space at %04lx");
   1192 
   1193               buf = alloca (strlen (msg) + 128);
   1194               sprintf (buf, msg, phys_page, phys_addr, insn_addr);
   1195               if (!((*info->callbacks->warning)
   1196                     (info, buf, name, input_bfd, input_section,
   1197                      insn_addr)))
   1198                 return FALSE;
   1199 
   1200               relocation = phys_addr;
   1201               break;
   1202             }
   1203 
   1204           /* If this is a banked address use the phys_addr so that
   1205              we stay in the banked window.  */
   1206           if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
   1207             relocation = phys_addr;
   1208           break;
   1209         }
   1210 
   1211       /* If we are linking an XGATE instruction against an S12 symbol, we
   1212          need to change the offset of the symbol value so that it's correct
   1213 	 from the XGATE's perspective.  */
   1214       if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
   1215           || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
   1216         {
   1217           /* We can only offset S12 addresses that lie within the non-paged
   1218              area of RAM.  */
   1219           if (!is_xgate_symbol && !is_section_symbol)
   1220             {
   1221               /* The ram in the global space is mapped to 0x2000 and stops at
   1222                  0x4000 in the 16-bit address space for S12 and 0xE000 in the
   1223                  16-bit address space for XGATE.  */
   1224               if (relocation >= 0x2000 && relocation < 0x4000)
   1225                  /* We offset the address by the difference
   1226                    between these two mappings.  */
   1227                 relocation += 0xC000;
   1228               else
   1229                 {
   1230                   const char * msg;
   1231                   char * buf;
   1232 
   1233                   /* Get virtual address of instruction having the relocation.  */
   1234                   insn_addr = input_section->output_section->vma
   1235                       + input_section->output_offset + rel->r_offset;
   1236 
   1237                   msg = _("S12 address (%lx) is not within shared RAM"
   1238                       "(0x2000-0x4000), therefore you must manually "
   1239                       "offset the address in your code");
   1240                   buf = alloca (strlen (msg) + 128);
   1241                   sprintf (buf, msg, phys_addr);
   1242                   if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
   1243 						     input_section, insn_addr)))
   1244                     return FALSE;
   1245                   break;
   1246                 }
   1247             }
   1248         }
   1249 
   1250       if (r_type != R_M68HC11_NONE)
   1251         {
   1252           if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
   1253             r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   1254                                       contents, rel->r_offset,
   1255                                       relocation - 2, rel->r_addend);
   1256           else
   1257             r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   1258                                           contents, rel->r_offset,
   1259                                           relocation, rel->r_addend);
   1260         }
   1261 
   1262       if (r != bfd_reloc_ok)
   1263 	{
   1264 	  const char * msg = (const char *) 0;
   1265 
   1266 	  switch (r)
   1267 	    {
   1268 	    case bfd_reloc_overflow:
   1269 	      if (!((*info->callbacks->reloc_overflow)
   1270 		    (info, NULL, name, howto->name, (bfd_vma) 0,
   1271 		     input_bfd, input_section, rel->r_offset)))
   1272 		return FALSE;
   1273 	      break;
   1274 
   1275 	    case bfd_reloc_undefined:
   1276 	      if (!((*info->callbacks->undefined_symbol)
   1277 		    (info, name, input_bfd, input_section,
   1278 		     rel->r_offset, TRUE)))
   1279 		return FALSE;
   1280 	      break;
   1281 
   1282 	    case bfd_reloc_outofrange:
   1283 	      msg = _ ("internal error: out of range error");
   1284 	      goto common_error;
   1285 
   1286 	    case bfd_reloc_notsupported:
   1287 	      msg = _ ("internal error: unsupported relocation error");
   1288 	      goto common_error;
   1289 
   1290 	    case bfd_reloc_dangerous:
   1291 	      msg = _ ("internal error: dangerous error");
   1292 	      goto common_error;
   1293 
   1294 	    default:
   1295 	      msg = _ ("internal error: unknown error");
   1296 	      /* fall through */
   1297 
   1298 	    common_error:
   1299 	      if (!((*info->callbacks->warning)
   1300 		    (info, msg, name, input_bfd, input_section,
   1301 		     rel->r_offset)))
   1302 		return FALSE;
   1303 	      break;
   1304 	    }
   1305 	}
   1306     }
   1307 
   1308   return TRUE;
   1309 }
   1310 
   1311 
   1312 
   1313 /* Set and control ELF flags in ELF header.  */
   1315 
   1316 bfd_boolean
   1317 _bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
   1318 {
   1319   BFD_ASSERT (!elf_flags_init (abfd)
   1320 	      || elf_elfheader (abfd)->e_flags == flags);
   1321 
   1322   elf_elfheader (abfd)->e_flags = flags;
   1323   elf_flags_init (abfd) = TRUE;
   1324   return TRUE;
   1325 }
   1326 
   1327 /* Merge backend specific data from an object file to the output
   1328    object file when linking.  */
   1329 
   1330 bfd_boolean
   1331 _bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   1332 {
   1333   flagword old_flags;
   1334   flagword new_flags;
   1335   bfd_boolean ok = TRUE;
   1336 
   1337   /* Check if we have the same endianness */
   1338   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
   1339     return FALSE;
   1340 
   1341   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1342       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1343     return TRUE;
   1344 
   1345   new_flags = elf_elfheader (ibfd)->e_flags;
   1346   elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
   1347   old_flags = elf_elfheader (obfd)->e_flags;
   1348 
   1349   if (! elf_flags_init (obfd))
   1350     {
   1351       elf_flags_init (obfd) = TRUE;
   1352       elf_elfheader (obfd)->e_flags = new_flags;
   1353       elf_elfheader (obfd)->e_ident[EI_CLASS]
   1354 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
   1355 
   1356       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   1357 	  && bfd_get_arch_info (obfd)->the_default)
   1358 	{
   1359 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   1360 				   bfd_get_mach (ibfd)))
   1361 	    return FALSE;
   1362 	}
   1363 
   1364       return TRUE;
   1365     }
   1366 
   1367   /* Check ABI compatibility.  */
   1368   if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
   1369     {
   1370       (*_bfd_error_handler)
   1371 	(_("%B: linking files compiled for 16-bit integers (-mshort) "
   1372            "and others for 32-bit integers"), ibfd);
   1373       ok = FALSE;
   1374     }
   1375   if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
   1376     {
   1377       (*_bfd_error_handler)
   1378 	(_("%B: linking files compiled for 32-bit double (-fshort-double) "
   1379            "and others for 64-bit double"), ibfd);
   1380       ok = FALSE;
   1381     }
   1382 
   1383   /* Processor compatibility.  */
   1384   if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
   1385     {
   1386       (*_bfd_error_handler)
   1387 	(_("%B: linking files compiled for HCS12 with "
   1388            "others compiled for HC12"), ibfd);
   1389       ok = FALSE;
   1390     }
   1391   new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
   1392                | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
   1393 
   1394   elf_elfheader (obfd)->e_flags = new_flags;
   1395 
   1396   new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
   1397   old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
   1398 
   1399   /* Warn about any other mismatches */
   1400   if (new_flags != old_flags)
   1401     {
   1402       (*_bfd_error_handler)
   1403 	(_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
   1404 	 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
   1405       ok = FALSE;
   1406     }
   1407 
   1408   if (! ok)
   1409     {
   1410       bfd_set_error (bfd_error_bad_value);
   1411       return FALSE;
   1412     }
   1413 
   1414   return TRUE;
   1415 }
   1416 
   1417 bfd_boolean
   1418 _bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   1419 {
   1420   FILE *file = (FILE *) ptr;
   1421 
   1422   BFD_ASSERT (abfd != NULL && ptr != NULL);
   1423 
   1424   /* Print normal ELF private data.  */
   1425   _bfd_elf_print_private_bfd_data (abfd, ptr);
   1426 
   1427   /* xgettext:c-format */
   1428   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   1429 
   1430   if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
   1431     fprintf (file, _("[abi=32-bit int, "));
   1432   else
   1433     fprintf (file, _("[abi=16-bit int, "));
   1434 
   1435   if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
   1436     fprintf (file, _("64-bit double, "));
   1437   else
   1438     fprintf (file, _("32-bit double, "));
   1439 
   1440   if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
   1441     fprintf (file, _("cpu=HC11]"));
   1442   else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
   1443     fprintf (file, _("cpu=HCS12]"));
   1444   else
   1445     fprintf (file, _("cpu=HC12]"));
   1446 
   1447   if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
   1448     fprintf (file, _(" [memory=bank-model]"));
   1449   else
   1450     fprintf (file, _(" [memory=flat]"));
   1451 
   1452   if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
   1453     fprintf (file, _(" [XGATE RAM offsetting]"));
   1454 
   1455   fputc ('\n', file);
   1456 
   1457   return TRUE;
   1458 }
   1459 
   1460 static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
   1461                                    asection *asect, void *arg)
   1462 {
   1463   struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
   1464 
   1465   if (asect->vma >= p->pinfo->bank_virtual)
   1466     p->use_memory_banks = TRUE;
   1467 }
   1468 
   1469 /* Tweak the OSABI field of the elf header.  */
   1470 
   1471 void
   1472 elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
   1473 {
   1474   struct m68hc11_scan_param param;
   1475   struct m68hc11_elf_link_hash_table *htab;
   1476 
   1477   if (link_info == NULL)
   1478     return;
   1479 
   1480   htab = m68hc11_elf_hash_table (link_info);
   1481   if (htab == NULL)
   1482     return;
   1483 
   1484   m68hc11_elf_get_bank_parameters (link_info);
   1485 
   1486   param.use_memory_banks = FALSE;
   1487   param.pinfo = & htab->pinfo;
   1488 
   1489   bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
   1490 
   1491   if (param.use_memory_banks)
   1492     {
   1493       Elf_Internal_Ehdr * i_ehdrp;
   1494 
   1495       i_ehdrp = elf_elfheader (abfd);
   1496       i_ehdrp->e_flags |= E_M68HC12_BANKS;
   1497     }
   1498 }
   1499