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-2025 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}
44 #include "libiberty.h"
55 enum either_placement_stage
63 static int data_region = REGION_NONE;
64 static int code_region = REGION_NONE;
65 static bool disable_sec_transformation = false;
67 #define MAX_PREFIX_LENGTH 7
71 # Import any needed special functions and/or overrides.
73 if test -n "$EXTRA_EM_FILE" ; then
74 source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
77 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
81 gld${EMULATION_NAME}_before_parse (void)
83 #ifndef TARGET_ /* I.e., if not generic. */
84 ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
85 #endif /* not TARGET_ */
87 /* The MSP430 port *needs* linker relaxtion in order to cope with large
88 functions where conditional branches do not fit into a +/- 1024 byte range. */
89 if (!bfd_link_relocatable (&link_info))
90 TARGET_ENABLE_RELAXATION;
96 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
100 gld${EMULATION_NAME}_get_script (int *isfile)
103 if test x"$COMPILE_IN" = xyes
105 # Scripts compiled in.
107 # sed commands to quote an ld script as a C string.
108 sc="-f ${srcdir}/emultempl/stringify.sed"
114 if (bfd_link_relocatable (&link_info) && config.build_constructors)
117 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
118 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
119 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
120 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
121 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
122 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
123 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
124 echo ' ; else return' >> e${EMULATION_NAME}.c
125 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
126 echo '; }' >> e${EMULATION_NAME}.c
129 # Scripts read from the filesystem.
135 if (bfd_link_relocatable (&link_info) && config.build_constructors)
136 return "ldscripts/${EMULATION_NAME}.xu";
137 else if (bfd_link_relocatable (&link_info))
138 return "ldscripts/${EMULATION_NAME}.xr";
139 else if (!config.text_read_only)
140 return "ldscripts/${EMULATION_NAME}.xbn";
141 else if (!config.magic_demand_paged)
142 return "ldscripts/${EMULATION_NAME}.xn";
144 return "ldscripts/${EMULATION_NAME}.x";
150 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
154 data_statement_size (lang_data_statement_type *d)
156 unsigned int size = 0;
173 einfo (_("%P: error: unhandled data_statement size\n"));
179 /* Helper function for place_orphan that computes the size
180 of sections already mapped to the given statement. */
183 scan_children (lang_statement_union_type * l)
185 bfd_size_type amount = 0;
189 switch (l->header.type)
191 case lang_input_section_enum:
192 if (l->input_section.section->flags & SEC_ALLOC)
193 amount += l->input_section.section->size;
196 case lang_constructors_statement_enum:
197 case lang_assignment_statement_enum:
198 case lang_padding_statement_enum:
201 case lang_wild_statement_enum:
202 amount += scan_children (l->wild_statement.children.head);
205 case lang_data_statement_enum:
206 amount += data_statement_size (&l->data_statement);
210 fprintf (stderr, "msp430 orphan placer: unhandled lang type %d\n", l->header.type);
225 #define WARN_RODATA 3
227 /* Warn only once per output section.
228 * NAME starts with ".upper." or ".lower.". */
230 warn_no_output_section (const char *name)
232 static bool warned[2][4] = {{false, false, false, false},
233 {false, false, false, false}};
236 if (strncmp (name, ".upper.", 7) == 0)
239 if (!warned[i][WARN_TEXT] && strcmp (name + 6, ".text") == 0)
240 warned[i][WARN_TEXT] = true;
241 else if (!warned[i][WARN_DATA] && strcmp (name + 6, ".data") == 0)
242 warned[i][WARN_DATA] = true;
243 else if (!warned[i][WARN_BSS] && strcmp (name + 6, ".bss") == 0)
244 warned[i][WARN_BSS] = true;
245 else if (!warned[i][WARN_RODATA] && strcmp (name + 6, ".rodata") == 0)
246 warned[i][WARN_RODATA] = true;
249 einfo ("%P: warning: no input section rule matches %s in linker script\n",
254 /* Place an orphan section. We use this to put .either sections
255 into either their lower or their upper equivalents. */
257 static lang_output_section_statement_type *
258 gld${EMULATION_NAME}_place_orphan (asection * s,
259 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 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, NULL, lower);
341 change_output_section (lang_statement_union_type **head,
343 lang_output_section_statement_type *new_os,
344 lang_output_section_statement_type *old_os)
347 lang_statement_union_type * prev = NULL;
348 lang_statement_union_type * curr;
353 switch (curr->header.type)
355 case lang_input_section_enum:
356 is = curr->input_section.section;
359 lang_statement_list_type *old_list
360 = (lang_statement_list_type *) &old_os->children;
361 s->output_section = NULL;
362 lang_add_section (&new_os->children, s,
363 curr->input_section.pattern, 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 bfd_rename_section (s, 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);
448 gld${EMULATION_NAME}_add_options
449 (int ns, char **shortopts, int nl, struct option **longopts,
450 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
452 static const char xtra_short[] = { };
454 static const struct option xtra_long[] =
456 { "code-region", required_argument, NULL, OPTION_CODE_REGION },
457 { "data-region", required_argument, NULL, OPTION_DATA_REGION },
458 { "disable-sec-transformation", no_argument, NULL,
459 OPTION_DISABLE_TRANS },
460 { NULL, no_argument, NULL, 0 }
463 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
464 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
465 *longopts = (struct option *)
466 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
467 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
471 gld${EMULATION_NAME}_list_options (FILE * file)
473 fprintf (file, _(" --code-region={either,lower,upper,none}\n\
474 Transform .text* sections to {either,lower,upper,none}.text* sections\n"));
475 fprintf (file, _(" --data-region={either,lower,upper,none}\n\
476 Transform .data*, .rodata* and .bss* sections to\n\
477 {either,lower,upper,none}.{bss,data,rodata}* sections\n"));
478 fprintf (file, _(" --disable-sec-transformation\n\
479 Disable transformation of .{text,data,bss,rodata}* sections to\n\
480 add the {either,lower,upper,none} prefixes\n"));
484 gld${EMULATION_NAME}_handle_option (int optc)
488 case OPTION_CODE_REGION:
489 if (strcmp (optarg, "upper") == 0)
490 code_region = REGION_UPPER;
491 else if (strcmp (optarg, "lower") == 0)
492 code_region = REGION_LOWER;
493 else if (strcmp (optarg, "either") == 0)
494 code_region = REGION_EITHER;
495 else if (strcmp (optarg, "none") == 0)
496 code_region = REGION_NONE;
497 else if (strlen (optarg) == 0)
499 einfo (_("%P: --code-region requires an argument: "
500 "{upper,lower,either,none}\n"));
505 einfo (_("%P: error: unrecognized argument to --code-region= option: "
506 "\"%s\"\n"), optarg);
511 case OPTION_DATA_REGION:
512 if (strcmp (optarg, "upper") == 0)
513 data_region = REGION_UPPER;
514 else if (strcmp (optarg, "lower") == 0)
515 data_region = REGION_LOWER;
516 else if (strcmp (optarg, "either") == 0)
517 data_region = REGION_EITHER;
518 else if (strcmp (optarg, "none") == 0)
519 data_region = REGION_NONE;
520 else if (strlen (optarg) == 0)
522 einfo (_("%P: --data-region requires an argument: "
523 "{upper,lower,either,none}\n"));
528 einfo (_("%P: error: unrecognized argument to --data-region= option: "
529 "\"%s\"\n"), optarg);
534 case OPTION_DISABLE_TRANS:
535 disable_sec_transformation = true;
545 eval_upper_either_sections (bfd *abfd ATTRIBUTE_UNUSED,
546 asection *s, void *data)
548 const char * base_sec_name;
549 const char * curr_name;
553 lang_output_section_statement_type * lower;
554 lang_output_section_statement_type * upper;
555 static bfd_size_type *lower_size = 0;
556 static bfd_size_type *upper_size = 0;
557 static bfd_size_type lower_size_rom = 0;
558 static bfd_size_type lower_size_ram = 0;
559 static bfd_size_type upper_size_rom = 0;
560 static bfd_size_type upper_size_ram = 0;
562 if ((s->flags & SEC_ALLOC) == 0)
564 if (bfd_link_relocatable (&link_info))
567 base_sec_name = (const char *) data;
568 curr_name = bfd_section_name (s);
570 /* Only concerned with .either input sections in the upper output section. */
571 either_name = concat (".either", base_sec_name, NULL);
572 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
573 || strncmp (s->output_section->name, ".upper", 6) != 0)
576 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
577 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
579 if (upper == NULL || upper->region == NULL)
581 else if (lower == NULL)
582 lower = lang_output_section_find (base_sec_name);
583 if (lower == NULL || lower->region == NULL)
586 if (strcmp (base_sec_name, ".text") == 0
587 || strcmp (base_sec_name, ".rodata") == 0)
592 if (curr_region == ROM)
594 if (lower_size_rom == 0)
596 lower_size_rom = lower->region->current - lower->region->origin;
597 upper_size_rom = upper->region->current - upper->region->origin;
599 lower_size = &lower_size_rom;
600 upper_size = &upper_size_rom;
602 else if (curr_region == RAM)
604 if (lower_size_ram == 0)
606 lower_size_ram = lower->region->current - lower->region->origin;
607 upper_size_ram = upper->region->current - upper->region->origin;
609 lower_size = &lower_size_ram;
610 upper_size = &upper_size_ram;
613 /* If the upper region is overflowing, try moving sections to the lower
615 Note that there isn't any general benefit to using lower memory over upper
616 memory, so we only move sections around with the goal of making the program
618 if (*upper_size > upper->region->length
619 && *lower_size + s->size < lower->region->length)
621 if (change_output_section (&(upper->children.head), s, lower, upper))
623 *upper_size -= s->size;
624 *lower_size += s->size;
632 eval_lower_either_sections (bfd *abfd ATTRIBUTE_UNUSED,
633 asection *s, void *data)
635 const char * base_sec_name;
636 const char * curr_name;
639 lang_output_section_statement_type * output_sec;
640 lang_output_section_statement_type * lower;
641 lang_output_section_statement_type * upper;
643 static bfd_size_type *lower_size = 0;
644 static bfd_size_type lower_size_rom = 0;
645 static bfd_size_type lower_size_ram = 0;
647 if ((s->flags & SEC_ALLOC) == 0)
649 if (bfd_link_relocatable (&link_info))
652 base_sec_name = (const char *) data;
653 curr_name = bfd_section_name (s);
655 /* Only concerned with .either input sections in the lower or "default"
656 output section i.e. not in the upper output section. */
657 either_name = concat (".either", base_sec_name, NULL);
658 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
659 || strncmp (s->output_section->name, ".upper", 6) == 0)
662 if (strcmp (base_sec_name, ".text") == 0
663 || strcmp (base_sec_name, ".rodata") == 0)
668 output_sec = lang_output_section_find (s->output_section->name);
670 /* If the output_section doesn't exist, this has already been reported in
671 place_orphan, so don't need to warn again. */
672 if (output_sec == NULL || output_sec->region == NULL)
675 /* lower and output_sec might be the same, but in some cases an .either
676 section can end up in base_sec_name if it hasn't been placed by
678 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
679 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
683 if (curr_region == ROM)
685 if (lower_size_rom == 0)
687 /* Get the size of other items in the lower region that aren't the
688 sections to be moved around. */
690 = (output_sec->region->current - output_sec->region->origin)
691 - scan_children (output_sec->children.head);
692 if (output_sec != lower && lower != NULL)
693 lower_size_rom -= scan_children (lower->children.head);
695 lower_size = &lower_size_rom;
697 else if (curr_region == RAM)
699 if (lower_size_ram == 0)
702 = (output_sec->region->current - output_sec->region->origin)
703 - scan_children (output_sec->children.head);
704 if (output_sec != lower && lower != NULL)
705 lower_size_ram -= scan_children (lower->children.head);
707 lower_size = &lower_size_ram;
709 /* Move sections that cause the lower region to overflow to the upper region. */
710 if (*lower_size + s->size > output_sec->region->length)
711 change_output_section (&(output_sec->children.head), s, upper, output_sec);
713 *lower_size += s->size;
718 /* This function is similar to lang_relax_sections, but without the size
719 evaluation code that is always executed after relaxation. */
721 intermediate_relax_sections (void)
723 int i = link_info.relax_pass;
725 /* The backend can use it to determine the current pass. */
726 link_info.relax_pass = 0;
732 link_info.relax_trip = -1;
735 link_info.relax_trip++;
737 lang_do_assignments (lang_assigning_phase_enum);
739 lang_reset_memory_regions ();
742 lang_size_sections (&relax_again, false);
746 link_info.relax_pass++;
751 msp430_elf_after_allocation (void)
755 /* Go over each section twice, once to place either sections that don't fit
756 in lower into upper, and then again to move any sections in upper that
757 fit in lower into lower. */
758 for (i = 0; i < 8; i++)
760 int placement_stage = (i < 4) ? LOWER_TO_UPPER : UPPER_TO_LOWER;
761 const char * base_sec_name;
762 lang_output_section_statement_type * upper;
768 base_sec_name = ".text";
771 base_sec_name = ".data";
774 base_sec_name = ".bss";
777 base_sec_name = ".rodata";
780 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
783 /* Can't just use one iteration over the all the sections to make
784 both lower->upper and upper->lower transformations because the
785 iterator encounters upper sections before all lower sections have
789 if (placement_stage == LOWER_TO_UPPER)
791 /* Perform relaxation and get the final size of sections
792 before trying to fit .either sections in the correct
794 if (relax_count == 0)
796 intermediate_relax_sections ();
799 for (abfd = link_info.input_bfds; abfd != NULL;
800 abfd = abfd->link.next)
802 bfd_map_over_sections (abfd, eval_lower_either_sections,
803 (void *) base_sec_name);
806 else if (placement_stage == UPPER_TO_LOWER)
808 /* Relax again before moving upper->lower. */
809 if (relax_count == 1)
811 intermediate_relax_sections ();
814 for (abfd = link_info.input_bfds; abfd != NULL;
815 abfd = abfd->link.next)
817 bfd_map_over_sections (abfd, eval_upper_either_sections,
818 (void *) base_sec_name);
824 gld${EMULATION_NAME}_after_allocation ();
827 /* Return TRUE if a non-debug input section in L has positive size and matches
830 input_section_exists (lang_statement_union_type * l, const char * name)
834 switch (l->header.type)
836 case lang_input_section_enum:
837 if ((l->input_section.section->flags & SEC_ALLOC)
838 && l->input_section.section->size > 0
839 && !strcmp (l->input_section.section->name, name))
843 case lang_wild_statement_enum:
844 if (input_section_exists (l->wild_statement.children.head, name))
856 /* Some MSP430 linker scripts do not include ALIGN directives to ensure
857 __preinit_array_start, __init_array_start or __fini_array_start are word
859 If __*_array_start symbols are not word aligned, the code in crt0 to run
860 through the array and call the functions will crash.
861 To avoid warning unnecessarily when the .*_array sections are not being
862 used for running constructors/destructors, only emit the warning if
863 the associated section exists and has size. */
865 check_array_section_alignment (void)
868 lang_output_section_statement_type * rodata_sec;
869 lang_output_section_statement_type * rodata2_sec;
870 const char * array_names[3][2] = { { ".init_array", "__init_array_start" },
871 { ".preinit_array", "__preinit_array_start" },
872 { ".fini_array", "__fini_array_start" } };
874 /* .{preinit,init,fini}_array could be in either .rodata or .rodata2. */
875 rodata_sec = lang_output_section_find (".rodata");
876 rodata2_sec = lang_output_section_find (".rodata2");
877 if (rodata_sec == NULL && rodata2_sec == NULL)
880 /* There are 3 .*_array sections which must be checked for alignment. */
881 for (i = 0; i < 3; i++)
883 struct bfd_link_hash_entry * sym;
884 if (((rodata_sec && input_section_exists (rodata_sec->children.head,
886 || (rodata2_sec && input_section_exists (rodata2_sec->children.head,
888 && (sym = bfd_link_hash_lookup (link_info.hash, array_names[i][1],
890 && sym->type == bfd_link_hash_defined
891 && sym->u.def.value % 2)
893 einfo ("%P: warning: \"%s\" symbol (%pU) is not word aligned\n",
894 array_names[i][1], NULL);
900 gld${EMULATION_NAME}_finish (void)
903 check_array_section_alignment ();
907 LDEMUL_AFTER_OPEN=msp430_elf_after_open
908 LDEMUL_AFTER_ALLOCATION=msp430_elf_after_allocation
909 LDEMUL_PLACE_ORPHAN=${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan}
910 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
911 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
912 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
913 LDEMUL_LIST_OPTIONS=gld${EMULATION_NAME}_list_options
915 source_em ${srcdir}/emultempl/emulation.em