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