Home | History | Annotate | Download | only in emultempl
      1 # This shell script emits a C file. -*- C -*-
      2 #   Copyright (C) 2009-2014 Free Software Foundation, Inc.
      3 #   Contributed by ARM Ltd.
      4 #
      5 # This file is part of the GNU Binutils.
      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; see the file COPYING3. If not,
     19 # see <http://www.gnu.org/licenses/>.
     20 #
     21 
     22 # This file is sourced from elf32.em, and defines extra aarch64-elf
     23 # specific routines.
     24 #
     25 fragment <<EOF
     26 
     27 #include "ldctor.h"
     28 #include "elf/aarch64.h"
     29 
     30 static int no_enum_size_warning = 0;
     31 static int no_wchar_size_warning = 0;
     32 static int pic_veneer = 0;
     33 static int fix_erratum_835769 = 0;
     34 static int fix_erratum_843419 = 0;
     35 
     36 static void
     37 gld${EMULATION_NAME}_before_parse (void)
     38 {
     39 #ifndef TARGET_			/* I.e., if not generic.  */
     40   ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
     41 #endif /* not TARGET_ */
     42   input_flags.dynamic = ${DYNAMIC_LINK-TRUE};
     43   config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
     44   config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo TRUE ; else echo FALSE ; fi`;
     45 }
     46 
     47 static void
     48 aarch64_elf_before_allocation (void)
     49 {
     50   /* We should be able to set the size of the interworking stub section.  We
     51      can't do it until later if we have dynamic sections, though.  */
     52   if (! elf_hash_table (&link_info)->dynamic_sections_created)
     53     {
     54       /* Here we rummage through the found bfds to collect information.  */
     55       LANG_FOR_EACH_INPUT_STATEMENT (is)
     56 	{
     57           /* Initialise mapping tables for code/data.  */
     58           bfd_elf${ELFSIZE}_aarch64_init_maps (is->the_bfd);
     59 	}
     60     }
     61 
     62   /* Call the standard elf routine.  */
     63   gld${EMULATION_NAME}_before_allocation ();
     64 }
     65 
     66 /* Fake input file for stubs.  */
     67 static lang_input_statement_type *stub_file;
     68 
     69 /* Whether we need to call gldarm_layout_sections_again.  */
     70 static int need_laying_out = 0;
     71 
     72 /* Maximum size of a group of input sections that can be handled by
     73    one stub section.  A value of +/-1 indicates the bfd back-end
     74    should use a suitable default size.  */
     75 static bfd_signed_vma group_size = 1;
     76 
     77 struct hook_stub_info
     78 {
     79   lang_statement_list_type add;
     80   asection *input_section;
     81 };
     82 
     83 /* Traverse the linker tree to find the spot where the stub goes.  */
     84 
     85 static bfd_boolean
     86 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
     87 {
     88   lang_statement_union_type *l;
     89   bfd_boolean ret;
     90 
     91   for (; (l = *lp) != NULL; lp = &l->header.next)
     92     {
     93       switch (l->header.type)
     94 	{
     95 	case lang_constructors_statement_enum:
     96 	  ret = hook_in_stub (info, &constructor_list.head);
     97 	  if (ret)
     98 	    return ret;
     99 	  break;
    100 
    101 	case lang_output_section_statement_enum:
    102 	  ret = hook_in_stub (info,
    103 			      &l->output_section_statement.children.head);
    104 	  if (ret)
    105 	    return ret;
    106 	  break;
    107 
    108 	case lang_wild_statement_enum:
    109 	  ret = hook_in_stub (info, &l->wild_statement.children.head);
    110 	  if (ret)
    111 	    return ret;
    112 	  break;
    113 
    114 	case lang_group_statement_enum:
    115 	  ret = hook_in_stub (info, &l->group_statement.children.head);
    116 	  if (ret)
    117 	    return ret;
    118 	  break;
    119 
    120 	case lang_input_section_enum:
    121 	  if (l->input_section.section == info->input_section)
    122 	    {
    123 	      /* We've found our section.  Insert the stub immediately
    124 		 after its associated input section.  */
    125 	      *(info->add.tail) = l->header.next;
    126 	      l->header.next = info->add.head;
    127 	      return TRUE;
    128 	    }
    129 	  break;
    130 
    131 	case lang_data_statement_enum:
    132 	case lang_reloc_statement_enum:
    133 	case lang_object_symbols_statement_enum:
    134 	case lang_output_statement_enum:
    135 	case lang_target_statement_enum:
    136 	case lang_input_statement_enum:
    137 	case lang_assignment_statement_enum:
    138 	case lang_padding_statement_enum:
    139 	case lang_address_statement_enum:
    140 	case lang_fill_statement_enum:
    141 	  break;
    142 
    143 	default:
    144 	  FAIL ();
    145 	  break;
    146 	}
    147     }
    148   return FALSE;
    149 }
    150 
    151 
    152 /* Call-back for elf${ELFSIZE}_aarch64_size_stubs.  */
    153 
    154 /* Create a new stub section, and arrange for it to be linked
    155    immediately after INPUT_SECTION.  */
    156 
    157 static asection *
    158 elf${ELFSIZE}_aarch64_add_stub_section (const char *stub_sec_name,
    159 				asection *input_section)
    160 {
    161   asection *stub_sec;
    162   flagword flags;
    163   asection *output_section;
    164   lang_output_section_statement_type *os;
    165   struct hook_stub_info info;
    166 
    167   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
    168 	   | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
    169   stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
    170 						 stub_sec_name, flags);
    171   if (stub_sec == NULL)
    172     goto err_ret;
    173 
    174   bfd_set_section_alignment (stub_file->the_bfd, stub_sec, 2);
    175 
    176   output_section = input_section->output_section;
    177   os = lang_output_section_get (output_section);
    178 
    179   info.input_section = input_section;
    180   lang_list_init (&info.add);
    181   lang_add_section (&info.add, stub_sec, NULL, os);
    182 
    183   if (info.add.head == NULL)
    184     goto err_ret;
    185 
    186   if (hook_in_stub (&info, &os->children.head))
    187     return stub_sec;
    188 
    189  err_ret:
    190   einfo ("%X%P: can not make stub section: %E\n");
    191   return NULL;
    192 }
    193 
    194 /* Another call-back for elf${ELFSIZE}_aarch64_size_stubs.  */
    195 
    196 static void
    197 gldaarch64_layout_sections_again (void)
    198 {
    199   /* If we have changed sizes of the stub sections, then we need
    200      to recalculate all the section offsets.  This may mean we need to
    201      add even more stubs.  */
    202   gld${EMULATION_NAME}_map_segments (TRUE);
    203   need_laying_out = -1;
    204 }
    205 
    206 static void
    207 build_section_lists (lang_statement_union_type *statement)
    208 {
    209   if (statement->header.type == lang_input_section_enum)
    210     {
    211       asection *i = statement->input_section.section;
    212 
    213       if (!((lang_input_statement_type *) i->owner->usrdata)->flags.just_syms
    214 	  && (i->flags & SEC_EXCLUDE) == 0
    215 	  && i->output_section != NULL
    216 	  && i->output_section->owner == link_info.output_bfd)
    217 	elf${ELFSIZE}_aarch64_next_input_section (& link_info, i);
    218     }
    219 }
    220 
    221 static void
    222 gld${EMULATION_NAME}_after_allocation (void)
    223 {
    224   int ret;
    225 
    226   /* bfd_elf32_discard_info just plays with debugging sections,
    227      ie. doesn't affect any code, so we can delay resizing the
    228      sections.  It's likely we'll resize everything in the process of
    229      adding stubs.  */
    230   ret = bfd_elf_discard_info (link_info.output_bfd, & link_info);
    231   if (ret < 0)
    232     {
    233       einfo ("%X%P: .eh_frame/.stab edit: %E\n");
    234       return;
    235     }
    236   else if (ret > 0)
    237     need_laying_out = 1;
    238 
    239   /* If generating a relocatable output file, then we don't
    240      have to examine the relocs.  */
    241   if (stub_file != NULL && !link_info.relocatable)
    242     {
    243       ret = elf${ELFSIZE}_aarch64_setup_section_lists (link_info.output_bfd,
    244 						       &link_info);
    245       if (ret != 0)
    246 	{
    247 	  if (ret < 0)
    248 	    {
    249 	      einfo ("%X%P: could not compute sections lists for stub generation: %E\n");
    250 	      return;
    251 	    }
    252 
    253 	  lang_for_each_statement (build_section_lists);
    254 
    255 	  /* Call into the BFD backend to do the real work.  */
    256 	  if (! elf${ELFSIZE}_aarch64_size_stubs (link_info.output_bfd,
    257 					  stub_file->the_bfd,
    258 					  & link_info,
    259 					  group_size,
    260 					  & elf${ELFSIZE}_aarch64_add_stub_section,
    261 					  & gldaarch64_layout_sections_again))
    262 	    {
    263 	      einfo ("%X%P: cannot size stub section: %E\n");
    264 	      return;
    265 	    }
    266 	}
    267     }
    268 
    269   if (need_laying_out != -1)
    270     gld${EMULATION_NAME}_map_segments (need_laying_out);
    271 }
    272 
    273 static void
    274 gld${EMULATION_NAME}_finish (void)
    275 {
    276   if (! link_info.relocatable)
    277     {
    278       /* Now build the linker stubs.  */
    279       if (stub_file->the_bfd->sections != NULL)
    280 	{
    281 	  if (! elf${ELFSIZE}_aarch64_build_stubs (& link_info))
    282 	    einfo ("%X%P: can not build stubs: %E\n");
    283 	}
    284     }
    285 
    286   finish_default ();
    287 }
    288 
    289 /* This is a convenient point to tell BFD about target specific flags.
    290    After the output has been created, but before inputs are read.  */
    291 static void
    292 aarch64_elf_create_output_section_statements (void)
    293 {
    294   if (strstr (bfd_get_target (link_info.output_bfd), "aarch64") == NULL)
    295     {
    296       /* The arm backend needs special fields in the output hash structure.
    297 	 These will only be created if the output format is an arm format,
    298 	 hence we do not support linking and changing output formats at the
    299 	 same time.  Use a link followed by objcopy to change output formats.  */
    300       einfo ("%F%X%P: error: Cannot change output format whilst linking AArch64 binaries.\n");
    301       return;
    302     }
    303 
    304   bfd_elf${ELFSIZE}_aarch64_set_options (link_info.output_bfd, &link_info,
    305 				 no_enum_size_warning,
    306 				 no_wchar_size_warning,
    307 				 pic_veneer,
    308 				 fix_erratum_835769, fix_erratum_843419);
    309 
    310   stub_file = lang_add_input_file ("linker stubs",
    311 				   lang_input_file_is_fake_enum,
    312 				   NULL);
    313   stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
    314   if (stub_file->the_bfd == NULL
    315       || ! bfd_set_arch_mach (stub_file->the_bfd,
    316 			      bfd_get_arch (link_info.output_bfd),
    317 			      bfd_get_mach (link_info.output_bfd)))
    318     {
    319       einfo ("%X%P: can not create BFD %E\n");
    320       return;
    321     }
    322 
    323   stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
    324   ldlang_add_file (stub_file);
    325 }
    326 
    327 /* Avoid processing the fake stub_file in vercheck, stat_needed and
    328    check_needed routines.  */
    329 
    330 static void (*real_func) (lang_input_statement_type *);
    331 
    332 static void aarch64_for_each_input_file_wrapper (lang_input_statement_type *l)
    333 {
    334   if (l != stub_file)
    335     (*real_func) (l);
    336 }
    337 
    338 static void
    339 aarch64_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
    340 {
    341   real_func = func;
    342   lang_for_each_input_file (&aarch64_for_each_input_file_wrapper);
    343 }
    344 
    345 #define lang_for_each_input_file aarch64_lang_for_each_input_file
    346 
    347 EOF
    348 
    349 # Define some shell vars to insert bits of code into the standard elf
    350 # parse_args and list_options functions.
    351 #
    352 PARSE_AND_LIST_PROLOGUE='
    353 #define OPTION_NO_ENUM_SIZE_WARNING	309
    354 #define OPTION_PIC_VENEER		310
    355 #define OPTION_STUBGROUP_SIZE           311
    356 #define OPTION_NO_WCHAR_SIZE_WARNING	312
    357 #define OPTION_FIX_ERRATUM_835769	313
    358 #define OPTION_FIX_ERRATUM_843419	314
    359 '
    360 
    361 PARSE_AND_LIST_SHORTOPTS=p
    362 
    363 PARSE_AND_LIST_LONGOPTS='
    364   { "no-pipeline-knowledge", no_argument, NULL, '\'p\''},
    365   { "no-enum-size-warning", no_argument, NULL, OPTION_NO_ENUM_SIZE_WARNING},
    366   { "pic-veneer", no_argument, NULL, OPTION_PIC_VENEER},
    367   { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
    368   { "no-wchar-size-warning", no_argument, NULL, OPTION_NO_WCHAR_SIZE_WARNING},
    369   { "fix-cortex-a53-835769", no_argument, NULL, OPTION_FIX_ERRATUM_835769},
    370   { "fix-cortex-a53-843419", no_argument, NULL, OPTION_FIX_ERRATUM_843419},
    371 '
    372 
    373 PARSE_AND_LIST_OPTIONS='
    374   fprintf (file, _("  --no-enum-size-warning      Don'\''t warn about objects with incompatible\n"
    375 		   "                                enum sizes\n"));
    376   fprintf (file, _("  --no-wchar-size-warning     Don'\''t warn about objects with incompatible"
    377 		   "                                wchar_t sizes\n"));
    378   fprintf (file, _("  --pic-veneer                Always generate PIC interworking veneers\n"));
    379   fprintf (file, _("\
    380    --stub-group-size=N   Maximum size of a group of input sections that can be\n\
    381                            handled by one stub section.  A negative value\n\
    382                            locates all stubs after their branches (with a\n\
    383                            group size of -N), while a positive value allows\n\
    384                            two groups of input sections, one before, and one\n\
    385                            after each stub section.  Values of +/-1 indicate\n\
    386                            the linker should choose suitable defaults.\n"
    387 		   ));
    388   fprintf (file, _("  --fix-cortex-a53-835769      Fix erratum 835769\n"));
    389   fprintf (file, _("  --fix-cortex-a53-843419      Fix erratum 843419\n"));
    390 '
    391 
    392 PARSE_AND_LIST_ARGS_CASES='
    393     case '\'p\'':
    394       /* Only here for backwards compatibility.  */
    395       break;
    396 
    397     case OPTION_NO_ENUM_SIZE_WARNING:
    398       no_enum_size_warning = 1;
    399       break;
    400 
    401     case OPTION_NO_WCHAR_SIZE_WARNING:
    402       no_wchar_size_warning = 1;
    403       break;
    404 
    405     case OPTION_PIC_VENEER:
    406       pic_veneer = 1;
    407       break;
    408 
    409     case OPTION_FIX_ERRATUM_835769:
    410       fix_erratum_835769 = 1;
    411       break;
    412 
    413     case OPTION_FIX_ERRATUM_843419:
    414       fix_erratum_843419 = 1;
    415       break;
    416 
    417     case OPTION_STUBGROUP_SIZE:
    418       {
    419 	const char *end;
    420 
    421         group_size = bfd_scan_vma (optarg, &end, 0);
    422         if (*end)
    423 	  einfo (_("%P%F: invalid number `%s'\''\n"), optarg);
    424       }
    425       break;
    426 '
    427 
    428 # We have our own before_allocation etc. functions, but they call
    429 # the standard routines, so give them a different name.
    430 LDEMUL_BEFORE_ALLOCATION=aarch64_elf_before_allocation
    431 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
    432 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=aarch64_elf_create_output_section_statements
    433 
    434 # Replace the elf before_parse function with our own.
    435 LDEMUL_BEFORE_PARSE=gld"${EMULATION_NAME}"_before_parse
    436 
    437 # Call the extra arm-elf function
    438 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
    439