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-2020 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}
43 #include "libiberty.h"
54 enum either_placement_stage
62 static int data_region = REGION_NONE;
63 static int code_region = REGION_NONE;
64 static bfd_boolean disable_sec_transformation = FALSE;
66 #define MAX_PREFIX_LENGTH 7
70 # Import any needed special functions and/or overrides.
72 if test -n "$EXTRA_EM_FILE" ; then
73 source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
76 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
80 gld${EMULATION_NAME}_before_parse (void)
82 #ifndef TARGET_ /* I.e., if not generic. */
83 ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
84 #endif /* not TARGET_ */
86 /* The MSP430 port *needs* linker relaxtion in order to cope with large
87 functions where conditional branches do not fit into a +/- 1024 byte range. */
88 if (!bfd_link_relocatable (&link_info))
89 TARGET_ENABLE_RELAXATION;
95 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
99 gld${EMULATION_NAME}_get_script (int *isfile)
102 if test x"$COMPILE_IN" = xyes
104 # Scripts compiled in.
106 # sed commands to quote an ld script as a C string.
107 sc="-f stringify.sed"
113 if (bfd_link_relocatable (&link_info) && config.build_constructors)
116 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
117 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
118 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
119 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
120 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
121 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
122 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
123 echo ' ; else return' >> e${EMULATION_NAME}.c
124 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
125 echo '; }' >> e${EMULATION_NAME}.c
128 # Scripts read from the filesystem.
134 if (bfd_link_relocatable (&link_info) && config.build_constructors)
135 return "ldscripts/${EMULATION_NAME}.xu";
136 else if (bfd_link_relocatable (&link_info))
137 return "ldscripts/${EMULATION_NAME}.xr";
138 else if (!config.text_read_only)
139 return "ldscripts/${EMULATION_NAME}.xbn";
140 else if (!config.magic_demand_paged)
141 return "ldscripts/${EMULATION_NAME}.xn";
143 return "ldscripts/${EMULATION_NAME}.x";
149 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
153 data_statement_size (lang_data_statement_type *d)
155 unsigned int size = 0;
172 einfo (_("%P: error: unhandled data_statement size\n"));
178 /* Helper function for place_orphan that computes the size
179 of sections already mapped to the given statement. */
182 scan_children (lang_statement_union_type * l)
184 bfd_size_type amount = 0;
188 switch (l->header.type)
190 case lang_input_section_enum:
191 if (l->input_section.section->flags & SEC_ALLOC)
192 amount += l->input_section.section->size;
195 case lang_constructors_statement_enum:
196 case lang_assignment_statement_enum:
197 case lang_padding_statement_enum:
200 case lang_wild_statement_enum:
201 amount += scan_children (l->wild_statement.children.head);
204 case lang_data_statement_enum:
205 amount += data_statement_size (&l->data_statement);
209 fprintf (stderr, "msp430 orphan placer: unhandled lang type %d\n", l->header.type);
224 #define WARN_RODATA 3
226 /* Warn only once per output section.
227 * NAME starts with ".upper." or ".lower.". */
229 warn_no_output_section (const char *name)
231 static bfd_boolean warned[2][4] = {{FALSE, FALSE, FALSE, FALSE},
232 {FALSE, FALSE, FALSE, FALSE}};
235 if (strncmp (name, ".upper.", 7) == 0)
238 if (!warned[i][WARN_TEXT] && strcmp (name + 6, ".text") == 0)
239 warned[i][WARN_TEXT] = TRUE;
240 else if (!warned[i][WARN_DATA] && strcmp (name + 6, ".data") == 0)
241 warned[i][WARN_DATA] = TRUE;
242 else if (!warned[i][WARN_BSS] && strcmp (name + 6, ".bss") == 0)
243 warned[i][WARN_BSS] = TRUE;
244 else if (!warned[i][WARN_RODATA] && strcmp (name + 6, ".rodata") == 0)
245 warned[i][WARN_RODATA] = TRUE;
248 einfo ("%P: warning: no input section rule matches %s in linker script\n",
253 /* Place an orphan section. We use this to put .either sections
254 into either their lower or their upper equivalents. */
256 static lang_output_section_statement_type *
257 gld${EMULATION_NAME}_place_orphan (asection * s,
258 const char * secname,
265 lang_output_section_statement_type * lower;
266 lang_output_section_statement_type * upper;
268 if ((s->flags & SEC_ALLOC) == 0)
271 if (bfd_link_relocatable (&link_info))
274 /* If constraints are involved let the linker handle the placement normally. */
278 if (strncmp (secname, ".upper.", 7) == 0
279 || strncmp (secname, ".lower.", 7) == 0)
281 warn_no_output_section (secname);
285 /* We only need special handling for .either sections. */
286 if (strncmp (secname, ".either.", 8) != 0)
289 /* Skip the .either prefix. */
292 /* Compute the names of the corresponding upper and lower
293 sections. If the input section name contains another period,
294 only use the part of the name before the second dot. */
295 if (strchr (secname + 1, '.') != NULL)
297 buf = name = xstrdup (secname);
299 * strchr (name + 1, '.') = 0;
302 name = (char *) secname;
304 lower_name = concat (".lower", name, NULL);
305 upper_name = concat (".upper", name, NULL);
307 /* Find the corresponding lower and upper sections. */
308 lower = lang_output_section_find (lower_name);
309 upper = lang_output_section_find (upper_name);
311 if (lower == NULL && upper == NULL)
313 einfo (_("%P: error: no section named %s or %s in linker script\n"),
314 lower_name, upper_name);
317 else if (lower == NULL)
319 lower = lang_output_section_find (name);
322 einfo (_("%P: error: no section named %s in linker script\n"), name);
327 /* Always place orphaned sections in lower. Optimal placement of either
328 sections is performed later, once section sizes have been finalized. */
329 lang_add_section (& lower->children, s, NULL, lower);
342 change_output_section (lang_statement_union_type **head,
344 lang_output_section_statement_type *new_os,
345 lang_output_section_statement_type *old_os)
348 lang_statement_union_type * prev = NULL;
349 lang_statement_union_type * curr;
354 switch (curr->header.type)
356 case lang_input_section_enum:
357 is = curr->input_section.section;
360 lang_statement_list_type *old_list
361 = (lang_statement_list_type *) &old_os->children;
362 s->output_section = NULL;
363 lang_add_section (&new_os->children, s, NULL, new_os);
365 /* Remove the section from the old output section. */
367 *head = curr->header.next;
369 prev->header.next = curr->header.next;
370 /* If the input section we just moved is the tail of the old
371 output section, then we also need to adjust that tail. */
372 if (old_list->tail == (lang_statement_union_type **) curr)
373 old_list->tail = (lang_statement_union_type **) prev;
378 case lang_wild_statement_enum:
379 if (change_output_section (&(curr->wild_statement.children.head),
387 curr = curr->header.next;
393 add_region_prefix (bfd *abfd ATTRIBUTE_UNUSED, asection *s,
394 void *unused ATTRIBUTE_UNUSED)
396 const char *curr_name = bfd_section_name (s);
397 int region = REGION_NONE;
399 if (strncmp (curr_name, ".text", 5) == 0)
400 region = code_region;
401 else if (strncmp (curr_name, ".data", 5) == 0)
402 region = data_region;
403 else if (strncmp (curr_name, ".bss", 4) == 0)
404 region = data_region;
405 else if (strncmp (curr_name, ".rodata", 7) == 0)
406 region = data_region;
415 bfd_rename_section (s, concat (".upper", curr_name, NULL));
418 bfd_rename_section (s, concat (".lower", curr_name, NULL));
421 s->name = concat (".either", curr_name, NULL);
431 msp430_elf_after_open (void)
435 gld${EMULATION_NAME}_after_open ();
437 /* If neither --code-region or --data-region have been passed, do not
438 transform sections names. */
439 if ((code_region == REGION_NONE && data_region == REGION_NONE)
440 || disable_sec_transformation)
443 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
444 bfd_map_over_sections (abfd, add_region_prefix, NULL);
447 #define OPTION_CODE_REGION 321
448 #define OPTION_DATA_REGION (OPTION_CODE_REGION + 1)
449 #define OPTION_DISABLE_TRANS (OPTION_CODE_REGION + 2)
452 gld${EMULATION_NAME}_add_options
453 (int ns, char **shortopts, int nl, struct option **longopts,
454 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
456 static const char xtra_short[] = { };
458 static const struct option xtra_long[] =
460 { "code-region", required_argument, NULL, OPTION_CODE_REGION },
461 { "data-region", required_argument, NULL, OPTION_DATA_REGION },
462 { "disable-sec-transformation", no_argument, NULL,
463 OPTION_DISABLE_TRANS },
464 { NULL, no_argument, NULL, 0 }
467 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
468 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
469 *longopts = (struct option *)
470 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
471 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
475 gld${EMULATION_NAME}_list_options (FILE * file)
477 fprintf (file, _(" --code-region={either,lower,upper,none}\n\
478 Transform .text* sections to {either,lower,upper,none}.text* sections\n"));
479 fprintf (file, _(" --data-region={either,lower,upper,none}\n\
480 Transform .data*, .rodata* and .bss* sections to\n\
481 {either,lower,upper,none}.{bss,data,rodata}* sections\n"));
482 fprintf (file, _(" --disable-sec-transformation\n\
483 Disable transformation of .{text,data,bss,rodata}* sections to\n\
484 add the {either,lower,upper,none} prefixes\n"));
488 gld${EMULATION_NAME}_handle_option (int optc)
492 case OPTION_CODE_REGION:
493 if (strcmp (optarg, "upper") == 0)
494 code_region = REGION_UPPER;
495 else if (strcmp (optarg, "lower") == 0)
496 code_region = REGION_LOWER;
497 else if (strcmp (optarg, "either") == 0)
498 code_region = REGION_EITHER;
499 else if (strcmp (optarg, "none") == 0)
500 code_region = REGION_NONE;
501 else if (strlen (optarg) == 0)
503 einfo (_("%P: --code-region requires an argument: "
504 "{upper,lower,either,none}\n"));
509 einfo (_("%P: error: unrecognized argument to --code-region= option: "
510 "\"%s\"\n"), optarg);
515 case OPTION_DATA_REGION:
516 if (strcmp (optarg, "upper") == 0)
517 data_region = REGION_UPPER;
518 else if (strcmp (optarg, "lower") == 0)
519 data_region = REGION_LOWER;
520 else if (strcmp (optarg, "either") == 0)
521 data_region = REGION_EITHER;
522 else if (strcmp (optarg, "none") == 0)
523 data_region = REGION_NONE;
524 else if (strlen (optarg) == 0)
526 einfo (_("%P: --data-region requires an argument: "
527 "{upper,lower,either,none}\n"));
532 einfo (_("%P: error: unrecognized argument to --data-region= option: "
533 "\"%s\"\n"), optarg);
538 case OPTION_DISABLE_TRANS:
539 disable_sec_transformation = TRUE;
549 eval_upper_either_sections (bfd *abfd ATTRIBUTE_UNUSED,
550 asection *s, void *data)
552 const char * base_sec_name;
553 const char * curr_name;
557 lang_output_section_statement_type * lower;
558 lang_output_section_statement_type * upper;
559 static bfd_size_type *lower_size = 0;
560 static bfd_size_type *upper_size = 0;
561 static bfd_size_type lower_size_rom = 0;
562 static bfd_size_type lower_size_ram = 0;
563 static bfd_size_type upper_size_rom = 0;
564 static bfd_size_type upper_size_ram = 0;
566 if ((s->flags & SEC_ALLOC) == 0)
568 if (bfd_link_relocatable (&link_info))
571 base_sec_name = (const char *) data;
572 curr_name = bfd_section_name (s);
574 /* Only concerned with .either input sections in the upper output section. */
575 either_name = concat (".either", base_sec_name, NULL);
576 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
577 || strncmp (s->output_section->name, ".upper", 6) != 0)
580 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
581 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
583 if (upper == NULL || upper->region == NULL)
585 else if (lower == NULL)
586 lower = lang_output_section_find (base_sec_name);
587 if (lower == NULL || lower->region == NULL)
590 if (strcmp (base_sec_name, ".text") == 0
591 || strcmp (base_sec_name, ".rodata") == 0)
596 if (curr_region == ROM)
598 if (lower_size_rom == 0)
600 lower_size_rom = lower->region->current - lower->region->origin;
601 upper_size_rom = upper->region->current - upper->region->origin;
603 lower_size = &lower_size_rom;
604 upper_size = &upper_size_rom;
606 else if (curr_region == RAM)
608 if (lower_size_ram == 0)
610 lower_size_ram = lower->region->current - lower->region->origin;
611 upper_size_ram = upper->region->current - upper->region->origin;
613 lower_size = &lower_size_ram;
614 upper_size = &upper_size_ram;
617 /* If the upper region is overflowing, try moving sections to the lower
619 Note that there isn't any general benefit to using lower memory over upper
620 memory, so we only move sections around with the goal of making the program
622 if (*upper_size > upper->region->length
623 && *lower_size + s->size < lower->region->length)
625 if (change_output_section (&(upper->children.head), s, lower, upper))
627 *upper_size -= s->size;
628 *lower_size += s->size;
636 eval_lower_either_sections (bfd *abfd ATTRIBUTE_UNUSED,
637 asection *s, void *data)
639 const char * base_sec_name;
640 const char * curr_name;
643 lang_output_section_statement_type * output_sec;
644 lang_output_section_statement_type * lower;
645 lang_output_section_statement_type * upper;
647 static bfd_size_type *lower_size = 0;
648 static bfd_size_type lower_size_rom = 0;
649 static bfd_size_type lower_size_ram = 0;
651 if ((s->flags & SEC_ALLOC) == 0)
653 if (bfd_link_relocatable (&link_info))
656 base_sec_name = (const char *) data;
657 curr_name = bfd_section_name (s);
659 /* Only concerned with .either input sections in the lower or "default"
660 output section i.e. not in the upper output section. */
661 either_name = concat (".either", base_sec_name, NULL);
662 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
663 || strncmp (s->output_section->name, ".upper", 6) == 0)
666 if (strcmp (base_sec_name, ".text") == 0
667 || strcmp (base_sec_name, ".rodata") == 0)
672 output_sec = lang_output_section_find (s->output_section->name);
674 /* If the output_section doesn't exist, this has already been reported in
675 place_orphan, so don't need to warn again. */
676 if (output_sec == NULL || output_sec->region == NULL)
679 /* lower and output_sec might be the same, but in some cases an .either
680 section can end up in base_sec_name if it hasn't been placed by
682 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
683 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
687 if (curr_region == ROM)
689 if (lower_size_rom == 0)
691 /* Get the size of other items in the lower region that aren't the
692 sections to be moved around. */
694 = (output_sec->region->current - output_sec->region->origin)
695 - scan_children (output_sec->children.head);
696 if (output_sec != lower && lower != NULL)
697 lower_size_rom -= scan_children (lower->children.head);
699 lower_size = &lower_size_rom;
701 else if (curr_region == RAM)
703 if (lower_size_ram == 0)
706 = (output_sec->region->current - output_sec->region->origin)
707 - scan_children (output_sec->children.head);
708 if (output_sec != lower && lower != NULL)
709 lower_size_ram -= scan_children (lower->children.head);
711 lower_size = &lower_size_ram;
713 /* Move sections that cause the lower region to overflow to the upper region. */
714 if (*lower_size + s->size > output_sec->region->length)
715 change_output_section (&(output_sec->children.head), s, upper, output_sec);
717 *lower_size += s->size;
722 /* This function is similar to lang_relax_sections, but without the size
723 evaluation code that is always executed after relaxation. */
725 intermediate_relax_sections (void)
727 int i = link_info.relax_pass;
729 /* The backend can use it to determine the current pass. */
730 link_info.relax_pass = 0;
734 bfd_boolean relax_again;
736 link_info.relax_trip = -1;
739 link_info.relax_trip++;
741 lang_do_assignments (lang_assigning_phase_enum);
743 lang_reset_memory_regions ();
746 lang_size_sections (&relax_again, FALSE);
750 link_info.relax_pass++;
755 msp430_elf_after_allocation (void)
759 /* Go over each section twice, once to place either sections that don't fit
760 in lower into upper, and then again to move any sections in upper that
761 fit in lower into lower. */
762 for (i = 0; i < 8; i++)
764 int placement_stage = (i < 4) ? LOWER_TO_UPPER : UPPER_TO_LOWER;
765 const char * base_sec_name;
766 lang_output_section_statement_type * upper;
772 base_sec_name = ".text";
775 base_sec_name = ".data";
778 base_sec_name = ".bss";
781 base_sec_name = ".rodata";
784 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
787 /* Can't just use one iteration over the all the sections to make
788 both lower->upper and upper->lower transformations because the
789 iterator encounters upper sections before all lower sections have
793 if (placement_stage == LOWER_TO_UPPER)
795 /* Perform relaxation and get the final size of sections
796 before trying to fit .either sections in the correct
798 if (relax_count == 0)
800 intermediate_relax_sections ();
803 for (abfd = link_info.input_bfds; abfd != NULL;
804 abfd = abfd->link.next)
806 bfd_map_over_sections (abfd, eval_lower_either_sections,
807 (void *) base_sec_name);
810 else if (placement_stage == UPPER_TO_LOWER)
812 /* Relax again before moving upper->lower. */
813 if (relax_count == 1)
815 intermediate_relax_sections ();
818 for (abfd = link_info.input_bfds; abfd != NULL;
819 abfd = abfd->link.next)
821 bfd_map_over_sections (abfd, eval_upper_either_sections,
822 (void *) base_sec_name);
828 gld${EMULATION_NAME}_after_allocation ();
831 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
833 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
834 ${LDEMUL_SYSLIB-syslib_default},
835 ${LDEMUL_HLL-hll_default},
836 ${LDEMUL_AFTER_PARSE-after_parse_default},
837 msp430_elf_after_open,
838 after_check_relocs_default,
839 before_place_orphans_default,
840 msp430_elf_after_allocation,
841 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
842 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
843 ${LDEMUL_BEFORE_ALLOCATION-before_allocation_default},
844 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
847 ${LDEMUL_FINISH-finish_default},
848 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
849 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-NULL},
850 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
851 ${LDEMUL_SET_SYMBOLS-NULL},
852 ${LDEMUL_PARSE_ARGS-NULL},
853 gld${EMULATION_NAME}_add_options,
854 gld${EMULATION_NAME}_handle_option,
855 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
856 gld${EMULATION_NAME}_list_options,
857 ${LDEMUL_RECOGNIZED_FILE-NULL},
858 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
859 ${LDEMUL_NEW_VERS_PATTERN-NULL},
860 ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL},
861 ${LDEMUL_EMIT_CTF_EARLY-NULL},
862 ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL},
863 ${LDEMUL_PRINT_SYMBOL-NULL}