Update year range in copyright notice of binutils files
[deliverable/binutils-gdb.git] / ld / emultempl / mipself.em
CommitLineData
73934d31 1# This shell script emits a C file. -*- C -*-
82704155 2# Copyright (C) 2004-2019 Free Software Foundation, Inc.
73934d31 3#
f96b4a7b 4# This file is part of the GNU Binutils.
73934d31
RS
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
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
73934d31
RS
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
f96b4a7b
NC
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.
73934d31 20
47275900
MR
21case ${target} in
22 *-*-*gnu*)
23 gnu_target=TRUE
24 ;;
25 *)
26 gnu_target=FALSE
27 ;;
28esac
29
92b93329 30fragment <<EOF
861fb55a
DJ
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 \
4dfe6ac6 39 && elf_object_id (bfd) == MIPS_ELF_DATA)
861fb55a
DJ
40
41/* Fake input file for stubs. */
42static lang_input_statement_type *stub_file;
43static bfd *stub_bfd;
44
833794fc 45static bfd_boolean insn32;
8b10b0b3 46static bfd_boolean ignore_branch_isa;
833794fc 47
73934d31
RS
48static void
49mips_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 {
d003af55 56 einfo (_("%X%P: .gnu.hash is incompatible with the MIPS ABI\n"));
73934d31
RS
57 link_info.emit_hash = TRUE;
58 link_info.emit_gnu_hash = FALSE;
59 }
5fe2850d 60 gld${EMULATION_NAME}_after_parse ();
73934d31 61}
861fb55a
DJ
62
63struct 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
71static bfd_boolean
72hook_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
143static asection *
144mips_add_stub_section (const char *stub_sec_name, asection *input_section,
145 asection *output_section)
146{
147 asection *stub_sec;
148 flagword flags;
861fb55a
DJ
149 lang_output_section_statement_type *os;
150 struct hook_stub_info info;
151
ba85c43e
NC
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
861fb55a
DJ
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 {
df5f2391 169 einfo (_("%F%P: can not create BFD: %E\n"));
861fb55a
DJ
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
24ef1aa7 188 os = lang_output_section_get (output_section);
861fb55a
DJ
189
190 /* Initialize a statement list that contains only the new statement. */
191 lang_list_init (&info.add);
b9c361e0 192 lang_add_section (&info.add, stub_sec, NULL, os);
861fb55a
DJ
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:
d003af55 202 einfo (_("%X%P: can not make stub section: %E\n"));
861fb55a
DJ
203 return NULL;
204}
205
206/* This is called before the input files are opened. */
207
208static void
209mips_create_output_section_statements (void)
210{
833794fc
MR
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))
47275900
MR
215 _bfd_mips_elf_linker_flags (&link_info, insn32, ignore_branch_isa,
216 ${gnu_target});
833794fc 217
861fb55a
DJ
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
224static void
225mips_before_allocation (void)
226{
e54cb31a
MR
227 if (is_mips_elf (link_info.output_bfd))
228 {
229 flagword flags;
861fb55a 230
e54cb31a
MR
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 }
861fb55a
DJ
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
244static void (*real_func) (lang_input_statement_type *);
245
246static void mips_for_each_input_file_wrapper (lang_input_statement_type *l)
247{
248 if (l != stub_file)
249 (*real_func) (l);
250}
251
252static void
253mips_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
73934d31
RS
261EOF
262
833794fc
MR
263# Define some shell vars to insert bits of code into the standard elf
264# parse_args and list_options functions.
265#
266PARSE_AND_LIST_PROLOGUE='
2f2da8fd
MR
267enum
268 {
269 OPTION_INSN32 = 301,
8b10b0b3
MR
270 OPTION_NO_INSN32,
271 OPTION_IGNORE_BRANCH_ISA,
272 OPTION_NO_IGNORE_BRANCH_ISA
2f2da8fd 273 };
833794fc
MR
274'
275
276PARSE_AND_LIST_LONGOPTS='
277 { "insn32", no_argument, NULL, OPTION_INSN32 },
278 { "no-insn32", no_argument, NULL, OPTION_NO_INSN32 },
8b10b0b3
MR
279 { "ignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA },
280 { "no-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA },
833794fc
MR
281'
282
283PARSE_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 ));
8b10b0b3
MR
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 ));
833794fc
MR
298'
299
300PARSE_AND_LIST_ARGS_CASES='
301 case OPTION_INSN32:
302 insn32 = TRUE;
303 break;
304
305 case OPTION_NO_INSN32:
306 insn32 = FALSE;
307 break;
8b10b0b3
MR
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;
833794fc
MR
316'
317
73934d31 318LDEMUL_AFTER_PARSE=mips_after_parse
861fb55a
DJ
319LDEMUL_BEFORE_ALLOCATION=mips_before_allocation
320LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=mips_create_output_section_statements
This page took 0.522478 seconds and 4 git commands to generate.