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} | |
b3adc24a | 7 | Copyright (C) 2014-2020 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" | |
1ff6de03 | 33 | #include "ctf-api.h" |
837a17b3 NC |
34 | |
35 | #include "ld.h" | |
7ef3addb | 36 | #include "getopt.h" |
837a17b3 NC |
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" | |
7ef3addb JL |
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 | |
837a17b3 NC |
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. */ | |
0e1862bb | 88 | if (!bfd_link_relocatable (&link_info)) |
837a17b3 NC |
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 | ||
0e1862bb | 113 | if (bfd_link_relocatable (&link_info) && config.build_constructors) |
837a17b3 NC |
114 | return |
115 | EOF | |
116 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c | |
0e1862bb | 117 | echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c |
837a17b3 NC |
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 | ||
0e1862bb | 134 | if (bfd_link_relocatable (&link_info) && config.build_constructors) |
837a17b3 | 135 | return "ldscripts/${EMULATION_NAME}.xu"; |
0e1862bb | 136 | else if (bfd_link_relocatable (&link_info)) |
837a17b3 NC |
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 | ||
7ef3addb JL |
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: | |
d003af55 | 172 | einfo (_("%P: error: unhandled data_statement size\n")); |
7ef3addb JL |
173 | FAIL (); |
174 | } | |
175 | return size; | |
176 | } | |
177 | ||
837a17b3 NC |
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: | |
7ef3addb | 197 | case lang_padding_statement_enum: |
837a17b3 NC |
198 | break; |
199 | ||
200 | case lang_wild_statement_enum: | |
6c19b93b | 201 | amount += scan_children (l->wild_statement.children.head); |
837a17b3 NC |
202 | break; |
203 | ||
7ef3addb JL |
204 | case lang_data_statement_enum: |
205 | amount += data_statement_size (&l->data_statement); | |
206 | break; | |
207 | ||
837a17b3 NC |
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 | } | |
6c19b93b | 218 | |
e25de718 JL |
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 | ||
837a17b3 NC |
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; | |
e1fa0163 | 264 | char * buf = NULL; |
837a17b3 NC |
265 | lang_output_section_statement_type * lower; |
266 | lang_output_section_statement_type * upper; | |
837a17b3 NC |
267 | |
268 | if ((s->flags & SEC_ALLOC) == 0) | |
269 | return NULL; | |
270 | ||
0e1862bb | 271 | if (bfd_link_relocatable (&link_info)) |
837a17b3 NC |
272 | return NULL; |
273 | ||
274 | /* If constraints are involved let the linker handle the placement normally. */ | |
275 | if (constraint != 0) | |
276 | return NULL; | |
277 | ||
e25de718 JL |
278 | if (strncmp (secname, ".upper.", 7) == 0 |
279 | || strncmp (secname, ".lower.", 7) == 0) | |
280 | { | |
281 | warn_no_output_section (secname); | |
282 | return NULL; | |
283 | } | |
284 | ||
837a17b3 NC |
285 | /* We only need special handling for .either sections. */ |
286 | if (strncmp (secname, ".either.", 8) != 0) | |
287 | return NULL; | |
288 | ||
289 | /* Skip the .either prefix. */ | |
290 | secname += 7; | |
291 | ||
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 | { | |
e1fa0163 | 297 | buf = name = xstrdup (secname); |
837a17b3 NC |
298 | |
299 | * strchr (name + 1, '.') = 0; | |
300 | } | |
301 | else | |
302 | name = (char *) secname; | |
6c19b93b | 303 | |
e1fa0163 NC |
304 | lower_name = concat (".lower", name, NULL); |
305 | upper_name = concat (".upper", name, NULL); | |
837a17b3 NC |
306 | |
307 | /* Find the corresponding lower and upper sections. */ | |
308 | lower = lang_output_section_find (lower_name); | |
309 | upper = lang_output_section_find (upper_name); | |
837a17b3 | 310 | |
7ef3addb JL |
311 | if (lower == NULL && upper == NULL) |
312 | { | |
d003af55 | 313 | einfo (_("%P: error: no section named %s or %s in linker script\n"), |
7ef3addb JL |
314 | lower_name, upper_name); |
315 | goto end; | |
316 | } | |
317 | else if (lower == NULL) | |
837a17b3 | 318 | { |
7ef3addb JL |
319 | lower = lang_output_section_find (name); |
320 | if (lower == NULL) | |
321 | { | |
d003af55 | 322 | einfo (_("%P: error: no section named %s in linker script\n"), name); |
e1fa0163 | 323 | goto end; |
837a17b3 NC |
324 | } |
325 | } | |
7ef3addb JL |
326 | |
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, lower); | |
330 | end: | |
331 | free (upper_name); | |
332 | free (lower_name); | |
5e2ab612 | 333 | free (buf); |
7ef3addb JL |
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 | 384 | static void |
fd361982 AM |
385 | add_region_prefix (bfd *abfd ATTRIBUTE_UNUSED, asection *s, |
386 | void *unused ATTRIBUTE_UNUSED) | |
7ef3addb | 387 | { |
fd361982 | 388 | const char *curr_name = bfd_section_name (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: | |
fd361982 | 407 | bfd_rename_section (s, concat (".upper", curr_name, NULL)); |
7ef3addb JL |
408 | break; |
409 | case REGION_LOWER: | |
fd361982 | 410 | bfd_rename_section (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 | |
fd361982 AM |
541 | eval_upper_either_sections (bfd *abfd ATTRIBUTE_UNUSED, |
542 | asection *s, void *data) | |
7ef3addb | 543 | { |
96d01d93 | 544 | const char * base_sec_name; |
7ef3addb JL |
545 | const char * curr_name; |
546 | char * either_name; | |
547 | int curr_region; | |
548 | ||
549 | lang_output_section_statement_type * lower; | |
550 | lang_output_section_statement_type * upper; | |
551 | static bfd_size_type *lower_size = 0; | |
552 | static bfd_size_type *upper_size = 0; | |
553 | static bfd_size_type lower_size_rom = 0; | |
554 | static bfd_size_type lower_size_ram = 0; | |
555 | static bfd_size_type upper_size_rom = 0; | |
556 | static bfd_size_type upper_size_ram = 0; | |
557 | ||
558 | if ((s->flags & SEC_ALLOC) == 0) | |
559 | return; | |
560 | if (bfd_link_relocatable (&link_info)) | |
561 | return; | |
562 | ||
96d01d93 | 563 | base_sec_name = (const char *) data; |
fd361982 | 564 | curr_name = bfd_section_name (s); |
7ef3addb JL |
565 | |
566 | /* Only concerned with .either input sections in the upper output section. */ | |
567 | either_name = concat (".either", base_sec_name, NULL); | |
568 | if (strncmp (curr_name, either_name, strlen (either_name)) != 0 | |
569 | || strncmp (s->output_section->name, ".upper", 6) != 0) | |
570 | goto end; | |
571 | ||
572 | lower = lang_output_section_find (concat (".lower", base_sec_name, NULL)); | |
573 | upper = lang_output_section_find (concat (".upper", base_sec_name, NULL)); | |
574 | ||
575 | if (upper == NULL || upper->region == NULL) | |
576 | goto end; | |
577 | else if (lower == NULL) | |
578 | lower = lang_output_section_find (base_sec_name); | |
579 | if (lower == NULL || lower->region == NULL) | |
580 | goto end; | |
581 | ||
582 | if (strcmp (base_sec_name, ".text") == 0 | |
583 | || strcmp (base_sec_name, ".rodata") == 0) | |
584 | curr_region = ROM; | |
585 | else | |
586 | curr_region = RAM; | |
587 | ||
588 | if (curr_region == ROM) | |
589 | { | |
590 | if (lower_size_rom == 0) | |
591 | { | |
592 | lower_size_rom = lower->region->current - lower->region->origin; | |
593 | upper_size_rom = upper->region->current - upper->region->origin; | |
594 | } | |
595 | lower_size = &lower_size_rom; | |
596 | upper_size = &upper_size_rom; | |
597 | } | |
598 | else if (curr_region == RAM) | |
599 | { | |
600 | if (lower_size_ram == 0) | |
601 | { | |
602 | lower_size_ram = lower->region->current - lower->region->origin; | |
603 | upper_size_ram = upper->region->current - upper->region->origin; | |
604 | } | |
605 | lower_size = &lower_size_ram; | |
606 | upper_size = &upper_size_ram; | |
837a17b3 NC |
607 | } |
608 | ||
7ef3addb JL |
609 | /* Move sections in the upper region that would fit in the lower |
610 | region to the lower region. */ | |
611 | if (*lower_size + s->size < lower->region->length) | |
612 | { | |
613 | if (change_output_section (&(upper->children.head), s, lower)) | |
614 | { | |
615 | *upper_size -= s->size; | |
616 | *lower_size += s->size; | |
617 | } | |
618 | } | |
e1fa0163 | 619 | end: |
7ef3addb | 620 | free (either_name); |
837a17b3 | 621 | } |
837a17b3 | 622 | |
7ef3addb | 623 | static void |
fd361982 AM |
624 | eval_lower_either_sections (bfd *abfd ATTRIBUTE_UNUSED, |
625 | asection *s, void *data) | |
7ef3addb | 626 | { |
96d01d93 | 627 | const char * base_sec_name; |
7ef3addb JL |
628 | const char * curr_name; |
629 | char * either_name; | |
630 | int curr_region; | |
631 | lang_output_section_statement_type * output_sec; | |
632 | lang_output_section_statement_type * lower; | |
633 | lang_output_section_statement_type * upper; | |
634 | ||
635 | static bfd_size_type *lower_size = 0; | |
636 | static bfd_size_type lower_size_rom = 0; | |
637 | static bfd_size_type lower_size_ram = 0; | |
638 | ||
639 | if ((s->flags & SEC_ALLOC) == 0) | |
640 | return; | |
641 | if (bfd_link_relocatable (&link_info)) | |
642 | return; | |
643 | ||
96d01d93 | 644 | base_sec_name = (const char *) data; |
fd361982 | 645 | curr_name = bfd_section_name (s); |
7ef3addb JL |
646 | |
647 | /* Only concerned with .either input sections in the lower or "default" | |
648 | output section i.e. not in the upper output section. */ | |
649 | either_name = concat (".either", base_sec_name, NULL); | |
650 | if (strncmp (curr_name, either_name, strlen (either_name)) != 0 | |
651 | || strncmp (s->output_section->name, ".upper", 6) == 0) | |
652 | return; | |
653 | ||
654 | if (strcmp (base_sec_name, ".text") == 0 | |
655 | || strcmp (base_sec_name, ".rodata") == 0) | |
656 | curr_region = ROM; | |
657 | else | |
658 | curr_region = RAM; | |
659 | ||
660 | output_sec = lang_output_section_find (s->output_section->name); | |
661 | ||
662 | /* If the output_section doesn't exist, this has already been reported in | |
663 | place_orphan, so don't need to warn again. */ | |
664 | if (output_sec == NULL || output_sec->region == NULL) | |
665 | goto end; | |
666 | ||
667 | /* lower and output_sec might be the same, but in some cases an .either | |
668 | section can end up in base_sec_name if it hasn't been placed by | |
669 | place_orphan. */ | |
670 | lower = lang_output_section_find (concat (".lower", base_sec_name, NULL)); | |
671 | upper = lang_output_section_find (concat (".upper", base_sec_name, NULL)); | |
672 | if (upper == NULL) | |
673 | goto end; | |
674 | ||
675 | if (curr_region == ROM) | |
676 | { | |
677 | if (lower_size_rom == 0) | |
678 | { | |
679 | /* Get the size of other items in the lower region that aren't the | |
680 | sections to be moved around. */ | |
681 | lower_size_rom | |
682 | = (output_sec->region->current - output_sec->region->origin) | |
683 | - scan_children (output_sec->children.head); | |
684 | if (output_sec != lower && lower != NULL) | |
685 | lower_size_rom -= scan_children (lower->children.head); | |
686 | } | |
687 | lower_size = &lower_size_rom; | |
688 | } | |
689 | else if (curr_region == RAM) | |
690 | { | |
691 | if (lower_size_ram == 0) | |
692 | { | |
693 | lower_size_ram | |
694 | = (output_sec->region->current - output_sec->region->origin) | |
695 | - scan_children (output_sec->children.head); | |
696 | if (output_sec != lower && lower != NULL) | |
697 | lower_size_ram -= scan_children (lower->children.head); | |
698 | } | |
699 | lower_size = &lower_size_ram; | |
700 | } | |
701 | /* Move sections that cause the lower region to overflow to the upper region. */ | |
702 | if (*lower_size + s->size > output_sec->region->length) | |
703 | change_output_section (&(output_sec->children.head), s, upper); | |
704 | else | |
705 | *lower_size += s->size; | |
706 | end: | |
707 | free (either_name); | |
708 | } | |
709 | ||
710 | /* This function is similar to lang_relax_sections, but without the size | |
711 | evaluation code that is always executed after relaxation. */ | |
712 | static void | |
713 | intermediate_relax_sections (void) | |
714 | { | |
715 | int i = link_info.relax_pass; | |
716 | ||
717 | /* The backend can use it to determine the current pass. */ | |
718 | link_info.relax_pass = 0; | |
719 | ||
720 | while (i--) | |
721 | { | |
722 | bfd_boolean relax_again; | |
723 | ||
724 | link_info.relax_trip = -1; | |
725 | do | |
726 | { | |
727 | link_info.relax_trip++; | |
728 | ||
729 | lang_do_assignments (lang_assigning_phase_enum); | |
730 | ||
731 | lang_reset_memory_regions (); | |
732 | ||
733 | relax_again = FALSE; | |
734 | lang_size_sections (&relax_again, FALSE); | |
735 | } | |
736 | while (relax_again); | |
737 | ||
738 | link_info.relax_pass++; | |
739 | } | |
740 | } | |
741 | ||
742 | static void | |
743 | msp430_elf_after_allocation (void) | |
744 | { | |
745 | int relax_count = 0; | |
96d01d93 | 746 | unsigned int i; |
7ef3addb JL |
747 | /* Go over each section twice, once to place either sections that don't fit |
748 | in lower into upper, and then again to move any sections in upper that | |
749 | fit in lower into lower. */ | |
750 | for (i = 0; i < 8; i++) | |
751 | { | |
752 | int placement_stage = (i < 4) ? LOWER_TO_UPPER : UPPER_TO_LOWER; | |
96d01d93 | 753 | const char * base_sec_name; |
7ef3addb JL |
754 | lang_output_section_statement_type * upper; |
755 | ||
756 | switch (i % 4) | |
757 | { | |
96d01d93 | 758 | default: |
7ef3addb | 759 | case 0: |
96d01d93 | 760 | base_sec_name = ".text"; |
7ef3addb JL |
761 | break; |
762 | case 1: | |
96d01d93 | 763 | base_sec_name = ".data"; |
7ef3addb JL |
764 | break; |
765 | case 2: | |
96d01d93 | 766 | base_sec_name = ".bss"; |
7ef3addb JL |
767 | break; |
768 | case 3: | |
96d01d93 | 769 | base_sec_name = ".rodata"; |
7ef3addb JL |
770 | break; |
771 | } | |
772 | upper = lang_output_section_find (concat (".upper", base_sec_name, NULL)); | |
773 | if (upper != NULL) | |
774 | { | |
775 | /* Can't just use one iteration over the all the sections to make | |
776 | both lower->upper and upper->lower transformations because the | |
777 | iterator encounters upper sections before all lower sections have | |
778 | been examined. */ | |
779 | bfd *abfd; | |
780 | ||
781 | if (placement_stage == LOWER_TO_UPPER) | |
782 | { | |
783 | /* Perform relaxation and get the final size of sections | |
784 | before trying to fit .either sections in the correct | |
785 | ouput sections. */ | |
786 | if (relax_count == 0) | |
787 | { | |
788 | intermediate_relax_sections (); | |
789 | relax_count++; | |
790 | } | |
791 | for (abfd = link_info.input_bfds; abfd != NULL; | |
792 | abfd = abfd->link.next) | |
793 | { | |
794 | bfd_map_over_sections (abfd, eval_lower_either_sections, | |
96d01d93 | 795 | (void *) base_sec_name); |
7ef3addb JL |
796 | } |
797 | } | |
798 | else if (placement_stage == UPPER_TO_LOWER) | |
799 | { | |
800 | /* Relax again before moving upper->lower. */ | |
801 | if (relax_count == 1) | |
802 | { | |
803 | intermediate_relax_sections (); | |
804 | relax_count++; | |
805 | } | |
806 | for (abfd = link_info.input_bfds; abfd != NULL; | |
807 | abfd = abfd->link.next) | |
808 | { | |
809 | bfd_map_over_sections (abfd, eval_upper_either_sections, | |
96d01d93 | 810 | (void *) base_sec_name); |
7ef3addb JL |
811 | } |
812 | } | |
813 | ||
814 | } | |
7ef3addb JL |
815 | } |
816 | gld${EMULATION_NAME}_after_allocation (); | |
817 | } | |
837a17b3 NC |
818 | |
819 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = | |
820 | { | |
821 | ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse}, | |
822 | ${LDEMUL_SYSLIB-syslib_default}, | |
823 | ${LDEMUL_HLL-hll_default}, | |
824 | ${LDEMUL_AFTER_PARSE-after_parse_default}, | |
7ef3addb | 825 | msp430_elf_after_open, |
5c3261b0 | 826 | after_check_relocs_default, |
9b538ba7 | 827 | before_place_orphans_default, |
7ef3addb | 828 | msp430_elf_after_allocation, |
837a17b3 NC |
829 | ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default}, |
830 | ${LDEMUL_CHOOSE_TARGET-ldemul_default_target}, | |
831 | ${LDEMUL_BEFORE_ALLOCATION-before_allocation_default}, | |
832 | ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script}, | |
833 | "${EMULATION_NAME}", | |
834 | "${OUTPUT_FORMAT}", | |
835 | ${LDEMUL_FINISH-finish_default}, | |
836 | ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL}, | |
837 | ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-NULL}, | |
838 | ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan}, | |
839 | ${LDEMUL_SET_SYMBOLS-NULL}, | |
840 | ${LDEMUL_PARSE_ARGS-NULL}, | |
7ef3addb JL |
841 | gld${EMULATION_NAME}_add_options, |
842 | gld${EMULATION_NAME}_handle_option, | |
837a17b3 | 843 | ${LDEMUL_UNRECOGNIZED_FILE-NULL}, |
7ef3addb | 844 | gld${EMULATION_NAME}_list_options, |
837a17b3 NC |
845 | ${LDEMUL_RECOGNIZED_FILE-NULL}, |
846 | ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL}, | |
847 | ${LDEMUL_NEW_VERS_PATTERN-NULL}, | |
1ff6de03 NA |
848 | ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}, |
849 | ${LDEMUL_EMIT_CTF_EARLY-NULL}, | |
3edf7b9f DR |
850 | ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}, |
851 | ${LDEMUL_PRINT_SYMBOL-NULL} | |
837a17b3 NC |
852 | }; |
853 | EOF | |
854 | # \f | |
855 | # Local Variables: | |
856 | # mode: c | |
857 | # End: |