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