* emultempl/armelf.em (elf32_arm_add_stub_section): Use
[platform/upstream/binutils.git] / ld / emultempl / armelf.em
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright 1991, 1993, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 #   2004, 2005, 2007, 2008
4 #   Free Software Foundation, Inc.
5 #
6 # This file is part of the GNU Binutils.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 # MA 02110-1301, USA.
22 #
23
24 # This file is sourced from elf32.em, and defines extra arm-elf
25 # specific routines.
26 #
27 test -z "$TARGET2_TYPE" && TARGET2_TYPE="rel"
28 fragment <<EOF
29
30 #include "ldctor.h"
31 #include "elf/arm.h"
32
33 static char *thumb_entry_symbol = NULL;
34 static bfd *bfd_for_interwork;
35 static int byteswap_code = 0;
36 static int target1_is_rel = 0${TARGET1_IS_REL};
37 static char *target2_type = "${TARGET2_TYPE}";
38 static int fix_v4bx = 0;
39 static int use_blx = 0;
40 static bfd_arm_vfp11_fix vfp11_denorm_fix = BFD_ARM_VFP11_FIX_DEFAULT;
41 static int no_enum_size_warning = 0;
42 static int pic_veneer = 0;
43
44 static void
45 gld${EMULATION_NAME}_before_parse (void)
46 {
47 #ifndef TARGET_                 /* I.e., if not generic.  */
48   ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
49 #endif /* not TARGET_ */
50   config.dynamic_link = ${DYNAMIC_LINK-TRUE};
51   config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
52 }
53
54 static void
55 arm_elf_after_open (void)
56 {
57   if (strstr (bfd_get_target (link_info.output_bfd), "arm") == NULL)
58     {
59       /* The arm backend needs special fields in the output hash structure.
60          These will only be created if the output format is an arm format,
61          hence we do not support linking and changing output formats at the
62          same time.  Use a link followed by objcopy to change output formats.  */
63       einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
64       return;
65     }
66
67   {
68     LANG_FOR_EACH_INPUT_STATEMENT (is)
69       {
70         bfd_elf32_arm_add_glue_sections_to_bfd (is->the_bfd, & link_info);
71       }
72   }
73
74   /* Call the standard elf routine.  */
75   gld${EMULATION_NAME}_after_open ();
76 }
77
78 static void
79 arm_elf_set_bfd_for_interworking (lang_statement_union_type *statement)
80 {
81   if (statement->header.type == lang_input_section_enum)
82     {
83       asection *i = statement->input_section.section;
84
85       if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
86           && (i->flags & SEC_EXCLUDE) == 0)
87         {
88           asection *output_section = i->output_section;
89
90           ASSERT (output_section->owner == link_info.output_bfd);
91
92           /* Don't attach the interworking stubs to a dynamic object, to
93              an empty section, etc.  */
94           if ((output_section->flags & SEC_HAS_CONTENTS) != 0
95               && (i->flags & SEC_NEVER_LOAD) == 0
96               && ! (i->owner->flags & DYNAMIC)
97               && ! i->owner->output_has_begun)
98             {
99               bfd_for_interwork = i->owner;
100               bfd_for_interwork->output_has_begun = TRUE;
101             }
102         }
103     }
104 }
105
106 static void
107 arm_elf_before_allocation (void)
108 {
109   bfd *tem;
110
111   if (link_info.input_bfds != NULL)
112     {
113       /* The interworking bfd must be the last one in the link.  */
114       bfd_for_interwork = NULL;
115       for (tem = link_info.input_bfds; tem != NULL; tem = tem->link_next)
116         tem->output_has_begun = FALSE;
117
118       lang_for_each_statement (arm_elf_set_bfd_for_interworking);
119       for (tem = link_info.input_bfds; tem != NULL; tem = tem->link_next)
120         tem->output_has_begun = FALSE;
121
122       /* If bfd_for_interwork is NULL, then there are no loadable sections
123          with real contents to be linked, so we are not going to have to
124          create any interworking stubs, so it is OK not to call
125          bfd_elf32_arm_get_bfd_for_interworking.  */
126       if (bfd_for_interwork != NULL)
127         bfd_elf32_arm_get_bfd_for_interworking (bfd_for_interwork, &link_info);
128     }
129
130   bfd_elf32_arm_set_byteswap_code (&link_info, byteswap_code);
131
132   /* Choose type of VFP11 erratum fix, or warn if specified fix is unnecessary
133      due to architecture version.  */
134   bfd_elf32_arm_set_vfp11_fix (link_info.output_bfd, &link_info);
135
136   /* We should be able to set the size of the interworking stub section.  We
137      can't do it until later if we have dynamic sections, though.  */
138   if (! elf_hash_table (&link_info)->dynamic_sections_created)
139     {
140       /* Here we rummage through the found bfds to collect glue information.  */
141       LANG_FOR_EACH_INPUT_STATEMENT (is)
142         {
143           /* Initialise mapping tables for code/data.  */
144           bfd_elf32_arm_init_maps (is->the_bfd);
145
146           if (!bfd_elf32_arm_process_before_allocation (is->the_bfd,
147                                                         &link_info)
148               || !bfd_elf32_arm_vfp11_erratum_scan (is->the_bfd, &link_info))
149             /* xgettext:c-format */
150             einfo (_("Errors encountered processing file %s"), is->filename);
151         }
152     }
153
154   /* Call the standard elf routine.  */
155   gld${EMULATION_NAME}_before_allocation ();
156
157   /* We have seen it all. Allocate it, and carry on.  */
158   bfd_elf32_arm_allocate_interworking_sections (& link_info);
159 }
160
161 static void
162 arm_elf_after_allocation (void)
163 {
164   /* Call the standard elf routine.  */
165   after_allocation_default ();
166
167   {
168     LANG_FOR_EACH_INPUT_STATEMENT (is)
169       {
170         /* Figure out where VFP11 erratum veneers (and the labels returning
171            from same) have been placed.  */
172         bfd_elf32_arm_vfp11_fix_veneer_locations (is->the_bfd, &link_info);
173       }
174   }
175 }
176
177 /* Fake input file for stubs.  */
178 static lang_input_statement_type *stub_file;
179
180 /* Whether we need to call gldarm_layout_sections_again.  */
181 static int need_laying_out = 0;
182
183 /* Maximum size of a group of input sections that can be handled by
184    one stub section.  A value of +/-1 indicates the bfd back-end
185    should use a suitable default size.  */
186 static bfd_signed_vma group_size = 1;
187
188 struct hook_stub_info
189 {
190   lang_statement_list_type add;
191   asection *input_section;
192 };
193
194 /* Traverse the linker tree to find the spot where the stub goes.  */
195
196 static bfd_boolean
197 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
198 {
199   lang_statement_union_type *l;
200   bfd_boolean ret;
201
202   for (; (l = *lp) != NULL; lp = &l->header.next)
203     {
204       switch (l->header.type)
205         {
206         case lang_constructors_statement_enum:
207           ret = hook_in_stub (info, &constructor_list.head);
208           if (ret)
209             return ret;
210           break;
211
212         case lang_output_section_statement_enum:
213           ret = hook_in_stub (info,
214                               &l->output_section_statement.children.head);
215           if (ret)
216             return ret;
217           break;
218
219         case lang_wild_statement_enum:
220           ret = hook_in_stub (info, &l->wild_statement.children.head);
221           if (ret)
222             return ret;
223           break;
224
225         case lang_group_statement_enum:
226           ret = hook_in_stub (info, &l->group_statement.children.head);
227           if (ret)
228             return ret;
229           break;
230
231         case lang_input_section_enum:
232           if (l->input_section.section == info->input_section)
233             {
234               /* We've found our section.  Insert the stub immediately
235                  before its associated input section.  */
236               *lp = info->add.head;
237               *(info->add.tail) = l;
238               return TRUE;
239             }
240           break;
241
242         case lang_data_statement_enum:
243         case lang_reloc_statement_enum:
244         case lang_object_symbols_statement_enum:
245         case lang_output_statement_enum:
246         case lang_target_statement_enum:
247         case lang_input_statement_enum:
248         case lang_assignment_statement_enum:
249         case lang_padding_statement_enum:
250         case lang_address_statement_enum:
251         case lang_fill_statement_enum:
252           break;
253
254         default:
255           FAIL ();
256           break;
257         }
258     }
259   return FALSE;
260 }
261
262
263 /* Call-back for elf32_arm_size_stubs.  */
264
265 /* Create a new stub section, and arrange for it to be linked
266    immediately before INPUT_SECTION.  */
267
268 static asection *
269 elf32_arm_add_stub_section (const char *stub_sec_name,
270                             asection *input_section)
271 {
272   asection *stub_sec;
273   flagword flags;
274   asection *output_section;
275   const char *secname;
276   lang_output_section_statement_type *os;
277   struct hook_stub_info info;
278
279   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
280            | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
281   stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
282                                                  stub_sec_name, flags);
283   if (stub_sec == NULL)
284     goto err_ret;
285
286   bfd_set_section_alignment (stub_file->the_bfd, stub_sec, 3);
287
288   output_section = input_section->output_section;
289   secname = bfd_get_section_name (output_section->owner, output_section);
290   os = lang_output_section_find (secname);
291
292   info.input_section = input_section;
293   lang_list_init (&info.add);
294   lang_add_section (&info.add, stub_sec, os);
295
296   if (info.add.head == NULL)
297     goto err_ret;
298
299   if (hook_in_stub (&info, &os->children.head))
300     return stub_sec;
301
302  err_ret:
303   einfo ("%X%P: can not make stub section: %E\n");
304   return NULL;
305 }
306
307 /* Another call-back for elf_arm_size_stubs.  */
308
309 static void
310 gldarm_layout_sections_again (void)
311 {
312   /* If we have changed sizes of the stub sections, then we need
313      to recalculate all the section offsets.  This may mean we need to
314      add even more stubs.  */
315   gld${EMULATION_NAME}_map_segments (TRUE);
316   need_laying_out = -1;
317 }
318
319 static void
320 build_section_lists (lang_statement_union_type *statement)
321 {
322   if (statement->header.type == lang_input_section_enum)
323     {
324       asection *i = statement->input_section.section;
325
326       if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
327           && (i->flags & SEC_EXCLUDE) == 0
328           && i->output_section != NULL
329           && i->output_section->owner == link_info.output_bfd)
330         elf32_arm_next_input_section (& link_info, i);
331     }
332 }
333
334 static void
335 gld${EMULATION_NAME}_finish (void)
336 {
337   struct bfd_link_hash_entry * h;
338
339   /* bfd_elf32_discard_info just plays with debugging sections,
340      ie. doesn't affect any code, so we can delay resizing the
341      sections.  It's likely we'll resize everything in the process of
342      adding stubs.  */
343   if (bfd_elf_discard_info (link_info.output_bfd, & link_info))
344     need_laying_out = 1;
345
346   /* If generating a relocatable output file, then we don't
347      have to examine the relocs.  */
348   if (stub_file != NULL && !link_info.relocatable)
349     {
350       int  ret = elf32_arm_setup_section_lists (link_info.output_bfd, & link_info);
351
352       if (ret != 0)
353         {
354           if (ret < 0)
355             {
356               einfo ("%X%P: could not compute sections lists for stub generation: %E\n");
357               return;
358             }
359
360           lang_for_each_statement (build_section_lists);
361
362           /* Call into the BFD backend to do the real work.  */
363           if (! elf32_arm_size_stubs (link_info.output_bfd,
364                                       stub_file->the_bfd,
365                                       & link_info,
366                                       group_size,
367                                       & elf32_arm_add_stub_section,
368                                       & gldarm_layout_sections_again))
369             {
370               einfo ("%X%P: cannot size stub section: %E\n");
371               return;
372             }
373         }
374     }
375
376   if (need_laying_out != -1)
377     gld${EMULATION_NAME}_map_segments (need_laying_out);
378
379   if (! link_info.relocatable)
380     {
381       /* Now build the linker stubs.  */
382       if (stub_file->the_bfd->sections != NULL)
383         {
384           if (! elf32_arm_build_stubs (& link_info))
385             einfo ("%X%P: can not build stubs: %E\n");
386         }
387     }
388
389   finish_default ();
390
391   if (thumb_entry_symbol)
392     {
393       h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
394                                 FALSE, FALSE, TRUE);
395     }
396   else
397     {
398       struct elf_link_hash_entry * eh;
399
400       if (!entry_symbol.name)
401         return;
402
403       h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
404                                 FALSE, FALSE, TRUE);
405       eh = (struct elf_link_hash_entry *)h;
406       if (!h || ELF_ST_TYPE(eh->type) != STT_ARM_TFUNC)
407         return;
408     }
409
410
411   if (h != (struct bfd_link_hash_entry *) NULL
412       && (h->type == bfd_link_hash_defined
413           || h->type == bfd_link_hash_defweak)
414       && h->u.def.section->output_section != NULL)
415     {
416       static char buffer[32];
417       bfd_vma val;
418
419       /* Special procesing is required for a Thumb entry symbol.  The
420          bottom bit of its address must be set.  */
421       val = (h->u.def.value
422              + bfd_get_section_vma (link_info.output_bfd,
423                                     h->u.def.section->output_section)
424              + h->u.def.section->output_offset);
425
426       val |= 1;
427
428       /* Now convert this value into a string and store it in entry_symbol
429          where the lang_finish() function will pick it up.  */
430       buffer[0] = '0';
431       buffer[1] = 'x';
432
433       sprintf_vma (buffer + 2, val);
434
435       if (thumb_entry_symbol != NULL && entry_symbol.name != NULL
436           && entry_from_cmdline)
437         einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
438                thumb_entry_symbol, entry_symbol.name);
439       entry_symbol.name = buffer;
440     }
441   else
442     einfo (_("%P: warning: cannot find thumb start symbol %s\n"),
443            thumb_entry_symbol);
444 }
445
446 /* This is a convenient point to tell BFD about target specific flags.
447    After the output has been created, but before inputs are read.  */
448 static void
449 arm_elf_create_output_section_statements (void)
450 {
451   bfd_elf32_arm_set_target_relocs (link_info.output_bfd, &link_info,
452                                    target1_is_rel,
453                                    target2_type, fix_v4bx, use_blx,
454                                    vfp11_denorm_fix, no_enum_size_warning,
455                                    pic_veneer);
456
457   stub_file = lang_add_input_file ("linker stubs",
458                                    lang_input_file_is_fake_enum,
459                                    NULL);
460   stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
461   if (stub_file->the_bfd == NULL
462       || ! bfd_set_arch_mach (stub_file->the_bfd,
463                               bfd_get_arch (link_info.output_bfd),
464                               bfd_get_mach (link_info.output_bfd)))
465     {
466       einfo ("%X%P: can not create BFD %E\n");
467       return;
468     }
469  
470   stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
471   ldlang_add_file (stub_file);
472 }
473
474 /* Avoid processing the fake stub_file in vercheck, stat_needed and
475    check_needed routines.  */
476
477 static void (*real_func) (lang_input_statement_type *);
478
479 static void arm_for_each_input_file_wrapper (lang_input_statement_type *l)
480 {
481   if (l != stub_file)
482     (*real_func) (l);
483 }
484
485 static void
486 arm_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
487 {
488   real_func = func;
489   lang_for_each_input_file (&arm_for_each_input_file_wrapper);
490 }
491
492 #define lang_for_each_input_file arm_lang_for_each_input_file
493
494 EOF
495
496 # Define some shell vars to insert bits of code into the standard elf
497 # parse_args and list_options functions.
498 #
499 PARSE_AND_LIST_PROLOGUE='
500 #define OPTION_THUMB_ENTRY              301
501 #define OPTION_BE8                      302
502 #define OPTION_TARGET1_REL              303
503 #define OPTION_TARGET1_ABS              304
504 #define OPTION_TARGET2                  305
505 #define OPTION_FIX_V4BX                 306
506 #define OPTION_USE_BLX                  307
507 #define OPTION_VFP11_DENORM_FIX         308
508 #define OPTION_NO_ENUM_SIZE_WARNING     309
509 #define OPTION_PIC_VENEER               310
510 #define OPTION_FIX_V4BX_INTERWORKING    311
511 #define OPTION_STUBGROUP_SIZE           312
512 '
513
514 PARSE_AND_LIST_SHORTOPTS=p
515
516 PARSE_AND_LIST_LONGOPTS='
517   { "no-pipeline-knowledge", no_argument, NULL, '\'p\''},
518   { "thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
519   { "be8", no_argument, NULL, OPTION_BE8},
520   { "target1-rel", no_argument, NULL, OPTION_TARGET1_REL},
521   { "target1-abs", no_argument, NULL, OPTION_TARGET1_ABS},
522   { "target2", required_argument, NULL, OPTION_TARGET2},
523   { "fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
524   { "fix-v4bx-interworking", no_argument, NULL, OPTION_FIX_V4BX_INTERWORKING},
525   { "use-blx", no_argument, NULL, OPTION_USE_BLX},
526   { "vfp11-denorm-fix", required_argument, NULL, OPTION_VFP11_DENORM_FIX},
527   { "no-enum-size-warning", no_argument, NULL, OPTION_NO_ENUM_SIZE_WARNING},
528   { "pic-veneer", no_argument, NULL, OPTION_PIC_VENEER},
529   { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
530 '
531
532 PARSE_AND_LIST_OPTIONS='
533   fprintf (file, _("  --thumb-entry=<sym>         Set the entry point to be Thumb symbol <sym>\n"));
534   fprintf (file, _("  --be8                       Oputput BE8 format image\n"));
535   fprintf (file, _("  --target1=rel               Interpret R_ARM_TARGET1 as R_ARM_REL32\n"));
536   fprintf (file, _("  --target1=abs               Interpret R_ARM_TARGET1 as R_ARM_ABS32\n"));
537   fprintf (file, _("  --target2=<type>            Specify definition of R_ARM_TARGET2\n"));
538   fprintf (file, _("  --fix-v4bx                  Rewrite BX rn as MOV pc, rn for ARMv4\n"));
539   fprintf (file, _("  --fix-v4bx-interworking     Rewrite BX rn branch to ARMv4 interworking veneer\n"));
540   fprintf (file, _("  --use-blx                   Enable use of BLX instructions\n"));
541   fprintf (file, _("  --vfp11-denorm-fix          Specify how to fix VFP11 denorm erratum\n"));
542   fprintf (file, _("  --no-enum-size-warning      Don'\''t warn about objects with incompatible"
543                    "                                enum sizes\n"));
544   fprintf (file, _("  --pic-veneer                Always generate PIC interworking veneers\n"));
545   fprintf (file, _("\
546    --stub-group-size=N   Maximum size of a group of input sections that can be\n\
547                            handled by one stub section.  A negative value\n\
548                            locates all stubs before their branches (with a\n\
549                            group size of -N), while a positive value allows\n\
550                            two groups of input sections, one before, and one\n\
551                            after each stub section.  Values of +/-1 indicate\n\
552                            the linker should choose suitable defaults.\n"
553                    ));
554 '
555
556 PARSE_AND_LIST_ARGS_CASES='
557     case '\'p\'':
558       /* Only here for backwards compatibility.  */
559       break;
560
561     case OPTION_THUMB_ENTRY:
562       thumb_entry_symbol = optarg;
563       break;
564
565     case OPTION_BE8:
566       byteswap_code = 1;
567       break;
568
569     case OPTION_TARGET1_REL:
570       target1_is_rel = 1;
571       break;
572
573     case OPTION_TARGET1_ABS:
574       target1_is_rel = 0;
575       break;
576
577     case OPTION_TARGET2:
578       target2_type = optarg;
579       break;
580
581     case OPTION_FIX_V4BX:
582       fix_v4bx = 1;
583       break;
584
585     case OPTION_FIX_V4BX_INTERWORKING:
586       fix_v4bx = 2;
587       break;
588
589     case OPTION_USE_BLX:
590       use_blx = 1;
591       break;
592
593     case OPTION_VFP11_DENORM_FIX:
594       if (strcmp (optarg, "none") == 0)
595         vfp11_denorm_fix = BFD_ARM_VFP11_FIX_NONE;
596       else if (strcmp (optarg, "scalar") == 0)
597         vfp11_denorm_fix = BFD_ARM_VFP11_FIX_SCALAR;
598       else if (strcmp (optarg, "vector") == 0)
599         vfp11_denorm_fix = BFD_ARM_VFP11_FIX_VECTOR;
600       else
601         einfo (_("Unrecognized VFP11 fix type '\''%s'\''.\n"), optarg);
602       break;
603
604     case OPTION_NO_ENUM_SIZE_WARNING:
605       no_enum_size_warning = 1;
606       break;
607
608     case OPTION_PIC_VENEER:
609       pic_veneer = 1;
610       break;
611
612     case OPTION_STUBGROUP_SIZE:
613       {
614         const char *end;
615
616         group_size = bfd_scan_vma (optarg, &end, 0);
617         if (*end)
618           einfo (_("%P%F: invalid number `%s'\''\n"), optarg);
619       }
620       break;
621 '
622
623 # We have our own after_open and before_allocation functions, but they call
624 # the standard routines, so give them a different name.
625 LDEMUL_AFTER_OPEN=arm_elf_after_open
626 LDEMUL_BEFORE_ALLOCATION=arm_elf_before_allocation
627 LDEMUL_AFTER_ALLOCATION=arm_elf_after_allocation
628 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=arm_elf_create_output_section_statements
629
630 # Replace the elf before_parse function with our own.
631 LDEMUL_BEFORE_PARSE=gld"${EMULATION_NAME}"_before_parse
632
633 # Call the extra arm-elf function
634 LDEMUL_FINISH=gld${EMULATION_NAME}_finish