Home | History | Annotate | Download | only in emultempl
      1 # This shell script emits a C file. -*- C -*-
      2 #   Copyright (C) 2006-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 avr-elf specific
     23 # routines.  It is used to generate the trampolines for the avr6 family
     24 # of devices where one needs to address the issue that it is not possible
     25 # to reach the whole program memory by using 16 bit pointers.
     26 
     27 fragment <<EOF
     28 
     29 #include "elf32-avr.h"
     30 #include "ldctor.h"
     31 
     32 /* The fake file and it's corresponding section meant to hold
     33    the linker stubs if needed.  */
     34 
     35 static lang_input_statement_type *stub_file;
     36 static asection *avr_stub_section;
     37 
     38 /* Variables set by the command-line parameters and transfered
     39    to the bfd without use of global shared variables.  */
     40 
     41 static bfd_boolean avr_no_stubs = FALSE;
     42 static bfd_boolean avr_debug_relax = FALSE;
     43 static bfd_boolean avr_debug_stubs = FALSE;
     44 static bfd_boolean avr_replace_call_ret_sequences = TRUE;
     45 static bfd_vma avr_pc_wrap_around = 0x10000000;
     46 
     47 /* Transfers information to the bfd frontend.  */
     48 
     49 static void
     50 avr_elf_set_global_bfd_parameters (void)
     51 {
     52   elf32_avr_setup_params (& link_info,
     53                           stub_file->the_bfd,
     54                           avr_stub_section,
     55                           avr_no_stubs,
     56                           avr_debug_stubs,
     57                           avr_debug_relax,
     58                           avr_pc_wrap_around,
     59                           avr_replace_call_ret_sequences);
     60 }
     61 
     62 
     63 /* Makes a conservative estimate of the trampoline section size that could
     64    be corrected later on.  */
     65 
     66 static void
     67 avr_elf_${EMULATION_NAME}_before_allocation (void)
     68 {
     69   int ret;
     70 
     71   gld${EMULATION_NAME}_before_allocation ();
     72 
     73   /* We only need stubs for avr6, avrxmega6, and avrxmega7. */
     74   if (strcmp ("${EMULATION_NAME}","avr6")
     75       && strcmp ("${EMULATION_NAME}","avrxmega6")
     76       && strcmp ("${EMULATION_NAME}","avrxmega7") )
     77     avr_no_stubs = TRUE;
     78 
     79   avr_elf_set_global_bfd_parameters ();
     80 
     81   /* If generating a relocatable output file, then
     82      we don't  have to generate the trampolines.  */
     83   if (link_info.relocatable)
     84     avr_no_stubs = TRUE;
     85 
     86   if (avr_no_stubs)
     87     return;
     88 
     89   ret = elf32_avr_setup_section_lists (link_info.output_bfd, &link_info);
     90 
     91   if (ret < 0)
     92     einfo ("%X%P: can not setup the input section list: %E\n");
     93 
     94   if (ret <= 0)
     95     return;
     96 
     97   /* Call into the BFD backend to do the real "stub"-work.  */
     98   if (! elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE))
     99     einfo ("%X%P: can not size stub section: %E\n");
    100 }
    101 
    102 /* This is called before the input files are opened.  We create a new
    103    fake input file to hold the stub section and generate the section itself.  */
    104 
    105 static void
    106 avr_elf_create_output_section_statements (void)
    107 {
    108   flagword flags;
    109 
    110   stub_file = lang_add_input_file ("linker stubs",
    111                                    lang_input_file_is_fake_enum,
    112                                    NULL);
    113 
    114   stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
    115   if (stub_file->the_bfd == NULL
    116       || !bfd_set_arch_mach (stub_file->the_bfd,
    117                              bfd_get_arch (link_info.output_bfd),
    118                              bfd_get_mach (link_info.output_bfd)))
    119     {
    120       einfo ("%X%P: can not create stub BFD %E\n");
    121       return;
    122     }
    123 
    124   /* Now we add the stub section.  */
    125 
    126   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
    127            | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
    128   avr_stub_section = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
    129 							 ".trampolines",
    130 							 flags);
    131   if (avr_stub_section == NULL)
    132     goto err_ret;
    133 
    134   avr_stub_section->alignment_power = 1;
    135 
    136   ldlang_add_file (stub_file);
    137 
    138   return;
    139 
    140   err_ret:
    141    einfo ("%X%P: can not make stub section: %E\n");
    142    return;
    143 }
    144 
    145 /* Re-calculates the size of the stubs so that we won't waste space.  */
    146 
    147 static void
    148 avr_elf_after_allocation (void)
    149 {
    150   if (!avr_no_stubs && ! RELAXATION_ENABLED)
    151     {
    152       /* If relaxing, elf32_avr_size_stubs will be called from
    153 	 elf32_avr_relax_section.  */
    154       if (!elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE))
    155 	einfo ("%X%P: can not size stub section: %E\n");
    156     }
    157 
    158   gld${EMULATION_NAME}_after_allocation ();
    159 
    160   /* Now build the linker stubs.  */
    161   if (!avr_no_stubs)
    162     {
    163       if (!elf32_avr_build_stubs (&link_info))
    164 	einfo ("%X%P: can not build stubs: %E\n");
    165     }
    166 }
    167 
    168 static void
    169 avr_elf_before_parse (void)
    170 {
    171   /* Don't create a demand-paged executable, since this feature isn't
    172      meaningful in AVR. */
    173   config.magic_demand_paged = FALSE;
    174 
    175   gld${EMULATION_NAME}_before_parse ();
    176 }
    177 
    178 EOF
    179 
    180 
    181 PARSE_AND_LIST_PROLOGUE='
    182 
    183 #define OPTION_NO_CALL_RET_REPLACEMENT 301
    184 #define OPTION_PMEM_WRAP_AROUND        302
    185 #define OPTION_NO_STUBS                303
    186 #define OPTION_DEBUG_STUBS             304
    187 #define OPTION_DEBUG_RELAX             305
    188 '
    189 
    190 PARSE_AND_LIST_LONGOPTS='
    191   { "no-call-ret-replacement", no_argument,
    192      NULL, OPTION_NO_CALL_RET_REPLACEMENT},
    193   { "pmem-wrap-around", required_argument,
    194     NULL, OPTION_PMEM_WRAP_AROUND},
    195   { "no-stubs", no_argument,
    196     NULL, OPTION_NO_STUBS},
    197   { "debug-stubs", no_argument,
    198     NULL, OPTION_DEBUG_STUBS},
    199   { "debug-relax", no_argument,
    200     NULL, OPTION_DEBUG_RELAX},
    201 '
    202 
    203 PARSE_AND_LIST_OPTIONS='
    204   fprintf (file, _("  --pmem-wrap-around=<val>    "
    205 		   "Make the linker relaxation machine assume that a\n"
    206 		   "                              "
    207 		   "  program counter wrap-around occures at address\n"
    208 		   "                              "
    209 		   "  <val>.  Supported values: 8k, 16k, 32k and 64k.\n"));
    210   fprintf (file, _("  --no-call-ret-replacement   "
    211 		   "The relaxation machine normally will\n"
    212 		   "                              "
    213 		   "  substitute two immediately following call/ret\n"
    214 		   "                              "
    215 		   "  instructions by a single jump instruction.\n"
    216 		   "                              "
    217 		   "  This option disables this optimization.\n"));
    218   fprintf (file, _("  --no-stubs                  "
    219 		   "If the linker detects to attempt to access\n"
    220 		   "                              "
    221 		   "  an instruction beyond 128k by a reloc that\n"
    222 		   "                              "
    223 		   "  is limited to 128k max, it inserts a jump\n"
    224 		   "                              "
    225 		   "  stub. You can de-active this with this switch.\n"));
    226   fprintf (file, _("  --debug-stubs               "
    227 		   "Used for debugging avr-ld.\n"));
    228   fprintf (file, _("  --debug-relax               "
    229 		   "Used for debugging avr-ld.\n"));
    230 '
    231 
    232 PARSE_AND_LIST_ARGS_CASES='
    233 
    234     case OPTION_PMEM_WRAP_AROUND:
    235       {
    236         /* This variable is defined in the bfd library.  */
    237         if ((!strcmp (optarg,"32k"))      || (!strcmp (optarg,"32K")))
    238           avr_pc_wrap_around = 32768;
    239         else if ((!strcmp (optarg,"8k")) || (!strcmp (optarg,"8K")))
    240           avr_pc_wrap_around = 8192;
    241         else if ((!strcmp (optarg,"16k")) || (!strcmp (optarg,"16K")))
    242           avr_pc_wrap_around = 16384;
    243         else if ((!strcmp (optarg,"64k")) || (!strcmp (optarg,"64K")))
    244           avr_pc_wrap_around = 0x10000;
    245         else
    246           return FALSE;
    247       }
    248       break;
    249 
    250     case OPTION_DEBUG_STUBS:
    251       avr_debug_stubs = TRUE;
    252       break;
    253 
    254     case OPTION_DEBUG_RELAX:
    255       avr_debug_relax = TRUE;
    256       break;
    257 
    258     case OPTION_NO_STUBS:
    259       avr_no_stubs = TRUE;
    260       break;
    261 
    262     case OPTION_NO_CALL_RET_REPLACEMENT:
    263       {
    264         /* This variable is defined in the bfd library.  */
    265         avr_replace_call_ret_sequences = FALSE;
    266       }
    267       break;
    268 '
    269 
    270 #
    271 # Put these extra avr-elf routines in ld_${EMULATION_NAME}_emulation
    272 #
    273 LDEMUL_BEFORE_PARSE=avr_elf_before_parse
    274 LDEMUL_BEFORE_ALLOCATION=avr_elf_${EMULATION_NAME}_before_allocation
    275 LDEMUL_AFTER_ALLOCATION=avr_elf_after_allocation
    276 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=avr_elf_create_output_section_statements
    277