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