]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/emultempl/msp430.em
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / emultempl / msp430.em
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! */
5
6 /* Emulate the original gld for the given ${EMULATION_NAME}
7 Copyright (C) 2014-2021 Free Software Foundation, Inc.
8 Written by Steve Chamberlain steve@cygnus.com
9 Extended for the MSP430 by Nick Clifton nickc@redhat.com
10
11 This file is part of the GNU Binutils.
12
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.
17
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.
22
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. */
27
28 #define TARGET_IS_${EMULATION_NAME}
29
30 #include "sysdep.h"
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "ctf-api.h"
34
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 "ldemul.h"
43 #include "libiberty.h"
44 #include <ldgram.h>
45
46 enum regions
47 {
48 REGION_NONE = 0,
49 REGION_LOWER,
50 REGION_UPPER,
51 REGION_EITHER = 3,
52 };
53
54 enum either_placement_stage
55 {
56 LOWER_TO_UPPER,
57 UPPER_TO_LOWER,
58 };
59
60 enum { ROM, RAM };
61
62 static int data_region = REGION_NONE;
63 static int code_region = REGION_NONE;
64 static bfd_boolean disable_sec_transformation = FALSE;
65
66 #define MAX_PREFIX_LENGTH 7
67
68 EOF
69
70 # Import any needed special functions and/or overrides.
71 #
72 if test -n "$EXTRA_EM_FILE" ; then
73 source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
74 fi
75
76 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
77 fragment <<EOF
78
79 static void
80 gld${EMULATION_NAME}_before_parse (void)
81 {
82 #ifndef TARGET_ /* I.e., if not generic. */
83 ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
84 #endif /* not TARGET_ */
85
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;
90 }
91
92 EOF
93 fi
94
95 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
96 fragment <<EOF
97
98 static char *
99 gld${EMULATION_NAME}_get_script (int *isfile)
100 EOF
101
102 if test x"$COMPILE_IN" = xyes
103 then
104 # Scripts compiled in.
105
106 # sed commands to quote an ld script as a C string.
107 sc="-f stringify.sed"
108
109 fragment <<EOF
110 {
111 *isfile = 0;
112
113 if (bfd_link_relocatable (&link_info) && config.build_constructors)
114 return
115 EOF
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
126
127 else
128 # Scripts read from the filesystem.
129
130 fragment <<EOF
131 {
132 *isfile = 1;
133
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";
142 else
143 return "ldscripts/${EMULATION_NAME}.x";
144 }
145 EOF
146 fi
147 fi
148
149 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
150 fragment <<EOF
151
152 static unsigned int
153 data_statement_size (lang_data_statement_type *d)
154 {
155 unsigned int size = 0;
156 switch (d->type)
157 {
158 case QUAD:
159 case SQUAD:
160 size = QUAD_SIZE;
161 break;
162 case LONG:
163 size = LONG_SIZE;
164 break;
165 case SHORT:
166 size = SHORT_SIZE;
167 break;
168 case BYTE:
169 size = BYTE_SIZE;
170 break;
171 default:
172 einfo (_("%P: error: unhandled data_statement size\n"));
173 FAIL ();
174 }
175 return size;
176 }
177
178 /* Helper function for place_orphan that computes the size
179 of sections already mapped to the given statement. */
180
181 static bfd_size_type
182 scan_children (lang_statement_union_type * l)
183 {
184 bfd_size_type amount = 0;
185
186 while (l != NULL)
187 {
188 switch (l->header.type)
189 {
190 case lang_input_section_enum:
191 if (l->input_section.section->flags & SEC_ALLOC)
192 amount += l->input_section.section->size;
193 break;
194
195 case lang_constructors_statement_enum:
196 case lang_assignment_statement_enum:
197 case lang_padding_statement_enum:
198 break;
199
200 case lang_wild_statement_enum:
201 amount += scan_children (l->wild_statement.children.head);
202 break;
203
204 case lang_data_statement_enum:
205 amount += data_statement_size (&l->data_statement);
206 break;
207
208 default:
209 fprintf (stderr, "msp430 orphan placer: unhandled lang type %d\n", l->header.type);
210 break;
211 }
212
213 l = l->header.next;
214 }
215
216 return amount;
217 }
218
219 #define WARN_UPPER 0
220 #define WARN_LOWER 1
221 #define WARN_TEXT 0
222 #define WARN_DATA 1
223 #define WARN_BSS 2
224 #define WARN_RODATA 3
225
226 /* Warn only once per output section.
227 * NAME starts with ".upper." or ".lower.". */
228 static void
229 warn_no_output_section (const char *name)
230 {
231 static bfd_boolean warned[2][4] = {{FALSE, FALSE, FALSE, FALSE},
232 {FALSE, FALSE, FALSE, FALSE}};
233 int i = WARN_LOWER;
234
235 if (strncmp (name, ".upper.", 7) == 0)
236 i = WARN_UPPER;
237
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;
246 else
247 return;
248 einfo ("%P: warning: no input section rule matches %s in linker script\n",
249 name);
250 }
251
252
253 /* Place an orphan section. We use this to put .either sections
254 into either their lower or their upper equivalents. */
255
256 static lang_output_section_statement_type *
257 gld${EMULATION_NAME}_place_orphan (asection * s,
258 const char * secname,
259 int constraint)
260 {
261 char * lower_name;
262 char * upper_name;
263 char * name;
264 lang_output_section_statement_type * lower;
265 lang_output_section_statement_type * upper;
266
267 if ((s->flags & SEC_ALLOC) == 0)
268 return NULL;
269
270 if (bfd_link_relocatable (&link_info))
271 return NULL;
272
273 /* If constraints are involved let the linker handle the placement normally. */
274 if (constraint != 0)
275 return NULL;
276
277 if (strncmp (secname, ".upper.", 7) == 0
278 || strncmp (secname, ".lower.", 7) == 0)
279 {
280 warn_no_output_section (secname);
281 return NULL;
282 }
283
284 /* We only need special handling for .either sections. */
285 if (strncmp (secname, ".either.", 8) != 0)
286 return NULL;
287
288 /* Skip the .either prefix. */
289 secname += 7;
290
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)
295 {
296 name = xstrdup (secname);
297
298 * strchr (name + 1, '.') = 0;
299 }
300 else
301 name = (char *) secname;
302
303 lower_name = concat (".lower", name, NULL);
304 upper_name = concat (".upper", name, NULL);
305
306 /* Find the corresponding lower and upper sections. */
307 lower = lang_output_section_find (lower_name);
308 upper = lang_output_section_find (upper_name);
309
310 if (lower == NULL && upper == NULL)
311 {
312 einfo (_("%P: error: no section named %s or %s in linker script\n"),
313 lower_name, upper_name);
314 goto end;
315 }
316 else if (lower == NULL)
317 {
318 lower = lang_output_section_find (name);
319 if (lower == NULL)
320 {
321 einfo (_("%P: error: no section named %s in linker script\n"), name);
322 goto end;
323 }
324 }
325
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, lower);
329 end:
330 free (upper_name);
331 free (lower_name);
332 return lower;
333 }
334 EOF
335 fi
336
337 fragment <<EOF
338
339 static bfd_boolean
340 change_output_section (lang_statement_union_type **head,
341 asection *s,
342 lang_output_section_statement_type *new_os,
343 lang_output_section_statement_type *old_os)
344 {
345 asection *is;
346 lang_statement_union_type * prev = NULL;
347 lang_statement_union_type * curr;
348
349 curr = *head;
350 while (curr != NULL)
351 {
352 switch (curr->header.type)
353 {
354 case lang_input_section_enum:
355 is = curr->input_section.section;
356 if (is == s)
357 {
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, NULL, new_os);
362
363 /* Remove the section from the old output section. */
364 if (prev == NULL)
365 *head = curr->header.next;
366 else
367 prev->header.next = curr->header.next;
368 /* If the input section we just moved is the tail of the old
369 output section, then we also need to adjust that tail. */
370 if (old_list->tail == (lang_statement_union_type **) curr)
371 old_list->tail = (lang_statement_union_type **) prev;
372
373 return TRUE;
374 }
375 break;
376 case lang_wild_statement_enum:
377 if (change_output_section (&(curr->wild_statement.children.head),
378 s, new_os, old_os))
379 return TRUE;
380 break;
381 default:
382 break;
383 }
384 prev = curr;
385 curr = curr->header.next;
386 }
387 return FALSE;
388 }
389
390 static void
391 add_region_prefix (bfd *abfd ATTRIBUTE_UNUSED, asection *s,
392 void *unused ATTRIBUTE_UNUSED)
393 {
394 const char *curr_name = bfd_section_name (s);
395 int region = REGION_NONE;
396
397 if (strncmp (curr_name, ".text", 5) == 0)
398 region = code_region;
399 else if (strncmp (curr_name, ".data", 5) == 0)
400 region = data_region;
401 else if (strncmp (curr_name, ".bss", 4) == 0)
402 region = data_region;
403 else if (strncmp (curr_name, ".rodata", 7) == 0)
404 region = data_region;
405 else
406 return;
407
408 switch (region)
409 {
410 case REGION_NONE:
411 break;
412 case REGION_UPPER:
413 bfd_rename_section (s, concat (".upper", curr_name, NULL));
414 break;
415 case REGION_LOWER:
416 bfd_rename_section (s, concat (".lower", curr_name, NULL));
417 break;
418 case REGION_EITHER:
419 s->name = concat (".either", curr_name, NULL);
420 break;
421 default:
422 /* Unreachable. */
423 FAIL ();
424 break;
425 }
426 }
427
428 static void
429 msp430_elf_after_open (void)
430 {
431 bfd *abfd;
432
433 gld${EMULATION_NAME}_after_open ();
434
435 /* If neither --code-region or --data-region have been passed, do not
436 transform sections names. */
437 if ((code_region == REGION_NONE && data_region == REGION_NONE)
438 || disable_sec_transformation)
439 return;
440
441 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
442 bfd_map_over_sections (abfd, add_region_prefix, NULL);
443 }
444
445 #define OPTION_CODE_REGION 321
446 #define OPTION_DATA_REGION (OPTION_CODE_REGION + 1)
447 #define OPTION_DISABLE_TRANS (OPTION_CODE_REGION + 2)
448
449 static void
450 gld${EMULATION_NAME}_add_options
451 (int ns, char **shortopts, int nl, struct option **longopts,
452 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
453 {
454 static const char xtra_short[] = { };
455
456 static const struct option xtra_long[] =
457 {
458 { "code-region", required_argument, NULL, OPTION_CODE_REGION },
459 { "data-region", required_argument, NULL, OPTION_DATA_REGION },
460 { "disable-sec-transformation", no_argument, NULL,
461 OPTION_DISABLE_TRANS },
462 { NULL, no_argument, NULL, 0 }
463 };
464
465 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
466 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
467 *longopts = (struct option *)
468 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
469 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
470 }
471
472 static void
473 gld${EMULATION_NAME}_list_options (FILE * file)
474 {
475 fprintf (file, _(" --code-region={either,lower,upper,none}\n\
476 Transform .text* sections to {either,lower,upper,none}.text* sections\n"));
477 fprintf (file, _(" --data-region={either,lower,upper,none}\n\
478 Transform .data*, .rodata* and .bss* sections to\n\
479 {either,lower,upper,none}.{bss,data,rodata}* sections\n"));
480 fprintf (file, _(" --disable-sec-transformation\n\
481 Disable transformation of .{text,data,bss,rodata}* sections to\n\
482 add the {either,lower,upper,none} prefixes\n"));
483 }
484
485 static bfd_boolean
486 gld${EMULATION_NAME}_handle_option (int optc)
487 {
488 switch (optc)
489 {
490 case OPTION_CODE_REGION:
491 if (strcmp (optarg, "upper") == 0)
492 code_region = REGION_UPPER;
493 else if (strcmp (optarg, "lower") == 0)
494 code_region = REGION_LOWER;
495 else if (strcmp (optarg, "either") == 0)
496 code_region = REGION_EITHER;
497 else if (strcmp (optarg, "none") == 0)
498 code_region = REGION_NONE;
499 else if (strlen (optarg) == 0)
500 {
501 einfo (_("%P: --code-region requires an argument: "
502 "{upper,lower,either,none}\n"));
503 return FALSE;
504 }
505 else
506 {
507 einfo (_("%P: error: unrecognized argument to --code-region= option: "
508 "\"%s\"\n"), optarg);
509 return FALSE;
510 }
511 break;
512
513 case OPTION_DATA_REGION:
514 if (strcmp (optarg, "upper") == 0)
515 data_region = REGION_UPPER;
516 else if (strcmp (optarg, "lower") == 0)
517 data_region = REGION_LOWER;
518 else if (strcmp (optarg, "either") == 0)
519 data_region = REGION_EITHER;
520 else if (strcmp (optarg, "none") == 0)
521 data_region = REGION_NONE;
522 else if (strlen (optarg) == 0)
523 {
524 einfo (_("%P: --data-region requires an argument: "
525 "{upper,lower,either,none}\n"));
526 return FALSE;
527 }
528 else
529 {
530 einfo (_("%P: error: unrecognized argument to --data-region= option: "
531 "\"%s\"\n"), optarg);
532 return FALSE;
533 }
534 break;
535
536 case OPTION_DISABLE_TRANS:
537 disable_sec_transformation = TRUE;
538 break;
539
540 default:
541 return FALSE;
542 }
543 return TRUE;
544 }
545
546 static void
547 eval_upper_either_sections (bfd *abfd ATTRIBUTE_UNUSED,
548 asection *s, void *data)
549 {
550 const char * base_sec_name;
551 const char * curr_name;
552 char * either_name;
553 int curr_region;
554
555 lang_output_section_statement_type * lower;
556 lang_output_section_statement_type * upper;
557 static bfd_size_type *lower_size = 0;
558 static bfd_size_type *upper_size = 0;
559 static bfd_size_type lower_size_rom = 0;
560 static bfd_size_type lower_size_ram = 0;
561 static bfd_size_type upper_size_rom = 0;
562 static bfd_size_type upper_size_ram = 0;
563
564 if ((s->flags & SEC_ALLOC) == 0)
565 return;
566 if (bfd_link_relocatable (&link_info))
567 return;
568
569 base_sec_name = (const char *) data;
570 curr_name = bfd_section_name (s);
571
572 /* Only concerned with .either input sections in the upper output section. */
573 either_name = concat (".either", base_sec_name, NULL);
574 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
575 || strncmp (s->output_section->name, ".upper", 6) != 0)
576 goto end;
577
578 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
579 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
580
581 if (upper == NULL || upper->region == NULL)
582 goto end;
583 else if (lower == NULL)
584 lower = lang_output_section_find (base_sec_name);
585 if (lower == NULL || lower->region == NULL)
586 goto end;
587
588 if (strcmp (base_sec_name, ".text") == 0
589 || strcmp (base_sec_name, ".rodata") == 0)
590 curr_region = ROM;
591 else
592 curr_region = RAM;
593
594 if (curr_region == ROM)
595 {
596 if (lower_size_rom == 0)
597 {
598 lower_size_rom = lower->region->current - lower->region->origin;
599 upper_size_rom = upper->region->current - upper->region->origin;
600 }
601 lower_size = &lower_size_rom;
602 upper_size = &upper_size_rom;
603 }
604 else if (curr_region == RAM)
605 {
606 if (lower_size_ram == 0)
607 {
608 lower_size_ram = lower->region->current - lower->region->origin;
609 upper_size_ram = upper->region->current - upper->region->origin;
610 }
611 lower_size = &lower_size_ram;
612 upper_size = &upper_size_ram;
613 }
614
615 /* If the upper region is overflowing, try moving sections to the lower
616 region.
617 Note that there isn't any general benefit to using lower memory over upper
618 memory, so we only move sections around with the goal of making the program
619 fit. */
620 if (*upper_size > upper->region->length
621 && *lower_size + s->size < lower->region->length)
622 {
623 if (change_output_section (&(upper->children.head), s, lower, upper))
624 {
625 *upper_size -= s->size;
626 *lower_size += s->size;
627 }
628 }
629 end:
630 free (either_name);
631 }
632
633 static void
634 eval_lower_either_sections (bfd *abfd ATTRIBUTE_UNUSED,
635 asection *s, void *data)
636 {
637 const char * base_sec_name;
638 const char * curr_name;
639 char * either_name;
640 int curr_region;
641 lang_output_section_statement_type * output_sec;
642 lang_output_section_statement_type * lower;
643 lang_output_section_statement_type * upper;
644
645 static bfd_size_type *lower_size = 0;
646 static bfd_size_type lower_size_rom = 0;
647 static bfd_size_type lower_size_ram = 0;
648
649 if ((s->flags & SEC_ALLOC) == 0)
650 return;
651 if (bfd_link_relocatable (&link_info))
652 return;
653
654 base_sec_name = (const char *) data;
655 curr_name = bfd_section_name (s);
656
657 /* Only concerned with .either input sections in the lower or "default"
658 output section i.e. not in the upper output section. */
659 either_name = concat (".either", base_sec_name, NULL);
660 if (strncmp (curr_name, either_name, strlen (either_name)) != 0
661 || strncmp (s->output_section->name, ".upper", 6) == 0)
662 return;
663
664 if (strcmp (base_sec_name, ".text") == 0
665 || strcmp (base_sec_name, ".rodata") == 0)
666 curr_region = ROM;
667 else
668 curr_region = RAM;
669
670 output_sec = lang_output_section_find (s->output_section->name);
671
672 /* If the output_section doesn't exist, this has already been reported in
673 place_orphan, so don't need to warn again. */
674 if (output_sec == NULL || output_sec->region == NULL)
675 goto end;
676
677 /* lower and output_sec might be the same, but in some cases an .either
678 section can end up in base_sec_name if it hasn't been placed by
679 place_orphan. */
680 lower = lang_output_section_find (concat (".lower", base_sec_name, NULL));
681 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
682 if (upper == NULL)
683 goto end;
684
685 if (curr_region == ROM)
686 {
687 if (lower_size_rom == 0)
688 {
689 /* Get the size of other items in the lower region that aren't the
690 sections to be moved around. */
691 lower_size_rom
692 = (output_sec->region->current - output_sec->region->origin)
693 - scan_children (output_sec->children.head);
694 if (output_sec != lower && lower != NULL)
695 lower_size_rom -= scan_children (lower->children.head);
696 }
697 lower_size = &lower_size_rom;
698 }
699 else if (curr_region == RAM)
700 {
701 if (lower_size_ram == 0)
702 {
703 lower_size_ram
704 = (output_sec->region->current - output_sec->region->origin)
705 - scan_children (output_sec->children.head);
706 if (output_sec != lower && lower != NULL)
707 lower_size_ram -= scan_children (lower->children.head);
708 }
709 lower_size = &lower_size_ram;
710 }
711 /* Move sections that cause the lower region to overflow to the upper region. */
712 if (*lower_size + s->size > output_sec->region->length)
713 change_output_section (&(output_sec->children.head), s, upper, output_sec);
714 else
715 *lower_size += s->size;
716 end:
717 free (either_name);
718 }
719
720 /* This function is similar to lang_relax_sections, but without the size
721 evaluation code that is always executed after relaxation. */
722 static void
723 intermediate_relax_sections (void)
724 {
725 int i = link_info.relax_pass;
726
727 /* The backend can use it to determine the current pass. */
728 link_info.relax_pass = 0;
729
730 while (i--)
731 {
732 bfd_boolean relax_again;
733
734 link_info.relax_trip = -1;
735 do
736 {
737 link_info.relax_trip++;
738
739 lang_do_assignments (lang_assigning_phase_enum);
740
741 lang_reset_memory_regions ();
742
743 relax_again = FALSE;
744 lang_size_sections (&relax_again, FALSE);
745 }
746 while (relax_again);
747
748 link_info.relax_pass++;
749 }
750 }
751
752 static void
753 msp430_elf_after_allocation (void)
754 {
755 int relax_count = 0;
756 unsigned int i;
757 /* Go over each section twice, once to place either sections that don't fit
758 in lower into upper, and then again to move any sections in upper that
759 fit in lower into lower. */
760 for (i = 0; i < 8; i++)
761 {
762 int placement_stage = (i < 4) ? LOWER_TO_UPPER : UPPER_TO_LOWER;
763 const char * base_sec_name;
764 lang_output_section_statement_type * upper;
765
766 switch (i % 4)
767 {
768 default:
769 case 0:
770 base_sec_name = ".text";
771 break;
772 case 1:
773 base_sec_name = ".data";
774 break;
775 case 2:
776 base_sec_name = ".bss";
777 break;
778 case 3:
779 base_sec_name = ".rodata";
780 break;
781 }
782 upper = lang_output_section_find (concat (".upper", base_sec_name, NULL));
783 if (upper != NULL)
784 {
785 /* Can't just use one iteration over the all the sections to make
786 both lower->upper and upper->lower transformations because the
787 iterator encounters upper sections before all lower sections have
788 been examined. */
789 bfd *abfd;
790
791 if (placement_stage == LOWER_TO_UPPER)
792 {
793 /* Perform relaxation and get the final size of sections
794 before trying to fit .either sections in the correct
795 ouput sections. */
796 if (relax_count == 0)
797 {
798 intermediate_relax_sections ();
799 relax_count++;
800 }
801 for (abfd = link_info.input_bfds; abfd != NULL;
802 abfd = abfd->link.next)
803 {
804 bfd_map_over_sections (abfd, eval_lower_either_sections,
805 (void *) base_sec_name);
806 }
807 }
808 else if (placement_stage == UPPER_TO_LOWER)
809 {
810 /* Relax again before moving upper->lower. */
811 if (relax_count == 1)
812 {
813 intermediate_relax_sections ();
814 relax_count++;
815 }
816 for (abfd = link_info.input_bfds; abfd != NULL;
817 abfd = abfd->link.next)
818 {
819 bfd_map_over_sections (abfd, eval_upper_either_sections,
820 (void *) base_sec_name);
821 }
822 }
823
824 }
825 }
826 gld${EMULATION_NAME}_after_allocation ();
827 }
828
829 /* Return TRUE if a non-debug input section in L has positive size and matches
830 the given name. */
831 static int
832 input_section_exists (lang_statement_union_type * l, const char * name)
833 {
834 while (l != NULL)
835 {
836 switch (l->header.type)
837 {
838 case lang_input_section_enum:
839 if ((l->input_section.section->flags & SEC_ALLOC)
840 && l->input_section.section->size > 0
841 && !strcmp (l->input_section.section->name, name))
842 return TRUE;
843 break;
844
845 case lang_wild_statement_enum:
846 if (input_section_exists (l->wild_statement.children.head, name))
847 return TRUE;
848 break;
849
850 default:
851 break;
852 }
853 l = l->header.next;
854 }
855 return FALSE;
856 }
857
858 /* Some MSP430 linker scripts do not include ALIGN directives to ensure
859 __preinit_array_start, __init_array_start or __fini_array_start are word
860 aligned.
861 If __*_array_start symbols are not word aligned, the code in crt0 to run
862 through the array and call the functions will crash.
863 To avoid warning unnecessarily when the .*_array sections are not being
864 used for running constructors/destructors, only emit the warning if
865 the associated section exists and has size. */
866 static void
867 check_array_section_alignment (void)
868 {
869 int i;
870 lang_output_section_statement_type * rodata_sec;
871 lang_output_section_statement_type * rodata2_sec;
872 const char * array_names[3][2] = { { ".init_array", "__init_array_start" },
873 { ".preinit_array", "__preinit_array_start" },
874 { ".fini_array", "__fini_array_start" } };
875
876 /* .{preinit,init,fini}_array could be in either .rodata or .rodata2. */
877 rodata_sec = lang_output_section_find (".rodata");
878 rodata2_sec = lang_output_section_find (".rodata2");
879 if (rodata_sec == NULL && rodata2_sec == NULL)
880 return;
881
882 /* There are 3 .*_array sections which must be checked for alignment. */
883 for (i = 0; i < 3; i++)
884 {
885 struct bfd_link_hash_entry * sym;
886 if (((rodata_sec && input_section_exists (rodata_sec->children.head,
887 array_names[i][0]))
888 || (rodata2_sec && input_section_exists (rodata2_sec->children.head,
889 array_names[i][0])))
890 && (sym = bfd_link_hash_lookup (link_info.hash, array_names[i][1],
891 FALSE, FALSE, TRUE))
892 && sym->type == bfd_link_hash_defined
893 && sym->u.def.value % 2)
894 {
895 einfo ("%P: warning: \"%s\" symbol (%pU) is not word aligned\n",
896 array_names[i][1], NULL);
897 }
898 }
899 }
900
901 static void
902 gld${EMULATION_NAME}_finish (void)
903 {
904 finish_default ();
905 check_array_section_alignment ();
906 }
907
908 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
909 {
910 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
911 ${LDEMUL_SYSLIB-syslib_default},
912 ${LDEMUL_HLL-hll_default},
913 ${LDEMUL_AFTER_PARSE-after_parse_default},
914 msp430_elf_after_open,
915 after_check_relocs_default,
916 before_place_orphans_default,
917 msp430_elf_after_allocation,
918 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
919 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
920 ${LDEMUL_BEFORE_ALLOCATION-before_allocation_default},
921 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
922 "${EMULATION_NAME}",
923 "${OUTPUT_FORMAT}",
924 gld${EMULATION_NAME}_finish,
925 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
926 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-NULL},
927 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
928 ${LDEMUL_SET_SYMBOLS-NULL},
929 ${LDEMUL_PARSE_ARGS-NULL},
930 gld${EMULATION_NAME}_add_options,
931 gld${EMULATION_NAME}_handle_option,
932 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
933 gld${EMULATION_NAME}_list_options,
934 ${LDEMUL_RECOGNIZED_FILE-NULL},
935 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
936 ${LDEMUL_NEW_VERS_PATTERN-NULL},
937 ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL},
938 ${LDEMUL_EMIT_CTF_EARLY-NULL},
939 ${LDEMUL_ACQUIRE_STRINGS_FOR_CTF-NULL},
940 ${LDEMUL_NEW_DYNSYM_FOR_CTF-NULL},
941 ${LDEMUL_PRINT_SYMBOL-NULL}
942 };
943 EOF
944 # \f
945 # Local Variables:
946 # mode: c
947 # End: