Home | History | Annotate | Download | only in emultempl
      1 # This shell script emits a C file. -*- C -*-
      2 #   Copyright (C) 2004-2016 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 fragment <<EOF
     22 
     23 #include "ldctor.h"
     24 #include "elf/mips.h"
     25 #include "elfxx-mips.h"
     26 
     27 #define is_mips_elf(bfd)				\
     28   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
     29    && elf_tdata (bfd) != NULL				\
     30    && elf_object_id (bfd) == MIPS_ELF_DATA)
     31 
     32 /* Fake input file for stubs.  */
     33 static lang_input_statement_type *stub_file;
     34 static bfd *stub_bfd;
     35 
     36 static bfd_boolean insn32;
     37 
     38 static void
     39 mips_after_parse (void)
     40 {
     41   /* .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
     42      ways.  .gnu.hash needs symbols to be grouped by hash code whereas the
     43      MIPS ABI requires a mapping between the GOT and the symbol table.  */
     44   if (link_info.emit_gnu_hash)
     45     {
     46       einfo ("%X%P: .gnu.hash is incompatible with the MIPS ABI\n");
     47       link_info.emit_hash = TRUE;
     48       link_info.emit_gnu_hash = FALSE;
     49     }
     50   gld${EMULATION_NAME}_after_parse ();
     51 }
     52 
     53 struct hook_stub_info
     54 {
     55   lang_statement_list_type add;
     56   asection *input_section;
     57 };
     58 
     59 /* Traverse the linker tree to find the spot where the stub goes.  */
     60 
     61 static bfd_boolean
     62 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
     63 {
     64   lang_statement_union_type *l;
     65   bfd_boolean ret;
     66 
     67   for (; (l = *lp) != NULL; lp = &l->header.next)
     68     {
     69       switch (l->header.type)
     70 	{
     71 	case lang_constructors_statement_enum:
     72 	  ret = hook_in_stub (info, &constructor_list.head);
     73 	  if (ret)
     74 	    return ret;
     75 	  break;
     76 
     77 	case lang_output_section_statement_enum:
     78 	  ret = hook_in_stub (info,
     79 			      &l->output_section_statement.children.head);
     80 	  if (ret)
     81 	    return ret;
     82 	  break;
     83 
     84 	case lang_wild_statement_enum:
     85 	  ret = hook_in_stub (info, &l->wild_statement.children.head);
     86 	  if (ret)
     87 	    return ret;
     88 	  break;
     89 
     90 	case lang_group_statement_enum:
     91 	  ret = hook_in_stub (info, &l->group_statement.children.head);
     92 	  if (ret)
     93 	    return ret;
     94 	  break;
     95 
     96 	case lang_input_section_enum:
     97 	  if (info->input_section == NULL
     98 	      || l->input_section.section == info->input_section)
     99 	    {
    100 	      /* We've found our section.  Insert the stub immediately
    101 		 before its associated input section.  */
    102 	      *lp = info->add.head;
    103 	      *(info->add.tail) = l;
    104 	      return TRUE;
    105 	    }
    106 	  break;
    107 
    108 	case lang_data_statement_enum:
    109 	case lang_reloc_statement_enum:
    110 	case lang_object_symbols_statement_enum:
    111 	case lang_output_statement_enum:
    112 	case lang_target_statement_enum:
    113 	case lang_input_statement_enum:
    114 	case lang_assignment_statement_enum:
    115 	case lang_padding_statement_enum:
    116 	case lang_address_statement_enum:
    117 	case lang_fill_statement_enum:
    118 	  break;
    119 
    120 	default:
    121 	  FAIL ();
    122 	  break;
    123 	}
    124     }
    125   return FALSE;
    126 }
    127 
    128 /* Create a new stub section called STUB_SEC_NAME and arrange for it to
    129    be linked in OUTPUT_SECTION.  The section should go at the beginning of
    130    OUTPUT_SECTION if INPUT_SECTION is null, otherwise it must go immediately
    131    before INPUT_SECTION.  */
    132 
    133 static asection *
    134 mips_add_stub_section (const char *stub_sec_name, asection *input_section,
    135 		       asection *output_section)
    136 {
    137   asection *stub_sec;
    138   flagword flags;
    139   lang_output_section_statement_type *os;
    140   struct hook_stub_info info;
    141 
    142   /* PR 12845: If the input section has been garbage collected it will
    143      not have its output section set to *ABS*.  */
    144   if (bfd_is_abs_section (output_section))
    145     return NULL;
    146 
    147   /* Create the stub file, if we haven't already.  */
    148   if (stub_file == NULL)
    149     {
    150       stub_file = lang_add_input_file ("linker stubs",
    151 				       lang_input_file_is_fake_enum,
    152 				       NULL);
    153       stub_bfd = bfd_create ("linker stubs", link_info.output_bfd);
    154       if (stub_bfd == NULL
    155 	  || !bfd_set_arch_mach (stub_bfd,
    156 				 bfd_get_arch (link_info.output_bfd),
    157 				 bfd_get_mach (link_info.output_bfd)))
    158 	{
    159 	  einfo ("%F%P: can not create BFD %E\n");
    160 	  return NULL;
    161 	}
    162       stub_bfd->flags |= BFD_LINKER_CREATED;
    163       stub_file->the_bfd = stub_bfd;
    164       ldlang_add_file (stub_file);
    165     }
    166 
    167   /* Create the section.  */
    168   stub_sec = bfd_make_section_anyway (stub_bfd, stub_sec_name);
    169   if (stub_sec == NULL)
    170     goto err_ret;
    171 
    172   /* Set the flags.  */
    173   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
    174 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP);
    175   if (!bfd_set_section_flags (stub_bfd, stub_sec, flags))
    176     goto err_ret;
    177 
    178   os = lang_output_section_get (output_section);
    179 
    180   /* Initialize a statement list that contains only the new statement.  */
    181   lang_list_init (&info.add);
    182   lang_add_section (&info.add, stub_sec, NULL, os);
    183   if (info.add.head == NULL)
    184     goto err_ret;
    185 
    186   /* Insert the new statement in the appropriate place.  */
    187   info.input_section = input_section;
    188   if (hook_in_stub (&info, &os->children.head))
    189     return stub_sec;
    190 
    191  err_ret:
    192   einfo ("%X%P: can not make stub section: %E\n");
    193   return NULL;
    194 }
    195 
    196 /* This is called before the input files are opened.  */
    197 
    198 static void
    199 mips_create_output_section_statements (void)
    200 {
    201   struct elf_link_hash_table *htab;
    202 
    203   htab = elf_hash_table (&link_info);
    204   if (is_elf_hash_table (htab) && is_mips_elf (link_info.output_bfd))
    205     _bfd_mips_elf_insn32 (&link_info, insn32);
    206 
    207   if (is_mips_elf (link_info.output_bfd))
    208     _bfd_mips_elf_init_stubs (&link_info, mips_add_stub_section);
    209 }
    210 
    211 /* This is called after we have merged the private data of the input bfds.  */
    212 
    213 static void
    214 mips_before_allocation (void)
    215 {
    216   flagword flags;
    217 
    218   flags = elf_elfheader (link_info.output_bfd)->e_flags;
    219   if (!bfd_link_pic (&link_info)
    220       && !link_info.nocopyreloc
    221       && (flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
    222     _bfd_mips_elf_use_plts_and_copy_relocs (&link_info);
    223 
    224   gld${EMULATION_NAME}_before_allocation ();
    225 }
    226 
    227 /* Avoid processing the fake stub_file in vercheck, stat_needed and
    228    check_needed routines.  */
    229 
    230 static void (*real_func) (lang_input_statement_type *);
    231 
    232 static void mips_for_each_input_file_wrapper (lang_input_statement_type *l)
    233 {
    234   if (l != stub_file)
    235     (*real_func) (l);
    236 }
    237 
    238 static void
    239 mips_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
    240 {
    241   real_func = func;
    242   lang_for_each_input_file (&mips_for_each_input_file_wrapper);
    243 }
    244 
    245 #define lang_for_each_input_file mips_lang_for_each_input_file
    246 
    247 EOF
    248 
    249 # Define some shell vars to insert bits of code into the standard elf
    250 # parse_args and list_options functions.
    251 #
    252 PARSE_AND_LIST_PROLOGUE='
    253 enum
    254   {
    255     OPTION_INSN32 = 301,
    256     OPTION_NO_INSN32
    257   };
    258 '
    259 
    260 PARSE_AND_LIST_LONGOPTS='
    261   { "insn32", no_argument, NULL, OPTION_INSN32 },
    262   { "no-insn32", no_argument, NULL, OPTION_NO_INSN32 },
    263 '
    264 
    265 PARSE_AND_LIST_OPTIONS='
    266   fprintf (file, _("\
    267   --insn32                    Only generate 32-bit microMIPS instructions\n"
    268 		   ));
    269   fprintf (file, _("\
    270   --no-insn32                 Generate all microMIPS instructions\n"
    271 		   ));
    272 '
    273 
    274 PARSE_AND_LIST_ARGS_CASES='
    275     case OPTION_INSN32:
    276       insn32 = TRUE;
    277       break;
    278 
    279     case OPTION_NO_INSN32:
    280       insn32 = FALSE;
    281       break;
    282 '
    283 
    284 LDEMUL_AFTER_PARSE=mips_after_parse
    285 LDEMUL_BEFORE_ALLOCATION=mips_before_allocation
    286 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=mips_create_output_section_statements
    287