1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2009-2018 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 elf32.em, and defines extra aarch64-elf
28 #include "elf/aarch64.h"
30 static int no_enum_size_warning = 0;
31 static int no_wchar_size_warning = 0;
32 static int pic_veneer = 0;
33 static int fix_erratum_835769 = 0;
34 static int fix_erratum_843419 = 0;
35 static int no_apply_dynamic_relocs = 0;
38 gld${EMULATION_NAME}_before_parse (void)
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`;
45 config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo TRUE ; else echo FALSE ; fi`;
46 link_info.check_relocs_after_open_input = TRUE;
47 link_info.relro = DEFAULT_LD_Z_RELRO;
51 aarch64_elf_before_allocation (void)
53 /* We should be able to set the size of the interworking stub section. We
54 can't do it until later if we have dynamic sections, though. */
55 if (! elf_hash_table (&link_info)->dynamic_sections_created)
57 /* Here we rummage through the found bfds to collect information. */
58 LANG_FOR_EACH_INPUT_STATEMENT (is)
60 /* Initialise mapping tables for code/data. */
61 bfd_elf${ELFSIZE}_aarch64_init_maps (is->the_bfd);
65 /* Call the standard elf routine. */
66 gld${EMULATION_NAME}_before_allocation ();
69 /* Fake input file for stubs. */
70 static lang_input_statement_type *stub_file;
72 /* Whether we need to call gldarm_layout_sections_again. */
73 static int need_laying_out = 0;
75 /* Maximum size of a group of input sections that can be handled by
76 one stub section. A value of +/-1 indicates the bfd back-end
77 should use a suitable default size. */
78 static bfd_signed_vma group_size = 1;
82 lang_statement_list_type add;
83 asection *input_section;
86 /* Traverse the linker tree to find the spot where the stub goes. */
89 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
91 lang_statement_union_type *l;
94 for (; (l = *lp) != NULL; lp = &l->header.next)
96 switch (l->header.type)
98 case lang_constructors_statement_enum:
99 ret = hook_in_stub (info, &constructor_list.head);
104 case lang_output_section_statement_enum:
105 ret = hook_in_stub (info,
106 &l->output_section_statement.children.head);
111 case lang_wild_statement_enum:
112 ret = hook_in_stub (info, &l->wild_statement.children.head);
117 case lang_group_statement_enum:
118 ret = hook_in_stub (info, &l->group_statement.children.head);
123 case lang_input_section_enum:
124 if (l->input_section.section == info->input_section)
126 /* We've found our section. Insert the stub immediately
127 after its associated input section. */
128 *(info->add.tail) = l->header.next;
129 l->header.next = info->add.head;
134 case lang_data_statement_enum:
135 case lang_reloc_statement_enum:
136 case lang_object_symbols_statement_enum:
137 case lang_output_statement_enum:
138 case lang_target_statement_enum:
139 case lang_input_statement_enum:
140 case lang_assignment_statement_enum:
141 case lang_padding_statement_enum:
142 case lang_address_statement_enum:
143 case lang_fill_statement_enum:
155 /* Call-back for elf${ELFSIZE}_aarch64_size_stubs. */
157 /* Create a new stub section, and arrange for it to be linked
158 immediately after INPUT_SECTION. */
161 elf${ELFSIZE}_aarch64_add_stub_section (const char *stub_sec_name,
162 asection *input_section)
166 asection *output_section;
167 lang_output_section_statement_type *os;
168 struct hook_stub_info info;
170 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
171 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
172 stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
173 stub_sec_name, flags);
174 if (stub_sec == NULL)
177 bfd_set_section_alignment (stub_file->the_bfd, stub_sec, 2);
179 output_section = input_section->output_section;
180 os = lang_output_section_get (output_section);
182 info.input_section = input_section;
183 lang_list_init (&info.add);
184 lang_add_section (&info.add, stub_sec, NULL, os);
186 if (info.add.head == NULL)
189 if (hook_in_stub (&info, &os->children.head))
193 einfo (_("%X%P: can not make stub section: %E\n"));
197 /* Another call-back for elf${ELFSIZE}_aarch64_size_stubs. */
200 gldaarch64_layout_sections_again (void)
202 /* If we have changed sizes of the stub sections, then we need
203 to recalculate all the section offsets. This may mean we need to
204 add even more stubs. */
205 gld${EMULATION_NAME}_map_segments (TRUE);
206 need_laying_out = -1;
210 build_section_lists (lang_statement_union_type *statement)
212 if (statement->header.type == lang_input_section_enum)
214 asection *i = statement->input_section.section;
216 if (!((lang_input_statement_type *) i->owner->usrdata)->flags.just_syms
217 && (i->flags & SEC_EXCLUDE) == 0
218 && i->output_section != NULL
219 && i->output_section->owner == link_info.output_bfd)
220 elf${ELFSIZE}_aarch64_next_input_section (& link_info, i);
225 gld${EMULATION_NAME}_after_allocation (void)
229 /* bfd_elf32_discard_info just plays with debugging sections,
230 ie. doesn't affect any code, so we can delay resizing the
231 sections. It's likely we'll resize everything in the process of
233 ret = bfd_elf_discard_info (link_info.output_bfd, & link_info);
236 einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
242 /* If generating a relocatable output file, then we don't
243 have to examine the relocs. */
244 if (stub_file != NULL && !bfd_link_relocatable (&link_info))
246 ret = elf${ELFSIZE}_aarch64_setup_section_lists (link_info.output_bfd,
252 einfo (_("%X%P: could not compute sections lists "
253 "for stub generation: %E\n"));
257 lang_for_each_statement (build_section_lists);
259 /* Call into the BFD backend to do the real work. */
260 if (! elf${ELFSIZE}_aarch64_size_stubs (link_info.output_bfd,
264 & elf${ELFSIZE}_aarch64_add_stub_section,
265 & gldaarch64_layout_sections_again))
267 einfo (_("%X%P: cannot size stub section: %E\n"));
273 if (need_laying_out != -1)
274 gld${EMULATION_NAME}_map_segments (need_laying_out);
278 gld${EMULATION_NAME}_finish (void)
280 if (!bfd_link_relocatable (&link_info))
282 /* Now build the linker stubs. */
283 if (stub_file->the_bfd->sections != NULL)
285 if (! elf${ELFSIZE}_aarch64_build_stubs (& link_info))
286 einfo (_("%X%P: can not build stubs: %E\n"));
293 /* This is a convenient point to tell BFD about target specific flags.
294 After the output has been created, but before inputs are read. */
296 aarch64_elf_create_output_section_statements (void)
298 if (strstr (bfd_get_target (link_info.output_bfd), "aarch64") == NULL)
300 /* The arm backend needs special fields in the output hash structure.
301 These will only be created if the output format is an arm format,
302 hence we do not support linking and changing output formats at the
303 same time. Use a link followed by objcopy to change output formats. */
304 einfo (_("%F%X%P: error: Cannot change output format "
305 "whilst linking AArch64 binaries.\n"));
309 bfd_elf${ELFSIZE}_aarch64_set_options (link_info.output_bfd, &link_info,
310 no_enum_size_warning,
311 no_wchar_size_warning,
313 fix_erratum_835769, fix_erratum_843419,
314 no_apply_dynamic_relocs);
316 stub_file = lang_add_input_file ("linker stubs",
317 lang_input_file_is_fake_enum,
319 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
320 if (stub_file->the_bfd == NULL
321 || ! bfd_set_arch_mach (stub_file->the_bfd,
322 bfd_get_arch (link_info.output_bfd),
323 bfd_get_mach (link_info.output_bfd)))
325 einfo (_("%X%P: can not create BFD %E\n"));
329 stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
330 ldlang_add_file (stub_file);
333 /* Avoid processing the fake stub_file in vercheck, stat_needed and
334 check_needed routines. */
336 static void (*real_func) (lang_input_statement_type *);
338 static void aarch64_for_each_input_file_wrapper (lang_input_statement_type *l)
345 aarch64_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
348 lang_for_each_input_file (&aarch64_for_each_input_file_wrapper);
351 #define lang_for_each_input_file aarch64_lang_for_each_input_file
355 # Define some shell vars to insert bits of code into the standard elf
356 # parse_args and list_options functions.
358 PARSE_AND_LIST_PROLOGUE='
359 #define OPTION_NO_ENUM_SIZE_WARNING 309
360 #define OPTION_PIC_VENEER 310
361 #define OPTION_STUBGROUP_SIZE 311
362 #define OPTION_NO_WCHAR_SIZE_WARNING 312
363 #define OPTION_FIX_ERRATUM_835769 313
364 #define OPTION_FIX_ERRATUM_843419 314
365 #define OPTION_NO_APPLY_DYNAMIC_RELOCS 315
368 PARSE_AND_LIST_SHORTOPTS=p
370 PARSE_AND_LIST_LONGOPTS='
371 { "no-pipeline-knowledge", no_argument, NULL, '\'p\''},
372 { "no-enum-size-warning", no_argument, NULL, OPTION_NO_ENUM_SIZE_WARNING},
373 { "pic-veneer", no_argument, NULL, OPTION_PIC_VENEER},
374 { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
375 { "no-wchar-size-warning", no_argument, NULL, OPTION_NO_WCHAR_SIZE_WARNING},
376 { "fix-cortex-a53-835769", no_argument, NULL, OPTION_FIX_ERRATUM_835769},
377 { "fix-cortex-a53-843419", no_argument, NULL, OPTION_FIX_ERRATUM_843419},
378 { "no-apply-dynamic-relocs", no_argument, NULL, OPTION_NO_APPLY_DYNAMIC_RELOCS},
381 PARSE_AND_LIST_OPTIONS='
382 fprintf (file, _(" --no-enum-size-warning Don'\''t warn about objects with incompatible\n"
384 fprintf (file, _(" --no-wchar-size-warning Don'\''t warn about objects with incompatible"
385 " wchar_t sizes\n"));
386 fprintf (file, _(" --pic-veneer Always generate PIC interworking veneers\n"));
388 --stub-group-size=N Maximum size of a group of input sections that can be\n\
389 handled by one stub section. A negative value\n\
390 locates all stubs after their branches (with a\n\
391 group size of -N), while a positive value allows\n\
392 two groups of input sections, one before, and one\n\
393 after each stub section. Values of +/-1 indicate\n\
394 the linker should choose suitable defaults.\n"
396 fprintf (file, _(" --fix-cortex-a53-835769 Fix erratum 835769\n"));
397 fprintf (file, _(" --fix-cortex-a53-843419 Fix erratum 843419\n"));
398 fprintf (file, _(" --no-apply-dynamic-relocs Do not apply link-time values for dynamic relocations\n"));
401 PARSE_AND_LIST_ARGS_CASES='
403 /* Only here for backwards compatibility. */
406 case OPTION_NO_ENUM_SIZE_WARNING:
407 no_enum_size_warning = 1;
410 case OPTION_NO_WCHAR_SIZE_WARNING:
411 no_wchar_size_warning = 1;
414 case OPTION_PIC_VENEER:
418 case OPTION_FIX_ERRATUM_835769:
419 fix_erratum_835769 = 1;
422 case OPTION_FIX_ERRATUM_843419:
423 fix_erratum_843419 = 1;
426 case OPTION_NO_APPLY_DYNAMIC_RELOCS:
427 no_apply_dynamic_relocs = 1;
430 case OPTION_STUBGROUP_SIZE:
434 group_size = bfd_scan_vma (optarg, &end, 0);
436 einfo (_("%P%F: invalid number `%s'\''\n"), optarg);
441 # We have our own before_allocation etc. functions, but they call
442 # the standard routines, so give them a different name.
443 LDEMUL_BEFORE_ALLOCATION=aarch64_elf_before_allocation
444 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
445 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=aarch64_elf_create_output_section_statements
447 # Replace the elf before_parse function with our own.
448 LDEMUL_BEFORE_PARSE=gld"${EMULATION_NAME}"_before_parse
450 # Call the extra arm-elf function
451 LDEMUL_FINISH=gld${EMULATION_NAME}_finish