Home | History | Annotate | Download | only in emultempl
      1 # This shell script emits a C file. -*- C -*-
      2 #   Copyright (C) 1991-2014 Free Software Foundation, Inc.
      3 #
      4 # This file is part of the GNU Binutils.
      5 #
      6 # This program is free software; you can redistribute it and/or modify
      7 # it under the terms of the GNU General Public License as published by
      8 # the Free Software Foundation; either version 3 of the License, or
      9 # (at your option) any later version.
     10 #
     11 # This program is distributed in the hope that it will be useful,
     12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 # GNU General Public License for more details.
     15 #
     16 # You should have received a copy of the GNU General Public License
     17 # along with this program; if not, write to the Free Software
     18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19 # MA 02110-1301, USA.
     20 #
     21 
     22 # This file is sourced from elf32.em, and defines extra m68hc12-elf
     23 # and m68hc11-elf specific routines.  It is used to generate the
     24 # HC11/HC12 trampolines to call a far function by using a normal 'jsr/bsr'.
     25 #
     26 # - The HC11/HC12 relocations are checked to see if there is a
     27 #   R_M68HC11_16 relocation to a symbol marked with STO_M68HC12_FAR.
     28 #   This relocation cannot be made on the symbol but must be made on
     29 #   its trampoline
     30 #   The trampolines to generate are collected during this pass
     31 #   (See elf32_m68hc11_size_stubs)
     32 #
     33 # - The trampolines are generated in a ".tramp" section.  The generation
     34 #   takes care of HC11 and HC12 specificities.
     35 #   (See elf32_m68hc11_build_stubs)
     36 #
     37 # - During relocation the R_M68HC11_16 relocation to the far symbols
     38 #   are redirected to the trampoline that was generated.
     39 #
     40 # Copied from hppaelf and adapted for M68HC11/M68HC12 specific needs.
     41 #
     42 fragment <<EOF
     43 
     44 #include "ldctor.h"
     45 #include "elf32-m68hc1x.h"
     46 
     47 static asection *m68hc11elf_add_stub_section (const char *, asection *);
     48 
     49 /* Fake input file for stubs.  */
     50 static lang_input_statement_type *stub_file;
     51 
     52 /* By default the HC11/HC12 trampolines to call a far function using
     53    a normal 'bsr' and 'jsr' convention are generated during the link.
     54    The --no-trampoline option prevents that.  */
     55 static int no_trampoline = 0;
     56 
     57 /* Name of memory bank window in the MEMORY description.
     58    This is set by --bank-window option.  */
     59 static const char* bank_window_name = 0;
     60 
     61 static void
     62 m68hc11_elf_${EMULATION_NAME}_before_allocation (void)
     63 {
     64   lang_memory_region_type* region;
     65   int ret;
     66 
     67   gld${EMULATION_NAME}_before_allocation ();
     68 
     69   /* If generating a relocatable output file, then we don't
     70      have to generate the trampolines.  */
     71   if (link_info.relocatable)
     72     return;
     73 
     74   ret = elf32_m68hc11_setup_section_lists (link_info.output_bfd, &link_info);
     75   if (ret != 0 && no_trampoline == 0)
     76     {
     77       if (ret < 0)
     78 	{
     79 	  einfo ("%X%P: can not size stub section: %E\n");
     80 	  return;
     81 	}
     82 
     83       /* Call into the BFD backend to do the real work.  */
     84       if (!elf32_m68hc11_size_stubs (link_info.output_bfd,
     85 				     stub_file->the_bfd,
     86 				     &link_info,
     87 				     &m68hc11elf_add_stub_section))
     88 	{
     89 	  einfo ("%X%P: can not size stub section: %E\n");
     90 	  return;
     91 	}
     92     }
     93 
     94   if (bank_window_name == 0)
     95     return;
     96 
     97   /* The 'bank_window_name' memory region is a special region that describes
     98      the memory bank window to access to paged memory.  For 68HC12
     99      this is fixed and should be:
    100 
    101      window (rx) : ORIGIN = 0x8000, LENGTH = 16K
    102 
    103      But for 68HC11 this is board specific.  The definition of such
    104      memory region allows to control how this paged memory is accessed.  */
    105   region = lang_memory_region_lookup (bank_window_name, FALSE);
    106 
    107   /* Check the length to see if it was defined in the script.  */
    108   if (region->length != 0)
    109     {
    110       struct m68hc11_page_info *pinfo;
    111       unsigned i;
    112 
    113       /* Get default values  */
    114       m68hc11_elf_get_bank_parameters (&link_info);
    115       pinfo = &m68hc11_elf_hash_table (&link_info)->pinfo;
    116 
    117       /* And override them with the region definition.  */
    118       pinfo->bank_size = region->length;
    119       pinfo->bank_shift = 0;
    120       for (i = pinfo->bank_size; i != 0; i >>= 1)
    121 	pinfo->bank_shift++;
    122       pinfo->bank_shift--;
    123       pinfo->bank_size = 1L << pinfo->bank_shift;
    124       pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
    125       pinfo->bank_physical = region->origin;
    126       pinfo->bank_physical_end = region->origin + pinfo->bank_size;
    127 
    128       if (pinfo->bank_size != region->length)
    129 	{
    130 	  einfo (_("warning: the size of the 'window' memory region "
    131 		   "is not a power of 2\n"));
    132 	  einfo (_("warning: its size %d is truncated to %d\n"),
    133 		 region->length, pinfo->bank_size);
    134 	}
    135     }
    136 }
    137 
    138 /* This is called before the input files are opened.  We create a new
    139    fake input file to hold the stub sections.  */
    140 
    141 static void
    142 m68hc11elf_create_output_section_statements (void)
    143 {
    144   stub_file = lang_add_input_file ("linker stubs",
    145 				   lang_input_file_is_fake_enum,
    146 				   NULL);
    147   stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
    148   if (stub_file->the_bfd == NULL
    149       || !bfd_set_arch_mach (stub_file->the_bfd,
    150 			     bfd_get_arch (link_info.output_bfd),
    151 			     bfd_get_mach (link_info.output_bfd)))
    152     {
    153       einfo ("%X%P: can not create BFD %E\n");
    154       return;
    155     }
    156 
    157   ldlang_add_file (stub_file);
    158 }
    159 
    160 
    161 struct hook_stub_info
    162 {
    163   lang_statement_list_type add;
    164   asection *input_section;
    165 };
    166 
    167 /* Traverse the linker tree to find the spot where the stub goes.  */
    168 
    169 static bfd_boolean
    170 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
    171 {
    172   lang_statement_union_type *l;
    173   bfd_boolean ret;
    174 
    175   for (; (l = *lp) != NULL; lp = &l->header.next)
    176     {
    177       switch (l->header.type)
    178 	{
    179 	case lang_constructors_statement_enum:
    180 	  ret = hook_in_stub (info, &constructor_list.head);
    181 	  if (ret)
    182 	    return ret;
    183 	  break;
    184 
    185 	case lang_output_section_statement_enum:
    186 	  ret = hook_in_stub (info,
    187 			      &l->output_section_statement.children.head);
    188 	  if (ret)
    189 	    return ret;
    190 	  break;
    191 
    192 	case lang_wild_statement_enum:
    193 	  ret = hook_in_stub (info, &l->wild_statement.children.head);
    194 	  if (ret)
    195 	    return ret;
    196 	  break;
    197 
    198 	case lang_group_statement_enum:
    199 	  ret = hook_in_stub (info, &l->group_statement.children.head);
    200 	  if (ret)
    201 	    return ret;
    202 	  break;
    203 
    204 	case lang_input_section_enum:
    205 	  if (l->input_section.section == info->input_section
    206 	      || strcmp (bfd_get_section_name (l->input_section.section->owner,
    207 					       l->input_section.section),
    208 			 bfd_get_section_name (info->input_section->owner,
    209 					       info->input_section)) == 0)
    210 	    {
    211 	      /* We've found our section.  Insert the stub immediately
    212 		 before its associated input section.  */
    213 	      *lp = info->add.head;
    214 	      *(info->add.tail) = l;
    215 	      return TRUE;
    216 	    }
    217 	  break;
    218 
    219 	case lang_data_statement_enum:
    220 	case lang_reloc_statement_enum:
    221 	case lang_object_symbols_statement_enum:
    222 	case lang_output_statement_enum:
    223 	case lang_target_statement_enum:
    224 	case lang_input_statement_enum:
    225 	case lang_assignment_statement_enum:
    226 	case lang_padding_statement_enum:
    227 	case lang_address_statement_enum:
    228 	case lang_fill_statement_enum:
    229 	  break;
    230 
    231 	default:
    232 	  FAIL ();
    233 	  break;
    234 	}
    235     }
    236   return FALSE;
    237 }
    238 
    239 
    240 /* Call-back for elf32_m68hc11_size_stubs.  */
    241 
    242 /* Create a new stub section, and arrange for it to be linked
    243    immediately before INPUT_SECTION.  */
    244 
    245 static asection *
    246 m68hc11elf_add_stub_section (const char *stub_sec_name,
    247 			     asection *tramp_section)
    248 {
    249   asection *stub_sec;
    250   flagword flags;
    251   asection *output_section;
    252   lang_output_section_statement_type *os;
    253   struct hook_stub_info info;
    254 
    255   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
    256 	   | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
    257   stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
    258 						 stub_sec_name, flags);
    259   if (stub_sec == NULL)
    260     goto err_ret;
    261 
    262   output_section = tramp_section->output_section;
    263   os = lang_output_section_get (output_section);
    264 
    265   /* Try to put the new section at the same place as an existing
    266      .tramp section.  Such .tramp section exists in most cases and
    267      contains the trampoline code.  This way we put the generated trampoline
    268      at the correct place.  */
    269   info.input_section = tramp_section;
    270   lang_list_init (&info.add);
    271   lang_add_section (&info.add, stub_sec, NULL, os);
    272 
    273   if (info.add.head == NULL)
    274     goto err_ret;
    275 
    276   if (hook_in_stub (&info, &os->children.head))
    277     return stub_sec;
    278 
    279  err_ret:
    280   einfo ("%X%P: can not make stub section: %E\n");
    281   return NULL;
    282 }
    283 
    284 /* For the 68HC12 we use this opportunity to build linker stubs.  */
    285 
    286 static void
    287 m68hc11elf_after_allocation (void)
    288 {
    289   /* Now build the linker stubs.  */
    290   if (stub_file->the_bfd->sections != NULL)
    291     {
    292       /* Call again the trampoline analyzer to initialize the trampoline
    293 	 stubs with the correct symbol addresses.  Since there could have
    294 	 been relaxation, the symbol addresses that were found during
    295 	 first call may no longer be correct.  */
    296       if (!elf32_m68hc11_size_stubs (link_info.output_bfd,
    297 				     stub_file->the_bfd,
    298 				     &link_info, 0))
    299 	{
    300 	  einfo ("%X%P: can not size stub section: %E\n");
    301 	  return;
    302 	}
    303       if (!elf32_m68hc11_build_stubs (link_info.output_bfd, &link_info))
    304 	einfo ("%X%P: can not build stubs: %E\n");
    305     }
    306 
    307   gld${EMULATION_NAME}_after_allocation ();
    308 }
    309 
    310 
    311 /* Avoid processing the fake stub_file in vercheck, stat_needed and
    312    check_needed routines.  */
    313 
    314 static void (*real_func) (lang_input_statement_type *);
    315 
    316 static void m68hc11_for_each_input_file_wrapper (lang_input_statement_type *l)
    317 {
    318   if (l != stub_file)
    319     (*real_func) (l);
    320 }
    321 
    322 static void
    323 m68hc11_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
    324 {
    325   real_func = func;
    326   lang_for_each_input_file (&m68hc11_for_each_input_file_wrapper);
    327 }
    328 
    329 #define lang_for_each_input_file m68hc11_lang_for_each_input_file
    330 
    331 EOF
    332 
    333 # Define some shell vars to insert bits of code into the standard elf
    334 # parse_args and list_options functions.
    335 #
    336 PARSE_AND_LIST_PROLOGUE='
    337 #define OPTION_NO_TRAMPOLINE		300
    338 #define OPTION_BANK_WINDOW		301
    339 '
    340 
    341 # The options are repeated below so that no abbreviations are allowed.
    342 # Otherwise -s matches stub-group-size
    343 PARSE_AND_LIST_LONGOPTS='
    344   { "no-trampoline", no_argument, NULL, OPTION_NO_TRAMPOLINE },
    345   { "bank-window",   required_argument, NULL, OPTION_BANK_WINDOW },
    346 '
    347 
    348 PARSE_AND_LIST_OPTIONS='
    349   fprintf (file, _(
    350 "  --no-trampoline             Do not generate the far trampolines used to call\n"
    351 "                                a far function using 'jsr' or 'bsr'.\n"
    352 "  --bank-window NAME          Specify the name of the memory region describing\n"
    353 "                                the layout of the memory bank window.\n"
    354 		   ));
    355 '
    356 
    357 PARSE_AND_LIST_ARGS_CASES='
    358     case OPTION_NO_TRAMPOLINE:
    359       no_trampoline = 1;
    360       break;
    361     case OPTION_BANK_WINDOW:
    362       bank_window_name = optarg;
    363       break;
    364 '
    365 
    366 # Put these extra m68hc11elf routines in ld_${EMULATION_NAME}_emulation
    367 #
    368 LDEMUL_BEFORE_ALLOCATION=m68hc11_elf_${EMULATION_NAME}_before_allocation
    369 LDEMUL_AFTER_ALLOCATION=m68hc11elf_after_allocation
    370 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=m68hc11elf_create_output_section_statements
    371