1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
4 /* This file is is generated by a shell script. DO NOT EDIT! */
6 /* Emulate the original gld for the given ${EMULATION_NAME}
7 Copyright (C) 2014-2018 Free Software Foundation, Inc.
8 Written by Steve Chamberlain steve@cygnus.com
9 Extended for the MSP430 by Nick Clifton nickc@redhat.com
11 This file is part of the GNU Binutils.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26 MA 02110-1301, USA. */
28 #define TARGET_IS_${EMULATION_NAME}
42 #include "libiberty.h"
53 enum either_placement_stage
61 static int data_region = REGION_NONE;
62 static int code_region = REGION_NONE;
63 static bfd_boolean disable_sec_transformation = FALSE;
65 #define MAX_PREFIX_LENGTH 7
69 # Import any needed special functions and/or overrides.
71 if test -n "$EXTRA_EM_FILE" ; then
72 source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
75 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
79 gld${EMULATION_NAME}_before_parse (void)
81 #ifndef TARGET_ /* I.e., if not generic. */
82 ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
83 #endif /* not TARGET_ */
85 /* The MSP430 port *needs* linker relaxtion in order to cope with large
86 functions where conditional branches do not fit into a +/- 1024 byte range. */
87 if (!bfd_link_relocatable (&link_info))
88 TARGET_ENABLE_RELAXATION;
94 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
98 gld${EMULATION_NAME}_get_script (int *isfile)
101 if test x"$COMPILE_IN" = xyes
103 # Scripts compiled in.
105 # sed commands to quote an ld script as a C string.
106 sc="-f stringify.sed"
112 if (bfd_link_relocatable (&link_info) && config.build_constructors)
115 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
116 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
117 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
118 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
119 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
120 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
121 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
122 echo ' ; else return' >> e${EMULATION_NAME}.c
123 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
124 echo '; }' >> e${EMULATION_NAME}.c
127 # Scripts read from the filesystem.
133 if (bfd_link_relocatable (&link_info) && config.build_constructors)
134 return "ldscripts/${EMULATION_NAME}.xu";
135 else if (bfd_link_relocatable (&link_info))
136 return "ldscripts/${EMULATION_NAME}.xr";
137 else if (!config.text_read_only)
138 return "ldscripts/${EMULATION_NAME}.xbn";
139 else if (!config.magic_demand_paged)
140 return "ldscripts/${EMULATION_NAME}.xn";
142 return "ldscripts/${EMULATION_NAME}.x";
148 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
152 data_statement_size (lang_data_statement_type *d)
154 unsigned int size = 0;
171 einfo (_("%P: error: unhandled data_statement size\n"));
177 /* Helper function for place_orphan that computes the size
178 of sections already mapped to the given statement. */
181 scan_children (lang_statement_union_type * l)
183 bfd_size_type amount = 0;
187 switch (l->header.type)
189 case lang_input_section_enum:
190 if (l->input_section.section->flags & SEC_ALLOC)
191 amount += l->input_section.section->size;
194 case lang_constructors_statement_enum:
195 case lang_assignment_statement_enum:
196 case lang_padding_statement_enum:
199 case lang_wild_statement_enum:
200 amount += scan_children (l->wild_statement.children.head);
203 case lang_data_statement_enum:
204 amount += data_statement_size (&l->data_statement);
208 fprintf (stderr, "msp430 orphan placer: unhandled lang type %d\n", l->header.type);
218 /* Place an orphan section. We use this to put .either sections
219 into either their lower or their upper equivalents. */
221 static lang_output_section_statement_type *
222 gld${EMULATION_NAME}_place_orphan (asection * s,
223 const char * secname,
230 lang_output_section_statement_type * lower;
231 lang_output_section_statement_type * upper;
233 if ((s->flags & SEC_ALLOC) == 0)
236 if (bfd_link_relocatable (&link_info))
239 /* If constraints are involved let the linker handle the placement normally. */
243 /* We only need special handling for .either sections. */
244 if (strncmp (secname, ".either.", 8) != 0)
247 /* Skip the .either prefix. */
250 /* Compute the names of the corresponding upper and lower
251 sections. If the input section name contains another period,
252 only use the part of the name before the second dot. */
253 if (strchr (secname + 1, '.') != NULL)
255 buf = name = xstrdup (secname);
257 * strchr (name + 1, '.') = 0;
260 name = (char *) secname;
262 lower_name = concat (".lower", name, NULL);
263 upper_name = concat (".upper", name, NULL);
265 /* Find the corresponding lower and upper sections. */
266 lower = lang_output_section_find (lower_name);
267 upper = lang_output_section_find (upper_name);
269 if (lower == NULL && upper == NULL)
271 einfo (_("%P: error: no section named %s or %s in linker script\n"),
272 lower_name, upper_name);
275 else if (lower == NULL)
277 lower = lang_output_section_find (name);
280 einfo (_("%P: error: no section named %s in linker script\n"), name);
285 /* Always place orphaned sections in lower. Optimal placement of either
286 sections is performed later, once section sizes have been finalized. */
287 lang_add_section (& lower->children, s, NULL, lower);
301 change_output_section (lang_statement_union_type ** head,
303 lang_output_section_statement_type * new_output_section)
306 lang_statement_union_type * prev = NULL;
307 lang_statement_union_type * curr;
312 switch (curr->header.type)
314 case lang_input_section_enum:
315 is = curr->input_section.section;
318 s->output_section = NULL;
319 lang_add_section (& (new_output_section->children), s, NULL,
321 /* Remove the section from the old output section. */
323 *head = curr->header.next;
325 prev->header.next = curr->header.next;
329 case lang_wild_statement_enum:
330 if (change_output_section (&(curr->wild_statement.children.head),
331 s, new_output_section))
338 curr = curr->header.next;
344 move_prefixed_section (asection *s, char *new_name,
345 lang_output_section_statement_type * new_output_sec)
348 if (s->output_section == NULL)
349 lang_add_section (& (new_output_sec->children), s, NULL, new_output_sec);
352 lang_output_section_statement_type * curr_output_sec
353 = lang_output_section_find (s->output_section->name);
354 change_output_section (&(curr_output_sec->children.head), s,
360 add_region_prefix (bfd *abfd, asection *s,
361 ATTRIBUTE_UNUSED void *unused)
363 const char *curr_name = bfd_get_section_name (abfd, s);
365 char * new_input_sec_name = NULL;
366 char * new_output_sec_name = NULL;
367 int region = REGION_NONE;
369 if (strncmp (curr_name, ".text", 5) == 0)
371 region = code_region;
372 base_name = concat (".text", NULL);
374 else if (strncmp (curr_name, ".data", 5) == 0)
376 region = data_region;
377 base_name = concat (".data", NULL);
379 else if (strncmp (curr_name, ".bss", 4) == 0)
381 region = data_region;
382 base_name = concat (".bss", NULL);
384 else if (strncmp (curr_name, ".rodata", 7) == 0)
386 region = data_region;
387 base_name = concat (".rodata", NULL);
397 new_input_sec_name = concat (".upper", curr_name, NULL);
398 new_output_sec_name = concat (".upper", base_name, NULL);
399 lang_output_section_statement_type * upper
400 = lang_output_section_find (new_output_sec_name);
403 move_prefixed_section (s, new_input_sec_name, upper);
406 einfo (_("%P: error: no section named %s in linker script\n"),
407 new_output_sec_name);
410 new_input_sec_name = concat (".lower", curr_name, NULL);
411 new_output_sec_name = concat (".lower", base_name, NULL);
412 lang_output_section_statement_type * lower
413 = lang_output_section_find (new_output_sec_name);
416 move_prefixed_section (s, new_input_sec_name, lower);
419 einfo (_("%P: error: no section named %s in linker script\n"),
420 new_output_sec_name);
423 s->name = concat (".either", curr_name, NULL);
431 if (new_input_sec_name)
433 free (new_input_sec_name);
434 free (new_output_sec_name);
439 msp430_elf_after_open (void)
443 gld${EMULATION_NAME}_after_open ();
445 /* If neither --code-region or --data-region have been passed, do not
446 transform sections names. */
447 if ((code_region == REGION_NONE && data_region == REGION_NONE)
448 || disable_sec_transformation)
451 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
452 bfd_map_over_sections (abfd, add_region_prefix, NULL);
455 #define OPTION_CODE_REGION 321
456 #define OPTION_DATA_REGION (OPTION_CODE_REGION + 1)
457 #define OPTION_DISABLE_TRANS (OPTION_CODE_REGION + 2)
460 gld${EMULATION_NAME}_add_options
461 (int ns, char **shortopts, int nl, struct option **longopts,
462 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
464 static const char xtra_short[] = { };
466 static const struct option xtra_long[] =
468 { "code-region", required_argument, NULL, OPTION_CODE_REGION },
469 { "data-region", required_argument, NULL, OPTION_DATA_REGION },
470 { "disable-sec-transformation", no_argument, NULL,
471 OPTION_DISABLE_TRANS },
472 { NULL, no_argument, NULL, 0 }
475 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
476 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
477 *longopts = (struct option *)
478 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
479 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
483 gld${EMULATION_NAME}_list_options (FILE * file)
485 fprintf (file, _(" --code-region={either,lower,upper,none}\n\
486 Transform .text* sections to {either,lower,upper,none}.text* sections\n"));
487 fprintf (file, _(" --data-region={either,lower,upper,none}\n\
488 Transform .data*, .rodata* and .bss* sections to\n\
489 {either,lower,upper,none}.{bss,data,rodata}* sections\n"));
490 fprintf (file, _(" --disable-sec-transformation\n\
491 Disable transformation of .{text,data,bss,rodata}* sections to\n\
492 add the {either,lower,upper,none} prefixes\n"));
496 gld${EMULATION_NAME}_handle_option (int optc)
500 case OPTION_CODE_REGION:
501 if (strcmp (optarg, "upper") == 0)
502 code_region = REGION_UPPER;
503 else if (strcmp (optarg, "lower") == 0)
504 code_region = REGION_LOWER;
505 else if (strcmp (optarg, "either") == 0)
506 code_region = REGION_EITHER;
507 else if (strcmp (optarg, "none") == 0)
508 code_region = REGION_NONE;
509 else if (strlen (optarg) == 0)
511 einfo (_("%P: --code-region requires an argument: "
512 "{upper,lower,either,none}\n"));
517 einfo (_("%P: error: unrecognized argument to --code-region= option: "
518 "\"%s\"\n"), optarg);
523 case OPTION_DATA_REGION:
524 if (strcmp (optarg, "upper") == 0)
525 data_region = REGION_UPPER;
526 else if (strcmp (optarg, "lower") == 0)
527 data_region = REGION_LOWER;
528 else if (strcmp (optarg, "either") == 0)
529 data_region = REGION_EITHER;
530 else if (strcmp (optarg, "none") == 0)
531 data_region = REGION_NONE;
532 else if (strlen (optarg) == 0)
534 einfo (_("%P: --data-region requires an argument: "
535 "{upper,lower,either,none}\n"));
540 einfo (_("%P: error: unrecognized argument to --data-region= option: "
541 "\"%s\"\n"), optarg);
546 case OPTION_DISABLE_TRANS:
547 disable_sec_transformation = TRUE;
557 eval_upper_either_sections (bfd *abfd, asection *s, void *data)
559 const char * base_sec_name;
560 const char * curr_name;
564 lang_output_section_statement_type * lower;
565 lang_output_section_statement_type * upper;
566 static bfd_size_type *lower_size = 0;
567 static bfd_size_type *upper_size = 0;
568 static bfd_size_type lower_size_rom = 0;
569 static bfd_size_type lower_size_ram = 0;
570 static bfd_size_type upper_size_rom = 0;
571 static bfd_size_type upper_size_ram = 0;
573 if ((s->flags & SEC_ALLOC) == 0)
575 if (bfd_link_relocatable (&link_info))
578 base_sec_name = (const char *) data;
579 curr_name = bfd_get_section_name (abfd, s);
581 /* Only concerned with .either input sections in the upper output section. */
582 either_name = concat (".either", base_sec_name, NULL);
583 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
584 || strncmp (s->output_section->name, ".upper", 6) != 0)
587 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
588 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
590 if (upper == NULL || upper->region == NULL)
592 else if (lower == NULL)
593 lower = lang_output_section_find (base_sec_name);
594 if (lower == NULL || lower->region == NULL)
597 if (strcmp (base_sec_name, ".text") == 0
598 || strcmp (base_sec_name, ".rodata") == 0)
603 if (curr_region == ROM)
605 if (lower_size_rom == 0)
607 lower_size_rom = lower->region->current - lower->region->origin;
608 upper_size_rom = upper->region->current - upper->region->origin;
610 lower_size = &lower_size_rom;
611 upper_size = &upper_size_rom;
613 else if (curr_region == RAM)
615 if (lower_size_ram == 0)
617 lower_size_ram = lower->region->current - lower->region->origin;
618 upper_size_ram = upper->region->current - upper->region->origin;
620 lower_size = &lower_size_ram;
621 upper_size = &upper_size_ram;
624 /* Move sections in the upper region that would fit in the lower
625 region to the lower region. */
626 if (*lower_size + s->size < lower->region->length)
628 if (change_output_section (&(upper->children.head), s, lower))
630 *upper_size -= s->size;
631 *lower_size += s->size;
639 eval_lower_either_sections (bfd *abfd, asection *s, void *data)
641 const char * base_sec_name;
642 const char * curr_name;
645 lang_output_section_statement_type * output_sec;
646 lang_output_section_statement_type * lower;
647 lang_output_section_statement_type * upper;
649 static bfd_size_type *lower_size = 0;
650 static bfd_size_type lower_size_rom = 0;
651 static bfd_size_type lower_size_ram = 0;
653 if ((s->flags & SEC_ALLOC) == 0)
655 if (bfd_link_relocatable (&link_info))
658 base_sec_name = (const char *) data;
659 curr_name = bfd_get_section_name (abfd, s);
661 /* Only concerned with .either input sections in the lower or "default"
662 output section i.e. not in the upper output section. */
663 either_name = concat (".either", base_sec_name, NULL);
664 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
665 || strncmp (s->output_section->name, ".upper", 6) == 0)
668 if (strcmp (base_sec_name, ".text") == 0
669 || strcmp (base_sec_name, ".rodata") == 0)
674 output_sec = lang_output_section_find (s->output_section->name);
676 /* If the output_section doesn't exist, this has already been reported in
677 place_orphan, so don't need to warn again. */
678 if (output_sec == NULL || output_sec->region == NULL)
681 /* lower and output_sec might be the same, but in some cases an .either
682 section can end up in base_sec_name if it hasn't been placed by
684 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
685 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
689 if (curr_region == ROM)
691 if (lower_size_rom == 0)
693 /* Get the size of other items in the lower region that aren't the
694 sections to be moved around. */
696 = (output_sec->region->current - output_sec->region->origin)
697 - scan_children (output_sec->children.head);
698 if (output_sec != lower && lower != NULL)
699 lower_size_rom -= scan_children (lower->children.head);
701 lower_size = &lower_size_rom;
703 else if (curr_region == RAM)
705 if (lower_size_ram == 0)
708 = (output_sec->region->current - output_sec->region->origin)
709 - scan_children (output_sec->children.head);
710 if (output_sec != lower && lower != NULL)
711 lower_size_ram -= scan_children (lower->children.head);
713 lower_size = &lower_size_ram;
715 /* Move sections that cause the lower region to overflow to the upper region. */
716 if (*lower_size + s->size > output_sec->region->length)
717 change_output_section (&(output_sec->children.head), s, upper);
719 *lower_size += s->size;
724 /* This function is similar to lang_relax_sections, but without the size
725 evaluation code that is always executed after relaxation. */
727 intermediate_relax_sections (void)
729 int i = link_info.relax_pass;
731 /* The backend can use it to determine the current pass. */
732 link_info.relax_pass = 0;
736 bfd_boolean relax_again;
738 link_info.relax_trip = -1;
741 link_info.relax_trip++;
743 lang_do_assignments (lang_assigning_phase_enum);
745 lang_reset_memory_regions ();
748 lang_size_sections (&relax_again, FALSE);
752 link_info.relax_pass++;
757 msp430_elf_after_allocation (void)
761 /* Go over each section twice, once to place either sections that don't fit
762 in lower into upper, and then again to move any sections in upper that
763 fit in lower into lower. */
764 for (i = 0; i < 8; i++)
766 int placement_stage = (i < 4) ? LOWER_TO_UPPER : UPPER_TO_LOWER;
767 const char * base_sec_name;
768 lang_output_section_statement_type * upper;
774 base_sec_name = ".text";
777 base_sec_name = ".data";
780 base_sec_name = ".bss";
783 base_sec_name = ".rodata";
786 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
789 /* Can't just use one iteration over the all the sections to make
790 both lower->upper and upper->lower transformations because the
791 iterator encounters upper sections before all lower sections have
795 if (placement_stage == LOWER_TO_UPPER)
797 /* Perform relaxation and get the final size of sections
798 before trying to fit .either sections in the correct
800 if (relax_count == 0)
802 intermediate_relax_sections ();
805 for (abfd = link_info.input_bfds; abfd != NULL;
806 abfd = abfd->link.next)
808 bfd_map_over_sections (abfd, eval_lower_either_sections,
809 (void *) base_sec_name);
812 else if (placement_stage == UPPER_TO_LOWER)
814 /* Relax again before moving upper->lower. */
815 if (relax_count == 1)
817 intermediate_relax_sections ();
820 for (abfd = link_info.input_bfds; abfd != NULL;
821 abfd = abfd->link.next)
823 bfd_map_over_sections (abfd, eval_upper_either_sections,
824 (void *) base_sec_name);
830 gld${EMULATION_NAME}_after_allocation ();
833 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
835 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
836 ${LDEMUL_SYSLIB-syslib_default},
837 ${LDEMUL_HLL-hll_default},
838 ${LDEMUL_AFTER_PARSE-after_parse_default},
839 msp430_elf_after_open,
840 after_check_relocs_default,
841 msp430_elf_after_allocation,
842 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
843 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
844 ${LDEMUL_BEFORE_ALLOCATION-before_allocation_default},
845 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
848 ${LDEMUL_FINISH-finish_default},
849 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
850 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-NULL},
851 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
852 ${LDEMUL_SET_SYMBOLS-NULL},
853 ${LDEMUL_PARSE_ARGS-NULL},
854 gld${EMULATION_NAME}_add_options,
855 gld${EMULATION_NAME}_handle_option,
856 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
857 gld${EMULATION_NAME}_list_options,
858 ${LDEMUL_RECOGNIZED_FILE-NULL},
859 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
860 ${LDEMUL_NEW_VERS_PATTERN-NULL},
861 ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}