1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2003, 2004, 2005, 2006, 2007
3 # Free Software Foundation, Inc.
5 # This file is part of the GNU Binutils.
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.
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.
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,
23 # This file is sourced from elf32.em, and defines extra xtensa-elf
28 #include <xtensa-config.h>
29 #include "../bfd/elf-bfd.h"
30 #include "../bfd/libbfd.h"
31 #include "elf/xtensa.h"
34 /* Provide default values for new configuration settings. */
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 *);
45 /* Flag for the emulation-specific "--no-relax" option. */
46 static bfd_boolean disable_relaxation = FALSE;
48 /* This number is irrelevant until we turn on use_literal_pages */
49 static bfd_vma xtensa_page_power = 12; /* 4K pages. */
51 /* To force a page break between literals and text, change
52 xtensa_use_literal_pages to "TRUE". */
53 static bfd_boolean xtensa_use_literal_pages = FALSE;
55 #define EXTRA_VALIDATION 0
59 elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
60 char **argv ATTRIBUTE_UNUSED)
63 return "${BIG_OUTPUT_FORMAT}";
65 return "${LITTLE_OUTPUT_FORMAT}";
70 elf_xtensa_before_parse (void)
72 /* Just call the default hook.... Tensilica's version of this function
73 does some other work that isn't relevant here. */
74 gld${EMULATION_NAME}_before_parse ();
79 remove_section (bfd *abfd, asection *os)
82 for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
86 os->owner->section_count--;
93 replace_insn_sec_with_prop_sec (bfd *abfd,
94 const char *insn_sec_name,
95 const char *prop_sec_name,
100 bfd_byte *prop_contents = NULL;
101 bfd_byte *insn_contents = NULL;
102 unsigned entry_count;
104 Elf_Internal_Shdr *symtab_hdr;
105 Elf_Internal_Rela *internal_relocs = NULL;
106 unsigned reloc_count;
109 insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
110 if (insn_sec == NULL)
112 entry_count = insn_sec->size / 8;
114 prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
115 if (prop_sec != NULL && insn_sec != NULL)
117 *error_message = _("file already has property tables");
121 if (insn_sec->size != 0)
123 insn_contents = (bfd_byte *) bfd_malloc (insn_sec->size);
124 if (insn_contents == NULL)
126 *error_message = _("out of memory");
129 if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
130 (file_ptr) 0, insn_sec->size))
132 *error_message = _("failed to read section contents");
137 /* Create a property table section for it. */
138 prop_sec_name = strdup (prop_sec_name);
139 prop_sec = bfd_make_section_with_flags
140 (abfd, prop_sec_name, bfd_get_section_flags (abfd, insn_sec));
142 || ! bfd_set_section_alignment (abfd, prop_sec, 2))
144 *error_message = _("could not create new section");
148 prop_sec->size = entry_count * 12;
149 prop_contents = (bfd_byte *) bfd_zalloc (abfd, prop_sec->size);
150 elf_section_data (prop_sec)->this_hdr.contents = prop_contents;
152 /* The entry size and size must be set to allow the linker to compute
153 the number of relocations since it does not use reloc_count. */
154 elf_section_data (prop_sec)->rel_hdr.sh_entsize =
155 sizeof (Elf32_External_Rela);
156 elf_section_data (prop_sec)->rel_hdr.sh_size =
157 elf_section_data (insn_sec)->rel_hdr.sh_size;
159 if (prop_contents == NULL && prop_sec->size != 0)
161 *error_message = _("could not allocate section contents");
165 /* Read the relocations. */
166 reloc_count = insn_sec->reloc_count;
167 if (reloc_count != 0)
169 /* If there is already an internal_reloc, then save it so that the
170 read_relocs function freshly allocates a copy. */
171 Elf_Internal_Rela *saved_relocs = elf_section_data (insn_sec)->relocs;
173 elf_section_data (insn_sec)->relocs = NULL;
175 _bfd_elf_link_read_relocs (abfd, insn_sec, NULL, NULL, FALSE);
176 elf_section_data (insn_sec)->relocs = saved_relocs;
178 if (internal_relocs == NULL)
180 *error_message = _("out of memory");
185 /* Create a relocation section for the property section. */
186 if (internal_relocs != NULL)
188 elf_section_data (prop_sec)->relocs = internal_relocs;
189 prop_sec->reloc_count = reloc_count;
192 /* Now copy each insn table entry to the prop table entry with
193 appropriate flags. */
194 for (entry = 0; entry < entry_count; ++entry)
197 unsigned flags = (XTENSA_PROP_INSN | XTENSA_PROP_NO_TRANSFORM
198 | XTENSA_PROP_INSN_NO_REORDER);
199 value = bfd_get_32 (abfd, insn_contents + entry * 8 + 0);
200 bfd_put_32 (abfd, value, prop_contents + entry * 12 + 0);
201 value = bfd_get_32 (abfd, insn_contents + entry * 8 + 4);
202 bfd_put_32 (abfd, value, prop_contents + entry * 12 + 4);
203 bfd_put_32 (abfd, flags, prop_contents + entry * 12 + 8);
206 /* Now copy all of the relocations. Change offsets for the
207 instruction table section to offsets in the property table
212 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
214 for (i = 0; i < reloc_count; i++)
216 Elf_Internal_Rela *rela;
219 rela = &internal_relocs[i];
221 /* If this relocation is to the .xt.insn section,
222 change the section number and the offset. */
223 r_offset = rela->r_offset;
224 r_offset += 4 * (r_offset / 8);
225 rela->r_offset = r_offset;
229 remove_section (abfd, insn_sec);
232 free (insn_contents);
237 if (prop_sec && prop_sec->owner)
238 remove_section (abfd, prop_sec);
240 free (insn_contents);
242 free (internal_relocs);
248 #define PROP_SEC_BASE_NAME ".xt.prop"
249 #define INSN_SEC_BASE_NAME ".xt.insn"
250 #define LINKONCE_SEC_OLD_TEXT_BASE_NAME ".gnu.linkonce.x."
254 replace_instruction_table_sections (bfd *abfd, asection *sec)
257 const char *insn_sec_name = NULL;
258 char *prop_sec_name = NULL;
259 char *owned_prop_sec_name = NULL;
260 const char *sec_name;
262 sec_name = bfd_get_section_name (abfd, sec);
263 if (strcmp (sec_name, INSN_SEC_BASE_NAME) == 0)
265 insn_sec_name = INSN_SEC_BASE_NAME;
266 prop_sec_name = PROP_SEC_BASE_NAME;
268 else if (CONST_STRNEQ (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME))
270 insn_sec_name = sec_name;
271 owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
272 prop_sec_name = owned_prop_sec_name;
273 strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
274 strcat (prop_sec_name,
275 sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
277 if (insn_sec_name != NULL)
279 if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
282 einfo (_("%P: warning: failed to convert %s table in %B (%s); subsequent disassembly may be incomplete\n"),
283 insn_sec_name, abfd, message);
286 if (owned_prop_sec_name)
287 free (owned_prop_sec_name);
291 /* This is called after all input sections have been opened to convert
292 instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
293 tables (.xt.prop) before any section placement. */
296 elf_xtensa_after_open (void)
298 /* First call the ELF version. */
299 gld${EMULATION_NAME}_after_open ();
301 /* Now search the input files looking for instruction table sections. */
302 LANG_FOR_EACH_INPUT_STATEMENT (f)
304 asection *sec = f->the_bfd->sections;
307 /* Do not use bfd_map_over_sections here since we are removing
308 sections as we iterate. */
311 next_sec = sec->next;
312 replace_instruction_table_sections (f->the_bfd, sec);
320 xt_config_info_unpack_and_check (char *data,
321 bfd_boolean *pmismatch,
337 /* Overwrite the equal sign. */
340 /* Check if this is a quoted string or a number. */
343 /* No string values are currently checked by LD;
344 just skip over the quotes. */
349 /* Overwrite the trailing quote. */
356 num = strtoul (d, &d, 0);
358 if (! strcmp (key, "ABI"))
360 if (num != XSHAL_ABI)
363 *pmsg = "ABI does not match";
366 else if (! strcmp (key, "USE_ABSOLUTE_LITERALS"))
368 if (num != XSHAL_USE_ABSOLUTE_LITERALS)
371 *pmsg = "incompatible use of the Extended L32R option";
387 #define XTINFO_NAME "Xtensa_Info"
388 #define XTINFO_NAMESZ 12
389 #define XTINFO_TYPE 1
392 check_xtensa_info (bfd *abfd, asection *info_sec)
394 char *data, *errmsg = "";
395 bfd_boolean mismatch;
397 data = xmalloc (info_sec->size);
398 if (! bfd_get_section_contents (abfd, info_sec, data, 0, info_sec->size))
399 einfo (_("%F%P:%B: cannot read contents of section %A\n"), abfd, info_sec);
401 if (info_sec->size > 24
402 && info_sec->size >= 24 + bfd_get_32 (abfd, data + 4)
403 && bfd_get_32 (abfd, data + 0) == XTINFO_NAMESZ
404 && bfd_get_32 (abfd, data + 8) == XTINFO_TYPE
405 && strcmp (data + 12, XTINFO_NAME) == 0
406 && xt_config_info_unpack_and_check (data + 12 + XTINFO_NAMESZ,
410 einfo (_("%P:%B: warning: incompatible Xtensa configuration (%s)\n"),
414 einfo (_("%P:%B: warning: cannot parse .xtensa.info section\n"), abfd);
420 /* This is called after the sections have been attached to output
421 sections, but before any sizes or addresses have been set. */
424 elf_xtensa_before_allocation (void)
426 asection *info_sec, *first_info_sec;
428 bfd_boolean is_big_endian = XCHAL_HAVE_BE;
430 /* Check that the output endianness matches the Xtensa
431 configuration. The BFD library always includes both big and
432 little endian target vectors for Xtensa, but it only supports the
433 detailed instruction encode/decode operations (such as are
434 required to process relocations) for the selected Xtensa
437 if (is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
439 einfo (_("%F%P: little endian output does not match "
440 "Xtensa configuration\n"));
442 if (!is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
444 einfo (_("%F%P: big endian output does not match "
445 "Xtensa configuration\n"));
448 /* Keep track of the first input .xtensa.info section, and as a fallback,
449 the first input bfd where a .xtensa.info section could be created.
450 After the input .xtensa.info has been checked, the contents of the
451 first one will be replaced with the output .xtensa.info table. */
455 LANG_FOR_EACH_INPUT_STATEMENT (f)
457 /* Check that the endianness for each input file matches the output.
458 The merge_private_bfd_data hook has already reported any mismatches
459 as errors, but those errors are not fatal. At this point, we
460 cannot go any further if there are any mismatches. */
461 if ((is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
462 || (!is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
463 einfo (_("%F%P: cross-endian linking for %B not supported\n"),
467 first_bfd = f->the_bfd;
469 info_sec = bfd_get_section_by_name (f->the_bfd, ".xtensa.info");
473 if (! first_info_sec)
474 first_info_sec = info_sec;
476 /* Unpack the .xtensa.info section and check it against the current
477 Xtensa configuration. */
478 check_xtensa_info (f->the_bfd, info_sec);
480 /* Do not include this copy of .xtensa.info in the output. */
482 info_sec->flags |= SEC_EXCLUDE;
485 /* Reuse the first .xtensa.info input section to hold the output
486 .xtensa.info; or, if none were found, create a new section in the
487 first input bfd (assuming there is one). */
488 info_sec = first_info_sec;
489 if (! info_sec && first_bfd)
491 info_sec = bfd_make_section_with_flags (first_bfd, ".xtensa.info",
492 SEC_HAS_CONTENTS | SEC_READONLY);
494 einfo (_("%F%P: failed to create .xtensa.info section\n"));
498 int xtensa_info_size;
501 info_sec->flags &= ~SEC_EXCLUDE;
502 info_sec->flags |= SEC_IN_MEMORY;
504 data = xmalloc (100);
505 sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
506 XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
507 xtensa_info_size = strlen (data) + 1;
509 /* Add enough null terminators to pad to a word boundary. */
511 data[xtensa_info_size++] = 0;
512 while ((xtensa_info_size & 3) != 0);
514 info_sec->size = 12 + XTINFO_NAMESZ + xtensa_info_size;
515 info_sec->contents = xmalloc (info_sec->size);
516 bfd_put_32 (info_sec->owner, XTINFO_NAMESZ, info_sec->contents + 0);
517 bfd_put_32 (info_sec->owner, xtensa_info_size, info_sec->contents + 4);
518 bfd_put_32 (info_sec->owner, XTINFO_TYPE, info_sec->contents + 8);
519 memcpy (info_sec->contents + 12, XTINFO_NAME, XTINFO_NAMESZ);
520 memcpy (info_sec->contents + 12 + XTINFO_NAMESZ, data, xtensa_info_size);
524 /* Enable relaxation by default if the "--no-relax" option was not
525 specified. This is done here instead of in the before_parse hook
526 because there is a check in main() to prohibit use of --relax and
527 -r together and that combination should be allowed for Xtensa. */
529 if (!disable_relaxation)
530 command_line.relax = TRUE;
532 xtensa_strip_inconsistent_linkonce_sections (stat_ptr);
534 gld${EMULATION_NAME}_before_allocation ();
536 xtensa_wild_group_interleave (stat_ptr->head);
537 if (command_line.relax)
538 xtensa_colocate_output_literals (stat_ptr->head);
540 /* TBD: We need to force the page alignments to here and only do
541 them as needed for the entire output section. Finally, if this
542 is a relocatable link then we need to add alignment notes so
543 that the literals can be separated later. */
547 typedef struct wildcard_list section_name_list;
549 typedef struct reloc_deps_e_t reloc_deps_e;
550 typedef struct reloc_deps_section_t reloc_deps_section;
551 typedef struct reloc_deps_graph_t reloc_deps_graph;
554 struct reloc_deps_e_t
556 asection *src; /* Contains l32rs. */
557 asection *tgt; /* Contains literals. */
561 /* Place these in the userdata field. */
562 struct reloc_deps_section_t
566 bfd_boolean is_only_literal;
570 struct reloc_deps_graph_t
577 static void xtensa_layout_wild
578 (const reloc_deps_graph *, lang_wild_statement_type *);
580 typedef void (*deps_callback_t) (asection *, /* src_sec */
581 bfd_vma, /* src_offset */
582 asection *, /* target_sec */
583 bfd_vma, /* target_offset */
584 void *); /* closure */
586 extern bfd_boolean xtensa_callback_required_dependence
587 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
588 static void xtensa_ldlang_clear_addresses (lang_statement_union_type *);
589 static bfd_boolean ld_local_file_relocations_fit
590 (lang_statement_union_type *, const reloc_deps_graph *);
591 static bfd_vma ld_assign_relative_paged_dot
592 (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
594 static bfd_vma ld_xtensa_insert_page_offsets
595 (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
597 static size_t ld_count_children (lang_statement_union_type *);
600 extern lang_statement_list_type constructor_list;
602 /* Begin verbatim code from ldlang.c:
603 the following are copied from ldlang.c because they are defined
607 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
608 lang_statement_union_type *s)
610 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
614 switch (s->header.type)
616 case lang_constructors_statement_enum:
617 lang_for_each_statement_worker (func, constructor_list.head);
619 case lang_output_section_statement_enum:
620 lang_for_each_statement_worker
622 s->output_section_statement.children.head);
624 case lang_wild_statement_enum:
625 lang_for_each_statement_worker
627 s->wild_statement.children.head);
629 case lang_group_statement_enum:
630 lang_for_each_statement_worker (func,
631 s->group_statement.children.head);
633 case lang_data_statement_enum:
634 case lang_reloc_statement_enum:
635 case lang_object_symbols_statement_enum:
636 case lang_output_statement_enum:
637 case lang_target_statement_enum:
638 case lang_input_section_enum:
639 case lang_input_statement_enum:
640 case lang_assignment_statement_enum:
641 case lang_padding_statement_enum:
642 case lang_address_statement_enum:
643 case lang_fill_statement_enum:
652 /* End of verbatim code from ldlang.c. */
655 static reloc_deps_section *
656 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
659 /* We have a separate function for this so that
660 we could in the future keep a completely independent
661 structure that maps a section to its dependence edges.
662 For now, we place these in the sec->userdata field. */
663 reloc_deps_section *sec_deps = sec->userdata;
668 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
670 reloc_deps_section *deps_section)
672 sec->userdata = deps_section;
676 /* This is used to keep a list of all of the sections participating in
677 the graph so we can clean them up quickly. */
680 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
682 if (deps->size <= deps->count)
684 asection **new_sections;
688 new_size = deps->size * 2;
692 new_sections = xmalloc (sizeof (asection *) * new_size);
693 memset (new_sections, 0, sizeof (asection *) * new_size);
694 for (i = 0; i < deps->count; i++)
696 new_sections[i] = deps->sections[i];
698 if (deps->sections != NULL)
699 free (deps->sections);
700 deps->sections = new_sections;
701 deps->size = new_size;
703 deps->sections[deps->count] = sec;
709 free_reloc_deps_graph (reloc_deps_graph *deps)
712 for (i = 0; i < deps->count; i++)
714 asection *sec = deps->sections[i];
715 reloc_deps_section *sec_deps;
716 sec_deps = xtensa_get_section_deps (deps, sec);
720 while (sec_deps->succs != NULL)
722 next = sec_deps->succs->next;
723 free (sec_deps->succs);
724 sec_deps->succs = next;
727 while (sec_deps->preds != NULL)
729 next = sec_deps->preds->next;
730 free (sec_deps->preds);
731 sec_deps->preds = next;
735 xtensa_set_section_deps (deps, sec, NULL);
738 free (deps->sections);
745 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
746 lang_statement_union_type *s)
749 const reloc_deps_section *sec_deps;
751 if (s->header.type != lang_input_section_enum)
753 sec = s->input_section.section;
755 sec_deps = xtensa_get_section_deps (deps, sec);
756 return sec_deps && sec_deps->succs != NULL;
761 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
762 lang_statement_union_type *s)
765 const reloc_deps_section *sec_deps;
767 if (s->header.type != lang_input_section_enum)
769 sec = s->input_section.section;
771 sec_deps = xtensa_get_section_deps (deps, sec);
772 return sec_deps && sec_deps->preds != NULL;
777 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
778 lang_statement_union_type *s)
780 return (section_is_source (deps, s)
781 || section_is_target (deps, s));
785 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
786 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
788 struct xtensa_ld_iter_t
790 lang_statement_union_type *parent; /* Parent of the list. */
791 lang_statement_list_type *l; /* List that holds it. */
792 lang_statement_union_type **loc; /* Place in the list. */
795 struct xtensa_ld_iter_stack_t
797 xtensa_ld_iter iterloc; /* List that hold it. */
799 xtensa_ld_iter_stack *next; /* Next in the stack. */
800 xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
805 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
807 lang_statement_union_type *to_next;
808 lang_statement_union_type *current_next;
809 lang_statement_union_type **e;
812 size_t old_to_count, new_to_count;
813 size_t old_current_count, new_current_count;
820 old_to_count = ld_count_children (to->parent);
821 old_current_count = ld_count_children (current->parent);
824 to_next = *(to->loc);
825 current_next = (*current->loc)->header.next;
827 *(to->loc) = *(current->loc);
829 *(current->loc) = current_next;
830 (*(to->loc))->header.next = to_next;
832 /* reset "to" list tail */
833 for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
837 /* reset "current" list tail */
838 for (e = ¤t->l->head; *e != NULL; e = &(*e)->header.next)
840 current->l->tail = e;
843 new_to_count = ld_count_children (to->parent);
844 new_current_count = ld_count_children (current->parent);
846 ASSERT ((old_to_count + old_current_count)
847 == (new_to_count + new_current_count));
852 /* Can only be called with lang_statements that have lists. Returns
853 FALSE if the list is empty. */
856 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
858 return *stack_p == NULL;
863 iter_stack_push (xtensa_ld_iter_stack **stack_p,
864 lang_statement_union_type *parent)
866 xtensa_ld_iter_stack *stack;
867 lang_statement_list_type *l = NULL;
869 switch (parent->header.type)
871 case lang_output_section_statement_enum:
872 l = &parent->output_section_statement.children;
874 case lang_wild_statement_enum:
875 l = &parent->wild_statement.children;
877 case lang_group_statement_enum:
878 l = &parent->group_statement.children;
885 /* Empty. do not push. */
886 if (l->tail == &l->head)
889 stack = xmalloc (sizeof (xtensa_ld_iter_stack));
890 memset (stack, 0, sizeof (xtensa_ld_iter_stack));
891 stack->iterloc.parent = parent;
892 stack->iterloc.l = l;
893 stack->iterloc.loc = &l->head;
895 stack->next = *stack_p;
897 if (*stack_p != NULL)
898 (*stack_p)->prev = stack;
905 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
907 xtensa_ld_iter_stack *stack;
913 ASSERT (stack != NULL);
917 if (stack->next != NULL)
918 stack->next->prev = NULL;
920 *stack_p = stack->next;
925 /* This MUST be called if, during iteration, the user changes the
926 underlying structure. It will check for a NULL current and advance
930 iter_stack_update (xtensa_ld_iter_stack **stack_p)
932 if (!iter_stack_empty (stack_p)
933 && (*(*stack_p)->iterloc.loc) == NULL)
935 iter_stack_pop (stack_p);
937 while (!iter_stack_empty (stack_p)
938 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
940 iter_stack_pop (stack_p);
942 if (!iter_stack_empty (stack_p))
943 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
949 iter_stack_next (xtensa_ld_iter_stack **stack_p)
951 xtensa_ld_iter_stack *stack;
952 lang_statement_union_type *current;
955 current = *stack->iterloc.loc;
956 /* If we are on the first element. */
959 switch (current->header.type)
961 case lang_output_section_statement_enum:
962 case lang_wild_statement_enum:
963 case lang_group_statement_enum:
964 /* If the list if not empty, we are done. */
965 if (iter_stack_push (stack_p, *stack->iterloc.loc))
967 /* Otherwise increment the pointer as normal. */
974 while (!iter_stack_empty (stack_p)
975 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
977 iter_stack_pop (stack_p);
979 if (!iter_stack_empty (stack_p))
980 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
984 static lang_statement_union_type *
985 iter_stack_current (xtensa_ld_iter_stack **stack_p)
987 return *((*stack_p)->iterloc.loc);
991 /* The iter stack is a preorder. */
994 iter_stack_create (xtensa_ld_iter_stack **stack_p,
995 lang_statement_union_type *parent)
997 iter_stack_push (stack_p, parent);
1002 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p, xtensa_ld_iter *front)
1004 *front = (*stack_p)->iterloc;
1009 xtensa_colocate_literals (reloc_deps_graph *deps,
1010 lang_statement_union_type *statement)
1012 /* Keep a stack of pointers to control iteration through the contours. */
1013 xtensa_ld_iter_stack *stack = NULL;
1014 xtensa_ld_iter_stack **stack_p = &stack;
1016 xtensa_ld_iter front; /* Location where new insertion should occur. */
1017 xtensa_ld_iter *front_p = NULL;
1019 xtensa_ld_iter current; /* Location we are checking. */
1020 xtensa_ld_iter *current_p = NULL;
1021 bfd_boolean in_literals = FALSE;
1023 if (deps->count == 0)
1026 iter_stack_create (stack_p, statement);
1028 while (!iter_stack_empty (stack_p))
1030 bfd_boolean skip_increment = FALSE;
1031 lang_statement_union_type *l = iter_stack_current (stack_p);
1033 switch (l->header.type)
1035 case lang_assignment_statement_enum:
1036 /* Any assignment statement should block reordering across it. */
1038 in_literals = FALSE;
1041 case lang_input_section_enum:
1042 if (front_p == NULL)
1044 in_literals = (section_is_target (deps, l)
1045 && !section_is_source (deps, l));
1049 iter_stack_copy_current (stack_p, front_p);
1054 bfd_boolean is_target;
1055 current_p = ¤t;
1056 iter_stack_copy_current (stack_p, current_p);
1057 is_target = (section_is_target (deps, l)
1058 && !section_is_source (deps, l));
1062 iter_stack_copy_current (stack_p, front_p);
1064 in_literals = FALSE;
1070 /* Try to insert in place. */
1071 ld_xtensa_move_section_after (front_p, current_p);
1072 ld_assign_relative_paged_dot (0x100000,
1075 xtensa_use_literal_pages);
1077 /* We use this code because it's already written. */
1078 if (!ld_local_file_relocations_fit (statement, deps))
1081 ld_xtensa_move_section_after (current_p, front_p);
1082 /* Reset the literal placement. */
1083 iter_stack_copy_current (stack_p, front_p);
1087 /* Move front pointer up by one. */
1088 front_p->loc = &(*front_p->loc)->header.next;
1090 /* Do not increment the current pointer. */
1091 skip_increment = TRUE;
1101 if (!skip_increment)
1102 iter_stack_next (stack_p);
1104 /* Be careful to update the stack_p if it now is a null. */
1105 iter_stack_update (stack_p);
1108 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
1113 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
1114 lang_wild_statement_type *w)
1116 /* Keep a front pointer and a current pointer. */
1117 lang_statement_union_type **front;
1118 lang_statement_union_type **current;
1120 /* Walk to the end of the targets. */
1121 for (front = &w->children.head;
1122 (*front != NULL) && section_is_source_or_target (deps, *front);
1123 front = &(*front)->header.next)
1129 current = &(*front)->header.next;
1130 while (*current != NULL)
1132 if (section_is_source_or_target (deps, *current))
1134 /* Insert in place. */
1135 xtensa_ld_iter front_iter;
1136 xtensa_ld_iter current_iter;
1138 front_iter.parent = (lang_statement_union_type *) w;
1139 front_iter.l = &w->children;
1140 front_iter.loc = front;
1142 current_iter.parent = (lang_statement_union_type *) w;
1143 current_iter.l = &w->children;
1144 current_iter.loc = current;
1146 ld_xtensa_move_section_after (&front_iter, ¤t_iter);
1147 front = &(*front)->header.next;
1151 current = &(*current)->header.next;
1158 deps_has_sec_edge (const reloc_deps_graph *deps, asection *src, asection *tgt)
1160 const reloc_deps_section *sec_deps;
1161 const reloc_deps_e *sec_deps_e;
1163 sec_deps = xtensa_get_section_deps (deps, src);
1164 if (sec_deps == NULL)
1167 for (sec_deps_e = sec_deps->succs;
1169 sec_deps_e = sec_deps_e->next)
1171 ASSERT (sec_deps_e->src == src);
1172 if (sec_deps_e->tgt == tgt)
1180 deps_has_edge (const reloc_deps_graph *deps,
1181 lang_statement_union_type *src,
1182 lang_statement_union_type *tgt)
1184 if (!section_is_source (deps, src))
1186 if (!section_is_target (deps, tgt))
1189 if (src->header.type != lang_input_section_enum)
1191 if (tgt->header.type != lang_input_section_enum)
1194 return deps_has_sec_edge (deps, src->input_section.section,
1195 tgt->input_section.section);
1200 add_deps_edge (reloc_deps_graph *deps, asection *src_sec, asection *tgt_sec)
1202 reloc_deps_section *src_sec_deps;
1203 reloc_deps_section *tgt_sec_deps;
1205 reloc_deps_e *src_edge;
1206 reloc_deps_e *tgt_edge;
1208 if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1211 src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1212 if (src_sec_deps == NULL)
1214 /* Add a section. */
1215 src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1216 memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1217 src_sec_deps->is_only_literal = 0;
1218 src_sec_deps->preds = NULL;
1219 src_sec_deps->succs = NULL;
1220 xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1221 xtensa_append_section_deps (deps, src_sec);
1224 tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1225 if (tgt_sec_deps == NULL)
1227 /* Add a section. */
1228 tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1229 memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1230 tgt_sec_deps->is_only_literal = 0;
1231 tgt_sec_deps->preds = NULL;
1232 tgt_sec_deps->succs = NULL;
1233 xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1234 xtensa_append_section_deps (deps, tgt_sec);
1237 /* Add the edges. */
1238 src_edge = xmalloc (sizeof (reloc_deps_e));
1239 memset (src_edge, 0, sizeof (reloc_deps_e));
1240 src_edge->src = src_sec;
1241 src_edge->tgt = tgt_sec;
1242 src_edge->next = src_sec_deps->succs;
1243 src_sec_deps->succs = src_edge;
1245 tgt_edge = xmalloc (sizeof (reloc_deps_e));
1246 memset (tgt_edge, 0, sizeof (reloc_deps_e));
1247 tgt_edge->src = src_sec;
1248 tgt_edge->tgt = tgt_sec;
1249 tgt_edge->next = tgt_sec_deps->preds;
1250 tgt_sec_deps->preds = tgt_edge;
1255 build_deps_graph_callback (asection *src_sec,
1256 bfd_vma src_offset ATTRIBUTE_UNUSED,
1257 asection *target_sec,
1258 bfd_vma target_offset ATTRIBUTE_UNUSED,
1261 reloc_deps_graph *deps = closure;
1263 /* If the target is defined. */
1264 if (target_sec != NULL)
1265 add_deps_edge (deps, src_sec, target_sec);
1269 static reloc_deps_graph *
1270 ld_build_required_section_dependence (lang_statement_union_type *s)
1272 reloc_deps_graph *deps;
1273 xtensa_ld_iter_stack *stack = NULL;
1275 deps = xmalloc (sizeof (reloc_deps_graph));
1276 deps->sections = NULL;
1280 for (iter_stack_create (&stack, s);
1281 !iter_stack_empty (&stack);
1282 iter_stack_next (&stack))
1284 lang_statement_union_type *l = iter_stack_current (&stack);
1286 if (l->header.type == lang_input_section_enum)
1288 lang_input_section_type *input;
1289 input = &l->input_section;
1290 xtensa_callback_required_dependence (input->section->owner,
1293 /* Use the same closure. */
1294 build_deps_graph_callback,
1302 #if EXTRA_VALIDATION
1304 ld_count_children (lang_statement_union_type *s)
1307 xtensa_ld_iter_stack *stack = NULL;
1308 for (iter_stack_create (&stack, s);
1309 !iter_stack_empty (&stack);
1310 iter_stack_next (&stack))
1312 lang_statement_union_type *l = iter_stack_current (&stack);
1318 #endif /* EXTRA_VALIDATION */
1321 /* Check if a particular section is included in the link. This will only
1322 be true for one instance of a particular linkonce section. */
1324 static bfd_boolean input_section_found = FALSE;
1325 static asection *input_section_target = NULL;
1328 input_section_linked_worker (lang_statement_union_type *statement)
1330 if ((statement->header.type == lang_input_section_enum
1331 && (statement->input_section.section == input_section_target)))
1332 input_section_found = TRUE;
1336 input_section_linked (asection *sec)
1338 input_section_found = FALSE;
1339 input_section_target = sec;
1340 lang_for_each_statement_worker (input_section_linked_worker, stat_ptr->head);
1341 return input_section_found;
1345 /* Strip out any linkonce property tables or XCC exception tables where the
1346 associated linkonce text is from a different object file. Normally,
1347 a matching set of linkonce sections is taken from the same object file,
1348 but sometimes the files are compiled differently so that some of the
1349 linkonce sections are not present in all files. Stripping the
1350 inconsistent sections like this is not completely robust -- a much
1351 better solution is to use comdat groups. */
1353 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
1356 is_inconsistent_linkonce_section (asection *sec)
1358 bfd *abfd = sec->owner;
1359 const char *sec_name = bfd_get_section_name (abfd, sec);
1362 if ((bfd_get_section_flags (abfd, sec) & SEC_LINK_ONCE) == 0
1363 || strncmp (sec_name, ".gnu.linkonce.", linkonce_len) != 0)
1366 /* Check if this is an Xtensa property section or an exception table
1367 for Tensilica's XCC compiler. */
1368 name = sec_name + linkonce_len;
1369 if (CONST_STRNEQ (name, "prop."))
1370 name = strchr (name + 5, '.') + 1;
1371 else if (name[1] == '.'
1372 && (name[0] == 'p' || name[0] == 'e' || name[0] == 'h'))
1379 char *dep_sec_name = xmalloc (strlen (sec_name) + 1);
1382 /* Get the associated linkonce text section and check if it is
1383 included in the link. If not, this section is inconsistent
1384 and should be stripped. */
1385 strcpy (dep_sec_name, ".gnu.linkonce.t.");
1386 strcat (dep_sec_name, name);
1387 dep_sec = bfd_get_section_by_name (abfd, dep_sec_name);
1388 if (dep_sec == NULL || ! input_section_linked (dep_sec))
1390 free (dep_sec_name);
1393 free (dep_sec_name);
1401 xtensa_strip_inconsistent_linkonce_sections (lang_statement_list_type *slist)
1403 lang_statement_union_type **s_p = &slist->head;
1406 lang_statement_union_type *s = *s_p;
1407 lang_statement_union_type *s_next = (*s_p)->header.next;
1409 switch (s->header.type)
1411 case lang_input_section_enum:
1412 if (is_inconsistent_linkonce_section (s->input_section.section))
1414 s->input_section.section->output_section = bfd_abs_section_ptr;
1420 case lang_constructors_statement_enum:
1421 xtensa_strip_inconsistent_linkonce_sections (&constructor_list);
1424 case lang_output_section_statement_enum:
1425 if (s->output_section_statement.children.head)
1426 xtensa_strip_inconsistent_linkonce_sections
1427 (&s->output_section_statement.children);
1430 case lang_wild_statement_enum:
1431 xtensa_strip_inconsistent_linkonce_sections
1432 (&s->wild_statement.children);
1435 case lang_group_statement_enum:
1436 xtensa_strip_inconsistent_linkonce_sections
1437 (&s->group_statement.children);
1440 case lang_data_statement_enum:
1441 case lang_reloc_statement_enum:
1442 case lang_object_symbols_statement_enum:
1443 case lang_output_statement_enum:
1444 case lang_target_statement_enum:
1445 case lang_input_statement_enum:
1446 case lang_assignment_statement_enum:
1447 case lang_padding_statement_enum:
1448 case lang_address_statement_enum:
1449 case lang_fill_statement_enum:
1457 s_p = &(*s_p)->header.next;
1460 /* Reset the tail of the list, in case the last entry was removed. */
1461 if (s_p != slist->tail)
1467 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1469 lang_wild_statement_type *w;
1470 reloc_deps_graph *deps;
1471 if (statement->header.type == lang_wild_statement_enum)
1473 #if EXTRA_VALIDATION
1474 size_t old_child_count;
1475 size_t new_child_count;
1477 bfd_boolean no_reorder;
1479 w = &statement->wild_statement;
1483 /* If it has 0 or 1 section bound, then do not reorder. */
1484 if (w->children.head == NULL
1485 || (w->children.head->header.type == lang_input_section_enum
1486 && w->children.head->header.next == NULL))
1489 if (w->filenames_sorted)
1492 /* Check for sorting in a section list wildcard spec as well. */
1495 struct wildcard_list *l;
1496 for (l = w->section_list; l != NULL; l = l->next)
1498 if (l->spec.sorted == TRUE)
1506 /* Special case until the NOREORDER linker directive is supported:
1507 *(.init) output sections and *(.fini) specs may NOT be reordered. */
1509 /* Check for sorting in a section list wildcard spec as well. */
1512 struct wildcard_list *l;
1513 for (l = w->section_list; l != NULL; l = l->next)
1516 && ((strcmp (".init", l->spec.name) == 0)
1517 || (strcmp (".fini", l->spec.name) == 0)))
1525 #if EXTRA_VALIDATION
1526 old_child_count = ld_count_children (statement);
1529 /* It is now officially a target. Build the graph of source
1530 section -> target section (kept as a list of edges). */
1531 deps = ld_build_required_section_dependence (statement);
1533 /* If this wildcard does not reorder.... */
1534 if (!no_reorder && deps->count != 0)
1536 /* First check for reverse dependences. Fix if possible. */
1537 xtensa_layout_wild (deps, w);
1539 xtensa_move_dependencies_to_front (deps, w);
1540 #if EXTRA_VALIDATION
1541 new_child_count = ld_count_children (statement);
1542 ASSERT (new_child_count == old_child_count);
1545 xtensa_colocate_literals (deps, statement);
1547 #if EXTRA_VALIDATION
1548 new_child_count = ld_count_children (statement);
1549 ASSERT (new_child_count == old_child_count);
1554 free_reloc_deps_graph (deps);
1560 xtensa_wild_group_interleave (lang_statement_union_type *s)
1562 lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1567 xtensa_layout_wild (const reloc_deps_graph *deps, lang_wild_statement_type *w)
1569 /* If it does not fit initially, we need to do this step. Move all
1570 of the wild literal sections to a new list, then move each of
1571 them back in just before the first section they depend on. */
1572 lang_statement_union_type **s_p;
1573 #if EXTRA_VALIDATION
1574 size_t old_count, new_count;
1578 lang_wild_statement_type literal_wild;
1579 literal_wild.header.next = NULL;
1580 literal_wild.header.type = lang_wild_statement_enum;
1581 literal_wild.filename = NULL;
1582 literal_wild.filenames_sorted = FALSE;
1583 literal_wild.section_list = NULL;
1584 literal_wild.keep_sections = FALSE;
1585 literal_wild.children.head = NULL;
1586 literal_wild.children.tail = &literal_wild.children.head;
1588 #if EXTRA_VALIDATION
1589 old_count = ld_count_children ((lang_statement_union_type*) w);
1592 s_p = &w->children.head;
1593 while (*s_p != NULL)
1595 lang_statement_union_type *l = *s_p;
1596 if (l->header.type == lang_input_section_enum)
1598 if (section_is_target (deps, l)
1599 && ! section_is_source (deps, l))
1602 *s_p = l->header.next;
1604 w->children.tail = s_p;
1605 l->header.next = NULL;
1608 *literal_wild.children.tail = l;
1609 literal_wild.children.tail = &l->header.next;
1613 s_p = &(*s_p)->header.next;
1616 #if EXTRA_VALIDATION
1617 ct1 = ld_count_children ((lang_statement_union_type*) w);
1618 ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1620 ASSERT (old_count == (ct1 + ct2));
1623 /* Now place them back in front of their dependent sections. */
1625 while (literal_wild.children.head != NULL)
1627 lang_statement_union_type *lit = literal_wild.children.head;
1628 bfd_boolean placed = FALSE;
1630 #if EXTRA_VALIDATION
1636 literal_wild.children.head = lit->header.next;
1637 if (literal_wild.children.head == NULL)
1638 literal_wild.children.tail = &literal_wild.children.head;
1639 lit->header.next = NULL;
1641 /* Find a spot to place it. */
1642 for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1644 lang_statement_union_type *src = *s_p;
1645 if (deps_has_edge (deps, src, lit))
1647 /* Place it here. */
1648 lit->header.next = *s_p;
1657 /* Put it at the end. */
1658 *w->children.tail = lit;
1659 w->children.tail = &lit->header.next;
1663 #if EXTRA_VALIDATION
1664 new_count = ld_count_children ((lang_statement_union_type*) w);
1665 ASSERT (new_count == old_count);
1671 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1673 lang_output_section_statement_type *os;
1674 reloc_deps_graph *deps;
1675 if (statement->header.type == lang_output_section_statement_enum)
1677 /* Now, we walk over the contours of the output section statement.
1679 First we build the literal section dependences as before.
1681 At the first uniquely_literal section, we mark it as a good
1682 spot to place other literals. Continue walking (and counting
1683 sizes) until we find the next literal section. If this
1684 section can be moved to the first one, then we move it. If
1685 we every find a modification of ".", start over. If we find
1686 a labeling of the current location, start over. Finally, at
1687 the end, if we require page alignment, add page alignments. */
1689 #if EXTRA_VALIDATION
1690 size_t old_child_count;
1691 size_t new_child_count;
1693 bfd_boolean no_reorder = FALSE;
1695 os = &statement->output_section_statement;
1697 #if EXTRA_VALIDATION
1698 old_child_count = ld_count_children (statement);
1701 /* It is now officially a target. Build the graph of source
1702 section -> target section (kept as a list of edges). */
1704 deps = ld_build_required_section_dependence (statement);
1706 /* If this wildcard does not reorder.... */
1709 /* First check for reverse dependences. Fix if possible. */
1710 xtensa_colocate_literals (deps, statement);
1712 #if EXTRA_VALIDATION
1713 new_child_count = ld_count_children (statement);
1714 ASSERT (new_child_count == old_child_count);
1718 /* Insert align/offset assignment statement. */
1719 if (xtensa_use_literal_pages)
1721 ld_xtensa_insert_page_offsets (0, statement, deps,
1722 xtensa_use_literal_pages);
1723 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1728 free_reloc_deps_graph (deps);
1734 xtensa_colocate_output_literals (lang_statement_union_type *s)
1736 lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1741 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1743 switch (statement->header.type)
1745 case lang_input_section_enum:
1747 asection *bfd_section = statement->input_section.section;
1748 bfd_section->output_offset = 0;
1758 ld_assign_relative_paged_dot (bfd_vma dot,
1759 lang_statement_union_type *s,
1760 const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1761 bfd_boolean lit_align)
1763 /* Walk through all of the input statements in this wild statement
1764 assign dot to all of them. */
1766 xtensa_ld_iter_stack *stack = NULL;
1767 xtensa_ld_iter_stack **stack_p = &stack;
1769 bfd_boolean first_section = FALSE;
1770 bfd_boolean in_literals = FALSE;
1772 for (iter_stack_create (stack_p, s);
1773 !iter_stack_empty (stack_p);
1774 iter_stack_next (stack_p))
1776 lang_statement_union_type *l = iter_stack_current (stack_p);
1778 switch (l->header.type)
1780 case lang_input_section_enum:
1782 asection *section = l->input_section.section;
1783 size_t align_pow = section->alignment_power;
1784 bfd_boolean do_xtensa_alignment = FALSE;
1788 bfd_boolean sec_is_target = section_is_target (deps, l);
1789 bfd_boolean sec_is_source = section_is_source (deps, l);
1791 if (section->size != 0
1793 || (in_literals && !sec_is_target)
1794 || (!in_literals && sec_is_target)))
1796 do_xtensa_alignment = TRUE;
1798 first_section = FALSE;
1799 if (section->size != 0)
1800 in_literals = (sec_is_target && !sec_is_source);
1803 if (do_xtensa_alignment && xtensa_page_power != 0)
1804 dot += (1 << xtensa_page_power);
1806 dot = align_power (dot, align_pow);
1807 section->output_offset = dot;
1808 dot += section->size;
1811 case lang_fill_statement_enum:
1812 dot += l->fill_statement.size;
1814 case lang_padding_statement_enum:
1815 dot += l->padding_statement.size;
1826 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1827 const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1829 /* Walk over all of the dependencies that we identified and make
1830 sure that IF the source and target are here (addr != 0):
1831 1) target addr < source addr
1832 2) (roundup(source + source_size, 4) - rounddown(target, 4))
1833 < (256K - (1 << bad align))
1834 Need a worst-case proof.... */
1836 xtensa_ld_iter_stack *stack = NULL;
1837 xtensa_ld_iter_stack **stack_p = &stack;
1838 size_t max_align_power = 0;
1839 size_t align_penalty = 256;
1843 /* Find the worst-case alignment requirement for this set of statements. */
1844 for (iter_stack_create (stack_p, statement);
1845 !iter_stack_empty (stack_p);
1846 iter_stack_next (stack_p))
1848 lang_statement_union_type *l = iter_stack_current (stack_p);
1849 if (l->header.type == lang_input_section_enum)
1851 lang_input_section_type *input = &l->input_section;
1852 asection *section = input->section;
1853 if (section->alignment_power > max_align_power)
1854 max_align_power = section->alignment_power;
1858 /* Now check that everything fits. */
1859 for (i = 0; i < deps->count; i++)
1861 asection *sec = deps->sections[i];
1862 const reloc_deps_section *deps_section =
1863 xtensa_get_section_deps (deps, sec);
1866 /* We choose to walk through the successors. */
1867 for (e = deps_section->succs; e != NULL; e = e->next)
1869 if (e->src != e->tgt
1870 && e->src->output_section == e->tgt->output_section
1871 && e->src->output_offset != 0
1872 && e->tgt->output_offset != 0)
1875 align_power (e->src->output_offset + e->src->size, 2);
1876 bfd_vma target_addr = e->tgt->output_offset & ~3;
1877 if (l32r_addr < target_addr)
1879 fprintf (stderr, "Warning: "
1880 "l32r target section before l32r\n");
1884 if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1896 ld_xtensa_insert_page_offsets (bfd_vma dot,
1897 lang_statement_union_type *s,
1898 reloc_deps_graph *deps,
1899 bfd_boolean lit_align)
1901 xtensa_ld_iter_stack *stack = NULL;
1902 xtensa_ld_iter_stack **stack_p = &stack;
1904 bfd_boolean first_section = FALSE;
1905 bfd_boolean in_literals = FALSE;
1910 for (iter_stack_create (stack_p, s);
1911 !iter_stack_empty (stack_p);
1912 iter_stack_next (stack_p))
1914 lang_statement_union_type *l = iter_stack_current (stack_p);
1916 switch (l->header.type)
1918 case lang_input_section_enum:
1920 asection *section = l->input_section.section;
1921 bfd_boolean do_xtensa_alignment = FALSE;
1925 if (section->size != 0
1927 || (in_literals && !section_is_target (deps, l))
1928 || (!in_literals && section_is_target (deps, l))))
1930 do_xtensa_alignment = TRUE;
1932 first_section = FALSE;
1933 if (section->size != 0)
1935 in_literals = (section_is_target (deps, l)
1936 && !section_is_source (deps, l));
1940 if (do_xtensa_alignment && xtensa_page_power != 0)
1942 /* Create an expression that increments the current address,
1943 i.e., "dot", by (1 << xtensa_align_power). */
1944 etree_type *name_op = exp_nameop (NAME, ".");
1945 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1946 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1947 etree_type *assign_op = exp_assop ('=', ".", add_op);
1949 lang_assignment_statement_type *assign_stmt;
1950 lang_statement_union_type *assign_union;
1951 lang_statement_list_type tmplist;
1952 lang_statement_list_type *old_stat_ptr = stat_ptr;
1954 /* There is hidden state in "lang_add_assignment". It
1955 appends the new assignment statement to the stat_ptr
1956 list. Thus, we swap it before and after the call. */
1958 tmplist.head = NULL;
1959 tmplist.tail = &tmplist.head;
1961 stat_ptr = &tmplist;
1962 /* Warning: side effect; statement appended to stat_ptr. */
1963 assign_stmt = lang_add_assignment (assign_op);
1964 assign_union = (lang_statement_union_type *) assign_stmt;
1965 stat_ptr = old_stat_ptr;
1967 assign_union->header.next = l;
1968 *(*stack_p)->iterloc.loc = assign_union;
1969 iter_stack_next (stack_p);
1982 # Define some shell vars to insert bits of code into the standard ELF
1983 # parse_args and list_options functions.
1985 PARSE_AND_LIST_PROLOGUE='
1986 #define OPTION_OPT_SIZEOPT (300)
1987 #define OPTION_NO_RELAX (OPTION_OPT_SIZEOPT + 1)
1988 #define OPTION_LITERAL_MOVEMENT (OPTION_NO_RELAX + 1)
1989 #define OPTION_NO_LITERAL_MOVEMENT (OPTION_LITERAL_MOVEMENT + 1)
1990 extern int elf32xtensa_size_opt;
1991 extern int elf32xtensa_no_literal_movement;
1994 PARSE_AND_LIST_LONGOPTS='
1995 { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1996 { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1997 { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1998 { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
2001 PARSE_AND_LIST_OPTIONS='
2002 fprintf (file, _(" --size-opt\t\tWhen relaxing longcalls, prefer size optimization\n\t\t\t over branch target alignment\n"));
2003 fprintf (file, _(" --no-relax\t\tDo not relax branches or coalesce literals\n"));
2006 PARSE_AND_LIST_ARGS_CASES='
2007 case OPTION_OPT_SIZEOPT:
2008 elf32xtensa_size_opt = 1;
2010 case OPTION_NO_RELAX:
2011 disable_relaxation = TRUE;
2013 case OPTION_LITERAL_MOVEMENT:
2014 elf32xtensa_no_literal_movement = 0;
2016 case OPTION_NO_LITERAL_MOVEMENT:
2017 elf32xtensa_no_literal_movement = 1;
2021 # Replace some of the standard ELF functions with our own versions.
2023 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
2024 LDEMUL_AFTER_OPEN=elf_xtensa_after_open
2025 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
2026 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation