Commit | Line | Data |
---|---|---|
837a17b3 NC |
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} | |
82704155 | 7 | Copyright (C) 2014-2019 Free Software Foundation, Inc. |
837a17b3 NC |
8 | Written by Steve Chamberlain steve@cygnus.com |
9 | Extended for the MSP430 by Nick Clifton nickc@redhat.com | |
6c19b93b | 10 | |
837a17b3 NC |
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 | ||
34 | #include "ld.h" | |
7ef3addb | 35 | #include "getopt.h" |
837a17b3 NC |
36 | #include "ldmain.h" |
37 | #include "ldmisc.h" | |
38 | #include "ldexp.h" | |
39 | #include "ldlang.h" | |
40 | #include "ldfile.h" | |
41 | #include "ldemul.h" | |
42 | #include "libiberty.h" | |
7ef3addb JL |
43 | #include <ldgram.h> |
44 | ||
45 | enum regions | |
46 | { | |
47 | REGION_NONE = 0, | |
48 | REGION_LOWER, | |
49 | REGION_UPPER, | |
50 | REGION_EITHER = 3, | |
51 | }; | |
52 | ||
53 | enum either_placement_stage | |
54 | { | |
55 | LOWER_TO_UPPER, | |
56 | UPPER_TO_LOWER, | |
57 | }; | |
58 | ||
59 | enum { ROM, RAM }; | |
60 | ||
61 | static int data_region = REGION_NONE; | |
62 | static int code_region = REGION_NONE; | |
63 | static bfd_boolean disable_sec_transformation = FALSE; | |
64 | ||
65 | #define MAX_PREFIX_LENGTH 7 | |
837a17b3 NC |
66 | |
67 | EOF | |
68 | ||
69 | # Import any needed special functions and/or overrides. | |
70 | # | |
71 | if test -n "$EXTRA_EM_FILE" ; then | |
72 | source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em | |
73 | fi | |
74 | ||
75 | if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then | |
76 | fragment <<EOF | |
77 | ||
78 | static void | |
79 | gld${EMULATION_NAME}_before_parse (void) | |
80 | { | |
81 | #ifndef TARGET_ /* I.e., if not generic. */ | |
82 | ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown); | |
83 | #endif /* not TARGET_ */ | |
84 | ||
85 | /* The MSP430 port *needs* linker relaxtion in order to cope with large | |
86 | functions where conditional branches do not fit into a +/- 1024 byte range. */ | |
0e1862bb | 87 | if (!bfd_link_relocatable (&link_info)) |
837a17b3 NC |
88 | TARGET_ENABLE_RELAXATION; |
89 | } | |
90 | ||
91 | EOF | |
92 | fi | |
93 | ||
94 | if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then | |
95 | fragment <<EOF | |
96 | ||
97 | static char * | |
98 | gld${EMULATION_NAME}_get_script (int *isfile) | |
99 | EOF | |
100 | ||
101 | if test x"$COMPILE_IN" = xyes | |
102 | then | |
103 | # Scripts compiled in. | |
104 | ||
105 | # sed commands to quote an ld script as a C string. | |
106 | sc="-f stringify.sed" | |
107 | ||
108 | fragment <<EOF | |
109 | { | |
110 | *isfile = 0; | |
111 | ||
0e1862bb | 112 | if (bfd_link_relocatable (&link_info) && config.build_constructors) |
837a17b3 NC |
113 | return |
114 | EOF | |
115 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c | |
0e1862bb | 116 | echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c |
837a17b3 NC |
117 | sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c |
118 | echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c | |
119 | sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c | |
120 | echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c | |
121 | sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c | |
122 | echo ' ; else return' >> e${EMULATION_NAME}.c | |
123 | sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c | |
124 | echo '; }' >> e${EMULATION_NAME}.c | |
125 | ||
126 | else | |
127 | # Scripts read from the filesystem. | |
128 | ||
129 | fragment <<EOF | |
130 | { | |
131 | *isfile = 1; | |
132 | ||
0e1862bb | 133 | if (bfd_link_relocatable (&link_info) && config.build_constructors) |
837a17b3 | 134 | return "ldscripts/${EMULATION_NAME}.xu"; |
0e1862bb | 135 | else if (bfd_link_relocatable (&link_info)) |
837a17b3 NC |
136 | return "ldscripts/${EMULATION_NAME}.xr"; |
137 | else if (!config.text_read_only) | |
138 | return "ldscripts/${EMULATION_NAME}.xbn"; | |
139 | else if (!config.magic_demand_paged) | |
140 | return "ldscripts/${EMULATION_NAME}.xn"; | |
141 | else | |
142 | return "ldscripts/${EMULATION_NAME}.x"; | |
143 | } | |
144 | EOF | |
145 | fi | |
146 | fi | |
147 | ||
148 | if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then | |
149 | fragment <<EOF | |
150 | ||
7ef3addb JL |
151 | static unsigned int |
152 | data_statement_size (lang_data_statement_type *d) | |
153 | { | |
154 | unsigned int size = 0; | |
155 | switch (d->type) | |
156 | { | |
157 | case QUAD: | |
158 | case SQUAD: | |
159 | size = QUAD_SIZE; | |
160 | break; | |
161 | case LONG: | |
162 | size = LONG_SIZE; | |
163 | break; | |
164 | case SHORT: | |
165 | size = SHORT_SIZE; | |
166 | break; | |
167 | case BYTE: | |
168 | size = BYTE_SIZE; | |
169 | break; | |
170 | default: | |
d003af55 | 171 | einfo (_("%P: error: unhandled data_statement size\n")); |
7ef3addb JL |
172 | FAIL (); |
173 | } | |
174 | return size; | |
175 | } | |
176 | ||
837a17b3 NC |
177 | /* Helper function for place_orphan that computes the size |
178 | of sections already mapped to the given statement. */ | |
179 | ||
180 | static bfd_size_type | |
181 | scan_children (lang_statement_union_type * l) | |
182 | { | |
183 | bfd_size_type amount = 0; | |
184 | ||
185 | while (l != NULL) | |
186 | { | |
187 | switch (l->header.type) | |
188 | { | |
189 | case lang_input_section_enum: | |
190 | if (l->input_section.section->flags & SEC_ALLOC) | |
191 | amount += l->input_section.section->size; | |
192 | break; | |
193 | ||
194 | case lang_constructors_statement_enum: | |
195 | case lang_assignment_statement_enum: | |
7ef3addb | 196 | case lang_padding_statement_enum: |
837a17b3 NC |
197 | break; |
198 | ||
199 | case lang_wild_statement_enum: | |
6c19b93b | 200 | amount += scan_children (l->wild_statement.children.head); |
837a17b3 NC |
201 | break; |
202 | ||
7ef3addb JL |
203 | case lang_data_statement_enum: |
204 | amount += data_statement_size (&l->data_statement); | |
205 | break; | |
206 | ||
837a17b3 NC |
207 | default: |
208 | fprintf (stderr, "msp430 orphan placer: unhandled lang type %d\n", l->header.type); | |
209 | break; | |
210 | } | |
211 | ||
212 | l = l->header.next; | |
213 | } | |
214 | ||
215 | return amount; | |
216 | } | |
6c19b93b | 217 | |
e25de718 JL |
218 | #define WARN_UPPER 0 |
219 | #define WARN_LOWER 1 | |
220 | #define WARN_TEXT 0 | |
221 | #define WARN_DATA 1 | |
222 | #define WARN_BSS 2 | |
223 | #define WARN_RODATA 3 | |
224 | ||
225 | /* Warn only once per output section. | |
226 | * NAME starts with ".upper." or ".lower.". */ | |
227 | static void | |
228 | warn_no_output_section (const char *name) | |
229 | { | |
230 | static bfd_boolean warned[2][4] = {{FALSE, FALSE, FALSE, FALSE}, | |
231 | {FALSE, FALSE, FALSE, FALSE}}; | |
232 | int i = WARN_LOWER; | |
233 | ||
234 | if (strncmp (name, ".upper.", 7) == 0) | |
235 | i = WARN_UPPER; | |
236 | ||
237 | if (!warned[i][WARN_TEXT] && strcmp (name + 6, ".text") == 0) | |
238 | warned[i][WARN_TEXT] = TRUE; | |
239 | else if (!warned[i][WARN_DATA] && strcmp (name + 6, ".data") == 0) | |
240 | warned[i][WARN_DATA] = TRUE; | |
241 | else if (!warned[i][WARN_BSS] && strcmp (name + 6, ".bss") == 0) | |
242 | warned[i][WARN_BSS] = TRUE; | |
243 | else if (!warned[i][WARN_RODATA] && strcmp (name + 6, ".rodata") == 0) | |
244 | warned[i][WARN_RODATA] = TRUE; | |
245 | else | |
246 | return; | |
247 | einfo ("%P: warning: no input section rule matches %s in linker script\n", | |
248 | name); | |
249 | } | |
250 | ||
251 | ||
837a17b3 NC |
252 | /* Place an orphan section. We use this to put .either sections |
253 | into either their lower or their upper equivalents. */ | |
254 | ||
255 | static lang_output_section_statement_type * | |
256 | gld${EMULATION_NAME}_place_orphan (asection * s, | |
257 | const char * secname, | |
258 | int constraint) | |
259 | { | |
260 | char * lower_name; | |
261 | char * upper_name; | |
262 | char * name; | |
e1fa0163 | 263 | char * buf = NULL; |
837a17b3 NC |
264 | lang_output_section_statement_type * lower; |
265 | lang_output_section_statement_type * upper; | |
837a17b3 NC |
266 | |
267 | if ((s->flags & SEC_ALLOC) == 0) | |
268 | return NULL; | |
269 | ||
0e1862bb | 270 | if (bfd_link_relocatable (&link_info)) |
837a17b3 NC |
271 | return NULL; |
272 | ||
273 | /* If constraints are involved let the linker handle the placement normally. */ | |
274 | if (constraint != 0) | |
275 | return NULL; | |
276 | ||
e25de718 JL |
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 | ||
837a17b3 NC |
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 | { | |
e1fa0163 | 296 | buf = name = xstrdup (secname); |
837a17b3 NC |
297 | |
298 | * strchr (name + 1, '.') = 0; | |
299 | } | |
300 | else | |
301 | name = (char *) secname; | |
6c19b93b | 302 | |
e1fa0163 NC |
303 | lower_name = concat (".lower", name, NULL); |
304 | upper_name = concat (".upper", name, NULL); | |
837a17b3 NC |
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); | |
837a17b3 | 309 | |
7ef3addb JL |
310 | if (lower == NULL && upper == NULL) |
311 | { | |
d003af55 | 312 | einfo (_("%P: error: no section named %s or %s in linker script\n"), |
7ef3addb JL |
313 | lower_name, upper_name); |
314 | goto end; | |
315 | } | |
316 | else if (lower == NULL) | |
837a17b3 | 317 | { |
7ef3addb JL |
318 | lower = lang_output_section_find (name); |
319 | if (lower == NULL) | |
320 | { | |
d003af55 | 321 | einfo (_("%P: error: no section named %s in linker script\n"), name); |
e1fa0163 | 322 | goto end; |
837a17b3 NC |
323 | } |
324 | } | |
7ef3addb JL |
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 | if (buf) | |
333 | free (buf); | |
334 | return lower; | |
335 | } | |
336 | EOF | |
337 | fi | |
338 | ||
339 | fragment <<EOF | |
340 | ||
341 | static bfd_boolean | |
342 | change_output_section (lang_statement_union_type ** head, | |
343 | asection *s, | |
344 | lang_output_section_statement_type * new_output_section) | |
345 | { | |
346 | asection *is; | |
347 | lang_statement_union_type * prev = NULL; | |
348 | lang_statement_union_type * curr; | |
349 | ||
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 | s->output_section = NULL; | |
360 | lang_add_section (& (new_output_section->children), s, NULL, | |
361 | new_output_section); | |
362 | /* Remove the section from the old output section. */ | |
363 | if (prev == NULL) | |
364 | *head = curr->header.next; | |
365 | else | |
366 | prev->header.next = curr->header.next; | |
367 | return TRUE; | |
368 | } | |
369 | break; | |
370 | case lang_wild_statement_enum: | |
371 | if (change_output_section (&(curr->wild_statement.children.head), | |
372 | s, new_output_section)) | |
373 | return TRUE; | |
374 | break; | |
375 | default: | |
376 | break; | |
377 | } | |
378 | prev = curr; | |
379 | curr = curr->header.next; | |
380 | } | |
381 | return FALSE; | |
382 | } | |
383 | ||
7ef3addb JL |
384 | static void |
385 | add_region_prefix (bfd *abfd, asection *s, | |
386 | ATTRIBUTE_UNUSED void *unused) | |
387 | { | |
388 | const char *curr_name = bfd_get_section_name (abfd, s); | |
7ef3addb JL |
389 | int region = REGION_NONE; |
390 | ||
391 | if (strncmp (curr_name, ".text", 5) == 0) | |
e25de718 | 392 | region = code_region; |
7ef3addb | 393 | else if (strncmp (curr_name, ".data", 5) == 0) |
e25de718 | 394 | region = data_region; |
7ef3addb | 395 | else if (strncmp (curr_name, ".bss", 4) == 0) |
e25de718 | 396 | region = data_region; |
7ef3addb | 397 | else if (strncmp (curr_name, ".rodata", 7) == 0) |
e25de718 | 398 | region = data_region; |
7ef3addb JL |
399 | else |
400 | return; | |
401 | ||
402 | switch (region) | |
403 | { | |
404 | case REGION_NONE: | |
405 | break; | |
406 | case REGION_UPPER: | |
e25de718 | 407 | bfd_rename_section (abfd, s, concat (".upper", curr_name, NULL)); |
7ef3addb JL |
408 | break; |
409 | case REGION_LOWER: | |
e25de718 | 410 | bfd_rename_section (abfd, s, concat (".lower", curr_name, NULL)); |
7ef3addb JL |
411 | break; |
412 | case REGION_EITHER: | |
413 | s->name = concat (".either", curr_name, NULL); | |
414 | break; | |
415 | default: | |
416 | /* Unreachable. */ | |
417 | FAIL (); | |
418 | break; | |
419 | } | |
7ef3addb JL |
420 | } |
421 | ||
422 | static void | |
423 | msp430_elf_after_open (void) | |
424 | { | |
425 | bfd *abfd; | |
426 | ||
427 | gld${EMULATION_NAME}_after_open (); | |
837a17b3 | 428 | |
7ef3addb JL |
429 | /* If neither --code-region or --data-region have been passed, do not |
430 | transform sections names. */ | |
431 | if ((code_region == REGION_NONE && data_region == REGION_NONE) | |
432 | || disable_sec_transformation) | |
433 | return; | |
434 | ||
435 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) | |
436 | bfd_map_over_sections (abfd, add_region_prefix, NULL); | |
437 | } | |
438 | ||
439 | #define OPTION_CODE_REGION 321 | |
440 | #define OPTION_DATA_REGION (OPTION_CODE_REGION + 1) | |
441 | #define OPTION_DISABLE_TRANS (OPTION_CODE_REGION + 2) | |
442 | ||
443 | static void | |
444 | gld${EMULATION_NAME}_add_options | |
445 | (int ns, char **shortopts, int nl, struct option **longopts, | |
446 | int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED) | |
447 | { | |
448 | static const char xtra_short[] = { }; | |
837a17b3 | 449 | |
7ef3addb JL |
450 | static const struct option xtra_long[] = |
451 | { | |
452 | { "code-region", required_argument, NULL, OPTION_CODE_REGION }, | |
453 | { "data-region", required_argument, NULL, OPTION_DATA_REGION }, | |
454 | { "disable-sec-transformation", no_argument, NULL, | |
455 | OPTION_DISABLE_TRANS }, | |
456 | { NULL, no_argument, NULL, 0 } | |
457 | }; | |
458 | ||
459 | *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short)); | |
460 | memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short)); | |
461 | *longopts = (struct option *) | |
462 | xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long)); | |
463 | memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long)); | |
464 | } | |
837a17b3 | 465 | |
7ef3addb JL |
466 | static void |
467 | gld${EMULATION_NAME}_list_options (FILE * file) | |
468 | { | |
df5f2391 AM |
469 | fprintf (file, _(" --code-region={either,lower,upper,none}\n\ |
470 | Transform .text* sections to {either,lower,upper,none}.text* sections\n")); | |
471 | fprintf (file, _(" --data-region={either,lower,upper,none}\n\ | |
472 | Transform .data*, .rodata* and .bss* sections to\n\ | |
473 | {either,lower,upper,none}.{bss,data,rodata}* sections\n")); | |
474 | fprintf (file, _(" --disable-sec-transformation\n\ | |
475 | Disable transformation of .{text,data,bss,rodata}* sections to\n\ | |
476 | add the {either,lower,upper,none} prefixes\n")); | |
7ef3addb | 477 | } |
837a17b3 | 478 | |
7ef3addb JL |
479 | static bfd_boolean |
480 | gld${EMULATION_NAME}_handle_option (int optc) | |
481 | { | |
482 | switch (optc) | |
483 | { | |
484 | case OPTION_CODE_REGION: | |
485 | if (strcmp (optarg, "upper") == 0) | |
486 | code_region = REGION_UPPER; | |
487 | else if (strcmp (optarg, "lower") == 0) | |
488 | code_region = REGION_LOWER; | |
489 | else if (strcmp (optarg, "either") == 0) | |
490 | code_region = REGION_EITHER; | |
491 | else if (strcmp (optarg, "none") == 0) | |
492 | code_region = REGION_NONE; | |
493 | else if (strlen (optarg) == 0) | |
494 | { | |
df5f2391 AM |
495 | einfo (_("%P: --code-region requires an argument: " |
496 | "{upper,lower,either,none}\n")); | |
7ef3addb JL |
497 | return FALSE; |
498 | } | |
837a17b3 | 499 | else |
7ef3addb | 500 | { |
df5f2391 AM |
501 | einfo (_("%P: error: unrecognized argument to --code-region= option: " |
502 | "\"%s\"\n"), optarg); | |
7ef3addb JL |
503 | return FALSE; |
504 | } | |
505 | break; | |
506 | ||
507 | case OPTION_DATA_REGION: | |
508 | if (strcmp (optarg, "upper") == 0) | |
509 | data_region = REGION_UPPER; | |
510 | else if (strcmp (optarg, "lower") == 0) | |
511 | data_region = REGION_LOWER; | |
512 | else if (strcmp (optarg, "either") == 0) | |
513 | data_region = REGION_EITHER; | |
514 | else if (strcmp (optarg, "none") == 0) | |
515 | data_region = REGION_NONE; | |
516 | else if (strlen (optarg) == 0) | |
517 | { | |
df5f2391 AM |
518 | einfo (_("%P: --data-region requires an argument: " |
519 | "{upper,lower,either,none}\n")); | |
7ef3addb JL |
520 | return FALSE; |
521 | } | |
522 | else | |
523 | { | |
df5f2391 AM |
524 | einfo (_("%P: error: unrecognized argument to --data-region= option: " |
525 | "\"%s\"\n"), optarg); | |
7ef3addb JL |
526 | return FALSE; |
527 | } | |
528 | break; | |
529 | ||
530 | case OPTION_DISABLE_TRANS: | |
531 | disable_sec_transformation = TRUE; | |
532 | break; | |
533 | ||
534 | default: | |
535 | return FALSE; | |
536 | } | |
537 | return TRUE; | |
538 | } | |
539 | ||
540 | static void | |
541 | eval_upper_either_sections (bfd *abfd, asection *s, void *data) | |
542 | { | |
96d01d93 | 543 | const char * base_sec_name; |
7ef3addb JL |
544 | const char * curr_name; |
545 | char * either_name; | |
546 | int curr_region; | |
547 | ||
548 | lang_output_section_statement_type * lower; | |
549 | lang_output_section_statement_type * upper; | |
550 | static bfd_size_type *lower_size = 0; | |
551 | static bfd_size_type *upper_size = 0; | |
552 | static bfd_size_type lower_size_rom = 0; | |
553 | static bfd_size_type lower_size_ram = 0; | |
554 | static bfd_size_type upper_size_rom = 0; | |
555 | static bfd_size_type upper_size_ram = 0; | |
556 | ||
557 | if ((s->flags & SEC_ALLOC) == 0) | |
558 | return; | |
559 | if (bfd_link_relocatable (&link_info)) | |
560 | return; | |
561 | ||
96d01d93 | 562 | base_sec_name = (const char *) data; |
7ef3addb JL |
563 | curr_name = bfd_get_section_name (abfd, s); |
564 | ||
565 | /* Only concerned with .either input sections in the upper output section. */ | |
566 | either_name = concat (".either", base_sec_name, NULL); | |
567 | if (strncmp (curr_name, either_name, strlen (either_name)) != 0 | |
568 | || strncmp (s->output_section->name, ".upper", 6) != 0) | |
569 | goto end; | |
570 | ||
571 | lower = lang_output_section_find (concat (".lower", base_sec_name, NULL)); | |
572 | upper = lang_output_section_find (concat (".upper", base_sec_name, NULL)); | |
573 | ||
574 | if (upper == NULL || upper->region == NULL) | |
575 | goto end; | |
576 | else if (lower == NULL) | |
577 | lower = lang_output_section_find (base_sec_name); | |
578 | if (lower == NULL || lower->region == NULL) | |
579 | goto end; | |
580 | ||
581 | if (strcmp (base_sec_name, ".text") == 0 | |
582 | || strcmp (base_sec_name, ".rodata") == 0) | |
583 | curr_region = ROM; | |
584 | else | |
585 | curr_region = RAM; | |
586 | ||
587 | if (curr_region == ROM) | |
588 | { | |
589 | if (lower_size_rom == 0) | |
590 | { | |
591 | lower_size_rom = lower->region->current - lower->region->origin; | |
592 | upper_size_rom = upper->region->current - upper->region->origin; | |
593 | } | |
594 | lower_size = &lower_size_rom; | |
595 | upper_size = &upper_size_rom; | |
596 | } | |
597 | else if (curr_region == RAM) | |
598 | { | |
599 | if (lower_size_ram == 0) | |
600 | { | |
601 | lower_size_ram = lower->region->current - lower->region->origin; | |
602 | upper_size_ram = upper->region->current - upper->region->origin; | |
603 | } | |
604 | lower_size = &lower_size_ram; | |
605 | upper_size = &upper_size_ram; | |
837a17b3 NC |
606 | } |
607 | ||
7ef3addb JL |
608 | /* Move sections in the upper region that would fit in the lower |
609 | region to the lower region. */ | |
610 | if (*lower_size + s->size < lower->region->length) | |
611 | { | |
612 | if (change_output_section (&(upper->children.head), s, lower)) | |
613 | { | |
614 | *upper_size -= s->size; | |
615 | *lower_size += s->size; | |
616 | } | |
617 | } | |
e1fa0163 | 618 | end: |
7ef3addb | 619 | free (either_name); |
837a17b3 | 620 | } |
837a17b3 | 621 | |
7ef3addb JL |
622 | static void |
623 | eval_lower_either_sections (bfd *abfd, asection *s, void *data) | |
624 | { | |
96d01d93 | 625 | const char * base_sec_name; |
7ef3addb JL |
626 | const char * curr_name; |
627 | char * either_name; | |
628 | int curr_region; | |
629 | lang_output_section_statement_type * output_sec; | |
630 | lang_output_section_statement_type * lower; | |
631 | lang_output_section_statement_type * upper; | |
632 | ||
633 | static bfd_size_type *lower_size = 0; | |
634 | static bfd_size_type lower_size_rom = 0; | |
635 | static bfd_size_type lower_size_ram = 0; | |
636 | ||
637 | if ((s->flags & SEC_ALLOC) == 0) | |
638 | return; | |
639 | if (bfd_link_relocatable (&link_info)) | |
640 | return; | |
641 | ||
96d01d93 | 642 | base_sec_name = (const char *) data; |
7ef3addb JL |
643 | curr_name = bfd_get_section_name (abfd, s); |
644 | ||
645 | /* Only concerned with .either input sections in the lower or "default" | |
646 | output section i.e. not in the upper output section. */ | |
647 | either_name = concat (".either", base_sec_name, NULL); | |
648 | if (strncmp (curr_name, either_name, strlen (either_name)) != 0 | |
649 | || strncmp (s->output_section->name, ".upper", 6) == 0) | |
650 | return; | |
651 | ||
652 | if (strcmp (base_sec_name, ".text") == 0 | |
653 | || strcmp (base_sec_name, ".rodata") == 0) | |
654 | curr_region = ROM; | |
655 | else | |
656 | curr_region = RAM; | |
657 | ||
658 | output_sec = lang_output_section_find (s->output_section->name); | |
659 | ||
660 | /* If the output_section doesn't exist, this has already been reported in | |
661 | place_orphan, so don't need to warn again. */ | |
662 | if (output_sec == NULL || output_sec->region == NULL) | |
663 | goto end; | |
664 | ||
665 | /* lower and output_sec might be the same, but in some cases an .either | |
666 | section can end up in base_sec_name if it hasn't been placed by | |
667 | place_orphan. */ | |
668 | lower = lang_output_section_find (concat (".lower", base_sec_name, NULL)); | |
669 | upper = lang_output_section_find (concat (".upper", base_sec_name, NULL)); | |
670 | if (upper == NULL) | |
671 | goto end; | |
672 | ||
673 | if (curr_region == ROM) | |
674 | { | |
675 | if (lower_size_rom == 0) | |
676 | { | |
677 | /* Get the size of other items in the lower region that aren't the | |
678 | sections to be moved around. */ | |
679 | lower_size_rom | |
680 | = (output_sec->region->current - output_sec->region->origin) | |
681 | - scan_children (output_sec->children.head); | |
682 | if (output_sec != lower && lower != NULL) | |
683 | lower_size_rom -= scan_children (lower->children.head); | |
684 | } | |
685 | lower_size = &lower_size_rom; | |
686 | } | |
687 | else if (curr_region == RAM) | |
688 | { | |
689 | if (lower_size_ram == 0) | |
690 | { | |
691 | lower_size_ram | |
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_ram -= scan_children (lower->children.head); | |
696 | } | |
697 | lower_size = &lower_size_ram; | |
698 | } | |
699 | /* Move sections that cause the lower region to overflow to the upper region. */ | |
700 | if (*lower_size + s->size > output_sec->region->length) | |
701 | change_output_section (&(output_sec->children.head), s, upper); | |
702 | else | |
703 | *lower_size += s->size; | |
704 | end: | |
705 | free (either_name); | |
706 | } | |
707 | ||
708 | /* This function is similar to lang_relax_sections, but without the size | |
709 | evaluation code that is always executed after relaxation. */ | |
710 | static void | |
711 | intermediate_relax_sections (void) | |
712 | { | |
713 | int i = link_info.relax_pass; | |
714 | ||
715 | /* The backend can use it to determine the current pass. */ | |
716 | link_info.relax_pass = 0; | |
717 | ||
718 | while (i--) | |
719 | { | |
720 | bfd_boolean relax_again; | |
721 | ||
722 | link_info.relax_trip = -1; | |
723 | do | |
724 | { | |
725 | link_info.relax_trip++; | |
726 | ||
727 | lang_do_assignments (lang_assigning_phase_enum); | |
728 | ||
729 | lang_reset_memory_regions (); | |
730 | ||
731 | relax_again = FALSE; | |
732 | lang_size_sections (&relax_again, FALSE); | |
733 | } | |
734 | while (relax_again); | |
735 | ||
736 | link_info.relax_pass++; | |
737 | } | |
738 | } | |
739 | ||
740 | static void | |
741 | msp430_elf_after_allocation (void) | |
742 | { | |
743 | int relax_count = 0; | |
96d01d93 | 744 | unsigned int i; |
7ef3addb JL |
745 | /* Go over each section twice, once to place either sections that don't fit |
746 | in lower into upper, and then again to move any sections in upper that | |
747 | fit in lower into lower. */ | |
748 | for (i = 0; i < 8; i++) | |
749 | { | |
750 | int placement_stage = (i < 4) ? LOWER_TO_UPPER : UPPER_TO_LOWER; | |
96d01d93 | 751 | const char * base_sec_name; |
7ef3addb JL |
752 | lang_output_section_statement_type * upper; |
753 | ||
754 | switch (i % 4) | |
755 | { | |
96d01d93 | 756 | default: |
7ef3addb | 757 | case 0: |
96d01d93 | 758 | base_sec_name = ".text"; |
7ef3addb JL |
759 | break; |
760 | case 1: | |
96d01d93 | 761 | base_sec_name = ".data"; |
7ef3addb JL |
762 | break; |
763 | case 2: | |
96d01d93 | 764 | base_sec_name = ".bss"; |
7ef3addb JL |
765 | break; |
766 | case 3: | |
96d01d93 | 767 | base_sec_name = ".rodata"; |
7ef3addb JL |
768 | break; |
769 | } | |
770 | upper = lang_output_section_find (concat (".upper", base_sec_name, NULL)); | |
771 | if (upper != NULL) | |
772 | { | |
773 | /* Can't just use one iteration over the all the sections to make | |
774 | both lower->upper and upper->lower transformations because the | |
775 | iterator encounters upper sections before all lower sections have | |
776 | been examined. */ | |
777 | bfd *abfd; | |
778 | ||
779 | if (placement_stage == LOWER_TO_UPPER) | |
780 | { | |
781 | /* Perform relaxation and get the final size of sections | |
782 | before trying to fit .either sections in the correct | |
783 | ouput sections. */ | |
784 | if (relax_count == 0) | |
785 | { | |
786 | intermediate_relax_sections (); | |
787 | relax_count++; | |
788 | } | |
789 | for (abfd = link_info.input_bfds; abfd != NULL; | |
790 | abfd = abfd->link.next) | |
791 | { | |
792 | bfd_map_over_sections (abfd, eval_lower_either_sections, | |
96d01d93 | 793 | (void *) base_sec_name); |
7ef3addb JL |
794 | } |
795 | } | |
796 | else if (placement_stage == UPPER_TO_LOWER) | |
797 | { | |
798 | /* Relax again before moving upper->lower. */ | |
799 | if (relax_count == 1) | |
800 | { | |
801 | intermediate_relax_sections (); | |
802 | relax_count++; | |
803 | } | |
804 | for (abfd = link_info.input_bfds; abfd != NULL; | |
805 | abfd = abfd->link.next) | |
806 | { | |
807 | bfd_map_over_sections (abfd, eval_upper_either_sections, | |
96d01d93 | 808 | (void *) base_sec_name); |
7ef3addb JL |
809 | } |
810 | } | |
811 | ||
812 | } | |
7ef3addb JL |
813 | } |
814 | gld${EMULATION_NAME}_after_allocation (); | |
815 | } | |
837a17b3 NC |
816 | |
817 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = | |
818 | { | |
819 | ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse}, | |
820 | ${LDEMUL_SYSLIB-syslib_default}, | |
821 | ${LDEMUL_HLL-hll_default}, | |
822 | ${LDEMUL_AFTER_PARSE-after_parse_default}, | |
7ef3addb | 823 | msp430_elf_after_open, |
5c3261b0 | 824 | after_check_relocs_default, |
7ef3addb | 825 | msp430_elf_after_allocation, |
837a17b3 NC |
826 | ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default}, |
827 | ${LDEMUL_CHOOSE_TARGET-ldemul_default_target}, | |
828 | ${LDEMUL_BEFORE_ALLOCATION-before_allocation_default}, | |
829 | ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script}, | |
830 | "${EMULATION_NAME}", | |
831 | "${OUTPUT_FORMAT}", | |
832 | ${LDEMUL_FINISH-finish_default}, | |
833 | ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL}, | |
834 | ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-NULL}, | |
835 | ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan}, | |
836 | ${LDEMUL_SET_SYMBOLS-NULL}, | |
837 | ${LDEMUL_PARSE_ARGS-NULL}, | |
7ef3addb JL |
838 | gld${EMULATION_NAME}_add_options, |
839 | gld${EMULATION_NAME}_handle_option, | |
837a17b3 | 840 | ${LDEMUL_UNRECOGNIZED_FILE-NULL}, |
7ef3addb | 841 | gld${EMULATION_NAME}_list_options, |
837a17b3 NC |
842 | ${LDEMUL_RECOGNIZED_FILE-NULL}, |
843 | ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL}, | |
844 | ${LDEMUL_NEW_VERS_PATTERN-NULL}, | |
845 | ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL} | |
846 | }; | |
847 | EOF | |
848 | # \f | |
849 | # Local Variables: | |
850 | # mode: c | |
851 | # End: |