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