1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
438 && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
440 einfo (_("%F%P: little endian output does not match "
441 "Xtensa configuration\n"));
444 && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
446 einfo (_("%F%P: big endian output does not match "
447 "Xtensa configuration\n"));
450 /* Keep track of the first input .xtensa.info section, and as a fallback,
451 the first input bfd where a .xtensa.info section could be created.
452 After the input .xtensa.info has been checked, the contents of the
453 first one will be replaced with the output .xtensa.info table. */
457 LANG_FOR_EACH_INPUT_STATEMENT (f)
459 /* Check that the endianness for each input file matches the output.
460 The merge_private_bfd_data hook has already reported any mismatches
461 as errors, but those errors are not fatal. At this point, we
462 cannot go any further if there are any mismatches. */
463 if ((is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
464 || (!is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
465 einfo (_("%F%P: cross-endian linking for %B not supported\n"),
469 first_bfd = f->the_bfd;
471 info_sec = bfd_get_section_by_name (f->the_bfd, ".xtensa.info");
475 if (! first_info_sec)
476 first_info_sec = info_sec;
478 /* Unpack the .xtensa.info section and check it against the current
479 Xtensa configuration. */
480 check_xtensa_info (f->the_bfd, info_sec);
482 /* Do not include this copy of .xtensa.info in the output. */
484 info_sec->flags |= SEC_EXCLUDE;
487 /* Reuse the first .xtensa.info input section to hold the output
488 .xtensa.info; or, if none were found, create a new section in the
489 first input bfd (assuming there is one). */
490 info_sec = first_info_sec;
491 if (! info_sec && first_bfd)
493 info_sec = bfd_make_section_with_flags (first_bfd, ".xtensa.info",
494 SEC_HAS_CONTENTS | SEC_READONLY);
496 einfo (_("%F%P: failed to create .xtensa.info section\n"));
500 int xtensa_info_size;
503 info_sec->flags &= ~SEC_EXCLUDE;
504 info_sec->flags |= SEC_IN_MEMORY;
506 data = xmalloc (100);
507 sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
508 XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
509 xtensa_info_size = strlen (data) + 1;
511 /* Add enough null terminators to pad to a word boundary. */
513 data[xtensa_info_size++] = 0;
514 while ((xtensa_info_size & 3) != 0);
516 info_sec->size = 12 + XTINFO_NAMESZ + xtensa_info_size;
517 info_sec->contents = xmalloc (info_sec->size);
518 bfd_put_32 (info_sec->owner, XTINFO_NAMESZ, info_sec->contents + 0);
519 bfd_put_32 (info_sec->owner, xtensa_info_size, info_sec->contents + 4);
520 bfd_put_32 (info_sec->owner, XTINFO_TYPE, info_sec->contents + 8);
521 memcpy (info_sec->contents + 12, XTINFO_NAME, XTINFO_NAMESZ);
522 memcpy (info_sec->contents + 12 + XTINFO_NAMESZ, data, xtensa_info_size);
526 /* Enable relaxation by default if the "--no-relax" option was not
527 specified. This is done here instead of in the before_parse hook
528 because there is a check in main() to prohibit use of --relax and
529 -r together and that combination should be allowed for Xtensa. */
531 if (!disable_relaxation)
532 command_line.relax = TRUE;
534 xtensa_strip_inconsistent_linkonce_sections (stat_ptr);
536 gld${EMULATION_NAME}_before_allocation ();
538 xtensa_wild_group_interleave (stat_ptr->head);
539 if (command_line.relax)
540 xtensa_colocate_output_literals (stat_ptr->head);
542 /* TBD: We need to force the page alignments to here and only do
543 them as needed for the entire output section. Finally, if this
544 is a relocatable link then we need to add alignment notes so
545 that the literals can be separated later. */
549 typedef struct wildcard_list section_name_list;
551 typedef struct reloc_deps_e_t reloc_deps_e;
552 typedef struct reloc_deps_section_t reloc_deps_section;
553 typedef struct reloc_deps_graph_t reloc_deps_graph;
556 struct reloc_deps_e_t
558 asection *src; /* Contains l32rs. */
559 asection *tgt; /* Contains literals. */
563 /* Place these in the userdata field. */
564 struct reloc_deps_section_t
568 bfd_boolean is_only_literal;
572 struct reloc_deps_graph_t
579 static void xtensa_layout_wild
580 (const reloc_deps_graph *, lang_wild_statement_type *);
582 typedef void (*deps_callback_t) (asection *, /* src_sec */
583 bfd_vma, /* src_offset */
584 asection *, /* target_sec */
585 bfd_vma, /* target_offset */
586 void *); /* closure */
588 extern bfd_boolean xtensa_callback_required_dependence
589 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
590 static void xtensa_ldlang_clear_addresses (lang_statement_union_type *);
591 static bfd_boolean ld_local_file_relocations_fit
592 (lang_statement_union_type *, const reloc_deps_graph *);
593 static bfd_vma ld_assign_relative_paged_dot
594 (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
596 static bfd_vma ld_xtensa_insert_page_offsets
597 (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
599 static size_t ld_count_children (lang_statement_union_type *);
602 extern lang_statement_list_type constructor_list;
604 /* Begin verbatim code from ldlang.c:
605 the following are copied from ldlang.c because they are defined
609 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
610 lang_statement_union_type *s)
612 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
616 switch (s->header.type)
618 case lang_constructors_statement_enum:
619 lang_for_each_statement_worker (func, constructor_list.head);
621 case lang_output_section_statement_enum:
622 lang_for_each_statement_worker
624 s->output_section_statement.children.head);
626 case lang_wild_statement_enum:
627 lang_for_each_statement_worker
629 s->wild_statement.children.head);
631 case lang_group_statement_enum:
632 lang_for_each_statement_worker (func,
633 s->group_statement.children.head);
635 case lang_data_statement_enum:
636 case lang_reloc_statement_enum:
637 case lang_object_symbols_statement_enum:
638 case lang_output_statement_enum:
639 case lang_target_statement_enum:
640 case lang_input_section_enum:
641 case lang_input_statement_enum:
642 case lang_assignment_statement_enum:
643 case lang_padding_statement_enum:
644 case lang_address_statement_enum:
645 case lang_fill_statement_enum:
654 /* End of verbatim code from ldlang.c. */
657 static reloc_deps_section *
658 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
661 /* We have a separate function for this so that
662 we could in the future keep a completely independent
663 structure that maps a section to its dependence edges.
664 For now, we place these in the sec->userdata field. */
665 reloc_deps_section *sec_deps = sec->userdata;
670 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
672 reloc_deps_section *deps_section)
674 sec->userdata = deps_section;
678 /* This is used to keep a list of all of the sections participating in
679 the graph so we can clean them up quickly. */
682 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
684 if (deps->size <= deps->count)
686 asection **new_sections;
690 new_size = deps->size * 2;
694 new_sections = xmalloc (sizeof (asection *) * new_size);
695 memset (new_sections, 0, sizeof (asection *) * new_size);
696 for (i = 0; i < deps->count; i++)
698 new_sections[i] = deps->sections[i];
700 if (deps->sections != NULL)
701 free (deps->sections);
702 deps->sections = new_sections;
703 deps->size = new_size;
705 deps->sections[deps->count] = sec;
711 free_reloc_deps_graph (reloc_deps_graph *deps)
714 for (i = 0; i < deps->count; i++)
716 asection *sec = deps->sections[i];
717 reloc_deps_section *sec_deps;
718 sec_deps = xtensa_get_section_deps (deps, sec);
722 while (sec_deps->succs != NULL)
724 next = sec_deps->succs->next;
725 free (sec_deps->succs);
726 sec_deps->succs = next;
729 while (sec_deps->preds != NULL)
731 next = sec_deps->preds->next;
732 free (sec_deps->preds);
733 sec_deps->preds = next;
737 xtensa_set_section_deps (deps, sec, NULL);
740 free (deps->sections);
747 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
748 lang_statement_union_type *s)
751 const reloc_deps_section *sec_deps;
753 if (s->header.type != lang_input_section_enum)
755 sec = s->input_section.section;
757 sec_deps = xtensa_get_section_deps (deps, sec);
758 return sec_deps && sec_deps->succs != NULL;
763 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
764 lang_statement_union_type *s)
767 const reloc_deps_section *sec_deps;
769 if (s->header.type != lang_input_section_enum)
771 sec = s->input_section.section;
773 sec_deps = xtensa_get_section_deps (deps, sec);
774 return sec_deps && sec_deps->preds != NULL;
779 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
780 lang_statement_union_type *s)
782 return (section_is_source (deps, s)
783 || section_is_target (deps, s));
787 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
788 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
790 struct xtensa_ld_iter_t
792 lang_statement_union_type *parent; /* Parent of the list. */
793 lang_statement_list_type *l; /* List that holds it. */
794 lang_statement_union_type **loc; /* Place in the list. */
797 struct xtensa_ld_iter_stack_t
799 xtensa_ld_iter iterloc; /* List that hold it. */
801 xtensa_ld_iter_stack *next; /* Next in the stack. */
802 xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
807 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
809 lang_statement_union_type *to_next;
810 lang_statement_union_type *current_next;
811 lang_statement_union_type **e;
814 size_t old_to_count, new_to_count;
815 size_t old_current_count, new_current_count;
822 old_to_count = ld_count_children (to->parent);
823 old_current_count = ld_count_children (current->parent);
826 to_next = *(to->loc);
827 current_next = (*current->loc)->header.next;
829 *(to->loc) = *(current->loc);
831 *(current->loc) = current_next;
832 (*(to->loc))->header.next = to_next;
834 /* reset "to" list tail */
835 for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
839 /* reset "current" list tail */
840 for (e = ¤t->l->head; *e != NULL; e = &(*e)->header.next)
842 current->l->tail = e;
845 new_to_count = ld_count_children (to->parent);
846 new_current_count = ld_count_children (current->parent);
848 ASSERT ((old_to_count + old_current_count)
849 == (new_to_count + new_current_count));
854 /* Can only be called with lang_statements that have lists. Returns
855 FALSE if the list is empty. */
858 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
860 return *stack_p == NULL;
865 iter_stack_push (xtensa_ld_iter_stack **stack_p,
866 lang_statement_union_type *parent)
868 xtensa_ld_iter_stack *stack;
869 lang_statement_list_type *l = NULL;
871 switch (parent->header.type)
873 case lang_output_section_statement_enum:
874 l = &parent->output_section_statement.children;
876 case lang_wild_statement_enum:
877 l = &parent->wild_statement.children;
879 case lang_group_statement_enum:
880 l = &parent->group_statement.children;
887 /* Empty. do not push. */
888 if (l->tail == &l->head)
891 stack = xmalloc (sizeof (xtensa_ld_iter_stack));
892 memset (stack, 0, sizeof (xtensa_ld_iter_stack));
893 stack->iterloc.parent = parent;
894 stack->iterloc.l = l;
895 stack->iterloc.loc = &l->head;
897 stack->next = *stack_p;
899 if (*stack_p != NULL)
900 (*stack_p)->prev = stack;
907 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
909 xtensa_ld_iter_stack *stack;
915 ASSERT (stack != NULL);
919 if (stack->next != NULL)
920 stack->next->prev = NULL;
922 *stack_p = stack->next;
927 /* This MUST be called if, during iteration, the user changes the
928 underlying structure. It will check for a NULL current and advance
932 iter_stack_update (xtensa_ld_iter_stack **stack_p)
934 if (!iter_stack_empty (stack_p)
935 && (*(*stack_p)->iterloc.loc) == NULL)
937 iter_stack_pop (stack_p);
939 while (!iter_stack_empty (stack_p)
940 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
942 iter_stack_pop (stack_p);
944 if (!iter_stack_empty (stack_p))
945 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
951 iter_stack_next (xtensa_ld_iter_stack **stack_p)
953 xtensa_ld_iter_stack *stack;
954 lang_statement_union_type *current;
957 current = *stack->iterloc.loc;
958 /* If we are on the first element. */
961 switch (current->header.type)
963 case lang_output_section_statement_enum:
964 case lang_wild_statement_enum:
965 case lang_group_statement_enum:
966 /* If the list if not empty, we are done. */
967 if (iter_stack_push (stack_p, *stack->iterloc.loc))
969 /* Otherwise increment the pointer as normal. */
976 while (!iter_stack_empty (stack_p)
977 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
979 iter_stack_pop (stack_p);
981 if (!iter_stack_empty (stack_p))
982 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
986 static lang_statement_union_type *
987 iter_stack_current (xtensa_ld_iter_stack **stack_p)
989 return *((*stack_p)->iterloc.loc);
993 /* The iter stack is a preorder. */
996 iter_stack_create (xtensa_ld_iter_stack **stack_p,
997 lang_statement_union_type *parent)
999 iter_stack_push (stack_p, parent);
1004 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p, xtensa_ld_iter *front)
1006 *front = (*stack_p)->iterloc;
1011 xtensa_colocate_literals (reloc_deps_graph *deps,
1012 lang_statement_union_type *statement)
1014 /* Keep a stack of pointers to control iteration through the contours. */
1015 xtensa_ld_iter_stack *stack = NULL;
1016 xtensa_ld_iter_stack **stack_p = &stack;
1018 xtensa_ld_iter front; /* Location where new insertion should occur. */
1019 xtensa_ld_iter *front_p = NULL;
1021 xtensa_ld_iter current; /* Location we are checking. */
1022 xtensa_ld_iter *current_p = NULL;
1023 bfd_boolean in_literals = FALSE;
1025 if (deps->count == 0)
1028 iter_stack_create (stack_p, statement);
1030 while (!iter_stack_empty (stack_p))
1032 bfd_boolean skip_increment = FALSE;
1033 lang_statement_union_type *l = iter_stack_current (stack_p);
1035 switch (l->header.type)
1037 case lang_assignment_statement_enum:
1038 /* Any assignment statement should block reordering across it. */
1040 in_literals = FALSE;
1043 case lang_input_section_enum:
1044 if (front_p == NULL)
1046 in_literals = (section_is_target (deps, l)
1047 && !section_is_source (deps, l));
1051 iter_stack_copy_current (stack_p, front_p);
1056 bfd_boolean is_target;
1057 current_p = ¤t;
1058 iter_stack_copy_current (stack_p, current_p);
1059 is_target = (section_is_target (deps, l)
1060 && !section_is_source (deps, l));
1064 iter_stack_copy_current (stack_p, front_p);
1066 in_literals = FALSE;
1072 /* Try to insert in place. */
1073 ld_xtensa_move_section_after (front_p, current_p);
1074 ld_assign_relative_paged_dot (0x100000,
1077 xtensa_use_literal_pages);
1079 /* We use this code because it's already written. */
1080 if (!ld_local_file_relocations_fit (statement, deps))
1083 ld_xtensa_move_section_after (current_p, front_p);
1084 /* Reset the literal placement. */
1085 iter_stack_copy_current (stack_p, front_p);
1089 /* Move front pointer up by one. */
1090 front_p->loc = &(*front_p->loc)->header.next;
1092 /* Do not increment the current pointer. */
1093 skip_increment = TRUE;
1103 if (!skip_increment)
1104 iter_stack_next (stack_p);
1106 /* Be careful to update the stack_p if it now is a null. */
1107 iter_stack_update (stack_p);
1110 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
1115 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
1116 lang_wild_statement_type *w)
1118 /* Keep a front pointer and a current pointer. */
1119 lang_statement_union_type **front;
1120 lang_statement_union_type **current;
1122 /* Walk to the end of the targets. */
1123 for (front = &w->children.head;
1124 (*front != NULL) && section_is_source_or_target (deps, *front);
1125 front = &(*front)->header.next)
1131 current = &(*front)->header.next;
1132 while (*current != NULL)
1134 if (section_is_source_or_target (deps, *current))
1136 /* Insert in place. */
1137 xtensa_ld_iter front_iter;
1138 xtensa_ld_iter current_iter;
1140 front_iter.parent = (lang_statement_union_type *) w;
1141 front_iter.l = &w->children;
1142 front_iter.loc = front;
1144 current_iter.parent = (lang_statement_union_type *) w;
1145 current_iter.l = &w->children;
1146 current_iter.loc = current;
1148 ld_xtensa_move_section_after (&front_iter, ¤t_iter);
1149 front = &(*front)->header.next;
1153 current = &(*current)->header.next;
1160 deps_has_sec_edge (const reloc_deps_graph *deps, asection *src, asection *tgt)
1162 const reloc_deps_section *sec_deps;
1163 const reloc_deps_e *sec_deps_e;
1165 sec_deps = xtensa_get_section_deps (deps, src);
1166 if (sec_deps == NULL)
1169 for (sec_deps_e = sec_deps->succs;
1171 sec_deps_e = sec_deps_e->next)
1173 ASSERT (sec_deps_e->src == src);
1174 if (sec_deps_e->tgt == tgt)
1182 deps_has_edge (const reloc_deps_graph *deps,
1183 lang_statement_union_type *src,
1184 lang_statement_union_type *tgt)
1186 if (!section_is_source (deps, src))
1188 if (!section_is_target (deps, tgt))
1191 if (src->header.type != lang_input_section_enum)
1193 if (tgt->header.type != lang_input_section_enum)
1196 return deps_has_sec_edge (deps, src->input_section.section,
1197 tgt->input_section.section);
1202 add_deps_edge (reloc_deps_graph *deps, asection *src_sec, asection *tgt_sec)
1204 reloc_deps_section *src_sec_deps;
1205 reloc_deps_section *tgt_sec_deps;
1207 reloc_deps_e *src_edge;
1208 reloc_deps_e *tgt_edge;
1210 if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1213 src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1214 if (src_sec_deps == NULL)
1216 /* Add a section. */
1217 src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1218 memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1219 src_sec_deps->is_only_literal = 0;
1220 src_sec_deps->preds = NULL;
1221 src_sec_deps->succs = NULL;
1222 xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1223 xtensa_append_section_deps (deps, src_sec);
1226 tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1227 if (tgt_sec_deps == NULL)
1229 /* Add a section. */
1230 tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1231 memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1232 tgt_sec_deps->is_only_literal = 0;
1233 tgt_sec_deps->preds = NULL;
1234 tgt_sec_deps->succs = NULL;
1235 xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1236 xtensa_append_section_deps (deps, tgt_sec);
1239 /* Add the edges. */
1240 src_edge = xmalloc (sizeof (reloc_deps_e));
1241 memset (src_edge, 0, sizeof (reloc_deps_e));
1242 src_edge->src = src_sec;
1243 src_edge->tgt = tgt_sec;
1244 src_edge->next = src_sec_deps->succs;
1245 src_sec_deps->succs = src_edge;
1247 tgt_edge = xmalloc (sizeof (reloc_deps_e));
1248 memset (tgt_edge, 0, sizeof (reloc_deps_e));
1249 tgt_edge->src = src_sec;
1250 tgt_edge->tgt = tgt_sec;
1251 tgt_edge->next = tgt_sec_deps->preds;
1252 tgt_sec_deps->preds = tgt_edge;
1257 build_deps_graph_callback (asection *src_sec,
1258 bfd_vma src_offset ATTRIBUTE_UNUSED,
1259 asection *target_sec,
1260 bfd_vma target_offset ATTRIBUTE_UNUSED,
1263 reloc_deps_graph *deps = closure;
1265 /* If the target is defined. */
1266 if (target_sec != NULL)
1267 add_deps_edge (deps, src_sec, target_sec);
1271 static reloc_deps_graph *
1272 ld_build_required_section_dependence (lang_statement_union_type *s)
1274 reloc_deps_graph *deps;
1275 xtensa_ld_iter_stack *stack = NULL;
1277 deps = xmalloc (sizeof (reloc_deps_graph));
1278 deps->sections = NULL;
1282 for (iter_stack_create (&stack, s);
1283 !iter_stack_empty (&stack);
1284 iter_stack_next (&stack))
1286 lang_statement_union_type *l = iter_stack_current (&stack);
1288 if (l->header.type == lang_input_section_enum)
1290 lang_input_section_type *input;
1291 input = &l->input_section;
1292 xtensa_callback_required_dependence (input->section->owner,
1295 /* Use the same closure. */
1296 build_deps_graph_callback,
1304 #if EXTRA_VALIDATION
1306 ld_count_children (lang_statement_union_type *s)
1309 xtensa_ld_iter_stack *stack = NULL;
1310 for (iter_stack_create (&stack, s);
1311 !iter_stack_empty (&stack);
1312 iter_stack_next (&stack))
1314 lang_statement_union_type *l = iter_stack_current (&stack);
1320 #endif /* EXTRA_VALIDATION */
1323 /* Check if a particular section is included in the link. This will only
1324 be true for one instance of a particular linkonce section. */
1326 static bfd_boolean input_section_found = FALSE;
1327 static asection *input_section_target = NULL;
1330 input_section_linked_worker (lang_statement_union_type *statement)
1332 if ((statement->header.type == lang_input_section_enum
1333 && (statement->input_section.section == input_section_target)))
1334 input_section_found = TRUE;
1338 input_section_linked (asection *sec)
1340 input_section_found = FALSE;
1341 input_section_target = sec;
1342 lang_for_each_statement_worker (input_section_linked_worker, stat_ptr->head);
1343 return input_section_found;
1347 /* Strip out any linkonce property tables or XCC exception tables where the
1348 associated linkonce text is from a different object file. Normally,
1349 a matching set of linkonce sections is taken from the same object file,
1350 but sometimes the files are compiled differently so that some of the
1351 linkonce sections are not present in all files. Stripping the
1352 inconsistent sections like this is not completely robust -- a much
1353 better solution is to use comdat groups. */
1355 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
1358 is_inconsistent_linkonce_section (asection *sec)
1360 bfd *abfd = sec->owner;
1361 const char *sec_name = bfd_get_section_name (abfd, sec);
1364 if ((bfd_get_section_flags (abfd, sec) & SEC_LINK_ONCE) == 0
1365 || strncmp (sec_name, ".gnu.linkonce.", linkonce_len) != 0)
1368 /* Check if this is an Xtensa property section or an exception table
1369 for Tensilica's XCC compiler. */
1370 name = sec_name + linkonce_len;
1371 if (CONST_STRNEQ (name, "prop."))
1372 name = strchr (name + 5, '.') + 1;
1373 else if (name[1] == '.'
1374 && (name[0] == 'p' || name[0] == 'e' || name[0] == 'h'))
1381 char *dep_sec_name = xmalloc (strlen (sec_name) + 1);
1384 /* Get the associated linkonce text section and check if it is
1385 included in the link. If not, this section is inconsistent
1386 and should be stripped. */
1387 strcpy (dep_sec_name, ".gnu.linkonce.t.");
1388 strcat (dep_sec_name, name);
1389 dep_sec = bfd_get_section_by_name (abfd, dep_sec_name);
1390 if (dep_sec == NULL || ! input_section_linked (dep_sec))
1392 free (dep_sec_name);
1395 free (dep_sec_name);
1403 xtensa_strip_inconsistent_linkonce_sections (lang_statement_list_type *slist)
1405 lang_statement_union_type **s_p = &slist->head;
1408 lang_statement_union_type *s = *s_p;
1409 lang_statement_union_type *s_next = (*s_p)->header.next;
1411 switch (s->header.type)
1413 case lang_input_section_enum:
1414 if (is_inconsistent_linkonce_section (s->input_section.section))
1416 s->input_section.section->output_section = bfd_abs_section_ptr;
1422 case lang_constructors_statement_enum:
1423 xtensa_strip_inconsistent_linkonce_sections (&constructor_list);
1426 case lang_output_section_statement_enum:
1427 if (s->output_section_statement.children.head)
1428 xtensa_strip_inconsistent_linkonce_sections
1429 (&s->output_section_statement.children);
1432 case lang_wild_statement_enum:
1433 xtensa_strip_inconsistent_linkonce_sections
1434 (&s->wild_statement.children);
1437 case lang_group_statement_enum:
1438 xtensa_strip_inconsistent_linkonce_sections
1439 (&s->group_statement.children);
1442 case lang_data_statement_enum:
1443 case lang_reloc_statement_enum:
1444 case lang_object_symbols_statement_enum:
1445 case lang_output_statement_enum:
1446 case lang_target_statement_enum:
1447 case lang_input_statement_enum:
1448 case lang_assignment_statement_enum:
1449 case lang_padding_statement_enum:
1450 case lang_address_statement_enum:
1451 case lang_fill_statement_enum:
1459 s_p = &(*s_p)->header.next;
1462 /* Reset the tail of the list, in case the last entry was removed. */
1463 if (s_p != slist->tail)
1469 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1471 lang_wild_statement_type *w;
1472 reloc_deps_graph *deps;
1473 if (statement->header.type == lang_wild_statement_enum)
1475 #if EXTRA_VALIDATION
1476 size_t old_child_count;
1477 size_t new_child_count;
1479 bfd_boolean no_reorder;
1481 w = &statement->wild_statement;
1485 /* If it has 0 or 1 section bound, then do not reorder. */
1486 if (w->children.head == NULL
1487 || (w->children.head->header.type == lang_input_section_enum
1488 && w->children.head->header.next == NULL))
1491 if (w->filenames_sorted)
1494 /* Check for sorting in a section list wildcard spec as well. */
1497 struct wildcard_list *l;
1498 for (l = w->section_list; l != NULL; l = l->next)
1500 if (l->spec.sorted == TRUE)
1508 /* Special case until the NOREORDER linker directive is supported:
1509 *(.init) output sections and *(.fini) specs may NOT be reordered. */
1511 /* Check for sorting in a section list wildcard spec as well. */
1514 struct wildcard_list *l;
1515 for (l = w->section_list; l != NULL; l = l->next)
1518 && ((strcmp (".init", l->spec.name) == 0)
1519 || (strcmp (".fini", l->spec.name) == 0)))
1527 #if EXTRA_VALIDATION
1528 old_child_count = ld_count_children (statement);
1531 /* It is now officially a target. Build the graph of source
1532 section -> target section (kept as a list of edges). */
1533 deps = ld_build_required_section_dependence (statement);
1535 /* If this wildcard does not reorder.... */
1536 if (!no_reorder && deps->count != 0)
1538 /* First check for reverse dependences. Fix if possible. */
1539 xtensa_layout_wild (deps, w);
1541 xtensa_move_dependencies_to_front (deps, w);
1542 #if EXTRA_VALIDATION
1543 new_child_count = ld_count_children (statement);
1544 ASSERT (new_child_count == old_child_count);
1547 xtensa_colocate_literals (deps, statement);
1549 #if EXTRA_VALIDATION
1550 new_child_count = ld_count_children (statement);
1551 ASSERT (new_child_count == old_child_count);
1556 free_reloc_deps_graph (deps);
1562 xtensa_wild_group_interleave (lang_statement_union_type *s)
1564 lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1569 xtensa_layout_wild (const reloc_deps_graph *deps, lang_wild_statement_type *w)
1571 /* If it does not fit initially, we need to do this step. Move all
1572 of the wild literal sections to a new list, then move each of
1573 them back in just before the first section they depend on. */
1574 lang_statement_union_type **s_p;
1575 #if EXTRA_VALIDATION
1576 size_t old_count, new_count;
1580 lang_wild_statement_type literal_wild;
1581 literal_wild.header.next = NULL;
1582 literal_wild.header.type = lang_wild_statement_enum;
1583 literal_wild.filename = NULL;
1584 literal_wild.filenames_sorted = FALSE;
1585 literal_wild.section_list = NULL;
1586 literal_wild.keep_sections = FALSE;
1587 literal_wild.children.head = NULL;
1588 literal_wild.children.tail = &literal_wild.children.head;
1590 #if EXTRA_VALIDATION
1591 old_count = ld_count_children ((lang_statement_union_type*) w);
1594 s_p = &w->children.head;
1595 while (*s_p != NULL)
1597 lang_statement_union_type *l = *s_p;
1598 if (l->header.type == lang_input_section_enum)
1600 if (section_is_target (deps, l)
1601 && ! section_is_source (deps, l))
1604 *s_p = l->header.next;
1606 w->children.tail = s_p;
1607 l->header.next = NULL;
1610 *literal_wild.children.tail = l;
1611 literal_wild.children.tail = &l->header.next;
1615 s_p = &(*s_p)->header.next;
1618 #if EXTRA_VALIDATION
1619 ct1 = ld_count_children ((lang_statement_union_type*) w);
1620 ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1622 ASSERT (old_count == (ct1 + ct2));
1625 /* Now place them back in front of their dependent sections. */
1627 while (literal_wild.children.head != NULL)
1629 lang_statement_union_type *lit = literal_wild.children.head;
1630 bfd_boolean placed = FALSE;
1632 #if EXTRA_VALIDATION
1638 literal_wild.children.head = lit->header.next;
1639 if (literal_wild.children.head == NULL)
1640 literal_wild.children.tail = &literal_wild.children.head;
1641 lit->header.next = NULL;
1643 /* Find a spot to place it. */
1644 for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1646 lang_statement_union_type *src = *s_p;
1647 if (deps_has_edge (deps, src, lit))
1649 /* Place it here. */
1650 lit->header.next = *s_p;
1659 /* Put it at the end. */
1660 *w->children.tail = lit;
1661 w->children.tail = &lit->header.next;
1665 #if EXTRA_VALIDATION
1666 new_count = ld_count_children ((lang_statement_union_type*) w);
1667 ASSERT (new_count == old_count);
1673 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1675 lang_output_section_statement_type *os;
1676 reloc_deps_graph *deps;
1677 if (statement->header.type == lang_output_section_statement_enum)
1679 /* Now, we walk over the contours of the output section statement.
1681 First we build the literal section dependences as before.
1683 At the first uniquely_literal section, we mark it as a good
1684 spot to place other literals. Continue walking (and counting
1685 sizes) until we find the next literal section. If this
1686 section can be moved to the first one, then we move it. If
1687 we every find a modification of ".", start over. If we find
1688 a labeling of the current location, start over. Finally, at
1689 the end, if we require page alignment, add page alignments. */
1691 #if EXTRA_VALIDATION
1692 size_t old_child_count;
1693 size_t new_child_count;
1695 bfd_boolean no_reorder = FALSE;
1697 os = &statement->output_section_statement;
1699 #if EXTRA_VALIDATION
1700 old_child_count = ld_count_children (statement);
1703 /* It is now officially a target. Build the graph of source
1704 section -> target section (kept as a list of edges). */
1706 deps = ld_build_required_section_dependence (statement);
1708 /* If this wildcard does not reorder.... */
1711 /* First check for reverse dependences. Fix if possible. */
1712 xtensa_colocate_literals (deps, statement);
1714 #if EXTRA_VALIDATION
1715 new_child_count = ld_count_children (statement);
1716 ASSERT (new_child_count == old_child_count);
1720 /* Insert align/offset assignment statement. */
1721 if (xtensa_use_literal_pages)
1723 ld_xtensa_insert_page_offsets (0, statement, deps,
1724 xtensa_use_literal_pages);
1725 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1730 free_reloc_deps_graph (deps);
1736 xtensa_colocate_output_literals (lang_statement_union_type *s)
1738 lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1743 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1745 switch (statement->header.type)
1747 case lang_input_section_enum:
1749 asection *bfd_section = statement->input_section.section;
1750 bfd_section->output_offset = 0;
1760 ld_assign_relative_paged_dot (bfd_vma dot,
1761 lang_statement_union_type *s,
1762 const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1763 bfd_boolean lit_align)
1765 /* Walk through all of the input statements in this wild statement
1766 assign dot to all of them. */
1768 xtensa_ld_iter_stack *stack = NULL;
1769 xtensa_ld_iter_stack **stack_p = &stack;
1771 bfd_boolean first_section = FALSE;
1772 bfd_boolean in_literals = FALSE;
1774 for (iter_stack_create (stack_p, s);
1775 !iter_stack_empty (stack_p);
1776 iter_stack_next (stack_p))
1778 lang_statement_union_type *l = iter_stack_current (stack_p);
1780 switch (l->header.type)
1782 case lang_input_section_enum:
1784 asection *section = l->input_section.section;
1785 size_t align_pow = section->alignment_power;
1786 bfd_boolean do_xtensa_alignment = FALSE;
1790 bfd_boolean sec_is_target = section_is_target (deps, l);
1791 bfd_boolean sec_is_source = section_is_source (deps, l);
1793 if (section->size != 0
1795 || (in_literals && !sec_is_target)
1796 || (!in_literals && sec_is_target)))
1798 do_xtensa_alignment = TRUE;
1800 first_section = FALSE;
1801 if (section->size != 0)
1802 in_literals = (sec_is_target && !sec_is_source);
1805 if (do_xtensa_alignment && xtensa_page_power != 0)
1806 dot += (1 << xtensa_page_power);
1808 dot = align_power (dot, align_pow);
1809 section->output_offset = dot;
1810 dot += section->size;
1813 case lang_fill_statement_enum:
1814 dot += l->fill_statement.size;
1816 case lang_padding_statement_enum:
1817 dot += l->padding_statement.size;
1828 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1829 const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1831 /* Walk over all of the dependencies that we identified and make
1832 sure that IF the source and target are here (addr != 0):
1833 1) target addr < source addr
1834 2) (roundup(source + source_size, 4) - rounddown(target, 4))
1835 < (256K - (1 << bad align))
1836 Need a worst-case proof.... */
1838 xtensa_ld_iter_stack *stack = NULL;
1839 xtensa_ld_iter_stack **stack_p = &stack;
1840 size_t max_align_power = 0;
1841 size_t align_penalty = 256;
1845 /* Find the worst-case alignment requirement for this set of statements. */
1846 for (iter_stack_create (stack_p, statement);
1847 !iter_stack_empty (stack_p);
1848 iter_stack_next (stack_p))
1850 lang_statement_union_type *l = iter_stack_current (stack_p);
1851 if (l->header.type == lang_input_section_enum)
1853 lang_input_section_type *input = &l->input_section;
1854 asection *section = input->section;
1855 if (section->alignment_power > max_align_power)
1856 max_align_power = section->alignment_power;
1860 /* Now check that everything fits. */
1861 for (i = 0; i < deps->count; i++)
1863 asection *sec = deps->sections[i];
1864 const reloc_deps_section *deps_section =
1865 xtensa_get_section_deps (deps, sec);
1868 /* We choose to walk through the successors. */
1869 for (e = deps_section->succs; e != NULL; e = e->next)
1871 if (e->src != e->tgt
1872 && e->src->output_section == e->tgt->output_section
1873 && e->src->output_offset != 0
1874 && e->tgt->output_offset != 0)
1877 align_power (e->src->output_offset + e->src->size, 2);
1878 bfd_vma target_addr = e->tgt->output_offset & ~3;
1879 if (l32r_addr < target_addr)
1881 fprintf (stderr, "Warning: "
1882 "l32r target section before l32r\n");
1886 if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1898 ld_xtensa_insert_page_offsets (bfd_vma dot,
1899 lang_statement_union_type *s,
1900 reloc_deps_graph *deps,
1901 bfd_boolean lit_align)
1903 xtensa_ld_iter_stack *stack = NULL;
1904 xtensa_ld_iter_stack **stack_p = &stack;
1906 bfd_boolean first_section = FALSE;
1907 bfd_boolean in_literals = FALSE;
1912 for (iter_stack_create (stack_p, s);
1913 !iter_stack_empty (stack_p);
1914 iter_stack_next (stack_p))
1916 lang_statement_union_type *l = iter_stack_current (stack_p);
1918 switch (l->header.type)
1920 case lang_input_section_enum:
1922 asection *section = l->input_section.section;
1923 bfd_boolean do_xtensa_alignment = FALSE;
1927 if (section->size != 0
1929 || (in_literals && !section_is_target (deps, l))
1930 || (!in_literals && section_is_target (deps, l))))
1932 do_xtensa_alignment = TRUE;
1934 first_section = FALSE;
1935 if (section->size != 0)
1937 in_literals = (section_is_target (deps, l)
1938 && !section_is_source (deps, l));
1942 if (do_xtensa_alignment && xtensa_page_power != 0)
1944 /* Create an expression that increments the current address,
1945 i.e., "dot", by (1 << xtensa_align_power). */
1946 etree_type *name_op = exp_nameop (NAME, ".");
1947 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1948 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1949 etree_type *assign_op = exp_assop ('=', ".", add_op);
1951 lang_assignment_statement_type *assign_stmt;
1952 lang_statement_union_type *assign_union;
1953 lang_statement_list_type tmplist;
1955 /* There is hidden state in "lang_add_assignment". It
1956 appends the new assignment statement to the stat_ptr
1957 list. Thus, we swap it before and after the call. */
1959 lang_list_init (&tmplist);
1960 push_stat_ptr (&tmplist);
1961 /* Warning: side effect; statement appended to stat_ptr. */
1962 assign_stmt = lang_add_assignment (assign_op);
1963 assign_union = (lang_statement_union_type *) assign_stmt;
1966 assign_union->header.next = l;
1967 *(*stack_p)->iterloc.loc = assign_union;
1968 iter_stack_next (stack_p);
1981 # Define some shell vars to insert bits of code into the standard ELF
1982 # parse_args and list_options functions.
1984 PARSE_AND_LIST_PROLOGUE='
1985 #define OPTION_OPT_SIZEOPT (300)
1986 #define OPTION_NO_RELAX (OPTION_OPT_SIZEOPT + 1)
1987 #define OPTION_LITERAL_MOVEMENT (OPTION_NO_RELAX + 1)
1988 #define OPTION_NO_LITERAL_MOVEMENT (OPTION_LITERAL_MOVEMENT + 1)
1989 extern int elf32xtensa_size_opt;
1990 extern int elf32xtensa_no_literal_movement;
1993 PARSE_AND_LIST_LONGOPTS='
1994 { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1995 { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1996 { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1997 { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
2000 PARSE_AND_LIST_OPTIONS='
2002 --size-opt When relaxing longcalls, prefer size\n\
2003 optimization over branch target alignment\n"));
2005 --no-relax Do not relax branches or coalesce literals\n"));
2008 PARSE_AND_LIST_ARGS_CASES='
2009 case OPTION_OPT_SIZEOPT:
2010 elf32xtensa_size_opt = 1;
2012 case OPTION_NO_RELAX:
2013 disable_relaxation = TRUE;
2015 case OPTION_LITERAL_MOVEMENT:
2016 elf32xtensa_no_literal_movement = 0;
2018 case OPTION_NO_LITERAL_MOVEMENT:
2019 elf32xtensa_no_literal_movement = 1;
2023 # Replace some of the standard ELF functions with our own versions.
2025 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
2026 LDEMUL_AFTER_OPEN=elf_xtensa_after_open
2027 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
2028 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation