Add support for 64-bit ARM architecture: AArch64
[platform/upstream/binutils.git] / ld / emultempl / aarch64elf.em
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright 2009-2012  Free Software Foundation, Inc.
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 #
25 fragment <<EOF
26
27 #include "ldctor.h"
28 #include "elf/aarch64.h"
29
30 static int no_enum_size_warning = 0;
31 static int no_wchar_size_warning = 0;
32 static int pic_veneer = 0;
33
34 static void
35 gld${EMULATION_NAME}_before_parse (void)
36 {
37 #ifndef TARGET_                 /* I.e., if not generic.  */
38   ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
39 #endif /* not TARGET_ */
40   input_flags.dynamic = ${DYNAMIC_LINK-TRUE};
41   config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
42 }
43
44 static void
45 aarch64_elf_before_allocation (void)
46 {
47   /* We should be able to set the size of the interworking stub section.  We
48      can't do it until later if we have dynamic sections, though.  */
49   if (! elf_hash_table (&link_info)->dynamic_sections_created)
50     {
51       /* Here we rummage through the found bfds to collect information.  */
52       LANG_FOR_EACH_INPUT_STATEMENT (is)
53         {
54           /* Initialise mapping tables for code/data.  */
55           bfd_elf64_aarch64_init_maps (is->the_bfd);
56         }
57     }
58
59   /* Call the standard elf routine.  */
60   gld${EMULATION_NAME}_before_allocation ();
61 }
62
63 /* Fake input file for stubs.  */
64 static lang_input_statement_type *stub_file;
65
66 /* Whether we need to call gldarm_layout_sections_again.  */
67 static int need_laying_out = 0;
68
69 /* Maximum size of a group of input sections that can be handled by
70    one stub section.  A value of +/-1 indicates the bfd back-end
71    should use a suitable default size.  */
72 static bfd_signed_vma group_size = 1;
73
74 struct hook_stub_info
75 {
76   lang_statement_list_type add;
77   asection *input_section;
78 };
79
80 /* Traverse the linker tree to find the spot where the stub goes.  */
81
82 static bfd_boolean
83 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
84 {
85   lang_statement_union_type *l;
86   bfd_boolean ret;
87
88   for (; (l = *lp) != NULL; lp = &l->header.next)
89     {
90       switch (l->header.type)
91         {
92         case lang_constructors_statement_enum:
93           ret = hook_in_stub (info, &constructor_list.head);
94           if (ret)
95             return ret;
96           break;
97
98         case lang_output_section_statement_enum:
99           ret = hook_in_stub (info,
100                               &l->output_section_statement.children.head);
101           if (ret)
102             return ret;
103           break;
104
105         case lang_wild_statement_enum:
106           ret = hook_in_stub (info, &l->wild_statement.children.head);
107           if (ret)
108             return ret;
109           break;
110
111         case lang_group_statement_enum:
112           ret = hook_in_stub (info, &l->group_statement.children.head);
113           if (ret)
114             return ret;
115           break;
116
117         case lang_input_section_enum:
118           if (l->input_section.section == info->input_section)
119             {
120               /* We've found our section.  Insert the stub immediately
121                  after its associated input section.  */
122               *(info->add.tail) = l->header.next;
123               l->header.next = info->add.head;
124               return TRUE;
125             }
126           break;
127
128         case lang_data_statement_enum:
129         case lang_reloc_statement_enum:
130         case lang_object_symbols_statement_enum:
131         case lang_output_statement_enum:
132         case lang_target_statement_enum:
133         case lang_input_statement_enum:
134         case lang_assignment_statement_enum:
135         case lang_padding_statement_enum:
136         case lang_address_statement_enum:
137         case lang_fill_statement_enum:
138           break;
139
140         default:
141           FAIL ();
142           break;
143         }
144     }
145   return FALSE;
146 }
147
148
149 /* Call-back for elf64_aarch64_size_stubs.  */
150
151 /* Create a new stub section, and arrange for it to be linked
152    immediately after INPUT_SECTION.  */
153
154 static asection *
155 elf64_aarch64_add_stub_section (const char *stub_sec_name,
156                                 asection *input_section)
157 {
158   asection *stub_sec;
159   flagword flags;
160   asection *output_section;
161   const char *secname;
162   lang_output_section_statement_type *os;
163   struct hook_stub_info info;
164
165   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
166            | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
167   stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
168                                                  stub_sec_name, flags);
169   if (stub_sec == NULL)
170     goto err_ret;
171
172   bfd_set_section_alignment (stub_file->the_bfd, stub_sec, 3);
173
174   output_section = input_section->output_section;
175   secname = bfd_get_section_name (output_section->owner, output_section);
176   os = lang_output_section_find (secname);
177
178   info.input_section = input_section;
179   lang_list_init (&info.add);
180   lang_add_section (&info.add, stub_sec, NULL, os);
181
182   if (info.add.head == NULL)
183     goto err_ret;
184
185   if (hook_in_stub (&info, &os->children.head))
186     return stub_sec;
187
188  err_ret:
189   einfo ("%X%P: can not make stub section: %E\n");
190   return NULL;
191 }
192
193 /* Another call-back for elf_arm_size_stubs.  */
194
195 static void
196 gldaarch64_layout_sections_again (void)
197 {
198   /* If we have changed sizes of the stub sections, then we need
199      to recalculate all the section offsets.  This may mean we need to
200      add even more stubs.  */
201   gld${EMULATION_NAME}_map_segments (TRUE);
202   need_laying_out = -1;
203 }
204
205 static void
206 build_section_lists (lang_statement_union_type *statement)
207 {
208   if (statement->header.type == lang_input_section_enum)
209     {
210       asection *i = statement->input_section.section;
211
212       if (!((lang_input_statement_type *) i->owner->usrdata)->flags.just_syms
213           && (i->flags & SEC_EXCLUDE) == 0
214           && i->output_section != NULL
215           && i->output_section->owner == link_info.output_bfd)
216         elf64_aarch64_next_input_section (& link_info, i);
217     }
218 }
219
220 static void
221 gld${EMULATION_NAME}_after_allocation (void)
222 {
223   /* bfd_elf32_discard_info just plays with debugging sections,
224      ie. doesn't affect any code, so we can delay resizing the
225      sections.  It's likely we'll resize everything in the process of
226      adding stubs.  */
227   if (bfd_elf_discard_info (link_info.output_bfd, & link_info))
228     need_laying_out = 1;
229
230   /* If generating a relocatable output file, then we don't
231      have to examine the relocs.  */
232   if (stub_file != NULL && !link_info.relocatable)
233     {
234       int ret = elf64_aarch64_setup_section_lists (link_info.output_bfd,
235                                                    & link_info);
236
237       if (ret != 0)
238         {
239           if (ret < 0)
240             {
241               einfo ("%X%P: could not compute sections lists for stub generation: %E\n");
242               return;
243             }
244
245           lang_for_each_statement (build_section_lists);
246
247           /* Call into the BFD backend to do the real work.  */
248           if (! elf64_aarch64_size_stubs (link_info.output_bfd,
249                                           stub_file->the_bfd,
250                                           & link_info,
251                                           group_size,
252                                           & elf64_aarch64_add_stub_section,
253                                           & gldaarch64_layout_sections_again))
254             {
255               einfo ("%X%P: cannot size stub section: %E\n");
256               return;
257             }
258         }
259     }
260
261   if (need_laying_out != -1)
262     gld${EMULATION_NAME}_map_segments (need_laying_out);
263 }
264
265 static void
266 gld${EMULATION_NAME}_finish (void)
267 {
268   if (! link_info.relocatable)
269     {
270       /* Now build the linker stubs.  */
271       if (stub_file->the_bfd->sections != NULL)
272         {
273           if (! elf64_aarch64_build_stubs (& link_info))
274             einfo ("%X%P: can not build stubs: %E\n");
275         }
276     }
277
278   finish_default ();
279 }
280
281 /* This is a convenient point to tell BFD about target specific flags.
282    After the output has been created, but before inputs are read.  */
283 static void
284 aarch64_elf_create_output_section_statements (void)
285 {
286   if (strstr (bfd_get_target (link_info.output_bfd), "aarch64") == NULL)
287     {
288       /* The arm backend needs special fields in the output hash structure.
289          These will only be created if the output format is an arm format,
290          hence we do not support linking and changing output formats at the
291          same time.  Use a link followed by objcopy to change output formats.  */
292       einfo ("%F%X%P: error: Cannot change output format whilst linking AArch64 binaries.\n");
293       return;
294     }
295
296   bfd_elf64_aarch64_set_options (link_info.output_bfd, &link_info,
297                                  no_enum_size_warning,
298                                  no_wchar_size_warning,
299                                  pic_veneer);
300
301   stub_file = lang_add_input_file ("linker stubs",
302                                    lang_input_file_is_fake_enum,
303                                    NULL);
304   stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
305   if (stub_file->the_bfd == NULL
306       || ! bfd_set_arch_mach (stub_file->the_bfd,
307                               bfd_get_arch (link_info.output_bfd),
308                               bfd_get_mach (link_info.output_bfd)))
309     {
310       einfo ("%X%P: can not create BFD %E\n");
311       return;
312     }
313
314   stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
315   ldlang_add_file (stub_file);
316 }
317
318 /* Avoid processing the fake stub_file in vercheck, stat_needed and
319    check_needed routines.  */
320
321 static void (*real_func) (lang_input_statement_type *);
322
323 static void aarch64_for_each_input_file_wrapper (lang_input_statement_type *l)
324 {
325   if (l != stub_file)
326     (*real_func) (l);
327 }
328
329 static void
330 aarch64_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
331 {
332   real_func = func;
333   lang_for_each_input_file (&aarch64_for_each_input_file_wrapper);
334 }
335
336 #define lang_for_each_input_file aarch64_lang_for_each_input_file
337
338 EOF
339
340 # Define some shell vars to insert bits of code into the standard elf
341 # parse_args and list_options functions.
342 #
343 PARSE_AND_LIST_PROLOGUE='
344 #define OPTION_NO_ENUM_SIZE_WARNING     309
345 #define OPTION_PIC_VENEER               310
346 #define OPTION_STUBGROUP_SIZE           311
347 #define OPTION_NO_WCHAR_SIZE_WARNING    312
348 '
349
350 PARSE_AND_LIST_SHORTOPTS=p
351
352 PARSE_AND_LIST_LONGOPTS='
353   { "no-pipeline-knowledge", no_argument, NULL, '\'p\''},
354   { "no-enum-size-warning", no_argument, NULL, OPTION_NO_ENUM_SIZE_WARNING},
355   { "pic-veneer", no_argument, NULL, OPTION_PIC_VENEER},
356   { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
357   { "no-wchar-size-warning", no_argument, NULL, OPTION_NO_WCHAR_SIZE_WARNING},
358 '
359
360 PARSE_AND_LIST_OPTIONS='
361   fprintf (file, _("  --no-enum-size-warning      Don'\''t warn about objects with incompatible\n"
362                    "                                enum sizes\n"));
363   fprintf (file, _("  --no-wchar-size-warning     Don'\''t warn about objects with incompatible"
364                    "                                wchar_t sizes\n"));
365   fprintf (file, _("  --pic-veneer                Always generate PIC interworking veneers\n"));
366   fprintf (file, _("\
367    --stub-group-size=N   Maximum size of a group of input sections that can be\n\
368                            handled by one stub section.  A negative value\n\
369                            locates all stubs after their branches (with a\n\
370                            group size of -N), while a positive value allows\n\
371                            two groups of input sections, one before, and one\n\
372                            after each stub section.  Values of +/-1 indicate\n\
373                            the linker should choose suitable defaults.\n"
374                    ));
375 '
376
377 PARSE_AND_LIST_ARGS_CASES='
378     case '\'p\'':
379       /* Only here for backwards compatibility.  */
380       break;
381
382     case OPTION_NO_ENUM_SIZE_WARNING:
383       no_enum_size_warning = 1;
384       break;
385
386     case OPTION_NO_WCHAR_SIZE_WARNING:
387       no_wchar_size_warning = 1;
388       break;
389
390     case OPTION_PIC_VENEER:
391       pic_veneer = 1;
392       break;
393
394     case OPTION_STUBGROUP_SIZE:
395       {
396         const char *end;
397
398         group_size = bfd_scan_vma (optarg, &end, 0);
399         if (*end)
400           einfo (_("%P%F: invalid number `%s'\''\n"), optarg);
401       }
402       break;
403 '
404
405 # We have our own before_allocation etc. functions, but they call
406 # the standard routines, so give them a different name.
407 LDEMUL_BEFORE_ALLOCATION=aarch64_elf_before_allocation
408 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
409 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=aarch64_elf_create_output_section_statements
410
411 # Replace the elf before_parse function with our own.
412 LDEMUL_BEFORE_PARSE=gld"${EMULATION_NAME}"_before_parse
413
414 # Call the extra arm-elf function
415 LDEMUL_FINISH=gld${EMULATION_NAME}_finish