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