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