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-2022 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 bool 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 bool 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,
264 lang_output_section_statement_type * lower;
265 lang_output_section_statement_type * upper;
267 if ((s->flags & SEC_ALLOC) == 0)
270 if (bfd_link_relocatable (&link_info))
273 /* If constraints are involved let the linker handle the placement normally. */
277 if (strncmp (secname, ".upper.", 7) == 0
278 || strncmp (secname, ".lower.", 7) == 0)
280 warn_no_output_section (secname);
284 /* We only need special handling for .either sections. */
285 if (strncmp (secname, ".either.", 8) != 0)
288 /* Skip the .either prefix. */
291 /* Compute the names of the corresponding upper and lower
292 sections. If the input section name contains another period,
293 only use the part of the name before the second dot. */
294 if (strchr (secname + 1, '.') != NULL)
296 name = xstrdup (secname);
298 * strchr (name + 1, '.') = 0;
301 name = (char *) secname;
303 lower_name = concat (".lower", name, NULL);
304 upper_name = concat (".upper", name, NULL);
306 /* Find the corresponding lower and upper sections. */
307 lower = lang_output_section_find (lower_name);
308 upper = lang_output_section_find (upper_name);
310 if (lower == NULL && upper == NULL)
312 einfo (_("%P: error: no section named %s or %s in linker script\n"),
313 lower_name, upper_name);
316 else if (lower == NULL)
318 lower = lang_output_section_find (name);
321 einfo (_("%P: error: no section named %s in linker script\n"), name);
326 /* Always place orphaned sections in lower. Optimal placement of either
327 sections is performed later, once section sizes have been finalized. */
328 lang_add_section (& lower->children, s, NULL, NULL, lower);
340 change_output_section (lang_statement_union_type **head,
342 lang_output_section_statement_type *new_os,
343 lang_output_section_statement_type *old_os)
346 lang_statement_union_type * prev = NULL;
347 lang_statement_union_type * curr;
352 switch (curr->header.type)
354 case lang_input_section_enum:
355 is = curr->input_section.section;
358 lang_statement_list_type *old_list
359 = (lang_statement_list_type *) &old_os->children;
360 s->output_section = NULL;
361 lang_add_section (&new_os->children, s,
362 curr->input_section.pattern, NULL, new_os);
364 /* Remove the section from the old output section. */
366 *head = curr->header.next;
368 prev->header.next = curr->header.next;
369 /* If the input section we just moved is the tail of the old
370 output section, then we also need to adjust that tail. */
371 if (old_list->tail == (lang_statement_union_type **) curr)
372 old_list->tail = (lang_statement_union_type **) prev;
377 case lang_wild_statement_enum:
378 if (change_output_section (&(curr->wild_statement.children.head),
386 curr = curr->header.next;
392 add_region_prefix (bfd *abfd ATTRIBUTE_UNUSED, asection *s,
393 void *unused ATTRIBUTE_UNUSED)
395 const char *curr_name = bfd_section_name (s);
396 int region = REGION_NONE;
398 if (strncmp (curr_name, ".text", 5) == 0)
399 region = code_region;
400 else if (strncmp (curr_name, ".data", 5) == 0)
401 region = data_region;
402 else if (strncmp (curr_name, ".bss", 4) == 0)
403 region = data_region;
404 else if (strncmp (curr_name, ".rodata", 7) == 0)
405 region = data_region;
414 bfd_rename_section (s, concat (".upper", curr_name, NULL));
417 bfd_rename_section (s, concat (".lower", curr_name, NULL));
420 s->name = concat (".either", curr_name, NULL);
430 msp430_elf_after_open (void)
434 gld${EMULATION_NAME}_after_open ();
436 /* If neither --code-region or --data-region have been passed, do not
437 transform sections names. */
438 if ((code_region == REGION_NONE && data_region == REGION_NONE)
439 || disable_sec_transformation)
442 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
443 bfd_map_over_sections (abfd, add_region_prefix, NULL);
446 #define OPTION_CODE_REGION 321
447 #define OPTION_DATA_REGION (OPTION_CODE_REGION + 1)
448 #define OPTION_DISABLE_TRANS (OPTION_CODE_REGION + 2)
451 gld${EMULATION_NAME}_add_options
452 (int ns, char **shortopts, int nl, struct option **longopts,
453 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
455 static const char xtra_short[] = { };
457 static const struct option xtra_long[] =
459 { "code-region", required_argument, NULL, OPTION_CODE_REGION },
460 { "data-region", required_argument, NULL, OPTION_DATA_REGION },
461 { "disable-sec-transformation", no_argument, NULL,
462 OPTION_DISABLE_TRANS },
463 { NULL, no_argument, NULL, 0 }
466 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
467 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
468 *longopts = (struct option *)
469 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
470 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
474 gld${EMULATION_NAME}_list_options (FILE * file)
476 fprintf (file, _(" --code-region={either,lower,upper,none}\n\
477 Transform .text* sections to {either,lower,upper,none}.text* sections\n"));
478 fprintf (file, _(" --data-region={either,lower,upper,none}\n\
479 Transform .data*, .rodata* and .bss* sections to\n\
480 {either,lower,upper,none}.{bss,data,rodata}* sections\n"));
481 fprintf (file, _(" --disable-sec-transformation\n\
482 Disable transformation of .{text,data,bss,rodata}* sections to\n\
483 add the {either,lower,upper,none} prefixes\n"));
487 gld${EMULATION_NAME}_handle_option (int optc)
491 case OPTION_CODE_REGION:
492 if (strcmp (optarg, "upper") == 0)
493 code_region = REGION_UPPER;
494 else if (strcmp (optarg, "lower") == 0)
495 code_region = REGION_LOWER;
496 else if (strcmp (optarg, "either") == 0)
497 code_region = REGION_EITHER;
498 else if (strcmp (optarg, "none") == 0)
499 code_region = REGION_NONE;
500 else if (strlen (optarg) == 0)
502 einfo (_("%P: --code-region requires an argument: "
503 "{upper,lower,either,none}\n"));
508 einfo (_("%P: error: unrecognized argument to --code-region= option: "
509 "\"%s\"\n"), optarg);
514 case OPTION_DATA_REGION:
515 if (strcmp (optarg, "upper") == 0)
516 data_region = REGION_UPPER;
517 else if (strcmp (optarg, "lower") == 0)
518 data_region = REGION_LOWER;
519 else if (strcmp (optarg, "either") == 0)
520 data_region = REGION_EITHER;
521 else if (strcmp (optarg, "none") == 0)
522 data_region = REGION_NONE;
523 else if (strlen (optarg) == 0)
525 einfo (_("%P: --data-region requires an argument: "
526 "{upper,lower,either,none}\n"));
531 einfo (_("%P: error: unrecognized argument to --data-region= option: "
532 "\"%s\"\n"), optarg);
537 case OPTION_DISABLE_TRANS:
538 disable_sec_transformation = true;
548 eval_upper_either_sections (bfd *abfd ATTRIBUTE_UNUSED,
549 asection *s, void *data)
551 const char * base_sec_name;
552 const char * curr_name;
556 lang_output_section_statement_type * lower;
557 lang_output_section_statement_type * upper;
558 static bfd_size_type *lower_size = 0;
559 static bfd_size_type *upper_size = 0;
560 static bfd_size_type lower_size_rom = 0;
561 static bfd_size_type lower_size_ram = 0;
562 static bfd_size_type upper_size_rom = 0;
563 static bfd_size_type upper_size_ram = 0;
565 if ((s->flags & SEC_ALLOC) == 0)
567 if (bfd_link_relocatable (&link_info))
570 base_sec_name = (const char *) data;
571 curr_name = bfd_section_name (s);
573 /* Only concerned with .either input sections in the upper output section. */
574 either_name = concat (".either", base_sec_name, NULL);
575 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
576 || strncmp (s->output_section->name, ".upper", 6) != 0)
579 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
580 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
582 if (upper == NULL || upper->region == NULL)
584 else if (lower == NULL)
585 lower = lang_output_section_find (base_sec_name);
586 if (lower == NULL || lower->region == NULL)
589 if (strcmp (base_sec_name, ".text") == 0
590 || strcmp (base_sec_name, ".rodata") == 0)
595 if (curr_region == ROM)
597 if (lower_size_rom == 0)
599 lower_size_rom = lower->region->current - lower->region->origin;
600 upper_size_rom = upper->region->current - upper->region->origin;
602 lower_size = &lower_size_rom;
603 upper_size = &upper_size_rom;
605 else if (curr_region == RAM)
607 if (lower_size_ram == 0)
609 lower_size_ram = lower->region->current - lower->region->origin;
610 upper_size_ram = upper->region->current - upper->region->origin;
612 lower_size = &lower_size_ram;
613 upper_size = &upper_size_ram;
616 /* If the upper region is overflowing, try moving sections to the lower
618 Note that there isn't any general benefit to using lower memory over upper
619 memory, so we only move sections around with the goal of making the program
621 if (*upper_size > upper->region->length
622 && *lower_size + s->size < lower->region->length)
624 if (change_output_section (&(upper->children.head), s, lower, upper))
626 *upper_size -= s->size;
627 *lower_size += s->size;
635 eval_lower_either_sections (bfd *abfd ATTRIBUTE_UNUSED,
636 asection *s, void *data)
638 const char * base_sec_name;
639 const char * curr_name;
642 lang_output_section_statement_type * output_sec;
643 lang_output_section_statement_type * lower;
644 lang_output_section_statement_type * upper;
646 static bfd_size_type *lower_size = 0;
647 static bfd_size_type lower_size_rom = 0;
648 static bfd_size_type lower_size_ram = 0;
650 if ((s->flags & SEC_ALLOC) == 0)
652 if (bfd_link_relocatable (&link_info))
655 base_sec_name = (const char *) data;
656 curr_name = bfd_section_name (s);
658 /* Only concerned with .either input sections in the lower or "default"
659 output section i.e. not in the upper output section. */
660 either_name = concat (".either", base_sec_name, NULL);
661 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
662 || strncmp (s->output_section->name, ".upper", 6) == 0)
665 if (strcmp (base_sec_name, ".text") == 0
666 || strcmp (base_sec_name, ".rodata") == 0)
671 output_sec = lang_output_section_find (s->output_section->name);
673 /* If the output_section doesn't exist, this has already been reported in
674 place_orphan, so don't need to warn again. */
675 if (output_sec == NULL || output_sec->region == NULL)
678 /* lower and output_sec might be the same, but in some cases an .either
679 section can end up in base_sec_name if it hasn't been placed by
681 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
682 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
686 if (curr_region == ROM)
688 if (lower_size_rom == 0)
690 /* Get the size of other items in the lower region that aren't the
691 sections to be moved around. */
693 = (output_sec->region->current - output_sec->region->origin)
694 - scan_children (output_sec->children.head);
695 if (output_sec != lower && lower != NULL)
696 lower_size_rom -= scan_children (lower->children.head);
698 lower_size = &lower_size_rom;
700 else if (curr_region == RAM)
702 if (lower_size_ram == 0)
705 = (output_sec->region->current - output_sec->region->origin)
706 - scan_children (output_sec->children.head);
707 if (output_sec != lower && lower != NULL)
708 lower_size_ram -= scan_children (lower->children.head);
710 lower_size = &lower_size_ram;
712 /* Move sections that cause the lower region to overflow to the upper region. */
713 if (*lower_size + s->size > output_sec->region->length)
714 change_output_section (&(output_sec->children.head), s, upper, output_sec);
716 *lower_size += s->size;
721 /* This function is similar to lang_relax_sections, but without the size
722 evaluation code that is always executed after relaxation. */
724 intermediate_relax_sections (void)
726 int i = link_info.relax_pass;
728 /* The backend can use it to determine the current pass. */
729 link_info.relax_pass = 0;
735 link_info.relax_trip = -1;
738 link_info.relax_trip++;
740 lang_do_assignments (lang_assigning_phase_enum);
742 lang_reset_memory_regions ();
745 lang_size_sections (&relax_again, false);
749 link_info.relax_pass++;
754 msp430_elf_after_allocation (void)
758 /* Go over each section twice, once to place either sections that don't fit
759 in lower into upper, and then again to move any sections in upper that
760 fit in lower into lower. */
761 for (i = 0; i < 8; i++)
763 int placement_stage = (i < 4) ? LOWER_TO_UPPER : UPPER_TO_LOWER;
764 const char * base_sec_name;
765 lang_output_section_statement_type * upper;
771 base_sec_name = ".text";
774 base_sec_name = ".data";
777 base_sec_name = ".bss";
780 base_sec_name = ".rodata";
783 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
786 /* Can't just use one iteration over the all the sections to make
787 both lower->upper and upper->lower transformations because the
788 iterator encounters upper sections before all lower sections have
792 if (placement_stage == LOWER_TO_UPPER)
794 /* Perform relaxation and get the final size of sections
795 before trying to fit .either sections in the correct
797 if (relax_count == 0)
799 intermediate_relax_sections ();
802 for (abfd = link_info.input_bfds; abfd != NULL;
803 abfd = abfd->link.next)
805 bfd_map_over_sections (abfd, eval_lower_either_sections,
806 (void *) base_sec_name);
809 else if (placement_stage == UPPER_TO_LOWER)
811 /* Relax again before moving upper->lower. */
812 if (relax_count == 1)
814 intermediate_relax_sections ();
817 for (abfd = link_info.input_bfds; abfd != NULL;
818 abfd = abfd->link.next)
820 bfd_map_over_sections (abfd, eval_upper_either_sections,
821 (void *) base_sec_name);
827 gld${EMULATION_NAME}_after_allocation ();
830 /* Return TRUE if a non-debug input section in L has positive size and matches
833 input_section_exists (lang_statement_union_type * l, const char * name)
837 switch (l->header.type)
839 case lang_input_section_enum:
840 if ((l->input_section.section->flags & SEC_ALLOC)
841 && l->input_section.section->size > 0
842 && !strcmp (l->input_section.section->name, name))
846 case lang_wild_statement_enum:
847 if (input_section_exists (l->wild_statement.children.head, name))
859 /* Some MSP430 linker scripts do not include ALIGN directives to ensure
860 __preinit_array_start, __init_array_start or __fini_array_start are word
862 If __*_array_start symbols are not word aligned, the code in crt0 to run
863 through the array and call the functions will crash.
864 To avoid warning unnecessarily when the .*_array sections are not being
865 used for running constructors/destructors, only emit the warning if
866 the associated section exists and has size. */
868 check_array_section_alignment (void)
871 lang_output_section_statement_type * rodata_sec;
872 lang_output_section_statement_type * rodata2_sec;
873 const char * array_names[3][2] = { { ".init_array", "__init_array_start" },
874 { ".preinit_array", "__preinit_array_start" },
875 { ".fini_array", "__fini_array_start" } };
877 /* .{preinit,init,fini}_array could be in either .rodata or .rodata2. */
878 rodata_sec = lang_output_section_find (".rodata");
879 rodata2_sec = lang_output_section_find (".rodata2");
880 if (rodata_sec == NULL && rodata2_sec == NULL)
883 /* There are 3 .*_array sections which must be checked for alignment. */
884 for (i = 0; i < 3; i++)
886 struct bfd_link_hash_entry * sym;
887 if (((rodata_sec && input_section_exists (rodata_sec->children.head,
889 || (rodata2_sec && input_section_exists (rodata2_sec->children.head,
891 && (sym = bfd_link_hash_lookup (link_info.hash, array_names[i][1],
893 && sym->type == bfd_link_hash_defined
894 && sym->u.def.value % 2)
896 einfo ("%P: warning: \"%s\" symbol (%pU) is not word aligned\n",
897 array_names[i][1], NULL);
903 gld${EMULATION_NAME}_finish (void)
906 check_array_section_alignment ();
910 LDEMUL_AFTER_OPEN=msp430_elf_after_open
911 LDEMUL_AFTER_ALLOCATION=msp430_elf_after_allocation
912 LDEMUL_PLACE_ORPHAN=${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan}
913 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
914 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
915 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
916 LDEMUL_LIST_OPTIONS=gld${EMULATION_NAME}_list_options
918 source_em ${srcdir}/emultempl/emulation.em