1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2009-2025 Free Software Foundation, Inc.
3 # Contributed by ARM Ltd.
5 # This file is part of the GNU Binutils.
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.
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.
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/>.
22 # This file is sourced from elf.em, and defines extra aarch64-elf
28 #include "elf/aarch64.h"
29 #include "elfxx-aarch64.h"
31 static int no_enum_size_warning = 0;
32 static int no_wchar_size_warning = 0;
33 static int pic_veneer = 0;
34 static int fix_erratum_835769 = 0;
35 static erratum_84319_opts fix_erratum_843419 = ERRAT_NONE;
36 static int no_apply_dynamic_relocs = 0;
37 static aarch64_protection_opts sw_protections = {
38 .plt_type = PLT_NORMAL,
39 .bti_report = MARKING_WARN,
40 .gcs_type = GCS_IMPLICIT,
41 .gcs_report = MARKING_WARN,
44 #define COMPILE_TIME_STRLEN(s) \
48 gld${EMULATION_NAME}_before_parse (void)
50 #ifndef TARGET_ /* I.e., if not generic. */
51 ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
52 #endif /* not TARGET_ */
53 input_flags.dynamic = ${DYNAMIC_LINK-true};
54 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
55 config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo true ; else echo false ; fi`;
56 link_info.check_relocs_after_open_input = true;
58 if test -n "$COMMONPAGESIZE"; then
60 link_info.relro = DEFAULT_LD_Z_RELRO;
64 link_info.separate_code = DEFAULT_LD_Z_SEPARATE_CODE;
65 link_info.warn_execstack = DEFAULT_LD_WARN_EXECSTACK;
66 link_info.no_warn_rwx_segments = ! DEFAULT_LD_WARN_RWX_SEGMENTS;
67 link_info.default_execstack = DEFAULT_LD_EXECSTACK;
71 aarch64_elf_before_allocation (void)
73 /* We should be able to set the size of the interworking stub section. We
74 can't do it until later if we have dynamic sections, though. */
75 if (! elf_hash_table (&link_info)->dynamic_sections_created)
77 /* Here we rummage through the found bfds to collect information. */
78 LANG_FOR_EACH_INPUT_STATEMENT (is)
80 /* Initialise mapping tables for code/data. */
81 bfd_elf${ELFSIZE}_aarch64_init_maps (is->the_bfd);
85 /* Call the standard elf routine. */
86 gld${EMULATION_NAME}_before_allocation ();
89 /* Fake input file for stubs. */
90 static lang_input_statement_type *stub_file;
92 /* Whether we need to call gldarm_layout_sections_again. */
93 static int need_laying_out = 0;
95 /* Maximum size of a group of input sections that can be handled by
96 one stub section. A value of +/-1 indicates the bfd back-end
97 should use a suitable default size. */
98 static bfd_signed_vma group_size = 1;
100 struct hook_stub_info
102 lang_statement_list_type add;
103 asection *input_section;
106 /* Traverse the linker tree to find the spot where the stub goes. */
109 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
111 lang_statement_union_type *l;
114 for (; (l = *lp) != NULL; lp = &l->header.next)
116 switch (l->header.type)
118 case lang_constructors_statement_enum:
119 ret = hook_in_stub (info, &constructor_list.head);
124 case lang_output_section_statement_enum:
125 ret = hook_in_stub (info,
126 &l->output_section_statement.children.head);
131 case lang_wild_statement_enum:
132 ret = hook_in_stub (info, &l->wild_statement.children.head);
137 case lang_group_statement_enum:
138 ret = hook_in_stub (info, &l->group_statement.children.head);
143 case lang_input_section_enum:
144 if (l->input_section.section == info->input_section)
146 /* We've found our section. Insert the stub immediately
147 after its associated input section. */
148 *(info->add.tail) = l->header.next;
149 l->header.next = info->add.head;
154 case lang_data_statement_enum:
155 case lang_reloc_statement_enum:
156 case lang_object_symbols_statement_enum:
157 case lang_output_statement_enum:
158 case lang_target_statement_enum:
159 case lang_input_statement_enum:
160 case lang_assignment_statement_enum:
161 case lang_padding_statement_enum:
162 case lang_address_statement_enum:
163 case lang_fill_statement_enum:
175 /* Call-back for elf${ELFSIZE}_aarch64_size_stubs. */
177 /* Create a new stub section, and arrange for it to be linked
178 immediately after INPUT_SECTION. */
181 elf${ELFSIZE}_aarch64_add_stub_section (const char *stub_sec_name,
182 asection *input_section)
186 asection *output_section;
187 lang_output_section_statement_type *os;
188 struct hook_stub_info info;
190 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
191 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
192 stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
193 stub_sec_name, flags);
194 if (stub_sec == NULL)
197 /* Long branch stubs contain a 64-bit address, so the section requires
199 bfd_set_section_alignment (stub_sec, 3);
201 output_section = input_section->output_section;
202 os = lang_output_section_get (output_section);
204 info.input_section = input_section;
205 lang_list_init (&info.add);
206 lang_add_section (&info.add, stub_sec, NULL, NULL, os);
208 if (info.add.head == NULL)
211 if (hook_in_stub (&info, &os->children.head))
215 einfo (_("%X%P: can not make stub section: %E\n"));
219 /* Another call-back for elf${ELFSIZE}_aarch64_size_stubs. */
222 gldaarch64_layout_sections_again (void)
224 /* If we have changed sizes of the stub sections, then we need
225 to recalculate all the section offsets. This may mean we need to
226 add even more stubs. */
227 ldelf_map_segments (true);
228 need_laying_out = -1;
232 build_section_lists (lang_statement_union_type *statement)
234 if (statement->header.type == lang_input_section_enum)
236 asection *i = statement->input_section.section;
238 if (!bfd_input_just_syms (i->owner)
239 && (i->flags & SEC_EXCLUDE) == 0
240 && i->output_section != NULL
241 && i->output_section->owner == link_info.output_bfd)
242 elf${ELFSIZE}_aarch64_next_input_section (& link_info, i);
247 gld${EMULATION_NAME}_after_allocation (void)
251 /* bfd_elf32_discard_info just plays with debugging sections,
252 ie. doesn't affect any code, so we can delay resizing the
253 sections. It's likely we'll resize everything in the process of
255 ret = bfd_elf_discard_info (link_info.output_bfd, & link_info);
258 einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
264 /* If generating a relocatable output file, then we don't
265 have to examine the relocs. */
266 if (stub_file != NULL && !bfd_link_relocatable (&link_info))
268 ret = elf${ELFSIZE}_aarch64_setup_section_lists (link_info.output_bfd,
274 einfo (_("%X%P: could not compute sections lists "
275 "for stub generation: %E\n"));
279 lang_for_each_statement (build_section_lists);
281 /* Call into the BFD backend to do the real work. */
282 if (! elf${ELFSIZE}_aarch64_size_stubs (link_info.output_bfd,
286 & elf${ELFSIZE}_aarch64_add_stub_section,
287 & gldaarch64_layout_sections_again))
289 einfo (_("%X%P: can not size stub section: %E\n"));
295 if (need_laying_out != -1)
296 ldelf_map_segments (need_laying_out);
300 gld${EMULATION_NAME}_finish (void)
302 if (!bfd_link_relocatable (&link_info))
304 /* Now build the linker stubs. */
305 if (stub_file->the_bfd->sections != NULL)
307 if (! elf${ELFSIZE}_aarch64_build_stubs (& link_info))
308 einfo (_("%X%P: can not build stubs: %E\n"));
315 /* This is a convenient point to tell BFD about target specific flags.
316 After the output has been created, but before inputs are read. */
318 aarch64_elf_create_output_section_statements (void)
320 if (strstr (bfd_get_target (link_info.output_bfd), "aarch64") == NULL)
322 /* The AArch64 backend needs special fields in the output hash structure.
323 These will only be created if the output format is an AArch64 format,
324 hence we do not support linking and changing output formats at the
325 same time. Use a link followed by objcopy to change output formats. */
326 einfo (_("%F%P: error: cannot change output format "
327 "whilst linking %s binaries\n"), "AArch64");
331 bfd_elf${ELFSIZE}_aarch64_set_options (link_info.output_bfd, &link_info,
332 no_enum_size_warning,
333 no_wchar_size_warning,
335 fix_erratum_835769, fix_erratum_843419,
336 no_apply_dynamic_relocs,
339 stub_file = lang_add_input_file ("linker stubs",
340 lang_input_file_is_fake_enum,
342 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
343 if (stub_file->the_bfd == NULL
344 || ! bfd_set_arch_mach (stub_file->the_bfd,
345 bfd_get_arch (link_info.output_bfd),
346 bfd_get_mach (link_info.output_bfd)))
348 einfo (_("%F%P: can not create BFD: %E\n"));
352 stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
353 ldlang_add_file (stub_file);
357 aarch64_parse_feature_report_option (const char *_optarg,
358 const char *report_opt,
359 const size_t report_opt_len,
360 aarch64_feature_marking_report *level)
362 if (strncmp (_optarg, report_opt, report_opt_len) != 0)
365 if (strlen (_optarg) == report_opt_len
366 || strcmp (_optarg + report_opt_len, "=warning") == 0)
367 *level = MARKING_WARN;
368 else if (strcmp (_optarg + report_opt_len, "=none") == 0)
369 *level = MARKING_NONE;
370 else if (strcmp (_optarg + report_opt_len, "=error") == 0)
371 *level = MARKING_ERROR;
373 einfo (_("%X%P: error: unrecognized value '-z %s'\n"), _optarg);
379 aarch64_parse_bti_report_option (const char *_optarg)
381 #define BTI_REPORT "bti-report"
382 #define BTI_REPORT_LEN COMPILE_TIME_STRLEN (BTI_REPORT)
384 return aarch64_parse_feature_report_option (_optarg, BTI_REPORT,
385 BTI_REPORT_LEN, &sw_protections.bti_report);
388 #undef BTI_REPORT_LEN
392 aarch64_parse_gcs_report_option (const char *_optarg)
394 #define GCS_REPORT "gcs-report"
395 #define GCS_REPORT_LEN COMPILE_TIME_STRLEN (GCS_REPORT)
397 return aarch64_parse_feature_report_option (_optarg, GCS_REPORT,
398 GCS_REPORT_LEN, &sw_protections.gcs_report);
401 #undef GCS_REPORT_LEN
405 aarch64_parse_gcs_option (const char *_optarg)
408 #define GCS_LEN COMPILE_TIME_STRLEN (GCS)
410 if (strncmp (_optarg, GCS, GCS_LEN) != 0)
413 if (strcmp (_optarg + GCS_LEN, "=always") == 0)
414 sw_protections.gcs_type = GCS_ALWAYS;
415 else if (strcmp (_optarg + GCS_LEN, "=never") == 0)
416 sw_protections.gcs_type = GCS_NEVER;
417 else if (strcmp (_optarg + GCS_LEN, "=implicit") == 0)
418 sw_protections.gcs_type = GCS_IMPLICIT;
420 einfo (_("%X%P: error: unrecognized value '-z %s'\n"), _optarg);
429 # Define some shell vars to insert bits of code into the standard elf
430 # parse_args and list_options functions.
432 PARSE_AND_LIST_SHORTOPTS=p
434 PARSE_AND_LIST_LONGOPTS='
435 { "no-pipeline-knowledge", no_argument, NULL, '\'p\''},
436 { "no-enum-size-warning", no_argument, NULL, OPTION_NO_ENUM_SIZE_WARNING},
437 { "pic-veneer", no_argument, NULL, OPTION_PIC_VENEER},
438 { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
439 { "no-wchar-size-warning", no_argument, NULL, OPTION_NO_WCHAR_SIZE_WARNING},
440 { "fix-cortex-a53-835769", no_argument, NULL, OPTION_FIX_ERRATUM_835769},
441 { "fix-cortex-a53-843419", optional_argument, NULL, OPTION_FIX_ERRATUM_843419},
442 { "no-apply-dynamic-relocs", no_argument, NULL, OPTION_NO_APPLY_DYNAMIC_RELOCS},
445 PARSE_AND_LIST_OPTIONS='
446 fprintf (file, _(" --no-enum-size-warning Don'\''t warn about objects with incompatible\n"
448 fprintf (file, _(" --no-wchar-size-warning Don'\''t warn about objects with incompatible\n"
449 " wchar_t sizes\n"));
450 fprintf (file, _(" --pic-veneer Always generate PIC interworking veneers\n"));
452 --stub-group-size=N Maximum size of a group of input sections that\n\
453 can be handled by one stub section. A negative\n\
454 value locates all stubs after their branches\n\
455 (with a group size of -N), while a positive\n\
456 value allows two groups of input sections, one\n\
457 before, and one after each stub section.\n\
458 Values of +/-1 indicate the linker should\n\
459 choose suitable defaults.\n"));
460 fprintf (file, _(" --fix-cortex-a53-835769 Fix erratum 835769\n"));
462 --fix-cortex-a53-843419[=full|adr|adrp] Fix erratum 843419 and optionally specify which workaround to use.\n\
463 full (default): Use both ADRP and ADR workaround, this will \n\
464 increase the size of your binaries.\n\
465 adr: Only use the ADR workaround, this will not cause any increase\n\
466 in binary size but linking will fail if the referenced address is\n\
467 out of range of an ADR instruction. This will remove the need of using\n\
468 a veneer and results in both performance and size benefits.\n\
469 adrp: Use only the ADRP workaround, this will never rewrite your ADRP\n\
470 instruction into an ADR. As such the workaround will always use a\n\
471 veneer and this will give you both a performance and size overhead.\n"));
472 fprintf (file, _(" --no-apply-dynamic-relocs Do not apply link-time values for dynamic relocations\n"));
474 -z force-bti Turn on Branch Target Identification mechanism and generate PLTs with BTI.\n\
475 Generate warnings for missing BTI markings on inputs\n"));
477 -z bti-report[=none|warning|error] Emit warning/error on mismatch of BTI marking between input objects and ouput.\n\
478 none: Does not emit any warning/error messages.\n\
479 warning (default): Emit warning when the input objects are missing BTI markings\n\
480 and output has BTI marking.\n\
481 error: Emit error when the input objects are missing BTI markings\n\
482 and output has BTI marking.\n"));
484 -z pac-plt Protect PLTs with Pointer Authentication.\n"));
486 -z gcs=[always|never|implicit] Controls whether the output supports the Guarded Control Stack (GCS) mechanism.\n\
487 implicit (default if '\''-z gcs'\'' is omitted): deduce GCS from input objects.\n\
488 always: always marks the output with GCS.\n\
489 never: never marks the output with GCS.\n"));
491 -z gcs-report[=none|warning|error] Emit warning/error on mismatch of GCS marking between input objects and ouput.\n\
492 none: Does not emit any warning/error messages.\n\
493 warning (default): Emit warning when the input objects are missing GCS markings\n\
494 and output have GCS marking.\n\
495 error: Emit error when the input objects are missing GCS markings\n\
496 and output have GCS marking.\n"));
499 PARSE_AND_LIST_ARGS_CASE_Z_AARCH64='
500 else if (strcmp (optarg, "force-bti") == 0)
501 sw_protections.plt_type |= PLT_BTI;
502 else if (aarch64_parse_bti_report_option (optarg))
504 else if (strcmp (optarg, "pac-plt") == 0)
505 sw_protections.plt_type |= PLT_PAC;
506 else if (aarch64_parse_gcs_report_option (optarg))
508 else if (aarch64_parse_gcs_option (optarg))
511 PARSE_AND_LIST_ARGS_CASE_Z="$PARSE_AND_LIST_ARGS_CASE_Z $PARSE_AND_LIST_ARGS_CASE_Z_AARCH64"
513 PARSE_AND_LIST_ARGS_CASES='
515 /* Only here for backwards compatibility. */
518 case OPTION_NO_ENUM_SIZE_WARNING:
519 no_enum_size_warning = 1;
522 case OPTION_NO_WCHAR_SIZE_WARNING:
523 no_wchar_size_warning = 1;
526 case OPTION_PIC_VENEER:
530 case OPTION_FIX_ERRATUM_835769:
531 fix_erratum_835769 = 1;
534 case OPTION_FIX_ERRATUM_843419:
535 fix_erratum_843419 = ERRAT_ADR | ERRAT_ADRP;
536 if (optarg && *optarg)
538 if (strcmp ("full", optarg) == 0)
539 fix_erratum_843419 = ERRAT_ADR | ERRAT_ADRP;
540 else if (strcmp ("adrp", optarg) == 0)
541 fix_erratum_843419 = ERRAT_ADRP;
542 else if (strcmp ("adr", optarg) == 0)
543 fix_erratum_843419 = ERRAT_ADR;
545 einfo (_("%P: error: unrecognized option for "
546 "--fix-cortex-a53-843419: %s\n"), optarg);
550 case OPTION_NO_APPLY_DYNAMIC_RELOCS:
551 no_apply_dynamic_relocs = 1;
554 case OPTION_STUBGROUP_SIZE:
558 group_size = bfd_scan_vma (optarg, &end, 0);
560 einfo (_("%F%P: invalid number `%s'\''\n"), optarg);
565 # We have our own before_allocation etc. functions, but they call
566 # the standard routines, so give them a different name.
567 LDEMUL_BEFORE_ALLOCATION=aarch64_elf_before_allocation
568 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
569 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=aarch64_elf_create_output_section_statements
571 # Replace the elf before_parse function with our own.
572 LDEMUL_BEFORE_PARSE=gld"${EMULATION_NAME}"_before_parse
574 # Call the extra arm-elf function
575 LDEMUL_FINISH=gld${EMULATION_NAME}_finish