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