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