1 # This shell script emits a C file. -*- C -*-
3 # Free Software Foundation, Inc.
5 # This file is part of GLD, the Gnu Linker.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # This file is sourced from elf32.em, and defines extra xtensa-elf
25 cat >>e${EMULATION_NAME}.c <<EOF
27 #include <xtensa-config.h>
29 static void xtensa_wild_group_interleave (lang_statement_union_type *);
30 static void xtensa_colocate_output_literals (lang_statement_union_type *);
33 /* Flag for the emulation-specific "--no-relax" option. */
34 static bfd_boolean disable_relaxation = FALSE;
36 /* This number is irrelevant until we turn on use_literal_pages */
37 static bfd_vma xtensa_page_power = 12; /* 4K pages. */
39 /* To force a page break between literals and text, change
40 xtensa_use_literal_pages to "true". */
41 static bfd_boolean xtensa_use_literal_pages = FALSE;
43 #define EXTRA_VALIDATION 0
47 elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
48 char **argv ATTRIBUTE_UNUSED)
51 return "${BIG_OUTPUT_FORMAT}";
53 return "${LITTLE_OUTPUT_FORMAT}";
58 elf_xtensa_place_orphan (lang_input_statement_type *file, asection *s)
60 /* Early exit for relocatable links. */
61 if (link_info.relocatable)
64 return gld${EMULATION_NAME}_place_orphan (file, s);
69 elf_xtensa_before_parse (void)
71 /* Just call the default hook.... Tensilica's version of this function
72 does some other work that isn't relevant here. */
73 gld${EMULATION_NAME}_before_parse ();
77 /* This is called after the sections have been attached to output
78 sections, but before any sizes or addresses have been set. */
81 elf_xtensa_before_allocation (void)
84 bfd_boolean is_big_endian = XCHAL_HAVE_BE;
86 /* Check that the output endianness matches the Xtensa
87 configuration. The BFD library always includes both big and
88 little endian target vectors for Xtensa, but it only supports the
89 detailed instruction encode/decode operations (such as are
90 required to process relocations) for the selected Xtensa
93 if (is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
95 einfo (_("%F%P: little endian output does not match "
96 "Xtensa configuration\n"));
98 if (!is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
100 einfo (_("%F%P: big endian output does not match "
101 "Xtensa configuration\n"));
104 /* Check that the endianness for each input file matches the output.
105 The merge_private_bfd_data hook has already reported any mismatches
106 as errors, but those errors are not fatal. At this point, we
107 cannot go any further if there are any mismatches. */
109 for (in_bfd = link_info.input_bfds;
111 in_bfd = in_bfd->link_next)
113 if ((is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
114 || (!is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
115 einfo (_("%F%P: cross-endian linking not supported\n"));
118 /* Enable relaxation by default if the "--no-relax" option was not
119 specified. This is done here instead of in the before_parse hook
120 because there is a check in main() to prohibit use of --relax and
121 -r together and that combination should be allowed for Xtensa. */
123 if (!disable_relaxation)
124 command_line.relax = TRUE;
126 gld${EMULATION_NAME}_before_allocation ();
128 xtensa_wild_group_interleave (stat_ptr->head);
129 if (command_line.relax)
130 xtensa_colocate_output_literals (stat_ptr->head);
132 /* TBD: We need to force the page alignments to here and only do
133 them as needed for the entire output section. Finally, if this
134 is a relocatable link then we need to add alignment notes so
135 that the literals can be separated later. */
139 typedef struct wildcard_list section_name_list;
141 typedef struct reloc_deps_e_t reloc_deps_e;
142 typedef struct reloc_deps_section_t reloc_deps_section;
143 typedef struct reloc_deps_graph_t reloc_deps_graph;
146 struct reloc_deps_e_t
148 asection *src; /* Contains l32rs. */
149 asection *tgt; /* Contains literals. */
153 /* Place these in the userdata field. */
154 struct reloc_deps_section_t
158 bfd_boolean is_only_literal;
162 struct reloc_deps_graph_t
169 static void xtensa_layout_wild
170 (const reloc_deps_graph *, lang_wild_statement_type *);
172 typedef void (*deps_callback_t) (asection *, /* src_sec */
173 bfd_vma, /* src_offset */
174 asection *, /* target_sec */
175 bfd_vma, /* target_offset */
176 void *); /* closure */
178 extern bfd_boolean xtensa_callback_required_dependence
179 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
180 static void xtensa_ldlang_clear_addresses
181 (lang_statement_union_type *);
182 static bfd_boolean ld_local_file_relocations_fit
183 (lang_statement_union_type *, const reloc_deps_graph *);
184 static bfd_vma ld_assign_relative_paged_dot
185 (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
187 static bfd_vma ld_xtensa_insert_page_offsets
188 (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
190 static size_t ld_count_children
191 (lang_statement_union_type *);
194 extern lang_statement_list_type constructor_list;
196 /* Begin verbatim code from ldlang.c:
197 the following are copied from ldlang.c because they are defined
201 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
202 lang_statement_union_type *s)
204 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
208 switch (s->header.type)
210 case lang_constructors_statement_enum:
211 lang_for_each_statement_worker (func, constructor_list.head);
213 case lang_output_section_statement_enum:
214 lang_for_each_statement_worker
216 s->output_section_statement.children.head);
218 case lang_wild_statement_enum:
219 lang_for_each_statement_worker
221 s->wild_statement.children.head);
223 case lang_group_statement_enum:
224 lang_for_each_statement_worker (func,
225 s->group_statement.children.head);
227 case lang_data_statement_enum:
228 case lang_reloc_statement_enum:
229 case lang_object_symbols_statement_enum:
230 case lang_output_statement_enum:
231 case lang_target_statement_enum:
232 case lang_input_section_enum:
233 case lang_input_statement_enum:
234 case lang_assignment_statement_enum:
235 case lang_padding_statement_enum:
236 case lang_address_statement_enum:
237 case lang_fill_statement_enum:
246 /* End of verbatim code from ldlang.c. */
249 static reloc_deps_section *
250 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
253 /* We have a separate function for this so that
254 we could in the future keep a completely independent
255 structure that maps a section to its dependence edges.
256 For now, we place these in the sec->userdata field. */
257 reloc_deps_section *sec_deps = sec->userdata;
262 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
264 reloc_deps_section *deps_section)
266 sec->userdata = deps_section;
270 /* This is used to keep a list of all of the sections participating in
271 the graph so we can clean them up quickly. */
274 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
276 if (deps->size <= deps->count)
278 asection **new_sections;
282 new_size = deps->size * 2;
286 new_sections = xmalloc (sizeof (asection *) * new_size);
287 memset (new_sections, 0, sizeof (asection *) * new_size);
288 for (i = 0; i < deps->count; i++)
290 new_sections[i] = deps->sections[i];
292 if (deps->sections != NULL)
293 free (deps->sections);
294 deps->sections = new_sections;
295 deps->size = new_size;
297 deps->sections[deps->count] = sec;
303 free_reloc_deps_graph (reloc_deps_graph *deps)
306 for (i = 0; i < deps->count; i++)
308 asection *sec = deps->sections[i];
309 reloc_deps_section *sec_deps;
310 sec_deps = xtensa_get_section_deps (deps, sec);
314 while (sec_deps->succs != NULL)
316 next = sec_deps->succs->next;
317 free (sec_deps->succs);
318 sec_deps->succs = next;
321 while (sec_deps->preds != NULL)
323 next = sec_deps->preds->next;
324 free (sec_deps->preds);
325 sec_deps->preds = next;
329 xtensa_set_section_deps (deps, sec, NULL);
332 free (deps->sections);
339 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
340 lang_statement_union_type *s)
343 const reloc_deps_section *sec_deps;
345 if (s->header.type != lang_input_section_enum)
347 sec = s->input_section.section;
349 sec_deps = xtensa_get_section_deps (deps, sec);
350 return sec_deps && sec_deps->succs != NULL;
355 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
356 lang_statement_union_type *s)
359 const reloc_deps_section *sec_deps;
361 if (s->header.type != lang_input_section_enum)
363 sec = s->input_section.section;
365 sec_deps = xtensa_get_section_deps (deps, sec);
366 return sec_deps && sec_deps->preds != NULL;
370 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
371 lang_statement_union_type *s)
373 return (section_is_source (deps, s)
374 || section_is_target (deps, s));
378 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
379 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
381 struct xtensa_ld_iter_t
383 lang_statement_union_type *parent; /* Parent of the list. */
384 lang_statement_list_type *l; /* List that holds it. */
385 lang_statement_union_type **loc; /* Place in the list. */
388 struct xtensa_ld_iter_stack_t
390 xtensa_ld_iter iterloc; /* List that hold it. */
392 xtensa_ld_iter_stack *next; /* Next in the stack. */
393 xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
398 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
400 lang_statement_union_type *to_next;
401 lang_statement_union_type *current_next;
402 lang_statement_union_type **e;
405 size_t old_to_count, new_to_count;
406 size_t old_current_count, new_current_count;
413 old_to_count = ld_count_children (to->parent);
414 old_current_count = ld_count_children (current->parent);
417 to_next = *(to->loc);
418 current_next = (*current->loc)->header.next;
420 *(to->loc) = *(current->loc);
422 *(current->loc) = current_next;
423 (*(to->loc))->header.next = to_next;
425 /* reset "to" list tail */
426 for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
430 /* reset "current" list tail */
431 for (e = ¤t->l->head; *e != NULL; e = &(*e)->header.next)
433 current->l->tail = e;
436 new_to_count = ld_count_children (to->parent);
437 new_current_count = ld_count_children (current->parent);
439 ASSERT ((old_to_count + old_current_count)
440 == (new_to_count + new_current_count));
445 /* Can only be called with lang_statements that have lists. Returns
446 false if the list is empty. */
449 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
451 return *stack_p == NULL;
456 iter_stack_push (xtensa_ld_iter_stack **stack_p,
457 lang_statement_union_type *parent)
459 xtensa_ld_iter_stack *stack;
460 lang_statement_list_type *l = NULL;
462 switch (parent->header.type)
464 case lang_output_section_statement_enum:
465 l = &parent->output_section_statement.children;
467 case lang_wild_statement_enum:
468 l = &parent->wild_statement.children;
470 case lang_group_statement_enum:
471 l = &parent->group_statement.children;
478 /* Empty. do not push. */
479 if (l->tail == &l->head)
482 stack = xmalloc (sizeof (xtensa_ld_iter_stack));
483 memset (stack, 0, sizeof (xtensa_ld_iter_stack));
484 stack->iterloc.parent = parent;
485 stack->iterloc.l = l;
486 stack->iterloc.loc = &l->head;
488 stack->next = *stack_p;
490 if (*stack_p != NULL)
491 (*stack_p)->prev = stack;
498 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
500 xtensa_ld_iter_stack *stack;
506 ASSERT (stack != NULL);
510 if (stack->next != NULL)
511 stack->next->prev = NULL;
513 *stack_p = stack->next;
518 /* This MUST be called if, during iteration, the user changes the
519 underlying structure. It will check for a NULL current and advance
523 iter_stack_update (xtensa_ld_iter_stack **stack_p)
525 if (!iter_stack_empty (stack_p)
526 && (*(*stack_p)->iterloc.loc) == NULL)
528 iter_stack_pop (stack_p);
530 while (!iter_stack_empty (stack_p)
531 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
533 iter_stack_pop (stack_p);
535 if (!iter_stack_empty (stack_p))
536 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
542 iter_stack_next (xtensa_ld_iter_stack **stack_p)
544 xtensa_ld_iter_stack *stack;
545 lang_statement_union_type *current;
548 current = *stack->iterloc.loc;
549 /* If we are on the first element. */
552 switch (current->header.type)
554 case lang_output_section_statement_enum:
555 case lang_wild_statement_enum:
556 case lang_group_statement_enum:
557 /* If the list if not empty, we are done. */
558 if (iter_stack_push (stack_p, *stack->iterloc.loc))
560 /* Otherwise increment the pointer as normal. */
567 while (!iter_stack_empty (stack_p)
568 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
570 iter_stack_pop (stack_p);
572 if (!iter_stack_empty (stack_p))
573 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
577 static lang_statement_union_type *
578 iter_stack_current (xtensa_ld_iter_stack **stack_p)
580 return *((*stack_p)->iterloc.loc);
584 /* The iter stack is a preorder. */
587 iter_stack_create (xtensa_ld_iter_stack **stack_p,
588 lang_statement_union_type *parent)
590 iter_stack_push (stack_p, parent);
595 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p,
596 xtensa_ld_iter *front)
598 *front = (*stack_p)->iterloc;
603 xtensa_colocate_literals (reloc_deps_graph *deps,
604 lang_statement_union_type *statement)
606 /* Keep a stack of pointers to control iteration through the contours. */
607 xtensa_ld_iter_stack *stack = NULL;
608 xtensa_ld_iter_stack **stack_p = &stack;
610 xtensa_ld_iter front; /* Location where new insertion should occur. */
611 xtensa_ld_iter *front_p = NULL;
613 xtensa_ld_iter current; /* Location we are checking. */
614 xtensa_ld_iter *current_p = NULL;
615 bfd_boolean in_literals = FALSE;
617 if (deps->count == 0)
621 ld_assign_relative_paged_dot (0x100000, statement, deps,
622 xtensa_use_literal_pages);
624 if (!ld_local_file_relocations_fit (statement, deps))
625 fprintf (stderr, "initial relocation placement does not fit\n");
627 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
630 iter_stack_create (stack_p, statement);
632 while (!iter_stack_empty (stack_p))
634 bfd_boolean skip_increment = FALSE;
635 lang_statement_union_type *l = iter_stack_current (stack_p);
637 switch (l->header.type)
639 case lang_assignment_statement_enum:
640 /* Any assignment statement should block reordering across it. */
645 case lang_input_section_enum:
648 in_literals = (section_is_target (deps, l)
649 && !section_is_source (deps, l));
653 iter_stack_copy_current (stack_p, front_p);
658 bfd_boolean is_target;
659 current_p = ¤t;
660 iter_stack_copy_current (stack_p, current_p);
661 is_target = (section_is_target (deps, l)
662 && !section_is_source (deps, l));
666 iter_stack_copy_current (stack_p, front_p);
674 /* Try to insert in place. */
675 ld_xtensa_move_section_after (front_p, current_p);
676 ld_assign_relative_paged_dot (0x100000,
679 xtensa_use_literal_pages);
681 /* We use this code because it's already written. */
682 if (!ld_local_file_relocations_fit (statement, deps))
685 ld_xtensa_move_section_after (current_p, front_p);
686 /* Reset the literal placement. */
687 iter_stack_copy_current (stack_p, front_p);
691 /* Move front pointer up by one. */
692 front_p->loc = &(*front_p->loc)->header.next;
694 /* Do not increment the current pointer. */
695 skip_increment = TRUE;
706 iter_stack_next (stack_p);
708 /* Be careful to update the stack_p if it now is a null. */
709 iter_stack_update (stack_p);
712 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
717 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
718 lang_wild_statement_type *w)
720 /* Keep a front pointer and a current pointer. */
721 lang_statement_union_type **front;
722 lang_statement_union_type **current;
724 /* Walk to the end of the targets. */
725 for (front = &w->children.head;
726 (*front != NULL) && section_is_source_or_target (deps, *front);
727 front = &(*front)->header.next)
733 current = &(*front)->header.next;
734 while (*current != NULL)
736 if (section_is_source_or_target (deps, *current))
738 /* Insert in place. */
739 xtensa_ld_iter front_iter;
740 xtensa_ld_iter current_iter;
742 front_iter.parent = (lang_statement_union_type *) w;
743 front_iter.l = &w->children;
744 front_iter.loc = front;
746 current_iter.parent = (lang_statement_union_type *) w;
747 current_iter.l = &w->children;
748 current_iter.loc = current;
750 ld_xtensa_move_section_after (&front_iter, ¤t_iter);
751 front = &(*front)->header.next;
755 current = &(*current)->header.next;
762 deps_has_sec_edge (const reloc_deps_graph *deps,
766 const reloc_deps_section *sec_deps;
767 const reloc_deps_e *sec_deps_e;
769 sec_deps = xtensa_get_section_deps (deps, src);
770 if (sec_deps == NULL)
773 for (sec_deps_e = sec_deps->succs;
775 sec_deps_e = sec_deps_e->next)
777 ASSERT (sec_deps_e->src == src);
778 if (sec_deps_e->tgt == tgt)
786 deps_has_edge (const reloc_deps_graph *deps,
787 lang_statement_union_type *src,
788 lang_statement_union_type *tgt)
790 if (!section_is_source (deps, src))
792 if (!section_is_target (deps, tgt))
795 if (src->header.type != lang_input_section_enum)
797 if (tgt->header.type != lang_input_section_enum)
800 return deps_has_sec_edge (deps, src->input_section.section,
801 tgt->input_section.section);
806 add_deps_edge (reloc_deps_graph *deps,
810 reloc_deps_section *src_sec_deps;
811 reloc_deps_section *tgt_sec_deps;
813 reloc_deps_e *src_edge;
814 reloc_deps_e *tgt_edge;
816 if (deps_has_sec_edge (deps, src_sec, tgt_sec))
819 src_sec_deps = xtensa_get_section_deps (deps, src_sec);
820 if (src_sec_deps == NULL)
823 src_sec_deps = xmalloc (sizeof (reloc_deps_section));
824 memset (src_sec_deps, 0, sizeof (reloc_deps_section));
825 src_sec_deps->is_only_literal = 0;
826 src_sec_deps->preds = NULL;
827 src_sec_deps->succs = NULL;
828 xtensa_set_section_deps (deps, src_sec, src_sec_deps);
829 xtensa_append_section_deps (deps, src_sec);
832 tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
833 if (tgt_sec_deps == NULL)
836 tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
837 memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
838 tgt_sec_deps->is_only_literal = 0;
839 tgt_sec_deps->preds = NULL;
840 tgt_sec_deps->succs = NULL;
841 xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
842 xtensa_append_section_deps (deps, tgt_sec);
846 src_edge = xmalloc (sizeof (reloc_deps_e));
847 memset (src_edge, 0, sizeof (reloc_deps_e));
848 src_edge->src = src_sec;
849 src_edge->tgt = tgt_sec;
850 src_edge->next = src_sec_deps->succs;
851 src_sec_deps->succs = src_edge;
853 tgt_edge = xmalloc (sizeof (reloc_deps_e));
854 memset (tgt_edge, 0, sizeof (reloc_deps_e));
855 tgt_edge->src = src_sec;
856 tgt_edge->tgt = tgt_sec;
857 tgt_edge->next = tgt_sec_deps->preds;
858 tgt_sec_deps->preds = tgt_edge;
863 build_deps_graph_callback (asection *src_sec,
864 bfd_vma src_offset ATTRIBUTE_UNUSED,
865 asection *target_sec,
866 bfd_vma target_offset ATTRIBUTE_UNUSED,
869 reloc_deps_graph *deps = closure;
871 /* If the target is defined. */
872 if (target_sec != NULL)
873 add_deps_edge (deps, src_sec, target_sec);
877 static reloc_deps_graph *
878 ld_build_required_section_dependence (lang_statement_union_type *s)
880 reloc_deps_graph *deps;
881 xtensa_ld_iter_stack *stack = NULL;
883 deps = xmalloc (sizeof (reloc_deps_graph));
884 deps->sections = NULL;
888 for (iter_stack_create (&stack, s);
889 !iter_stack_empty (&stack);
890 iter_stack_next (&stack))
892 lang_statement_union_type *l = iter_stack_current (&stack);
894 if (l->header.type == lang_input_section_enum)
896 lang_input_section_type *input;
897 input = &l->input_section;
898 xtensa_callback_required_dependence (input->ifile->the_bfd,
901 /* Use the same closure. */
902 build_deps_graph_callback,
912 ld_count_children (lang_statement_union_type *s)
915 xtensa_ld_iter_stack *stack = NULL;
916 for (iter_stack_create (&stack, s);
917 !iter_stack_empty (&stack);
918 iter_stack_next (&stack))
920 lang_statement_union_type *l = iter_stack_current (&stack);
926 #endif /* EXTRA_VALIDATION */
930 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
932 lang_wild_statement_type *w;
933 reloc_deps_graph *deps;
934 if (statement->header.type == lang_wild_statement_enum)
937 size_t old_child_count;
938 size_t new_child_count;
940 bfd_boolean no_reorder;
942 w = &statement->wild_statement;
946 /* If it has 0 or 1 section bound, then do not reorder. */
947 if (w->children.head == NULL
948 || (w->children.head->header.type == lang_input_section_enum
949 && w->children.head->header.next == NULL))
952 if (w->filenames_sorted)
955 /* Check for sorting in a section list wildcard spec as well. */
958 struct wildcard_list *l;
959 for (l = w->section_list; l != NULL; l = l->next)
961 if (l->spec.sorted == TRUE)
969 /* Special case until the NOREORDER linker directive is supported:
970 *(.init) output sections and *(.fini) specs may NOT be reordered. */
972 /* Check for sorting in a section list wildcard spec as well. */
975 struct wildcard_list *l;
976 for (l = w->section_list; l != NULL; l = l->next)
979 && ((strcmp (".init", l->spec.name) == 0)
980 || (strcmp (".fini", l->spec.name) == 0)))
989 old_child_count = ld_count_children (statement);
992 /* It is now officially a target. Build the graph of source
993 section -> target section (kept as a list of edges). */
994 deps = ld_build_required_section_dependence (statement);
996 /* If this wildcard does not reorder.... */
997 if (!no_reorder && deps->count != 0)
999 /* First check for reverse dependences. Fix if possible. */
1000 xtensa_layout_wild (deps, w);
1002 xtensa_move_dependencies_to_front (deps, w);
1003 #if EXTRA_VALIDATION
1004 new_child_count = ld_count_children (statement);
1005 ASSERT (new_child_count == old_child_count);
1008 xtensa_colocate_literals (deps, statement);
1010 #if EXTRA_VALIDATION
1011 new_child_count = ld_count_children (statement);
1012 ASSERT (new_child_count == old_child_count);
1017 free_reloc_deps_graph (deps);
1023 xtensa_wild_group_interleave (lang_statement_union_type *s)
1025 lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1030 xtensa_layout_wild (const reloc_deps_graph *deps,
1031 lang_wild_statement_type *w)
1033 /* If it does not fit initially, we need to do this step. Move all
1034 of the wild literal sections to a new list, then move each of
1035 them back in just before the first section they depend on. */
1036 lang_statement_union_type **s_p;
1037 #if EXTRA_VALIDATION
1038 size_t old_count, new_count;
1042 lang_wild_statement_type literal_wild;
1043 literal_wild.header.next = NULL;
1044 literal_wild.header.type = lang_wild_statement_enum;
1045 literal_wild.filename = NULL;
1046 literal_wild.filenames_sorted = FALSE;
1047 literal_wild.section_list = NULL;
1048 literal_wild.keep_sections = FALSE;
1049 literal_wild.children.head = NULL;
1050 literal_wild.children.tail = &literal_wild.children.head;
1052 #if EXTRA_VALIDATION
1053 old_count = ld_count_children ((lang_statement_union_type*) w);
1056 s_p = &w->children.head;
1057 while (*s_p != NULL)
1059 lang_statement_union_type *l = *s_p;
1060 if (l->header.type == lang_input_section_enum)
1062 if (section_is_target (deps, l)
1063 && ! section_is_source (deps, l))
1066 *s_p = l->header.next;
1068 w->children.tail = s_p;
1069 l->header.next = NULL;
1072 *literal_wild.children.tail = l;
1073 literal_wild.children.tail = &l->header.next;
1077 s_p = &(*s_p)->header.next;
1080 #if EXTRA_VALIDATION
1081 ct1 = ld_count_children ((lang_statement_union_type*) w);
1082 ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1084 ASSERT (old_count == (ct1 + ct2));
1087 /* Now place them back in front of their dependent sections. */
1089 while (literal_wild.children.head != NULL)
1091 lang_statement_union_type *lit = literal_wild.children.head;
1092 bfd_boolean placed = FALSE;
1094 #if EXTRA_VALIDATION
1100 literal_wild.children.head = lit->header.next;
1101 if (literal_wild.children.head == NULL)
1102 literal_wild.children.tail = &literal_wild.children.head;
1103 lit->header.next = NULL;
1105 /* Find a spot to place it. */
1106 for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1108 lang_statement_union_type *src = *s_p;
1109 if (deps_has_edge (deps, src, lit))
1111 /* Place it here. */
1112 lit->header.next = *s_p;
1121 /* Put it at the end. */
1122 *w->children.tail = lit;
1123 w->children.tail = &lit->header.next;
1127 #if EXTRA_VALIDATION
1128 new_count = ld_count_children ((lang_statement_union_type*) w);
1129 ASSERT (new_count == old_count);
1135 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1137 lang_output_section_statement_type *os;
1138 reloc_deps_graph *deps;
1139 if (statement->header.type == lang_output_section_statement_enum)
1141 /* Now, we walk over the contours of the output section statement.
1143 First we build the literal section dependences as before.
1145 At the first uniquely_literal section, we mark it as a good
1146 spot to place other literals. Continue walking (and counting
1147 sizes) until we find the next literal section. If this
1148 section can be moved to the first one, then we move it. If
1149 we every find a modification of ".", start over. If we find
1150 a labeling of the current location, start over. Finally, at
1151 the end, if we require page alignment, add page alignments. */
1153 #if EXTRA_VALIDATION
1154 size_t old_child_count;
1155 size_t new_child_count;
1157 bfd_boolean no_reorder = FALSE;
1159 os = &statement->output_section_statement;
1161 #if EXTRA_VALIDATION
1162 old_child_count = ld_count_children (statement);
1165 /* It is now officially a target. Build the graph of source
1166 section -> target section (kept as a list of edges). */
1168 deps = ld_build_required_section_dependence (statement);
1170 /* If this wildcard does not reorder.... */
1173 /* First check for reverse dependences. Fix if possible. */
1174 xtensa_colocate_literals (deps, statement);
1176 #if EXTRA_VALIDATION
1177 new_child_count = ld_count_children (statement);
1178 ASSERT (new_child_count == old_child_count);
1182 /* Insert align/offset assignment statement. */
1183 if (xtensa_use_literal_pages)
1185 ld_xtensa_insert_page_offsets (0, statement, deps,
1186 xtensa_use_literal_pages);
1187 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1192 free_reloc_deps_graph (deps);
1198 xtensa_colocate_output_literals (lang_statement_union_type *s)
1200 lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1205 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1207 switch (statement->header.type)
1209 case lang_input_section_enum:
1211 asection *bfd_section = statement->input_section.section;
1212 bfd_section->output_offset = 0;
1222 ld_assign_relative_paged_dot (bfd_vma dot,
1223 lang_statement_union_type *s,
1224 const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1225 bfd_boolean lit_align)
1227 /* Walk through all of the input statements in this wild statement
1228 assign dot to all of them. */
1230 xtensa_ld_iter_stack *stack = NULL;
1231 xtensa_ld_iter_stack **stack_p = &stack;
1233 bfd_boolean first_section = FALSE;
1234 bfd_boolean in_literals = FALSE;
1236 for (iter_stack_create (stack_p, s);
1237 !iter_stack_empty (stack_p);
1238 iter_stack_next (stack_p))
1240 lang_statement_union_type *l = iter_stack_current (stack_p);
1242 switch (l->header.type)
1244 case lang_input_section_enum:
1246 asection *section = l->input_section.section;
1247 size_t align_pow = section->alignment_power;
1248 bfd_boolean do_xtensa_alignment = FALSE;
1252 bfd_boolean sec_is_target = section_is_target (deps, l);
1253 bfd_boolean sec_is_source = section_is_source (deps, l);
1255 if (section->_raw_size != 0
1257 || (in_literals && !sec_is_target)
1258 || (!in_literals && sec_is_target)))
1260 do_xtensa_alignment = TRUE;
1262 first_section = FALSE;
1263 if (section->_raw_size != 0)
1264 in_literals = (sec_is_target && !sec_is_source);
1267 if (do_xtensa_alignment && xtensa_page_power != 0)
1268 dot += (1 << xtensa_page_power);
1270 dot = align_power (dot, align_pow);
1271 section->output_offset = dot;
1272 dot += section->_raw_size;
1275 case lang_fill_statement_enum:
1276 dot += l->fill_statement.size;
1278 case lang_padding_statement_enum:
1279 dot += l->padding_statement.size;
1290 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1291 const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1293 /* Walk over all of the dependencies that we identified and make
1294 sure that IF the source and target are here (addr != 0):
1295 1) target addr < source addr
1296 2) (roundup(source + source_size, 4) - rounddown(target, 4))
1297 < (256K - (1 << bad align))
1298 Need a worst-case proof.... */
1300 xtensa_ld_iter_stack *stack = NULL;
1301 xtensa_ld_iter_stack **stack_p = &stack;
1302 size_t max_align_power = 0;
1303 size_t align_penalty = 256;
1307 /* Find the worst-case alignment requirement for this set of statements. */
1308 for (iter_stack_create (stack_p, statement);
1309 !iter_stack_empty (stack_p);
1310 iter_stack_next (stack_p))
1312 lang_statement_union_type *l = iter_stack_current (stack_p);
1313 if (l->header.type == lang_input_section_enum)
1315 lang_input_section_type *input = &l->input_section;
1316 asection *section = input->section;
1317 if (section->alignment_power > max_align_power)
1318 max_align_power = section->alignment_power;
1322 /* Now check that everything fits. */
1323 for (i = 0; i < deps->count; i++)
1325 asection *sec = deps->sections[i];
1326 const reloc_deps_section *deps_section =
1327 xtensa_get_section_deps (deps, sec);
1330 /* We choose to walk through the successors. */
1331 for (e = deps_section->succs; e != NULL; e = e->next)
1333 if (e->src != e->tgt
1334 && e->src->output_section == e->tgt->output_section
1335 && e->src->output_offset != 0
1336 && e->tgt->output_offset != 0)
1339 align_power (e->src->output_offset + e->src->_raw_size, 2);
1340 bfd_vma target_addr = e->tgt->output_offset & ~3;
1341 if (l32r_addr < target_addr)
1343 fprintf (stderr, "Warning: "
1344 "l32r target section before l32r\n");
1348 if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1360 ld_xtensa_insert_page_offsets (bfd_vma dot,
1361 lang_statement_union_type *s,
1362 reloc_deps_graph *deps,
1363 bfd_boolean lit_align)
1365 xtensa_ld_iter_stack *stack = NULL;
1366 xtensa_ld_iter_stack **stack_p = &stack;
1368 bfd_boolean first_section = FALSE;
1369 bfd_boolean in_literals = FALSE;
1374 for (iter_stack_create (stack_p, s);
1375 !iter_stack_empty (stack_p);
1376 iter_stack_next (stack_p))
1378 lang_statement_union_type *l = iter_stack_current (stack_p);
1380 switch (l->header.type)
1382 case lang_input_section_enum:
1384 asection *section = l->input_section.section;
1385 bfd_boolean do_xtensa_alignment = FALSE;
1389 if (section->_raw_size != 0
1391 || (in_literals && !section_is_target (deps, l))
1392 || (!in_literals && section_is_target (deps, l))))
1394 do_xtensa_alignment = TRUE;
1396 first_section = FALSE;
1397 if (section->_raw_size != 0)
1399 in_literals = (section_is_target (deps, l)
1400 && !section_is_source (deps, l));
1404 if (do_xtensa_alignment && xtensa_page_power != 0)
1406 /* Create an expression that increments the current address,
1407 i.e., "dot", by (1 << xtensa_align_power). */
1408 etree_type *name_op = exp_nameop (NAME, ".");
1409 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1410 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1411 etree_type *assign_op = exp_assop ('=', ".", add_op);
1413 lang_assignment_statement_type *assign_stmt;
1414 lang_statement_union_type *assign_union;
1415 lang_statement_list_type tmplist;
1416 lang_statement_list_type *old_stat_ptr = stat_ptr;
1418 /* There is hidden state in "lang_add_assignment". It
1419 appends the new assignment statement to the stat_ptr
1420 list. Thus, we swap it before and after the call. */
1422 tmplist.head = NULL;
1423 tmplist.tail = &tmplist.head;
1425 stat_ptr = &tmplist;
1426 /* Warning: side effect; statement appended to stat_ptr. */
1427 assign_stmt = lang_add_assignment (assign_op);
1428 assign_union = (lang_statement_union_type *) assign_stmt;
1429 stat_ptr = old_stat_ptr;
1431 assign_union->header.next = l;
1432 *(*stack_p)->iterloc.loc = assign_union;
1433 iter_stack_next (stack_p);
1446 # Define some shell vars to insert bits of code into the standard elf
1447 # parse_args and list_options functions.
1449 PARSE_AND_LIST_PROLOGUE='
1450 #define OPTION_NO_RELAX 301
1453 PARSE_AND_LIST_LONGOPTS='
1454 { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1457 PARSE_AND_LIST_OPTIONS='
1458 fprintf (file, _(" --no-relax\t\tDo not relax branches or coalesce literals\n"));
1461 PARSE_AND_LIST_ARGS_CASES='
1462 case OPTION_NO_RELAX:
1463 disable_relaxation = TRUE;
1467 # Replace some of the standard ELF functions with our own versions.
1469 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
1470 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
1471 LDEMUL_PLACE_ORPHAN=elf_xtensa_place_orphan
1472 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation