Updated Malay translation for the bfd sub-directory
[binutils-gdb.git] / ld / emultempl / msp430.em
blob952e79aa3e61273804188cf06ca0fe02170b1532
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 fragment <<EOF
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}
30 #include "sysdep.h"
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "ctf-api.h"
35 #include "ld.h"
36 #include "getopt.h"
37 #include "ldmain.h"
38 #include "ldmisc.h"
39 #include "ldexp.h"
40 #include "ldlang.h"
41 #include "ldfile.h"
42 #include "ldlex.h"
43 #include "ldemul.h"
44 #include "libiberty.h"
45 #include <ldgram.h>
47 enum regions
49   REGION_NONE = 0,
50   REGION_LOWER,
51   REGION_UPPER,
52   REGION_EITHER = 3,
55 enum either_placement_stage
57   LOWER_TO_UPPER,
58   UPPER_TO_LOWER,
61 enum { ROM, RAM };
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
69 EOF
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
78 fragment <<EOF
80 static void
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;
93 EOF
96 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
97 fragment <<EOF
99 static char *
100 gld${EMULATION_NAME}_get_script (int *isfile)
103 if test x"$COMPILE_IN" = xyes
104 then
105 # Scripts compiled in.
107 # sed commands to quote an ld script as a C string.
108 sc="-f ${srcdir}/emultempl/stringify.sed"
110 fragment <<EOF
112   *isfile = 0;
114   if (bfd_link_relocatable (&link_info) && config.build_constructors)
115     return
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
128 else
129 # Scripts read from the filesystem.
131 fragment <<EOF
133   *isfile = 1;
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";
143   else
144     return "ldscripts/${EMULATION_NAME}.x";
150 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
151 fragment <<EOF
153 static unsigned int
154 data_statement_size (lang_data_statement_type *d)
156   unsigned int size = 0;
157   switch (d->type)
158     {
159     case QUAD:
160     case SQUAD:
161       size = QUAD_SIZE;
162       break;
163     case LONG:
164       size = LONG_SIZE;
165       break;
166     case SHORT:
167       size = SHORT_SIZE;
168       break;
169     case BYTE:
170       size = BYTE_SIZE;
171       break;
172     default:
173       einfo (_("%P: error: unhandled data_statement size\n"));
174       FAIL ();
175     }
176   return size;
179 /* Helper function for place_orphan that computes the size
180    of sections already mapped to the given statement.  */
182 static bfd_size_type
183 scan_children (lang_statement_union_type * l)
185   bfd_size_type amount = 0;
187   while (l != NULL)
188     {
189       switch (l->header.type)
190         {
191         case lang_input_section_enum:
192           if (l->input_section.section->flags & SEC_ALLOC)
193             amount += l->input_section.section->size;
194           break;
196         case lang_constructors_statement_enum:
197         case lang_assignment_statement_enum:
198         case lang_padding_statement_enum:
199           break;
201         case lang_wild_statement_enum:
202           amount += scan_children (l->wild_statement.children.head);
203           break;
205         case lang_data_statement_enum:
206           amount += data_statement_size (&l->data_statement);
207           break;
209         default:
210           fprintf (stderr, "msp430 orphan placer: unhandled lang type %d\n", l->header.type);
211           break;
212         }
214       l = l->header.next;
215     }
217   return amount;
220 #define WARN_UPPER 0
221 #define WARN_LOWER 1
222 #define WARN_TEXT 0
223 #define WARN_DATA 1
224 #define WARN_BSS 2
225 #define WARN_RODATA 3
227 /* Warn only once per output section.
228  * NAME starts with ".upper." or ".lower.".  */
229 static void
230 warn_no_output_section (const char *name)
232   static bool warned[2][4] = {{false, false, false, false},
233                               {false, false, false, false}};
234   int i = WARN_LOWER;
236   if (strncmp (name, ".upper.", 7) == 0)
237     i = WARN_UPPER;
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;
247   else
248     return;
249   einfo ("%P: warning: no input section rule matches %s in linker script\n",
250          name);
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,
260                                    int constraint)
262   char * lower_name;
263   char * upper_name;
264   char * name;
265   lang_output_section_statement_type * lower;
266   lang_output_section_statement_type * upper;
268   if ((s->flags & SEC_ALLOC) == 0)
269     return NULL;
271   if (bfd_link_relocatable (&link_info))
272     return NULL;
274   /* If constraints are involved let the linker handle the placement normally.  */
275   if (constraint != 0)
276     return NULL;
278   if (strncmp (secname, ".upper.", 7) == 0
279       || strncmp (secname, ".lower.", 7) == 0)
280     {
281       warn_no_output_section (secname);
282       return NULL;
283     }
285   /* We only need special handling for .either sections.  */
286   if (strncmp (secname, ".either.", 8) != 0)
287     return NULL;
289   /* Skip the .either prefix.  */
290   secname += 7;
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)
296     {
297       name = xstrdup (secname);
299       * strchr (name + 1, '.') = 0;
300     }
301   else
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)
312     {
313       einfo (_("%P: error: no section named %s or %s in linker script\n"),
314              lower_name, upper_name);
315       goto end;
316     }
317   else if (lower == NULL)
318     {
319       lower = lang_output_section_find (name);
320       if (lower == NULL)
321         {
322           einfo (_("%P: error: no section named %s in linker script\n"), name);
323           goto end;
324         }
325     }
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);
330  end:
331   free (upper_name);
332   free (lower_name);
333   return lower;
338 fragment <<EOF
340 static bool
341 change_output_section (lang_statement_union_type **head,
342                        asection *s,
343                        lang_output_section_statement_type *new_os,
344                        lang_output_section_statement_type *old_os)
346   asection *is;
347   lang_statement_union_type * prev = NULL;
348   lang_statement_union_type * curr;
350   curr = *head;
351   while (curr != NULL)
352     {
353       switch (curr->header.type)
354         {
355         case lang_input_section_enum:
356           is = curr->input_section.section;
357           if (is == s)
358             {
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.  */
366               if (prev == NULL)
367                 *head = curr->header.next;
368               else
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;
375               return true;
376             }
377           break;
378         case lang_wild_statement_enum:
379           if (change_output_section (&(curr->wild_statement.children.head),
380                                      s, new_os, old_os))
381             return true;
382           break;
383         default:
384           break;
385         }
386       prev = curr;
387       curr = curr->header.next;
388     }
389   return false;
392 static void
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;
407   else
408     return;
410   switch (region)
411     {
412     case REGION_NONE:
413       break;
414     case REGION_UPPER:
415       bfd_rename_section (s, concat (".upper", curr_name, NULL));
416       break;
417     case REGION_LOWER:
418       bfd_rename_section (s, concat (".lower", curr_name, NULL));
419       break;
420     case REGION_EITHER:
421       bfd_rename_section (s, concat (".either", curr_name, NULL));
422       break;
423     default:
424       /* Unreachable.  */
425       FAIL ();
426       break;
427     }
430 static void
431 msp430_elf_after_open (void)
433   bfd *abfd;
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)
441     return;
443   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
444     bfd_map_over_sections (abfd, add_region_prefix, NULL);
447 static void
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[] =
455     {
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 }
461     };
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));
470 static void
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"));
483 static bool
484 gld${EMULATION_NAME}_handle_option (int optc)
486   switch (optc)
487     {
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)
498         {
499           einfo (_("%P: --code-region requires an argument: "
500                    "{upper,lower,either,none}\n"));
501           return false;
502         }
503       else
504         {
505           einfo (_("%P: error: unrecognized argument to --code-region= option: "
506                    "\"%s\"\n"), optarg);
507           return false;
508         }
509       break;
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)
521         {
522           einfo (_("%P: --data-region requires an argument: "
523                    "{upper,lower,either,none}\n"));
524           return false;
525         }
526       else
527         {
528           einfo (_("%P: error: unrecognized argument to --data-region= option: "
529                    "\"%s\"\n"), optarg);
530           return false;
531         }
532       break;
534     case OPTION_DISABLE_TRANS:
535       disable_sec_transformation = true;
536       break;
538     default:
539       return false;
540     }
541   return true;
544 static void
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;
550   char * either_name;
551   int curr_region;
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)
563     return;
564   if (bfd_link_relocatable (&link_info))
565     return;
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)
574     goto end;
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)
580     goto end;
581   else if (lower == NULL)
582     lower = lang_output_section_find (base_sec_name);
583   if (lower == NULL || lower->region == NULL)
584     goto end;
586   if (strcmp (base_sec_name, ".text") == 0
587       || strcmp (base_sec_name, ".rodata") == 0)
588     curr_region = ROM;
589   else
590     curr_region = RAM;
592   if (curr_region == ROM)
593     {
594       if (lower_size_rom == 0)
595         {
596           lower_size_rom = lower->region->current - lower->region->origin;
597           upper_size_rom = upper->region->current - upper->region->origin;
598         }
599       lower_size = &lower_size_rom;
600       upper_size = &upper_size_rom;
601     }
602   else if (curr_region == RAM)
603     {
604       if (lower_size_ram == 0)
605         {
606           lower_size_ram = lower->region->current - lower->region->origin;
607           upper_size_ram = upper->region->current - upper->region->origin;
608         }
609       lower_size = &lower_size_ram;
610       upper_size = &upper_size_ram;
611     }
613   /* If the upper region is overflowing, try moving sections to the lower
614      region.
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
617      fit.  */
618   if (*upper_size > upper->region->length
619       && *lower_size + s->size < lower->region->length)
620     {
621       if (change_output_section (&(upper->children.head), s, lower, upper))
622         {
623           *upper_size -= s->size;
624           *lower_size += s->size;
625         }
626     }
627  end:
628   free (either_name);
631 static void
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;
637   char * either_name;
638   int curr_region;
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)
648     return;
649   if (bfd_link_relocatable (&link_info))
650     return;
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)
660     return;
662   if (strcmp (base_sec_name, ".text") == 0
663       || strcmp (base_sec_name, ".rodata") == 0)
664     curr_region = ROM;
665   else
666     curr_region = RAM;
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)
673     goto end;
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
677      place_orphan.  */
678   lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
679   upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
680   if (upper == NULL)
681     goto end;
683   if (curr_region == ROM)
684     {
685       if (lower_size_rom == 0)
686         {
687           /* Get the size of other items in the lower region that aren't the
688              sections to be moved around.  */
689           lower_size_rom
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);
694         }
695       lower_size = &lower_size_rom;
696     }
697   else if (curr_region == RAM)
698     {
699       if (lower_size_ram == 0)
700         {
701           lower_size_ram
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);
706         }
707       lower_size = &lower_size_ram;
708     }
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);
712   else
713     *lower_size += s->size;
714  end:
715   free (either_name);
718 /* This function is similar to lang_relax_sections, but without the size
719    evaluation code that is always executed after relaxation.  */
720 static void
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;
728   while (i--)
729     {
730       bool relax_again;
732       link_info.relax_trip = -1;
733       do
734         {
735           link_info.relax_trip++;
737           lang_do_assignments (lang_assigning_phase_enum);
739           lang_reset_memory_regions ();
741           relax_again = false;
742           lang_size_sections (&relax_again, false);
743         }
744       while (relax_again);
746       link_info.relax_pass++;
747     }
750 static void
751 msp430_elf_after_allocation (void)
753   int relax_count = 0;
754   unsigned int i;
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++)
759     {
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;
764       switch (i % 4)
765         {
766         default:
767         case 0:
768           base_sec_name = ".text";
769           break;
770         case 1:
771           base_sec_name = ".data";
772           break;
773         case 2:
774           base_sec_name = ".bss";
775           break;
776         case 3:
777           base_sec_name = ".rodata";
778           break;
779         }
780       upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
781       if (upper != NULL)
782         {
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
786              been examined.  */
787           bfd *abfd;
789           if (placement_stage == LOWER_TO_UPPER)
790             {
791               /* Perform relaxation and get the final size of sections
792                  before trying to fit .either sections in the correct
793                  ouput sections.  */
794               if (relax_count == 0)
795                 {
796                   intermediate_relax_sections ();
797                   relax_count++;
798                 }
799               for (abfd = link_info.input_bfds; abfd != NULL;
800                    abfd = abfd->link.next)
801                 {
802                   bfd_map_over_sections (abfd, eval_lower_either_sections,
803                                          (void *) base_sec_name);
804                 }
805             }
806           else if (placement_stage == UPPER_TO_LOWER)
807             {
808               /* Relax again before moving upper->lower.  */
809               if (relax_count == 1)
810                 {
811                   intermediate_relax_sections ();
812                   relax_count++;
813                 }
814               for (abfd = link_info.input_bfds; abfd != NULL;
815                    abfd = abfd->link.next)
816                 {
817                   bfd_map_over_sections (abfd, eval_upper_either_sections,
818                                          (void *) base_sec_name);
819                 }
820             }
822         }
823     }
824   gld${EMULATION_NAME}_after_allocation ();
827 /* Return TRUE if a non-debug input section in L has positive size and matches
828    the given name.  */
829 static int
830 input_section_exists (lang_statement_union_type * l, const char * name)
832   while (l != NULL)
833     {
834       switch (l->header.type)
835         {
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))
840             return true;
841           break;
843         case lang_wild_statement_enum:
844           if (input_section_exists (l->wild_statement.children.head, name))
845             return true;
846           break;
848         default:
849           break;
850         }
851       l = l->header.next;
852     }
853   return false;
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
858    aligned.
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.  */
864 static void
865 check_array_section_alignment (void)
867   int i;
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)
878     return;
880   /* There are 3 .*_array sections which must be checked for alignment.  */
881   for (i = 0; i < 3; i++)
882     {
883       struct bfd_link_hash_entry * sym;
884       if (((rodata_sec && input_section_exists (rodata_sec->children.head,
885                                                 array_names[i][0]))
886            || (rodata2_sec && input_section_exists (rodata2_sec->children.head,
887                                                     array_names[i][0])))
888           && (sym = bfd_link_hash_lookup (link_info.hash, array_names[i][1],
889                                           false, false, true))
890           && sym->type == bfd_link_hash_defined
891           && sym->u.def.value % 2)
892         {
893           einfo ("%P: warning: \"%s\" symbol (%pU) is not word aligned\n",
894                  array_names[i][1], NULL);
895         }
896     }
899 static void
900 gld${EMULATION_NAME}_finish (void)
902   finish_default ();
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
916 \f
917 # Local Variables:
918 # mode: c
919 # End: