37e7f5cb5d9df43b10eb5dfd497caacb416767db
[binutils-gdb.git] / ld / emultempl / xtensaelf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 # Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU Binutils.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21 #
22
23 # This file is sourced from elf32.em, and defines extra xtensa-elf
24 # specific routines.
25 #
26 fragment <<EOF
27
28 #include <xtensa-config.h>
29 #include "../bfd/elf-bfd.h"
30 #include "../bfd/libbfd.h"
31 #include "elf/xtensa.h"
32 #include "bfd.h"
33
34 /* Provide default values for new configuration settings. */
35 #ifndef XSHAL_ABI
36 #define XSHAL_ABI 0
37 #endif
38
39 static void xtensa_wild_group_interleave (lang_statement_union_type *);
40 static void xtensa_colocate_output_literals (lang_statement_union_type *);
41 static void xtensa_strip_inconsistent_linkonce_sections
42 (lang_statement_list_type *);
43
44
45 /* This number is irrelevant until we turn on use_literal_pages */
46 static bfd_vma xtensa_page_power = 12; /* 4K pages. */
47
48 /* To force a page break between literals and text, change
49 xtensa_use_literal_pages to "TRUE". */
50 static bfd_boolean xtensa_use_literal_pages = FALSE;
51
52 #define EXTRA_VALIDATION 0
53
54
55 static char *
56 elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
57 char **argv ATTRIBUTE_UNUSED)
58 {
59 if (XCHAL_HAVE_BE)
60 return "${BIG_OUTPUT_FORMAT}";
61 else
62 return "${LITTLE_OUTPUT_FORMAT}";
63 }
64
65
66 static void
67 elf_xtensa_before_parse (void)
68 {
69 /* Just call the default hook.... Tensilica's version of this function
70 does some other work that isn't relevant here. */
71 gld${EMULATION_NAME}_before_parse ();
72 }
73
74
75 static void
76 remove_section (bfd *abfd, asection *os)
77 {
78 asection **spp;
79 for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
80 if (*spp == os)
81 {
82 *spp = os->next;
83 os->owner->section_count--;
84 break;
85 }
86 }
87
88
89 static bfd_boolean
90 replace_insn_sec_with_prop_sec (bfd *abfd,
91 const char *insn_sec_name,
92 const char *prop_sec_name,
93 char **error_message)
94 {
95 asection *insn_sec;
96 asection *prop_sec;
97 bfd_byte *prop_contents = NULL;
98 bfd_byte *insn_contents = NULL;
99 unsigned entry_count;
100 unsigned entry;
101 Elf_Internal_Rela *internal_relocs = NULL;
102 unsigned reloc_count;
103
104 *error_message = "";
105 insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
106 if (insn_sec == NULL)
107 return TRUE;
108 entry_count = insn_sec->size / 8;
109
110 prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
111 if (prop_sec != NULL && insn_sec != NULL)
112 {
113 *error_message = _("file already has property tables");
114 return FALSE;
115 }
116
117 if (insn_sec->size != 0)
118 {
119 insn_contents = (bfd_byte *) bfd_malloc (insn_sec->size);
120 if (insn_contents == NULL)
121 {
122 *error_message = _("out of memory");
123 goto cleanup;
124 }
125 if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
126 (file_ptr) 0, insn_sec->size))
127 {
128 *error_message = _("failed to read section contents");
129 goto cleanup;
130 }
131 }
132
133 /* Create a property table section for it. */
134 prop_sec_name = strdup (prop_sec_name);
135 prop_sec = bfd_make_section_with_flags
136 (abfd, prop_sec_name, bfd_get_section_flags (abfd, insn_sec));
137 if (prop_sec == NULL
138 || ! bfd_set_section_alignment (abfd, prop_sec, 2))
139 {
140 *error_message = _("could not create new section");
141 goto cleanup;
142 }
143
144 prop_sec->size = entry_count * 12;
145 prop_contents = (bfd_byte *) bfd_zalloc (abfd, prop_sec->size);
146 elf_section_data (prop_sec)->this_hdr.contents = prop_contents;
147
148 /* The entry size and size must be set to allow the linker to compute
149 the number of relocations since it does not use reloc_count. */
150 elf_section_data (prop_sec)->rel_hdr.sh_entsize =
151 sizeof (Elf32_External_Rela);
152 elf_section_data (prop_sec)->rel_hdr.sh_size =
153 elf_section_data (insn_sec)->rel_hdr.sh_size;
154
155 if (prop_contents == NULL && prop_sec->size != 0)
156 {
157 *error_message = _("could not allocate section contents");
158 goto cleanup;
159 }
160
161 /* Read the relocations. */
162 reloc_count = insn_sec->reloc_count;
163 if (reloc_count != 0)
164 {
165 /* If there is already an internal_reloc, then save it so that the
166 read_relocs function freshly allocates a copy. */
167 Elf_Internal_Rela *saved_relocs = elf_section_data (insn_sec)->relocs;
168
169 elf_section_data (insn_sec)->relocs = NULL;
170 internal_relocs =
171 _bfd_elf_link_read_relocs (abfd, insn_sec, NULL, NULL, FALSE);
172 elf_section_data (insn_sec)->relocs = saved_relocs;
173
174 if (internal_relocs == NULL)
175 {
176 *error_message = _("out of memory");
177 goto cleanup;
178 }
179 }
180
181 /* Create a relocation section for the property section. */
182 if (internal_relocs != NULL)
183 {
184 elf_section_data (prop_sec)->relocs = internal_relocs;
185 prop_sec->reloc_count = reloc_count;
186 }
187
188 /* Now copy each insn table entry to the prop table entry with
189 appropriate flags. */
190 for (entry = 0; entry < entry_count; ++entry)
191 {
192 unsigned value;
193 unsigned flags = (XTENSA_PROP_INSN | XTENSA_PROP_NO_TRANSFORM
194 | XTENSA_PROP_INSN_NO_REORDER);
195 value = bfd_get_32 (abfd, insn_contents + entry * 8 + 0);
196 bfd_put_32 (abfd, value, prop_contents + entry * 12 + 0);
197 value = bfd_get_32 (abfd, insn_contents + entry * 8 + 4);
198 bfd_put_32 (abfd, value, prop_contents + entry * 12 + 4);
199 bfd_put_32 (abfd, flags, prop_contents + entry * 12 + 8);
200 }
201
202 /* Now copy all of the relocations. Change offsets for the
203 instruction table section to offsets in the property table
204 section. */
205 if (internal_relocs)
206 {
207 unsigned i;
208
209 for (i = 0; i < reloc_count; i++)
210 {
211 Elf_Internal_Rela *rela;
212 unsigned r_offset;
213
214 rela = &internal_relocs[i];
215
216 /* If this relocation is to the .xt.insn section,
217 change the section number and the offset. */
218 r_offset = rela->r_offset;
219 r_offset += 4 * (r_offset / 8);
220 rela->r_offset = r_offset;
221 }
222 }
223
224 remove_section (abfd, insn_sec);
225
226 if (insn_contents)
227 free (insn_contents);
228
229 return TRUE;
230
231 cleanup:
232 if (prop_sec && prop_sec->owner)
233 remove_section (abfd, prop_sec);
234 if (insn_contents)
235 free (insn_contents);
236 if (internal_relocs)
237 free (internal_relocs);
238
239 return FALSE;
240 }
241
242
243 #define PROP_SEC_BASE_NAME ".xt.prop"
244 #define INSN_SEC_BASE_NAME ".xt.insn"
245 #define LINKONCE_SEC_OLD_TEXT_BASE_NAME ".gnu.linkonce.x."
246
247
248 static void
249 replace_instruction_table_sections (bfd *abfd, asection *sec)
250 {
251 char *message = "";
252 const char *insn_sec_name = NULL;
253 char *prop_sec_name = NULL;
254 char *owned_prop_sec_name = NULL;
255 const char *sec_name;
256
257 sec_name = bfd_get_section_name (abfd, sec);
258 if (strcmp (sec_name, INSN_SEC_BASE_NAME) == 0)
259 {
260 insn_sec_name = INSN_SEC_BASE_NAME;
261 prop_sec_name = PROP_SEC_BASE_NAME;
262 }
263 else if (CONST_STRNEQ (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME))
264 {
265 insn_sec_name = sec_name;
266 owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
267 prop_sec_name = owned_prop_sec_name;
268 strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
269 strcat (prop_sec_name,
270 sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
271 }
272 if (insn_sec_name != NULL)
273 {
274 if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
275 &message))
276 {
277 einfo (_("%P: warning: failed to convert %s table in %B (%s); subsequent disassembly may be incomplete\n"),
278 insn_sec_name, abfd, message);
279 }
280 }
281 if (owned_prop_sec_name)
282 free (owned_prop_sec_name);
283 }
284
285
286 /* This is called after all input sections have been opened to convert
287 instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
288 tables (.xt.prop) before any section placement. */
289
290 static void
291 elf_xtensa_after_open (void)
292 {
293 /* First call the ELF version. */
294 gld${EMULATION_NAME}_after_open ();
295
296 /* Now search the input files looking for instruction table sections. */
297 LANG_FOR_EACH_INPUT_STATEMENT (f)
298 {
299 asection *sec = f->the_bfd->sections;
300 asection *next_sec;
301
302 /* Do not use bfd_map_over_sections here since we are removing
303 sections as we iterate. */
304 while (sec != NULL)
305 {
306 next_sec = sec->next;
307 replace_instruction_table_sections (f->the_bfd, sec);
308 sec = next_sec;
309 }
310 }
311 }
312
313
314 static bfd_boolean
315 xt_config_info_unpack_and_check (char *data,
316 bfd_boolean *pmismatch,
317 char **pmsg)
318 {
319 char *d, *key;
320 unsigned num;
321
322 *pmismatch = FALSE;
323
324 d = data;
325 while (*d)
326 {
327 key = d;
328 d = strchr (d, '=');
329 if (! d)
330 goto error;
331
332 /* Overwrite the equal sign. */
333 *d++ = 0;
334
335 /* Check if this is a quoted string or a number. */
336 if (*d == '"')
337 {
338 /* No string values are currently checked by LD;
339 just skip over the quotes. */
340 d++;
341 d = strchr (d, '"');
342 if (! d)
343 goto error;
344 /* Overwrite the trailing quote. */
345 *d++ = 0;
346 }
347 else
348 {
349 if (*d == 0)
350 goto error;
351 num = strtoul (d, &d, 0);
352
353 if (! strcmp (key, "ABI"))
354 {
355 if (num != XSHAL_ABI)
356 {
357 *pmismatch = TRUE;
358 *pmsg = "ABI does not match";
359 }
360 }
361 else if (! strcmp (key, "USE_ABSOLUTE_LITERALS"))
362 {
363 if (num != XSHAL_USE_ABSOLUTE_LITERALS)
364 {
365 *pmismatch = TRUE;
366 *pmsg = "incompatible use of the Extended L32R option";
367 }
368 }
369 }
370
371 if (*d++ != '\n')
372 goto error;
373 }
374
375 return TRUE;
376
377 error:
378 return FALSE;
379 }
380
381
382 #define XTINFO_NAME "Xtensa_Info"
383 #define XTINFO_NAMESZ 12
384 #define XTINFO_TYPE 1
385
386 static void
387 check_xtensa_info (bfd *abfd, asection *info_sec)
388 {
389 char *data, *errmsg = "";
390 bfd_boolean mismatch;
391
392 data = xmalloc (info_sec->size);
393 if (! bfd_get_section_contents (abfd, info_sec, data, 0, info_sec->size))
394 einfo (_("%F%P:%B: cannot read contents of section %A\n"), abfd, info_sec);
395
396 if (info_sec->size > 24
397 && info_sec->size >= 24 + bfd_get_32 (abfd, data + 4)
398 && bfd_get_32 (abfd, data + 0) == XTINFO_NAMESZ
399 && bfd_get_32 (abfd, data + 8) == XTINFO_TYPE
400 && strcmp (data + 12, XTINFO_NAME) == 0
401 && xt_config_info_unpack_and_check (data + 12 + XTINFO_NAMESZ,
402 &mismatch, &errmsg))
403 {
404 if (mismatch)
405 einfo (_("%P:%B: warning: incompatible Xtensa configuration (%s)\n"),
406 abfd, errmsg);
407 }
408 else
409 einfo (_("%P:%B: warning: cannot parse .xtensa.info section\n"), abfd);
410
411 free (data);
412 }
413
414
415 /* This is called after the sections have been attached to output
416 sections, but before any sizes or addresses have been set. */
417
418 static void
419 elf_xtensa_before_allocation (void)
420 {
421 asection *info_sec, *first_info_sec;
422 bfd *first_bfd;
423 bfd_boolean is_big_endian = XCHAL_HAVE_BE;
424
425 /* Check that the output endianness matches the Xtensa
426 configuration. The BFD library always includes both big and
427 little endian target vectors for Xtensa, but it only supports the
428 detailed instruction encode/decode operations (such as are
429 required to process relocations) for the selected Xtensa
430 configuration. */
431
432 if (is_big_endian
433 && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
434 {
435 einfo (_("%F%P: little endian output does not match "
436 "Xtensa configuration\n"));
437 }
438 if (!is_big_endian
439 && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
440 {
441 einfo (_("%F%P: big endian output does not match "
442 "Xtensa configuration\n"));
443 }
444
445 /* Keep track of the first input .xtensa.info section, and as a fallback,
446 the first input bfd where a .xtensa.info section could be created.
447 After the input .xtensa.info has been checked, the contents of the
448 first one will be replaced with the output .xtensa.info table. */
449 first_info_sec = 0;
450 first_bfd = 0;
451
452 LANG_FOR_EACH_INPUT_STATEMENT (f)
453 {
454 /* Check that the endianness for each input file matches the output.
455 The merge_private_bfd_data hook has already reported any mismatches
456 as errors, but those errors are not fatal. At this point, we
457 cannot go any further if there are any mismatches. */
458 if ((is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
459 || (!is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
460 einfo (_("%F%P: cross-endian linking for %B not supported\n"),
461 f->the_bfd);
462
463 if (! first_bfd)
464 first_bfd = f->the_bfd;
465
466 info_sec = bfd_get_section_by_name (f->the_bfd, ".xtensa.info");
467 if (! info_sec)
468 continue;
469
470 if (! first_info_sec)
471 first_info_sec = info_sec;
472
473 /* Unpack the .xtensa.info section and check it against the current
474 Xtensa configuration. */
475 check_xtensa_info (f->the_bfd, info_sec);
476
477 /* Do not include this copy of .xtensa.info in the output. */
478 info_sec->size = 0;
479 info_sec->flags |= SEC_EXCLUDE;
480 }
481
482 /* Reuse the first .xtensa.info input section to hold the output
483 .xtensa.info; or, if none were found, create a new section in the
484 first input bfd (assuming there is one). */
485 info_sec = first_info_sec;
486 if (! info_sec && first_bfd)
487 {
488 info_sec = bfd_make_section_with_flags (first_bfd, ".xtensa.info",
489 SEC_HAS_CONTENTS | SEC_READONLY);
490 if (! info_sec)
491 einfo (_("%F%P: failed to create .xtensa.info section\n"));
492 }
493 if (info_sec)
494 {
495 int xtensa_info_size;
496 char *data;
497
498 info_sec->flags &= ~SEC_EXCLUDE;
499 info_sec->flags |= SEC_IN_MEMORY;
500
501 data = xmalloc (100);
502 sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
503 XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
504 xtensa_info_size = strlen (data) + 1;
505
506 /* Add enough null terminators to pad to a word boundary. */
507 do
508 data[xtensa_info_size++] = 0;
509 while ((xtensa_info_size & 3) != 0);
510
511 info_sec->size = 12 + XTINFO_NAMESZ + xtensa_info_size;
512 info_sec->contents = xmalloc (info_sec->size);
513 bfd_put_32 (info_sec->owner, XTINFO_NAMESZ, info_sec->contents + 0);
514 bfd_put_32 (info_sec->owner, xtensa_info_size, info_sec->contents + 4);
515 bfd_put_32 (info_sec->owner, XTINFO_TYPE, info_sec->contents + 8);
516 memcpy (info_sec->contents + 12, XTINFO_NAME, XTINFO_NAMESZ);
517 memcpy (info_sec->contents + 12 + XTINFO_NAMESZ, data, xtensa_info_size);
518 free (data);
519 }
520
521 /* Enable relaxation by default if the "--no-relax" option was not
522 specified. This is done here instead of in the before_parse hook
523 because there is a check in main() to prohibit use of --relax and
524 -r together and that combination should be allowed for Xtensa. */
525 if (RELAXATION_DISABLED_BY_DEFAULT)
526 ENABLE_RELAXATION;
527
528 xtensa_strip_inconsistent_linkonce_sections (stat_ptr);
529
530 gld${EMULATION_NAME}_before_allocation ();
531
532 xtensa_wild_group_interleave (stat_ptr->head);
533
534 if (RELAXATION_ENABLED)
535 xtensa_colocate_output_literals (stat_ptr->head);
536
537 /* TBD: We need to force the page alignments to here and only do
538 them as needed for the entire output section. Finally, if this
539 is a relocatable link then we need to add alignment notes so
540 that the literals can be separated later. */
541 }
542
543
544 typedef struct wildcard_list section_name_list;
545
546 typedef struct reloc_deps_e_t reloc_deps_e;
547 typedef struct reloc_deps_section_t reloc_deps_section;
548 typedef struct reloc_deps_graph_t reloc_deps_graph;
549
550
551 struct reloc_deps_e_t
552 {
553 asection *src; /* Contains l32rs. */
554 asection *tgt; /* Contains literals. */
555 reloc_deps_e *next;
556 };
557
558 /* Place these in the userdata field. */
559 struct reloc_deps_section_t
560 {
561 reloc_deps_e *preds;
562 reloc_deps_e *succs;
563 bfd_boolean is_only_literal;
564 };
565
566
567 struct reloc_deps_graph_t
568 {
569 size_t count;
570 size_t size;
571 asection **sections;
572 };
573
574 static void xtensa_layout_wild
575 (const reloc_deps_graph *, lang_wild_statement_type *);
576
577 typedef void (*deps_callback_t) (asection *, /* src_sec */
578 bfd_vma, /* src_offset */
579 asection *, /* target_sec */
580 bfd_vma, /* target_offset */
581 void *); /* closure */
582
583 extern bfd_boolean xtensa_callback_required_dependence
584 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
585 static void xtensa_ldlang_clear_addresses (lang_statement_union_type *);
586 static bfd_boolean ld_local_file_relocations_fit
587 (lang_statement_union_type *, const reloc_deps_graph *);
588 static bfd_vma ld_assign_relative_paged_dot
589 (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
590 bfd_boolean);
591 static bfd_vma ld_xtensa_insert_page_offsets
592 (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
593 #if EXTRA_VALIDATION
594 static size_t ld_count_children (lang_statement_union_type *);
595 #endif
596
597 extern lang_statement_list_type constructor_list;
598
599 /* Begin verbatim code from ldlang.c:
600 the following are copied from ldlang.c because they are defined
601 there statically. */
602
603 static void
604 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
605 lang_statement_union_type *s)
606 {
607 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
608 {
609 func (s);
610
611 switch (s->header.type)
612 {
613 case lang_constructors_statement_enum:
614 lang_for_each_statement_worker (func, constructor_list.head);
615 break;
616 case lang_output_section_statement_enum:
617 lang_for_each_statement_worker
618 (func,
619 s->output_section_statement.children.head);
620 break;
621 case lang_wild_statement_enum:
622 lang_for_each_statement_worker
623 (func,
624 s->wild_statement.children.head);
625 break;
626 case lang_group_statement_enum:
627 lang_for_each_statement_worker (func,
628 s->group_statement.children.head);
629 break;
630 case lang_data_statement_enum:
631 case lang_reloc_statement_enum:
632 case lang_object_symbols_statement_enum:
633 case lang_output_statement_enum:
634 case lang_target_statement_enum:
635 case lang_input_section_enum:
636 case lang_input_statement_enum:
637 case lang_assignment_statement_enum:
638 case lang_padding_statement_enum:
639 case lang_address_statement_enum:
640 case lang_fill_statement_enum:
641 break;
642 default:
643 FAIL ();
644 break;
645 }
646 }
647 }
648
649 /* End of verbatim code from ldlang.c. */
650
651
652 static reloc_deps_section *
653 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
654 asection *sec)
655 {
656 /* We have a separate function for this so that
657 we could in the future keep a completely independent
658 structure that maps a section to its dependence edges.
659 For now, we place these in the sec->userdata field. */
660 reloc_deps_section *sec_deps = sec->userdata;
661 return sec_deps;
662 }
663
664 static void
665 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
666 asection *sec,
667 reloc_deps_section *deps_section)
668 {
669 sec->userdata = deps_section;
670 }
671
672
673 /* This is used to keep a list of all of the sections participating in
674 the graph so we can clean them up quickly. */
675
676 static void
677 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
678 {
679 if (deps->size <= deps->count)
680 {
681 asection **new_sections;
682 size_t i;
683 size_t new_size;
684
685 new_size = deps->size * 2;
686 if (new_size == 0)
687 new_size = 20;
688
689 new_sections = xmalloc (sizeof (asection *) * new_size);
690 memset (new_sections, 0, sizeof (asection *) * new_size);
691 for (i = 0; i < deps->count; i++)
692 {
693 new_sections[i] = deps->sections[i];
694 }
695 if (deps->sections != NULL)
696 free (deps->sections);
697 deps->sections = new_sections;
698 deps->size = new_size;
699 }
700 deps->sections[deps->count] = sec;
701 deps->count++;
702 }
703
704
705 static void
706 free_reloc_deps_graph (reloc_deps_graph *deps)
707 {
708 size_t i;
709 for (i = 0; i < deps->count; i++)
710 {
711 asection *sec = deps->sections[i];
712 reloc_deps_section *sec_deps;
713 sec_deps = xtensa_get_section_deps (deps, sec);
714 if (sec_deps)
715 {
716 reloc_deps_e *next;
717 while (sec_deps->succs != NULL)
718 {
719 next = sec_deps->succs->next;
720 free (sec_deps->succs);
721 sec_deps->succs = next;
722 }
723
724 while (sec_deps->preds != NULL)
725 {
726 next = sec_deps->preds->next;
727 free (sec_deps->preds);
728 sec_deps->preds = next;
729 }
730 free (sec_deps);
731 }
732 xtensa_set_section_deps (deps, sec, NULL);
733 }
734 if (deps->sections)
735 free (deps->sections);
736
737 free (deps);
738 }
739
740
741 static bfd_boolean
742 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
743 lang_statement_union_type *s)
744 {
745 asection *sec;
746 const reloc_deps_section *sec_deps;
747
748 if (s->header.type != lang_input_section_enum)
749 return FALSE;
750 sec = s->input_section.section;
751
752 sec_deps = xtensa_get_section_deps (deps, sec);
753 return sec_deps && sec_deps->succs != NULL;
754 }
755
756
757 static bfd_boolean
758 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
759 lang_statement_union_type *s)
760 {
761 asection *sec;
762 const reloc_deps_section *sec_deps;
763
764 if (s->header.type != lang_input_section_enum)
765 return FALSE;
766 sec = s->input_section.section;
767
768 sec_deps = xtensa_get_section_deps (deps, sec);
769 return sec_deps && sec_deps->preds != NULL;
770 }
771
772
773 static bfd_boolean
774 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
775 lang_statement_union_type *s)
776 {
777 return (section_is_source (deps, s)
778 || section_is_target (deps, s));
779 }
780
781
782 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
783 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
784
785 struct xtensa_ld_iter_t
786 {
787 lang_statement_union_type *parent; /* Parent of the list. */
788 lang_statement_list_type *l; /* List that holds it. */
789 lang_statement_union_type **loc; /* Place in the list. */
790 };
791
792 struct xtensa_ld_iter_stack_t
793 {
794 xtensa_ld_iter iterloc; /* List that hold it. */
795
796 xtensa_ld_iter_stack *next; /* Next in the stack. */
797 xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
798 };
799
800
801 static void
802 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
803 {
804 lang_statement_union_type *to_next;
805 lang_statement_union_type *current_next;
806 lang_statement_union_type **e;
807
808 #if EXTRA_VALIDATION
809 size_t old_to_count, new_to_count;
810 size_t old_current_count, new_current_count;
811 #endif
812
813 if (to == current)
814 return;
815
816 #if EXTRA_VALIDATION
817 old_to_count = ld_count_children (to->parent);
818 old_current_count = ld_count_children (current->parent);
819 #endif
820
821 to_next = *(to->loc);
822 current_next = (*current->loc)->header.next;
823
824 *(to->loc) = *(current->loc);
825
826 *(current->loc) = current_next;
827 (*(to->loc))->header.next = to_next;
828
829 /* reset "to" list tail */
830 for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
831 ;
832 to->l->tail = e;
833
834 /* reset "current" list tail */
835 for (e = &current->l->head; *e != NULL; e = &(*e)->header.next)
836 ;
837 current->l->tail = e;
838
839 #if EXTRA_VALIDATION
840 new_to_count = ld_count_children (to->parent);
841 new_current_count = ld_count_children (current->parent);
842
843 ASSERT ((old_to_count + old_current_count)
844 == (new_to_count + new_current_count));
845 #endif
846 }
847
848
849 /* Can only be called with lang_statements that have lists. Returns
850 FALSE if the list is empty. */
851
852 static bfd_boolean
853 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
854 {
855 return *stack_p == NULL;
856 }
857
858
859 static bfd_boolean
860 iter_stack_push (xtensa_ld_iter_stack **stack_p,
861 lang_statement_union_type *parent)
862 {
863 xtensa_ld_iter_stack *stack;
864 lang_statement_list_type *l = NULL;
865
866 switch (parent->header.type)
867 {
868 case lang_output_section_statement_enum:
869 l = &parent->output_section_statement.children;
870 break;
871 case lang_wild_statement_enum:
872 l = &parent->wild_statement.children;
873 break;
874 case lang_group_statement_enum:
875 l = &parent->group_statement.children;
876 break;
877 default:
878 ASSERT (0);
879 return FALSE;
880 }
881
882 /* Empty. do not push. */
883 if (l->tail == &l->head)
884 return FALSE;
885
886 stack = xmalloc (sizeof (xtensa_ld_iter_stack));
887 memset (stack, 0, sizeof (xtensa_ld_iter_stack));
888 stack->iterloc.parent = parent;
889 stack->iterloc.l = l;
890 stack->iterloc.loc = &l->head;
891
892 stack->next = *stack_p;
893 stack->prev = NULL;
894 if (*stack_p != NULL)
895 (*stack_p)->prev = stack;
896 *stack_p = stack;
897 return TRUE;
898 }
899
900
901 static void
902 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
903 {
904 xtensa_ld_iter_stack *stack;
905
906 stack = *stack_p;
907
908 if (stack == NULL)
909 {
910 ASSERT (stack != NULL);
911 return;
912 }
913
914 if (stack->next != NULL)
915 stack->next->prev = NULL;
916
917 *stack_p = stack->next;
918 free (stack);
919 }
920
921
922 /* This MUST be called if, during iteration, the user changes the
923 underlying structure. It will check for a NULL current and advance
924 accordingly. */
925
926 static void
927 iter_stack_update (xtensa_ld_iter_stack **stack_p)
928 {
929 if (!iter_stack_empty (stack_p)
930 && (*(*stack_p)->iterloc.loc) == NULL)
931 {
932 iter_stack_pop (stack_p);
933
934 while (!iter_stack_empty (stack_p)
935 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
936 {
937 iter_stack_pop (stack_p);
938 }
939 if (!iter_stack_empty (stack_p))
940 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
941 }
942 }
943
944
945 static void
946 iter_stack_next (xtensa_ld_iter_stack **stack_p)
947 {
948 xtensa_ld_iter_stack *stack;
949 lang_statement_union_type *current;
950 stack = *stack_p;
951
952 current = *stack->iterloc.loc;
953 /* If we are on the first element. */
954 if (current != NULL)
955 {
956 switch (current->header.type)
957 {
958 case lang_output_section_statement_enum:
959 case lang_wild_statement_enum:
960 case lang_group_statement_enum:
961 /* If the list if not empty, we are done. */
962 if (iter_stack_push (stack_p, *stack->iterloc.loc))
963 return;
964 /* Otherwise increment the pointer as normal. */
965 break;
966 default:
967 break;
968 }
969 }
970
971 while (!iter_stack_empty (stack_p)
972 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
973 {
974 iter_stack_pop (stack_p);
975 }
976 if (!iter_stack_empty (stack_p))
977 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
978 }
979
980
981 static lang_statement_union_type *
982 iter_stack_current (xtensa_ld_iter_stack **stack_p)
983 {
984 return *((*stack_p)->iterloc.loc);
985 }
986
987
988 /* The iter stack is a preorder. */
989
990 static void
991 iter_stack_create (xtensa_ld_iter_stack **stack_p,
992 lang_statement_union_type *parent)
993 {
994 iter_stack_push (stack_p, parent);
995 }
996
997
998 static void
999 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p, xtensa_ld_iter *front)
1000 {
1001 *front = (*stack_p)->iterloc;
1002 }
1003
1004
1005 static void
1006 xtensa_colocate_literals (reloc_deps_graph *deps,
1007 lang_statement_union_type *statement)
1008 {
1009 /* Keep a stack of pointers to control iteration through the contours. */
1010 xtensa_ld_iter_stack *stack = NULL;
1011 xtensa_ld_iter_stack **stack_p = &stack;
1012
1013 xtensa_ld_iter front; /* Location where new insertion should occur. */
1014 xtensa_ld_iter *front_p = NULL;
1015
1016 xtensa_ld_iter current; /* Location we are checking. */
1017 xtensa_ld_iter *current_p = NULL;
1018 bfd_boolean in_literals = FALSE;
1019
1020 if (deps->count == 0)
1021 return;
1022
1023 iter_stack_create (stack_p, statement);
1024
1025 while (!iter_stack_empty (stack_p))
1026 {
1027 bfd_boolean skip_increment = FALSE;
1028 lang_statement_union_type *l = iter_stack_current (stack_p);
1029
1030 switch (l->header.type)
1031 {
1032 case lang_assignment_statement_enum:
1033 /* Any assignment statement should block reordering across it. */
1034 front_p = NULL;
1035 in_literals = FALSE;
1036 break;
1037
1038 case lang_input_section_enum:
1039 if (front_p == NULL)
1040 {
1041 in_literals = (section_is_target (deps, l)
1042 && !section_is_source (deps, l));
1043 if (in_literals)
1044 {
1045 front_p = &front;
1046 iter_stack_copy_current (stack_p, front_p);
1047 }
1048 }
1049 else
1050 {
1051 bfd_boolean is_target;
1052 current_p = &current;
1053 iter_stack_copy_current (stack_p, current_p);
1054 is_target = (section_is_target (deps, l)
1055 && !section_is_source (deps, l));
1056
1057 if (in_literals)
1058 {
1059 iter_stack_copy_current (stack_p, front_p);
1060 if (!is_target)
1061 in_literals = FALSE;
1062 }
1063 else
1064 {
1065 if (is_target)
1066 {
1067 /* Try to insert in place. */
1068 ld_xtensa_move_section_after (front_p, current_p);
1069 ld_assign_relative_paged_dot (0x100000,
1070 statement,
1071 deps,
1072 xtensa_use_literal_pages);
1073
1074 /* We use this code because it's already written. */
1075 if (!ld_local_file_relocations_fit (statement, deps))
1076 {
1077 /* Move it back. */
1078 ld_xtensa_move_section_after (current_p, front_p);
1079 /* Reset the literal placement. */
1080 iter_stack_copy_current (stack_p, front_p);
1081 }
1082 else
1083 {
1084 /* Move front pointer up by one. */
1085 front_p->loc = &(*front_p->loc)->header.next;
1086
1087 /* Do not increment the current pointer. */
1088 skip_increment = TRUE;
1089 }
1090 }
1091 }
1092 }
1093 break;
1094 default:
1095 break;
1096 }
1097
1098 if (!skip_increment)
1099 iter_stack_next (stack_p);
1100 else
1101 /* Be careful to update the stack_p if it now is a null. */
1102 iter_stack_update (stack_p);
1103 }
1104
1105 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
1106 }
1107
1108
1109 static void
1110 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
1111 lang_wild_statement_type *w)
1112 {
1113 /* Keep a front pointer and a current pointer. */
1114 lang_statement_union_type **front;
1115 lang_statement_union_type **current;
1116
1117 /* Walk to the end of the targets. */
1118 for (front = &w->children.head;
1119 (*front != NULL) && section_is_source_or_target (deps, *front);
1120 front = &(*front)->header.next)
1121 ;
1122
1123 if (*front == NULL)
1124 return;
1125
1126 current = &(*front)->header.next;
1127 while (*current != NULL)
1128 {
1129 if (section_is_source_or_target (deps, *current))
1130 {
1131 /* Insert in place. */
1132 xtensa_ld_iter front_iter;
1133 xtensa_ld_iter current_iter;
1134
1135 front_iter.parent = (lang_statement_union_type *) w;
1136 front_iter.l = &w->children;
1137 front_iter.loc = front;
1138
1139 current_iter.parent = (lang_statement_union_type *) w;
1140 current_iter.l = &w->children;
1141 current_iter.loc = current;
1142
1143 ld_xtensa_move_section_after (&front_iter, &current_iter);
1144 front = &(*front)->header.next;
1145 }
1146 else
1147 {
1148 current = &(*current)->header.next;
1149 }
1150 }
1151 }
1152
1153
1154 static bfd_boolean
1155 deps_has_sec_edge (const reloc_deps_graph *deps, asection *src, asection *tgt)
1156 {
1157 const reloc_deps_section *sec_deps;
1158 const reloc_deps_e *sec_deps_e;
1159
1160 sec_deps = xtensa_get_section_deps (deps, src);
1161 if (sec_deps == NULL)
1162 return FALSE;
1163
1164 for (sec_deps_e = sec_deps->succs;
1165 sec_deps_e != NULL;
1166 sec_deps_e = sec_deps_e->next)
1167 {
1168 ASSERT (sec_deps_e->src == src);
1169 if (sec_deps_e->tgt == tgt)
1170 return TRUE;
1171 }
1172 return FALSE;
1173 }
1174
1175
1176 static bfd_boolean
1177 deps_has_edge (const reloc_deps_graph *deps,
1178 lang_statement_union_type *src,
1179 lang_statement_union_type *tgt)
1180 {
1181 if (!section_is_source (deps, src))
1182 return FALSE;
1183 if (!section_is_target (deps, tgt))
1184 return FALSE;
1185
1186 if (src->header.type != lang_input_section_enum)
1187 return FALSE;
1188 if (tgt->header.type != lang_input_section_enum)
1189 return FALSE;
1190
1191 return deps_has_sec_edge (deps, src->input_section.section,
1192 tgt->input_section.section);
1193 }
1194
1195
1196 static void
1197 add_deps_edge (reloc_deps_graph *deps, asection *src_sec, asection *tgt_sec)
1198 {
1199 reloc_deps_section *src_sec_deps;
1200 reloc_deps_section *tgt_sec_deps;
1201
1202 reloc_deps_e *src_edge;
1203 reloc_deps_e *tgt_edge;
1204
1205 if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1206 return;
1207
1208 src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1209 if (src_sec_deps == NULL)
1210 {
1211 /* Add a section. */
1212 src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1213 memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1214 src_sec_deps->is_only_literal = 0;
1215 src_sec_deps->preds = NULL;
1216 src_sec_deps->succs = NULL;
1217 xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1218 xtensa_append_section_deps (deps, src_sec);
1219 }
1220
1221 tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1222 if (tgt_sec_deps == NULL)
1223 {
1224 /* Add a section. */
1225 tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1226 memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1227 tgt_sec_deps->is_only_literal = 0;
1228 tgt_sec_deps->preds = NULL;
1229 tgt_sec_deps->succs = NULL;
1230 xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1231 xtensa_append_section_deps (deps, tgt_sec);
1232 }
1233
1234 /* Add the edges. */
1235 src_edge = xmalloc (sizeof (reloc_deps_e));
1236 memset (src_edge, 0, sizeof (reloc_deps_e));
1237 src_edge->src = src_sec;
1238 src_edge->tgt = tgt_sec;
1239 src_edge->next = src_sec_deps->succs;
1240 src_sec_deps->succs = src_edge;
1241
1242 tgt_edge = xmalloc (sizeof (reloc_deps_e));
1243 memset (tgt_edge, 0, sizeof (reloc_deps_e));
1244 tgt_edge->src = src_sec;
1245 tgt_edge->tgt = tgt_sec;
1246 tgt_edge->next = tgt_sec_deps->preds;
1247 tgt_sec_deps->preds = tgt_edge;
1248 }
1249
1250
1251 static void
1252 build_deps_graph_callback (asection *src_sec,
1253 bfd_vma src_offset ATTRIBUTE_UNUSED,
1254 asection *target_sec,
1255 bfd_vma target_offset ATTRIBUTE_UNUSED,
1256 void *closure)
1257 {
1258 reloc_deps_graph *deps = closure;
1259
1260 /* If the target is defined. */
1261 if (target_sec != NULL)
1262 add_deps_edge (deps, src_sec, target_sec);
1263 }
1264
1265
1266 static reloc_deps_graph *
1267 ld_build_required_section_dependence (lang_statement_union_type *s)
1268 {
1269 reloc_deps_graph *deps;
1270 xtensa_ld_iter_stack *stack = NULL;
1271
1272 deps = xmalloc (sizeof (reloc_deps_graph));
1273 deps->sections = NULL;
1274 deps->count = 0;
1275 deps->size = 0;
1276
1277 for (iter_stack_create (&stack, s);
1278 !iter_stack_empty (&stack);
1279 iter_stack_next (&stack))
1280 {
1281 lang_statement_union_type *l = iter_stack_current (&stack);
1282
1283 if (l->header.type == lang_input_section_enum)
1284 {
1285 lang_input_section_type *input;
1286 input = &l->input_section;
1287 xtensa_callback_required_dependence (input->section->owner,
1288 input->section,
1289 &link_info,
1290 /* Use the same closure. */
1291 build_deps_graph_callback,
1292 deps);
1293 }
1294 }
1295 return deps;
1296 }
1297
1298
1299 #if EXTRA_VALIDATION
1300 static size_t
1301 ld_count_children (lang_statement_union_type *s)
1302 {
1303 size_t count = 0;
1304 xtensa_ld_iter_stack *stack = NULL;
1305 for (iter_stack_create (&stack, s);
1306 !iter_stack_empty (&stack);
1307 iter_stack_next (&stack))
1308 {
1309 lang_statement_union_type *l = iter_stack_current (&stack);
1310 ASSERT (l != NULL);
1311 count++;
1312 }
1313 return count;
1314 }
1315 #endif /* EXTRA_VALIDATION */
1316
1317
1318 /* Check if a particular section is included in the link. This will only
1319 be true for one instance of a particular linkonce section. */
1320
1321 static bfd_boolean input_section_found = FALSE;
1322 static asection *input_section_target = NULL;
1323
1324 static void
1325 input_section_linked_worker (lang_statement_union_type *statement)
1326 {
1327 if ((statement->header.type == lang_input_section_enum
1328 && (statement->input_section.section == input_section_target)))
1329 input_section_found = TRUE;
1330 }
1331
1332 static bfd_boolean
1333 input_section_linked (asection *sec)
1334 {
1335 input_section_found = FALSE;
1336 input_section_target = sec;
1337 lang_for_each_statement_worker (input_section_linked_worker, stat_ptr->head);
1338 return input_section_found;
1339 }
1340
1341
1342 /* Strip out any linkonce property tables or XCC exception tables where the
1343 associated linkonce text is from a different object file. Normally,
1344 a matching set of linkonce sections is taken from the same object file,
1345 but sometimes the files are compiled differently so that some of the
1346 linkonce sections are not present in all files. Stripping the
1347 inconsistent sections like this is not completely robust -- a much
1348 better solution is to use comdat groups. */
1349
1350 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
1351
1352 static bfd_boolean
1353 is_inconsistent_linkonce_section (asection *sec)
1354 {
1355 bfd *abfd = sec->owner;
1356 const char *sec_name = bfd_get_section_name (abfd, sec);
1357 const char *name;
1358
1359 if ((bfd_get_section_flags (abfd, sec) & SEC_LINK_ONCE) == 0
1360 || strncmp (sec_name, ".gnu.linkonce.", linkonce_len) != 0)
1361 return FALSE;
1362
1363 /* Check if this is an Xtensa property section or an exception table
1364 for Tensilica's XCC compiler. */
1365 name = sec_name + linkonce_len;
1366 if (CONST_STRNEQ (name, "prop."))
1367 name = strchr (name + 5, '.') + 1;
1368 else if (name[1] == '.'
1369 && (name[0] == 'p' || name[0] == 'e' || name[0] == 'h'))
1370 name += 2;
1371 else
1372 name = 0;
1373
1374 if (name)
1375 {
1376 char *dep_sec_name = xmalloc (strlen (sec_name) + 1);
1377 asection *dep_sec;
1378
1379 /* Get the associated linkonce text section and check if it is
1380 included in the link. If not, this section is inconsistent
1381 and should be stripped. */
1382 strcpy (dep_sec_name, ".gnu.linkonce.t.");
1383 strcat (dep_sec_name, name);
1384 dep_sec = bfd_get_section_by_name (abfd, dep_sec_name);
1385 if (dep_sec == NULL || ! input_section_linked (dep_sec))
1386 {
1387 free (dep_sec_name);
1388 return TRUE;
1389 }
1390 free (dep_sec_name);
1391 }
1392
1393 return FALSE;
1394 }
1395
1396
1397 static void
1398 xtensa_strip_inconsistent_linkonce_sections (lang_statement_list_type *slist)
1399 {
1400 lang_statement_union_type **s_p = &slist->head;
1401 while (*s_p)
1402 {
1403 lang_statement_union_type *s = *s_p;
1404 lang_statement_union_type *s_next = (*s_p)->header.next;
1405
1406 switch (s->header.type)
1407 {
1408 case lang_input_section_enum:
1409 if (is_inconsistent_linkonce_section (s->input_section.section))
1410 {
1411 s->input_section.section->output_section = bfd_abs_section_ptr;
1412 *s_p = s_next;
1413 continue;
1414 }
1415 break;
1416
1417 case lang_constructors_statement_enum:
1418 xtensa_strip_inconsistent_linkonce_sections (&constructor_list);
1419 break;
1420
1421 case lang_output_section_statement_enum:
1422 if (s->output_section_statement.children.head)
1423 xtensa_strip_inconsistent_linkonce_sections
1424 (&s->output_section_statement.children);
1425 break;
1426
1427 case lang_wild_statement_enum:
1428 xtensa_strip_inconsistent_linkonce_sections
1429 (&s->wild_statement.children);
1430 break;
1431
1432 case lang_group_statement_enum:
1433 xtensa_strip_inconsistent_linkonce_sections
1434 (&s->group_statement.children);
1435 break;
1436
1437 case lang_data_statement_enum:
1438 case lang_reloc_statement_enum:
1439 case lang_object_symbols_statement_enum:
1440 case lang_output_statement_enum:
1441 case lang_target_statement_enum:
1442 case lang_input_statement_enum:
1443 case lang_assignment_statement_enum:
1444 case lang_padding_statement_enum:
1445 case lang_address_statement_enum:
1446 case lang_fill_statement_enum:
1447 break;
1448
1449 default:
1450 FAIL ();
1451 break;
1452 }
1453
1454 s_p = &(*s_p)->header.next;
1455 }
1456
1457 /* Reset the tail of the list, in case the last entry was removed. */
1458 if (s_p != slist->tail)
1459 slist->tail = s_p;
1460 }
1461
1462
1463 static void
1464 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1465 {
1466 lang_wild_statement_type *w;
1467 reloc_deps_graph *deps;
1468 if (statement->header.type == lang_wild_statement_enum)
1469 {
1470 #if EXTRA_VALIDATION
1471 size_t old_child_count;
1472 size_t new_child_count;
1473 #endif
1474 bfd_boolean no_reorder;
1475
1476 w = &statement->wild_statement;
1477
1478 no_reorder = FALSE;
1479
1480 /* If it has 0 or 1 section bound, then do not reorder. */
1481 if (w->children.head == NULL
1482 || (w->children.head->header.type == lang_input_section_enum
1483 && w->children.head->header.next == NULL))
1484 no_reorder = TRUE;
1485
1486 if (w->filenames_sorted)
1487 no_reorder = TRUE;
1488
1489 /* Check for sorting in a section list wildcard spec as well. */
1490 if (!no_reorder)
1491 {
1492 struct wildcard_list *l;
1493 for (l = w->section_list; l != NULL; l = l->next)
1494 {
1495 if (l->spec.sorted == TRUE)
1496 {
1497 no_reorder = TRUE;
1498 break;
1499 }
1500 }
1501 }
1502
1503 /* Special case until the NOREORDER linker directive is supported:
1504 *(.init) output sections and *(.fini) specs may NOT be reordered. */
1505
1506 /* Check for sorting in a section list wildcard spec as well. */
1507 if (!no_reorder)
1508 {
1509 struct wildcard_list *l;
1510 for (l = w->section_list; l != NULL; l = l->next)
1511 {
1512 if (l->spec.name
1513 && ((strcmp (".init", l->spec.name) == 0)
1514 || (strcmp (".fini", l->spec.name) == 0)))
1515 {
1516 no_reorder = TRUE;
1517 break;
1518 }
1519 }
1520 }
1521
1522 #if EXTRA_VALIDATION
1523 old_child_count = ld_count_children (statement);
1524 #endif
1525
1526 /* It is now officially a target. Build the graph of source
1527 section -> target section (kept as a list of edges). */
1528 deps = ld_build_required_section_dependence (statement);
1529
1530 /* If this wildcard does not reorder.... */
1531 if (!no_reorder && deps->count != 0)
1532 {
1533 /* First check for reverse dependences. Fix if possible. */
1534 xtensa_layout_wild (deps, w);
1535
1536 xtensa_move_dependencies_to_front (deps, w);
1537 #if EXTRA_VALIDATION
1538 new_child_count = ld_count_children (statement);
1539 ASSERT (new_child_count == old_child_count);
1540 #endif
1541
1542 xtensa_colocate_literals (deps, statement);
1543
1544 #if EXTRA_VALIDATION
1545 new_child_count = ld_count_children (statement);
1546 ASSERT (new_child_count == old_child_count);
1547 #endif
1548 }
1549
1550 /* Clean up. */
1551 free_reloc_deps_graph (deps);
1552 }
1553 }
1554
1555
1556 static void
1557 xtensa_wild_group_interleave (lang_statement_union_type *s)
1558 {
1559 lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1560 }
1561
1562
1563 static void
1564 xtensa_layout_wild (const reloc_deps_graph *deps, lang_wild_statement_type *w)
1565 {
1566 /* If it does not fit initially, we need to do this step. Move all
1567 of the wild literal sections to a new list, then move each of
1568 them back in just before the first section they depend on. */
1569 lang_statement_union_type **s_p;
1570 #if EXTRA_VALIDATION
1571 size_t old_count, new_count;
1572 size_t ct1, ct2;
1573 #endif
1574
1575 lang_wild_statement_type literal_wild;
1576 literal_wild.header.next = NULL;
1577 literal_wild.header.type = lang_wild_statement_enum;
1578 literal_wild.filename = NULL;
1579 literal_wild.filenames_sorted = FALSE;
1580 literal_wild.section_list = NULL;
1581 literal_wild.keep_sections = FALSE;
1582 literal_wild.children.head = NULL;
1583 literal_wild.children.tail = &literal_wild.children.head;
1584
1585 #if EXTRA_VALIDATION
1586 old_count = ld_count_children ((lang_statement_union_type*) w);
1587 #endif
1588
1589 s_p = &w->children.head;
1590 while (*s_p != NULL)
1591 {
1592 lang_statement_union_type *l = *s_p;
1593 if (l->header.type == lang_input_section_enum)
1594 {
1595 if (section_is_target (deps, l)
1596 && ! section_is_source (deps, l))
1597 {
1598 /* Detach. */
1599 *s_p = l->header.next;
1600 if (*s_p == NULL)
1601 w->children.tail = s_p;
1602 l->header.next = NULL;
1603
1604 /* Append. */
1605 *literal_wild.children.tail = l;
1606 literal_wild.children.tail = &l->header.next;
1607 continue;
1608 }
1609 }
1610 s_p = &(*s_p)->header.next;
1611 }
1612
1613 #if EXTRA_VALIDATION
1614 ct1 = ld_count_children ((lang_statement_union_type*) w);
1615 ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1616
1617 ASSERT (old_count == (ct1 + ct2));
1618 #endif
1619
1620 /* Now place them back in front of their dependent sections. */
1621
1622 while (literal_wild.children.head != NULL)
1623 {
1624 lang_statement_union_type *lit = literal_wild.children.head;
1625 bfd_boolean placed = FALSE;
1626
1627 #if EXTRA_VALIDATION
1628 ASSERT (ct2 > 0);
1629 ct2--;
1630 #endif
1631
1632 /* Detach. */
1633 literal_wild.children.head = lit->header.next;
1634 if (literal_wild.children.head == NULL)
1635 literal_wild.children.tail = &literal_wild.children.head;
1636 lit->header.next = NULL;
1637
1638 /* Find a spot to place it. */
1639 for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1640 {
1641 lang_statement_union_type *src = *s_p;
1642 if (deps_has_edge (deps, src, lit))
1643 {
1644 /* Place it here. */
1645 lit->header.next = *s_p;
1646 *s_p = lit;
1647 placed = TRUE;
1648 break;
1649 }
1650 }
1651
1652 if (!placed)
1653 {
1654 /* Put it at the end. */
1655 *w->children.tail = lit;
1656 w->children.tail = &lit->header.next;
1657 }
1658 }
1659
1660 #if EXTRA_VALIDATION
1661 new_count = ld_count_children ((lang_statement_union_type*) w);
1662 ASSERT (new_count == old_count);
1663 #endif
1664 }
1665
1666
1667 static void
1668 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1669 {
1670 reloc_deps_graph *deps;
1671 if (statement->header.type == lang_output_section_statement_enum)
1672 {
1673 /* Now, we walk over the contours of the output section statement.
1674
1675 First we build the literal section dependences as before.
1676
1677 At the first uniquely_literal section, we mark it as a good
1678 spot to place other literals. Continue walking (and counting
1679 sizes) until we find the next literal section. If this
1680 section can be moved to the first one, then we move it. If
1681 we every find a modification of ".", start over. If we find
1682 a labeling of the current location, start over. Finally, at
1683 the end, if we require page alignment, add page alignments. */
1684
1685 #if EXTRA_VALIDATION
1686 size_t old_child_count;
1687 size_t new_child_count;
1688 #endif
1689 bfd_boolean no_reorder = FALSE;
1690
1691 #if EXTRA_VALIDATION
1692 old_child_count = ld_count_children (statement);
1693 #endif
1694
1695 /* It is now officially a target. Build the graph of source
1696 section -> target section (kept as a list of edges). */
1697
1698 deps = ld_build_required_section_dependence (statement);
1699
1700 /* If this wildcard does not reorder.... */
1701 if (!no_reorder)
1702 {
1703 /* First check for reverse dependences. Fix if possible. */
1704 xtensa_colocate_literals (deps, statement);
1705
1706 #if EXTRA_VALIDATION
1707 new_child_count = ld_count_children (statement);
1708 ASSERT (new_child_count == old_child_count);
1709 #endif
1710 }
1711
1712 /* Insert align/offset assignment statement. */
1713 if (xtensa_use_literal_pages)
1714 {
1715 ld_xtensa_insert_page_offsets (0, statement, deps,
1716 xtensa_use_literal_pages);
1717 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1718 statement);
1719 }
1720
1721 /* Clean up. */
1722 free_reloc_deps_graph (deps);
1723 }
1724 }
1725
1726
1727 static void
1728 xtensa_colocate_output_literals (lang_statement_union_type *s)
1729 {
1730 lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1731 }
1732
1733
1734 static void
1735 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1736 {
1737 switch (statement->header.type)
1738 {
1739 case lang_input_section_enum:
1740 {
1741 asection *bfd_section = statement->input_section.section;
1742 bfd_section->output_offset = 0;
1743 }
1744 break;
1745 default:
1746 break;
1747 }
1748 }
1749
1750
1751 static bfd_vma
1752 ld_assign_relative_paged_dot (bfd_vma dot,
1753 lang_statement_union_type *s,
1754 const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1755 bfd_boolean lit_align)
1756 {
1757 /* Walk through all of the input statements in this wild statement
1758 assign dot to all of them. */
1759
1760 xtensa_ld_iter_stack *stack = NULL;
1761 xtensa_ld_iter_stack **stack_p = &stack;
1762
1763 bfd_boolean first_section = FALSE;
1764 bfd_boolean in_literals = FALSE;
1765
1766 for (iter_stack_create (stack_p, s);
1767 !iter_stack_empty (stack_p);
1768 iter_stack_next (stack_p))
1769 {
1770 lang_statement_union_type *l = iter_stack_current (stack_p);
1771
1772 switch (l->header.type)
1773 {
1774 case lang_input_section_enum:
1775 {
1776 asection *section = l->input_section.section;
1777 size_t align_pow = section->alignment_power;
1778 bfd_boolean do_xtensa_alignment = FALSE;
1779
1780 if (lit_align)
1781 {
1782 bfd_boolean sec_is_target = section_is_target (deps, l);
1783 bfd_boolean sec_is_source = section_is_source (deps, l);
1784
1785 if (section->size != 0
1786 && (first_section
1787 || (in_literals && !sec_is_target)
1788 || (!in_literals && sec_is_target)))
1789 {
1790 do_xtensa_alignment = TRUE;
1791 }
1792 first_section = FALSE;
1793 if (section->size != 0)
1794 in_literals = (sec_is_target && !sec_is_source);
1795 }
1796
1797 if (do_xtensa_alignment && xtensa_page_power != 0)
1798 dot += (1 << xtensa_page_power);
1799
1800 dot = align_power (dot, align_pow);
1801 section->output_offset = dot;
1802 dot += section->size;
1803 }
1804 break;
1805 case lang_fill_statement_enum:
1806 dot += l->fill_statement.size;
1807 break;
1808 case lang_padding_statement_enum:
1809 dot += l->padding_statement.size;
1810 break;
1811 default:
1812 break;
1813 }
1814 }
1815 return dot;
1816 }
1817
1818
1819 static bfd_boolean
1820 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1821 const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1822 {
1823 /* Walk over all of the dependencies that we identified and make
1824 sure that IF the source and target are here (addr != 0):
1825 1) target addr < source addr
1826 2) (roundup(source + source_size, 4) - rounddown(target, 4))
1827 < (256K - (1 << bad align))
1828 Need a worst-case proof.... */
1829
1830 xtensa_ld_iter_stack *stack = NULL;
1831 xtensa_ld_iter_stack **stack_p = &stack;
1832 size_t max_align_power = 0;
1833 size_t align_penalty = 256;
1834 reloc_deps_e *e;
1835 size_t i;
1836
1837 /* Find the worst-case alignment requirement for this set of statements. */
1838 for (iter_stack_create (stack_p, statement);
1839 !iter_stack_empty (stack_p);
1840 iter_stack_next (stack_p))
1841 {
1842 lang_statement_union_type *l = iter_stack_current (stack_p);
1843 if (l->header.type == lang_input_section_enum)
1844 {
1845 lang_input_section_type *input = &l->input_section;
1846 asection *section = input->section;
1847 if (section->alignment_power > max_align_power)
1848 max_align_power = section->alignment_power;
1849 }
1850 }
1851
1852 /* Now check that everything fits. */
1853 for (i = 0; i < deps->count; i++)
1854 {
1855 asection *sec = deps->sections[i];
1856 const reloc_deps_section *deps_section =
1857 xtensa_get_section_deps (deps, sec);
1858 if (deps_section)
1859 {
1860 /* We choose to walk through the successors. */
1861 for (e = deps_section->succs; e != NULL; e = e->next)
1862 {
1863 if (e->src != e->tgt
1864 && e->src->output_section == e->tgt->output_section
1865 && e->src->output_offset != 0
1866 && e->tgt->output_offset != 0)
1867 {
1868 bfd_vma l32r_addr =
1869 align_power (e->src->output_offset + e->src->size, 2);
1870 bfd_vma target_addr = e->tgt->output_offset & ~3;
1871 if (l32r_addr < target_addr)
1872 {
1873 fprintf (stderr, "Warning: "
1874 "l32r target section before l32r\n");
1875 return FALSE;
1876 }
1877
1878 if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1879 return FALSE;
1880 }
1881 }
1882 }
1883 }
1884
1885 return TRUE;
1886 }
1887
1888
1889 static bfd_vma
1890 ld_xtensa_insert_page_offsets (bfd_vma dot,
1891 lang_statement_union_type *s,
1892 reloc_deps_graph *deps,
1893 bfd_boolean lit_align)
1894 {
1895 xtensa_ld_iter_stack *stack = NULL;
1896 xtensa_ld_iter_stack **stack_p = &stack;
1897
1898 bfd_boolean first_section = FALSE;
1899 bfd_boolean in_literals = FALSE;
1900
1901 if (!lit_align)
1902 return FALSE;
1903
1904 for (iter_stack_create (stack_p, s);
1905 !iter_stack_empty (stack_p);
1906 iter_stack_next (stack_p))
1907 {
1908 lang_statement_union_type *l = iter_stack_current (stack_p);
1909
1910 switch (l->header.type)
1911 {
1912 case lang_input_section_enum:
1913 {
1914 asection *section = l->input_section.section;
1915 bfd_boolean do_xtensa_alignment = FALSE;
1916
1917 if (lit_align)
1918 {
1919 if (section->size != 0
1920 && (first_section
1921 || (in_literals && !section_is_target (deps, l))
1922 || (!in_literals && section_is_target (deps, l))))
1923 {
1924 do_xtensa_alignment = TRUE;
1925 }
1926 first_section = FALSE;
1927 if (section->size != 0)
1928 {
1929 in_literals = (section_is_target (deps, l)
1930 && !section_is_source (deps, l));
1931 }
1932 }
1933
1934 if (do_xtensa_alignment && xtensa_page_power != 0)
1935 {
1936 /* Create an expression that increments the current address,
1937 i.e., "dot", by (1 << xtensa_align_power). */
1938 etree_type *name_op = exp_nameop (NAME, ".");
1939 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1940 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1941 etree_type *assign_op = exp_assop ('=', ".", add_op);
1942
1943 lang_assignment_statement_type *assign_stmt;
1944 lang_statement_union_type *assign_union;
1945 lang_statement_list_type tmplist;
1946
1947 /* There is hidden state in "lang_add_assignment". It
1948 appends the new assignment statement to the stat_ptr
1949 list. Thus, we swap it before and after the call. */
1950
1951 lang_list_init (&tmplist);
1952 push_stat_ptr (&tmplist);
1953 /* Warning: side effect; statement appended to stat_ptr. */
1954 assign_stmt = lang_add_assignment (assign_op);
1955 assign_union = (lang_statement_union_type *) assign_stmt;
1956 pop_stat_ptr ();
1957
1958 assign_union->header.next = l;
1959 *(*stack_p)->iterloc.loc = assign_union;
1960 iter_stack_next (stack_p);
1961 }
1962 }
1963 break;
1964 default:
1965 break;
1966 }
1967 }
1968 return dot;
1969 }
1970
1971 EOF
1972
1973 # Define some shell vars to insert bits of code into the standard ELF
1974 # parse_args and list_options functions.
1975 #
1976 PARSE_AND_LIST_PROLOGUE='
1977 #define OPTION_OPT_SIZEOPT (300)
1978 #define OPTION_LITERAL_MOVEMENT (OPTION_OPT_SIZEOPT + 1)
1979 #define OPTION_NO_LITERAL_MOVEMENT (OPTION_LITERAL_MOVEMENT + 1)
1980 extern int elf32xtensa_size_opt;
1981 extern int elf32xtensa_no_literal_movement;
1982 '
1983
1984 PARSE_AND_LIST_LONGOPTS='
1985 { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1986 { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1987 { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
1988 '
1989
1990 PARSE_AND_LIST_OPTIONS='
1991 fprintf (file, _("\
1992 --size-opt When relaxing longcalls, prefer size\n\
1993 optimization over branch target alignment\n"));
1994 '
1995
1996 PARSE_AND_LIST_ARGS_CASES='
1997 case OPTION_OPT_SIZEOPT:
1998 elf32xtensa_size_opt = 1;
1999 break;
2000 case OPTION_LITERAL_MOVEMENT:
2001 elf32xtensa_no_literal_movement = 0;
2002 break;
2003 case OPTION_NO_LITERAL_MOVEMENT:
2004 elf32xtensa_no_literal_movement = 1;
2005 break;
2006 '
2007
2008 # Replace some of the standard ELF functions with our own versions.
2009 #
2010 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
2011 LDEMUL_AFTER_OPEN=elf_xtensa_after_open
2012 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
2013 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation