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 char *elf_xtensa_choose_target
30 PARAMS ((int, char **));
31 static bfd_boolean elf_xtensa_place_orphan
32 PARAMS ((lang_input_statement_type *, asection *));
33 static void elf_xtensa_before_parse
35 static void elf_xtensa_before_allocation
37 static void xtensa_wild_group_interleave
38 PARAMS ((lang_statement_union_type *));
39 static void xtensa_wild_group_interleave_callback
40 PARAMS ((lang_statement_union_type *));
41 static void xtensa_colocate_output_literals
42 PARAMS ((lang_statement_union_type *));
43 static void xtensa_colocate_output_literals_callback
44 PARAMS ((lang_statement_union_type *));
47 /* Flag for the emulation-specific "--no-relax" option. */
48 static bfd_boolean disable_relaxation = FALSE;
50 /* This number is irrelevant until we turn on use_literal_pages */
51 static bfd_vma xtensa_page_power = 12; /* 4K pages. */
53 /* To force a page break between literals and text, change
54 xtensa_use_literal_pages to "true". */
55 static bfd_boolean xtensa_use_literal_pages = FALSE;
57 #define EXTRA_VALIDATION 0
61 elf_xtensa_choose_target (argc, argv)
62 int argc ATTRIBUTE_UNUSED;
63 char **argv ATTRIBUTE_UNUSED;
66 return "${BIG_OUTPUT_FORMAT}";
68 return "${LITTLE_OUTPUT_FORMAT}";
73 elf_xtensa_place_orphan (file, s)
74 lang_input_statement_type *file;
77 /* Early exit for relocatable links. */
78 if (link_info.relocateable)
81 return gld${EMULATION_NAME}_place_orphan (file, s);
86 elf_xtensa_before_parse ()
88 /* Just call the default hook.... Tensilica's version of this function
89 does some other work that isn't relevant here. */
90 gld${EMULATION_NAME}_before_parse ();
94 /* This is called after the sections have been attached to output
95 sections, but before any sizes or addresses have been set. */
98 elf_xtensa_before_allocation ()
101 bfd_boolean is_big_endian = XCHAL_HAVE_BE;
103 /* Check that the output endianness matches the Xtensa
104 configuration. The BFD library always includes both big and
105 little endian target vectors for Xtensa, but it only supports the
106 detailed instruction encode/decode operations (such as are
107 required to process relocations) for the selected Xtensa
110 if (is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
112 einfo (_("%F%P: little endian output does not match "
113 "Xtensa configuration\n"));
115 if (!is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
117 einfo (_("%F%P: big endian output does not match "
118 "Xtensa configuration\n"));
121 /* Check that the endianness for each input file matches the output.
122 The merge_private_bfd_data hook has already reported any mismatches
123 as errors, but those errors are not fatal. At this point, we
124 cannot go any further if there are any mismatches. */
126 for (in_bfd = link_info.input_bfds;
128 in_bfd = in_bfd->link_next)
130 if ((is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
131 || (!is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
132 einfo (_("%F%P: cross-endian linking not supported\n"));
135 /* Enable relaxation by default if the "--no-relax" option was not
136 specified. This is done here instead of in the before_parse hook
137 because there is a check in main() to prohibit use of --relax and
138 -r together and that combination should be allowed for Xtensa. */
140 if (!disable_relaxation)
141 command_line.relax = TRUE;
143 gld${EMULATION_NAME}_before_allocation ();
145 xtensa_wild_group_interleave (stat_ptr->head);
146 if (command_line.relax)
147 xtensa_colocate_output_literals (stat_ptr->head);
149 /* TBD: We need to force the page alignments to here and only do
150 them as needed for the entire output section. Finally, if this
151 is a relocateable link then we need to add alignment notes so
152 that the literals can be separated later. */
156 typedef struct wildcard_list section_name_list;
158 typedef struct reloc_deps_e_t reloc_deps_e;
159 typedef struct reloc_deps_section_t reloc_deps_section;
160 typedef struct reloc_deps_graph_t reloc_deps_graph;
163 struct reloc_deps_e_t
165 asection *src; /* Contains l32rs. */
166 asection *tgt; /* Contains literals. */
170 /* Place these in the userdata field. */
171 struct reloc_deps_section_t
175 bfd_boolean is_only_literal;
179 struct reloc_deps_graph_t
186 static void xtensa_layout_wild
187 PARAMS ((const reloc_deps_graph *, lang_wild_statement_type *));
189 typedef void (*deps_callback_t)
190 PARAMS ((asection *, /* src_sec */
191 bfd_vma, /* src_offset */
192 asection *, /* target_sec */
193 bfd_vma, /* target_offset */
196 static void build_deps_graph_callback
197 PARAMS ((asection *, bfd_vma, asection *, bfd_vma, PTR));
198 extern bfd_boolean xtensa_callback_required_dependence
199 PARAMS ((bfd *, asection *, struct bfd_link_info *,
200 deps_callback_t, PTR));
201 static void xtensa_ldlang_clear_addresses
202 PARAMS ((lang_statement_union_type *));
203 static bfd_boolean ld_local_file_relocations_fit
204 PARAMS ((lang_statement_union_type *, const reloc_deps_graph *));
205 static bfd_vma ld_assign_relative_paged_dot
206 PARAMS ((bfd_vma, lang_statement_union_type *,
207 const reloc_deps_graph *, bfd_boolean));
208 static bfd_vma ld_xtensa_insert_page_offsets
209 PARAMS ((bfd_vma, lang_statement_union_type *, reloc_deps_graph *,
211 static void lang_for_each_statement_worker
212 PARAMS ((void (*) (lang_statement_union_type *),
213 lang_statement_union_type *));
214 static void xtensa_move_dependencies_to_front
215 PARAMS ((reloc_deps_graph *, lang_wild_statement_type *));
216 static reloc_deps_graph *ld_build_required_section_dependence
217 PARAMS ((lang_statement_union_type *));
218 static bfd_boolean section_is_source
219 PARAMS ((const reloc_deps_graph *, lang_statement_union_type *));
220 static bfd_boolean section_is_target
221 PARAMS ((const reloc_deps_graph *, lang_statement_union_type *));
222 static bfd_boolean section_is_source_or_target
223 PARAMS ((const reloc_deps_graph *, lang_statement_union_type *));
224 static bfd_boolean deps_has_sec_edge
225 PARAMS ((const reloc_deps_graph *, asection *, asection *));
226 static bfd_boolean deps_has_edge
227 PARAMS ((const reloc_deps_graph *, lang_statement_union_type *,
228 lang_statement_union_type *));
229 static void add_deps_edge
230 PARAMS ((reloc_deps_graph *, asection *, asection *));
232 static size_t ld_count_children
233 PARAMS ((lang_statement_union_type *));
235 static void free_reloc_deps_graph
236 PARAMS ((reloc_deps_graph *));
237 static void xtensa_colocate_literals
238 PARAMS ((reloc_deps_graph *, lang_statement_union_type *));
239 static reloc_deps_section *xtensa_get_section_deps
240 PARAMS ((const reloc_deps_graph *, asection *));
241 static void xtensa_set_section_deps
242 PARAMS ((const reloc_deps_graph *, asection *, reloc_deps_section *));
243 static void xtensa_append_section_deps
244 PARAMS ((reloc_deps_graph *, asection *));
246 extern lang_statement_list_type constructor_list;
248 /* Begin verbatim code from ldlang.c:
249 the following are copied from ldlang.c because they are defined
253 lang_for_each_statement_worker (func, s)
254 void (*func) PARAMS ((lang_statement_union_type *));
255 lang_statement_union_type *s;
257 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
261 switch (s->header.type)
263 case lang_constructors_statement_enum:
264 lang_for_each_statement_worker (func, constructor_list.head);
266 case lang_output_section_statement_enum:
267 lang_for_each_statement_worker
269 s->output_section_statement.children.head);
271 case lang_wild_statement_enum:
272 lang_for_each_statement_worker
274 s->wild_statement.children.head);
276 case lang_group_statement_enum:
277 lang_for_each_statement_worker (func,
278 s->group_statement.children.head);
280 case lang_data_statement_enum:
281 case lang_reloc_statement_enum:
282 case lang_object_symbols_statement_enum:
283 case lang_output_statement_enum:
284 case lang_target_statement_enum:
285 case lang_input_section_enum:
286 case lang_input_statement_enum:
287 case lang_assignment_statement_enum:
288 case lang_padding_statement_enum:
289 case lang_address_statement_enum:
290 case lang_fill_statement_enum:
299 /* End of verbatim code from ldlang.c. */
303 xtensa_get_section_deps (deps, sec)
304 const reloc_deps_graph *deps ATTRIBUTE_UNUSED;
307 /* We have a separate function for this so that
308 we could in the future keep a completely independent
309 structure that maps a section to its dependence edges.
310 For now, we place these in the sec->userdata field. */
311 reloc_deps_section *sec_deps = (reloc_deps_section *) sec->userdata;
316 xtensa_set_section_deps (deps, sec, deps_section)
317 const reloc_deps_graph *deps ATTRIBUTE_UNUSED;
319 reloc_deps_section *deps_section;
321 sec->userdata = (void *) deps_section;
325 /* This is used to keep a list of all of the sections participating in
326 the graph so we can clean them up quickly. */
329 xtensa_append_section_deps (deps, sec)
330 reloc_deps_graph *deps;
333 if (deps->size <= deps->count)
335 asection **new_sections;
339 new_size = deps->size * 2;
343 new_sections = (asection**) xmalloc (sizeof (asection*) * new_size);
344 memset (new_sections, 0, sizeof (asection*) * new_size);
345 for (i = 0; i < deps->count; i++)
347 new_sections[i] = deps->sections[i];
349 if (deps->sections != NULL)
350 free (deps->sections);
351 deps->sections = new_sections;
352 deps->size = new_size;
354 deps->sections[deps->count] = sec;
360 free_reloc_deps_graph (deps)
361 reloc_deps_graph *deps;
364 for (i = 0; i < deps->count; i++)
366 asection *sec = deps->sections[i];
367 reloc_deps_section *sec_deps;
368 sec_deps = xtensa_get_section_deps (deps, sec);
372 while (sec_deps->succs != NULL)
374 next = sec_deps->succs->next;
375 free (sec_deps->succs);
376 sec_deps->succs = next;
379 while (sec_deps->preds != NULL)
381 next = sec_deps->preds->next;
382 free (sec_deps->preds);
383 sec_deps->preds = next;
387 xtensa_set_section_deps (deps, sec, NULL);
390 free (deps->sections);
397 section_is_source (deps, s)
398 const reloc_deps_graph *deps ATTRIBUTE_UNUSED;
399 lang_statement_union_type *s;
402 const reloc_deps_section *sec_deps;
404 if (s->header.type != lang_input_section_enum)
406 sec = s->input_section.section;
408 sec_deps = xtensa_get_section_deps (deps, sec);
409 return (sec_deps && sec_deps->succs != NULL);
414 section_is_target (deps, s)
415 const reloc_deps_graph *deps ATTRIBUTE_UNUSED;
416 lang_statement_union_type *s;
419 const reloc_deps_section *sec_deps;
421 if (s->header.type != lang_input_section_enum)
423 sec = s->input_section.section;
425 sec_deps = xtensa_get_section_deps (deps, sec);
426 return (sec_deps && sec_deps->preds != NULL);
430 section_is_source_or_target (deps, s)
431 const reloc_deps_graph *deps ATTRIBUTE_UNUSED;
432 lang_statement_union_type *s;
434 return (section_is_source (deps, s)
435 || section_is_target (deps, s));
439 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
440 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
442 struct xtensa_ld_iter_t
444 lang_statement_union_type *parent; /* Parent of the list. */
445 lang_statement_list_type *l; /* List that holds it. */
446 lang_statement_union_type **loc; /* Place in the list. */
449 struct xtensa_ld_iter_stack_t
451 xtensa_ld_iter iterloc; /* List that hold it. */
453 xtensa_ld_iter_stack *next; /* Next in the stack. */
454 xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
457 static void ld_xtensa_move_section_after
458 PARAMS ((xtensa_ld_iter *, xtensa_ld_iter *));
462 ld_xtensa_move_section_after (to, current)
464 xtensa_ld_iter *current;
466 lang_statement_union_type *to_next;
467 lang_statement_union_type *current_next;
468 lang_statement_union_type **e;
471 size_t old_to_count, new_to_count;
472 size_t old_current_count, new_current_count;
479 old_to_count = ld_count_children (to->parent);
480 old_current_count = ld_count_children (current->parent);
483 to_next = *(to->loc);
484 current_next = (*current->loc)->header.next;
486 *(to->loc) = *(current->loc);
488 *(current->loc) = current_next;
489 (*(to->loc))->header.next = to_next;
491 /* reset "to" list tail */
492 for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
496 /* reset "current" list tail */
497 for (e = ¤t->l->head; *e != NULL; e = &(*e)->header.next)
499 current->l->tail = e;
502 new_to_count = ld_count_children (to->parent);
503 new_current_count = ld_count_children (current->parent);
505 ASSERT ((old_to_count + old_current_count)
506 == (new_to_count + new_current_count));
511 /* Can only be called with lang_statements that have lists. Returns
512 false if the list is empty. */
514 static bfd_boolean iter_stack_empty
515 PARAMS ((xtensa_ld_iter_stack **));
516 static bfd_boolean iter_stack_push
517 PARAMS ((xtensa_ld_iter_stack **, lang_statement_union_type *));
518 static void iter_stack_pop
519 PARAMS ((xtensa_ld_iter_stack **));
520 static void iter_stack_update
521 PARAMS ((xtensa_ld_iter_stack **));
522 static void iter_stack_next
523 PARAMS ((xtensa_ld_iter_stack **));
524 static lang_statement_union_type *iter_stack_current
525 PARAMS ((xtensa_ld_iter_stack **));
526 static void iter_stack_create
527 PARAMS ((xtensa_ld_iter_stack **, lang_statement_union_type *));
528 static void iter_stack_copy_current
529 PARAMS ((xtensa_ld_iter_stack **, xtensa_ld_iter *));
533 iter_stack_empty (stack_p)
534 xtensa_ld_iter_stack **stack_p;
536 return (*stack_p == NULL);
541 iter_stack_push (stack_p, parent)
542 xtensa_ld_iter_stack **stack_p;
543 lang_statement_union_type *parent;
545 xtensa_ld_iter_stack *stack;
546 lang_statement_list_type *l = NULL;
548 switch (parent->header.type)
550 case lang_output_section_statement_enum:
551 l = &parent->output_section_statement.children;
553 case lang_wild_statement_enum:
554 l = &parent->wild_statement.children;
556 case lang_group_statement_enum:
557 l = &parent->group_statement.children;
564 /* Empty. do not push. */
565 if (l->tail == &l->head)
568 stack = (xtensa_ld_iter_stack *) xmalloc (sizeof (xtensa_ld_iter_stack));
569 memset (stack, 0, sizeof (xtensa_ld_iter_stack));
570 stack->iterloc.parent = parent;
571 stack->iterloc.l = l;
572 stack->iterloc.loc = &l->head;
574 stack->next = *stack_p;
576 if (*stack_p != NULL)
577 (*stack_p)->prev = stack;
584 iter_stack_pop (stack_p)
585 xtensa_ld_iter_stack **stack_p;
587 xtensa_ld_iter_stack *stack;
593 ASSERT (stack != NULL);
597 if (stack->next != NULL)
598 stack->next->prev = NULL;
600 *stack_p = stack->next;
605 /* This MUST be called if, during iteration, the user changes the
606 underlying structure. It will check for a NULL current and advance
610 iter_stack_update (stack_p)
611 xtensa_ld_iter_stack **stack_p;
613 if (!iter_stack_empty (stack_p)
614 && (*(*stack_p)->iterloc.loc) == NULL)
616 iter_stack_pop (stack_p);
618 while (!iter_stack_empty (stack_p)
619 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
621 iter_stack_pop (stack_p);
623 if (!iter_stack_empty (stack_p))
624 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
630 iter_stack_next (stack_p)
631 xtensa_ld_iter_stack **stack_p;
633 xtensa_ld_iter_stack *stack;
634 lang_statement_union_type *current;
637 current = *stack->iterloc.loc;
638 /* If we are on the first element. */
641 switch (current->header.type)
643 case lang_output_section_statement_enum:
644 case lang_wild_statement_enum:
645 case lang_group_statement_enum:
646 /* If the list if not empty, we are done. */
647 if (iter_stack_push (stack_p, *stack->iterloc.loc))
649 /* Otherwise increment the pointer as normal. */
656 while (!iter_stack_empty (stack_p)
657 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
659 iter_stack_pop (stack_p);
661 if (!iter_stack_empty (stack_p))
662 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
666 static lang_statement_union_type *
667 iter_stack_current (stack_p)
668 xtensa_ld_iter_stack **stack_p;
670 return *((*stack_p)->iterloc.loc);
674 /* The iter stack is a preorder. */
677 iter_stack_create (stack_p, parent)
678 xtensa_ld_iter_stack **stack_p;
679 lang_statement_union_type *parent;
681 iter_stack_push (stack_p, parent);
686 iter_stack_copy_current (stack_p, front)
687 xtensa_ld_iter_stack **stack_p;
688 xtensa_ld_iter *front;
690 *front = (*stack_p)->iterloc;
695 xtensa_colocate_literals (deps, statement)
696 reloc_deps_graph *deps;
697 lang_statement_union_type *statement;
699 /* Keep a stack of pointers to control iteration through the contours. */
700 xtensa_ld_iter_stack *stack = NULL;
701 xtensa_ld_iter_stack **stack_p = &stack;
703 xtensa_ld_iter front; /* Location where new insertion should occur. */
704 xtensa_ld_iter *front_p = NULL;
706 xtensa_ld_iter current; /* Location we are checking. */
707 xtensa_ld_iter *current_p = NULL;
708 bfd_boolean in_literals = FALSE;
710 if (deps->count == 0)
714 ld_assign_relative_paged_dot (0x100000, statement, deps,
715 xtensa_use_literal_pages);
717 if (!ld_local_file_relocations_fit (statement, deps))
718 fprintf (stderr, "initial relocation placement does not fit\n");
720 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
723 iter_stack_create (stack_p, statement);
725 while (!iter_stack_empty (stack_p))
727 bfd_boolean skip_increment = FALSE;
728 lang_statement_union_type *l = iter_stack_current (stack_p);
730 switch (l->header.type)
732 case lang_assignment_statement_enum:
733 /* Any assignment statement should block reordering across it. */
738 case lang_input_section_enum:
741 in_literals = (section_is_target (deps, l)
742 && !section_is_source (deps, l));
746 iter_stack_copy_current (stack_p, front_p);
751 bfd_boolean is_target;
752 current_p = ¤t;
753 iter_stack_copy_current (stack_p, current_p);
754 is_target = (section_is_target (deps, l)
755 && !section_is_source (deps, l));
759 iter_stack_copy_current (stack_p, front_p);
767 /* Try to insert in place. */
768 ld_xtensa_move_section_after (front_p, current_p);
769 ld_assign_relative_paged_dot (0x100000,
772 xtensa_use_literal_pages);
774 /* We use this code because it's already written. */
775 if (!ld_local_file_relocations_fit (statement, deps))
778 ld_xtensa_move_section_after (current_p, front_p);
779 /* Reset the literal placement. */
780 iter_stack_copy_current (stack_p, front_p);
784 /* Move front pointer up by one. */
785 front_p->loc = &(*front_p->loc)->header.next;
787 /* Do not increment the current pointer. */
788 skip_increment = TRUE;
799 iter_stack_next (stack_p);
801 /* Be careful to update the stack_p if it now is a null. */
802 iter_stack_update (stack_p);
805 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
810 xtensa_move_dependencies_to_front (deps, w)
811 reloc_deps_graph *deps;
812 lang_wild_statement_type *w;
814 /* Keep a front pointer and a current pointer. */
815 lang_statement_union_type **front;
816 lang_statement_union_type **current;
818 /* Walk to the end of the targets. */
819 for (front = &w->children.head;
820 (*front != NULL) && section_is_source_or_target (deps, *front);
821 front = &(*front)->header.next)
827 current = &(*front)->header.next;
828 while (*current != NULL)
830 if (section_is_source_or_target (deps, *current))
832 /* Insert in place. */
833 xtensa_ld_iter front_iter;
834 xtensa_ld_iter current_iter;
836 front_iter.parent = (lang_statement_union_type *) w;
837 front_iter.l = &w->children;
838 front_iter.loc = front;
840 current_iter.parent = (lang_statement_union_type *) w;
841 current_iter.l = &w->children;
842 current_iter.loc = current;
844 ld_xtensa_move_section_after (&front_iter, ¤t_iter);
845 front = &(*front)->header.next;
849 current = &(*current)->header.next;
856 deps_has_sec_edge (deps, src, tgt)
857 const reloc_deps_graph *deps;
861 const reloc_deps_section *sec_deps;
862 const reloc_deps_e *sec_deps_e;
864 sec_deps = xtensa_get_section_deps (deps, src);
865 if (sec_deps == NULL)
868 for (sec_deps_e = sec_deps->succs;
870 sec_deps_e = sec_deps_e->next)
872 ASSERT (sec_deps_e->src == src);
873 if (sec_deps_e->tgt == tgt)
881 deps_has_edge (deps, src, tgt)
882 const reloc_deps_graph *deps;
883 lang_statement_union_type *src;
884 lang_statement_union_type *tgt;
886 if (!section_is_source (deps, src))
888 if (!section_is_target (deps, tgt))
891 if (src->header.type != lang_input_section_enum)
893 if (tgt->header.type != lang_input_section_enum)
896 return deps_has_sec_edge (deps, src->input_section.section,
897 tgt->input_section.section);
902 add_deps_edge (deps, src_sec, tgt_sec)
903 reloc_deps_graph *deps;
907 reloc_deps_section *src_sec_deps;
908 reloc_deps_section *tgt_sec_deps;
910 reloc_deps_e *src_edge;
911 reloc_deps_e *tgt_edge;
913 if (deps_has_sec_edge (deps, src_sec, tgt_sec))
916 src_sec_deps = xtensa_get_section_deps (deps, src_sec);
917 if (src_sec_deps == NULL)
920 src_sec_deps = (reloc_deps_section *)
921 xmalloc (sizeof (reloc_deps_section));
922 memset (src_sec_deps, 0, sizeof (reloc_deps_section));
923 src_sec_deps->is_only_literal = 0;
924 src_sec_deps->preds = NULL;
925 src_sec_deps->succs = NULL;
926 xtensa_set_section_deps (deps, src_sec, src_sec_deps);
927 xtensa_append_section_deps (deps, src_sec);
930 tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
931 if (tgt_sec_deps == NULL)
934 tgt_sec_deps = (reloc_deps_section *)
935 xmalloc (sizeof (reloc_deps_section));
936 memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
937 tgt_sec_deps->is_only_literal = 0;
938 tgt_sec_deps->preds = NULL;
939 tgt_sec_deps->succs = NULL;
940 xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
941 xtensa_append_section_deps (deps, tgt_sec);
945 src_edge = (reloc_deps_e *) xmalloc (sizeof (reloc_deps_e));
946 memset (src_edge, 0, sizeof (reloc_deps_e));
947 src_edge->src = src_sec;
948 src_edge->tgt = tgt_sec;
949 src_edge->next = src_sec_deps->succs;
950 src_sec_deps->succs = src_edge;
952 tgt_edge = (reloc_deps_e *) xmalloc (sizeof (reloc_deps_e));
953 memset (tgt_edge, 0, sizeof (reloc_deps_e));
954 tgt_edge->src = src_sec;
955 tgt_edge->tgt = tgt_sec;
956 tgt_edge->next = tgt_sec_deps->preds;
957 tgt_sec_deps->preds = tgt_edge;
962 build_deps_graph_callback (src_sec, src_offset,
963 target_sec, target_offset, closure)
965 bfd_vma src_offset ATTRIBUTE_UNUSED;
966 asection *target_sec;
967 bfd_vma target_offset ATTRIBUTE_UNUSED;
970 reloc_deps_graph *deps;
971 deps = (reloc_deps_graph*) closure;
973 /* If the target is defined. */
974 if (target_sec != NULL)
975 add_deps_edge (deps, src_sec, target_sec);
980 ld_build_required_section_dependence (s)
981 lang_statement_union_type *s;
983 reloc_deps_graph *deps;
984 xtensa_ld_iter_stack *stack = NULL;
986 deps = (reloc_deps_graph*) xmalloc (sizeof (reloc_deps_graph));
987 deps->sections = NULL;
991 for (iter_stack_create (&stack, s);
992 !iter_stack_empty (&stack);
993 iter_stack_next (&stack))
995 lang_statement_union_type *l = iter_stack_current (&stack);
997 if (l->header.type == lang_input_section_enum)
999 lang_input_section_type *input;
1000 input = &l->input_section;
1001 xtensa_callback_required_dependence (input->ifile->the_bfd,
1004 /* Use the same closure. */
1005 build_deps_graph_callback,
1013 #if EXTRA_VALIDATION
1015 ld_count_children (s)
1016 lang_statement_union_type *s;
1019 xtensa_ld_iter_stack *stack = NULL;
1020 for (iter_stack_create (&stack, s);
1021 !iter_stack_empty (&stack);
1022 iter_stack_next (&stack))
1024 lang_statement_union_type *l = iter_stack_current (&stack);
1030 #endif /* EXTRA_VALIDATION */
1034 xtensa_wild_group_interleave_callback (statement)
1035 lang_statement_union_type * statement;
1037 lang_wild_statement_type *w;
1038 reloc_deps_graph *deps;
1039 if (statement->header.type == lang_wild_statement_enum)
1041 #if EXTRA_VALIDATION
1042 size_t old_child_count;
1043 size_t new_child_count;
1045 bfd_boolean no_reorder;
1047 w = &statement->wild_statement;
1051 /* If it has 0 or 1 section bound, then do not reorder. */
1052 if (w->children.head == NULL
1053 || (w->children.head->header.type == lang_input_section_enum
1054 && w->children.head->header.next == NULL))
1057 if (w->filenames_sorted)
1060 /* Check for sorting in a section list wildcard spec as well. */
1063 struct wildcard_list *l;
1064 for (l = w->section_list; l != NULL; l = l->next)
1066 if (l->spec.sorted == TRUE)
1074 /* Special case until the NOREORDER linker directive is supported:
1075 *(.init) output sections and *(.fini) specs may NOT be reordered. */
1077 /* Check for sorting in a section list wildcard spec as well. */
1080 struct wildcard_list *l;
1081 for (l = w->section_list; l != NULL; l = l->next)
1084 && ((strcmp (".init", l->spec.name) == 0)
1085 || (strcmp (".fini", l->spec.name) == 0)))
1093 #if EXTRA_VALIDATION
1094 old_child_count = ld_count_children (statement);
1097 /* It is now officially a target. Build the graph of source
1098 section -> target section (kept as a list of edges). */
1099 deps = ld_build_required_section_dependence (statement);
1101 /* If this wildcard does not reorder.... */
1102 if (!no_reorder && deps->count != 0)
1104 /* First check for reverse dependences. Fix if possible. */
1105 xtensa_layout_wild (deps, w);
1107 xtensa_move_dependencies_to_front (deps, w);
1108 #if EXTRA_VALIDATION
1109 new_child_count = ld_count_children (statement);
1110 ASSERT (new_child_count == old_child_count);
1113 xtensa_colocate_literals (deps, statement);
1115 #if EXTRA_VALIDATION
1116 new_child_count = ld_count_children (statement);
1117 ASSERT (new_child_count == old_child_count);
1122 free_reloc_deps_graph (deps);
1128 xtensa_wild_group_interleave (s)
1129 lang_statement_union_type *s;
1131 lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1136 xtensa_layout_wild (deps, w)
1137 const reloc_deps_graph *deps;
1138 lang_wild_statement_type *w;
1140 /* If it does not fit initially, we need to do this step. Move all
1141 of the wild literal sections to a new list, then move each of
1142 them back in just before the first section they depend on. */
1143 lang_statement_union_type **s_p;
1144 #if EXTRA_VALIDATION
1145 size_t old_count, new_count;
1149 lang_wild_statement_type literal_wild;
1150 literal_wild.header.next = NULL;
1151 literal_wild.header.type = lang_wild_statement_enum;
1152 literal_wild.filename = NULL;
1153 literal_wild.filenames_sorted = FALSE;
1154 literal_wild.section_list = NULL;
1155 literal_wild.keep_sections = FALSE;
1156 literal_wild.children.head = NULL;
1157 literal_wild.children.tail = &literal_wild.children.head;
1159 #if EXTRA_VALIDATION
1160 old_count = ld_count_children ((lang_statement_union_type*) w);
1163 s_p = &w->children.head;
1164 while (*s_p != NULL)
1166 lang_statement_union_type *l = *s_p;
1167 if (l->header.type == lang_input_section_enum)
1169 if (section_is_target (deps, l)
1170 && ! section_is_source (deps, l))
1173 *s_p = l->header.next;
1175 w->children.tail = s_p;
1176 l->header.next = NULL;
1179 *literal_wild.children.tail = l;
1180 literal_wild.children.tail = &l->header.next;
1184 s_p = &(*s_p)->header.next;
1187 #if EXTRA_VALIDATION
1188 ct1 = ld_count_children ((lang_statement_union_type*) w);
1189 ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1191 ASSERT (old_count == (ct1 + ct2));
1194 /* Now place them back in front of their dependent sections. */
1196 while (literal_wild.children.head != NULL)
1198 lang_statement_union_type *lit = literal_wild.children.head;
1199 bfd_boolean placed = FALSE;
1201 #if EXTRA_VALIDATION
1207 literal_wild.children.head = lit->header.next;
1208 if (literal_wild.children.head == NULL)
1209 literal_wild.children.tail = &literal_wild.children.head;
1210 lit->header.next = NULL;
1212 /* Find a spot to place it. */
1213 for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1215 lang_statement_union_type *src = *s_p;
1216 if (deps_has_edge (deps, src, lit))
1218 /* Place it here. */
1219 lit->header.next = *s_p;
1228 /* Put it at the end. */
1229 *w->children.tail = lit;
1230 w->children.tail = &lit->header.next;
1234 #if EXTRA_VALIDATION
1235 new_count = ld_count_children ((lang_statement_union_type*) w);
1236 ASSERT (new_count == old_count);
1242 xtensa_colocate_output_literals_callback (statement)
1243 lang_statement_union_type * statement;
1245 lang_output_section_statement_type *os;
1246 reloc_deps_graph *deps;
1247 if (statement->header.type == lang_output_section_statement_enum)
1249 /* Now, we walk over the contours of the output section statement.
1251 First we build the literal section dependences as before.
1253 At the first uniquely_literal section, we mark it as a good
1254 spot to place other literals. Continue walking (and counting
1255 sizes) until we find the next literal section. If this
1256 section can be moved to the first one, then we move it. If
1257 we every find a modification of ".", start over. If we find
1258 a labeling of the current location, start over. Finally, at
1259 the end, if we require page alignment, add page alignments. */
1261 #if EXTRA_VALIDATION
1262 size_t old_child_count;
1263 size_t new_child_count;
1265 bfd_boolean no_reorder = FALSE;
1267 os = &statement->output_section_statement;
1269 #if EXTRA_VALIDATION
1270 old_child_count = ld_count_children (statement);
1273 /* It is now officially a target. Build the graph of source
1274 section -> target section (kept as a list of edges). */
1276 deps = ld_build_required_section_dependence (statement);
1278 /* If this wildcard does not reorder.... */
1281 /* First check for reverse dependences. Fix if possible. */
1282 xtensa_colocate_literals (deps, statement);
1284 #if EXTRA_VALIDATION
1285 new_child_count = ld_count_children (statement);
1286 ASSERT (new_child_count == old_child_count);
1290 /* Insert align/offset assignment statement. */
1291 if (xtensa_use_literal_pages)
1293 ld_xtensa_insert_page_offsets ((bfd_vma) 0, statement, deps,
1294 xtensa_use_literal_pages);
1295 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1300 free_reloc_deps_graph (deps);
1306 xtensa_colocate_output_literals (s)
1307 lang_statement_union_type *s;
1309 lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1314 xtensa_ldlang_clear_addresses (statement)
1315 lang_statement_union_type * statement;
1317 switch (statement->header.type)
1319 case lang_input_section_enum:
1321 asection *bfd_section = statement->input_section.section;
1322 bfd_section->output_offset = 0;
1332 ld_assign_relative_paged_dot (dot, s, deps, lit_align)
1334 lang_statement_union_type *s;
1335 const reloc_deps_graph *deps ATTRIBUTE_UNUSED;
1336 bfd_boolean lit_align;
1338 /* Walk through all of the input statements in this wild statement
1339 assign dot to all of them. */
1341 xtensa_ld_iter_stack *stack = NULL;
1342 xtensa_ld_iter_stack **stack_p = &stack;
1344 bfd_boolean first_section = FALSE;
1345 bfd_boolean in_literals = FALSE;
1347 for (iter_stack_create (stack_p, s);
1348 !iter_stack_empty (stack_p);
1349 iter_stack_next (stack_p))
1351 lang_statement_union_type *l = iter_stack_current (stack_p);
1353 switch (l->header.type)
1355 case lang_input_section_enum:
1357 asection *section = l->input_section.section;
1358 size_t align_pow = section->alignment_power;
1359 bfd_boolean do_xtensa_alignment = FALSE;
1363 bfd_boolean sec_is_target = section_is_target (deps, l);
1364 bfd_boolean sec_is_source = section_is_source (deps, l);
1366 if (section->_raw_size != 0
1368 || (in_literals && !sec_is_target)
1369 || (!in_literals && sec_is_target)))
1371 do_xtensa_alignment = TRUE;
1373 first_section = FALSE;
1374 if (section->_raw_size != 0)
1375 in_literals = (sec_is_target && !sec_is_source);
1378 if (do_xtensa_alignment && xtensa_page_power != 0)
1379 dot += (1 << xtensa_page_power);
1381 dot = align_power (dot, align_pow);
1382 section->output_offset = dot;
1383 dot += section->_raw_size;
1386 case lang_fill_statement_enum:
1387 dot += l->fill_statement.size;
1389 case lang_padding_statement_enum:
1390 dot += l->padding_statement.size;
1401 ld_local_file_relocations_fit (statement, deps)
1402 lang_statement_union_type *statement;
1403 const reloc_deps_graph *deps ATTRIBUTE_UNUSED;
1405 /* Walk over all of the dependencies that we identified and make
1406 sure that IF the source and target are here (addr != 0):
1407 1) target addr < source addr
1408 2) (roundup(source + source_size, 4) - rounddown(target, 4))
1409 < (256K - (1 << bad align))
1410 Need a worst-case proof.... */
1412 xtensa_ld_iter_stack *stack = NULL;
1413 xtensa_ld_iter_stack **stack_p = &stack;
1414 size_t max_align_power = 0;
1415 size_t align_penalty = 256;
1419 /* Find the worst-case alignment requirement for this set of statements. */
1420 for (iter_stack_create (stack_p, statement);
1421 !iter_stack_empty (stack_p);
1422 iter_stack_next (stack_p))
1424 lang_statement_union_type *l = iter_stack_current (stack_p);
1425 if (l->header.type == lang_input_section_enum)
1427 lang_input_section_type *input = &l->input_section;
1428 asection *section = input->section;
1429 if (section->alignment_power > max_align_power)
1430 max_align_power = section->alignment_power;
1434 /* Now check that everything fits. */
1435 for (i = 0; i < deps->count; i++)
1437 asection *sec = deps->sections[i];
1438 const reloc_deps_section *deps_section =
1439 xtensa_get_section_deps (deps, sec);
1442 /* We choose to walk through the successors. */
1443 for (e = deps_section->succs; e != NULL; e = e->next)
1445 if ((e->src != e->tgt)
1446 && e->src->output_section == e->tgt->output_section
1447 && e->src->output_offset != 0
1448 && e->tgt->output_offset != 0)
1451 align_power (e->src->output_offset + e->src->_raw_size, 2);
1452 bfd_vma target_addr = e->tgt->output_offset & (~3);
1453 if (l32r_addr < target_addr)
1455 fprintf (stderr, "Warning: "
1456 "l32r target section before l32r\n");
1460 if ((l32r_addr - target_addr) > (256*1024 - align_penalty))
1472 ld_xtensa_insert_page_offsets (dot, s, deps, lit_align)
1474 lang_statement_union_type *s;
1475 reloc_deps_graph *deps;
1476 bfd_boolean lit_align;
1478 xtensa_ld_iter_stack *stack = NULL;
1479 xtensa_ld_iter_stack **stack_p = &stack;
1481 bfd_boolean first_section = FALSE;
1482 bfd_boolean in_literals = FALSE;
1487 for (iter_stack_create (stack_p, s);
1488 !iter_stack_empty (stack_p);
1489 iter_stack_next (stack_p))
1491 lang_statement_union_type *l = iter_stack_current (stack_p);
1493 switch (l->header.type)
1495 case lang_input_section_enum:
1497 asection *section = l->input_section.section;
1498 bfd_boolean do_xtensa_alignment = FALSE;
1502 if (section->_raw_size != 0
1504 || (in_literals && !section_is_target (deps, l))
1505 || (!in_literals && section_is_target (deps, l))))
1507 do_xtensa_alignment = TRUE;
1509 first_section = FALSE;
1510 if (section->_raw_size != 0)
1512 in_literals = (section_is_target (deps, l)
1513 && !section_is_source (deps, l));
1517 if (do_xtensa_alignment && xtensa_page_power != 0)
1519 /* Create an expression that increments the current address,
1520 i.e., "dot", by (1 << xtensa_align_power). */
1521 etree_type *name_op = exp_nameop (NAME, ".");
1522 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1523 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1524 etree_type *assign_op = exp_assop ('=', ".", add_op);
1526 lang_assignment_statement_type *assign_stmt;
1527 lang_statement_union_type *assign_union;
1528 lang_statement_list_type tmplist;
1529 lang_statement_list_type *old_stat_ptr = stat_ptr;
1531 /* There is hidden state in "lang_add_assignment". It
1532 appends the new assignment statement to the stat_ptr
1533 list. Thus, we swap it before and after the call. */
1535 tmplist.head = NULL;
1536 tmplist.tail = &tmplist.head;
1538 stat_ptr = &tmplist;
1539 /* Warning: side effect; statement appended to stat_ptr. */
1540 assign_stmt = lang_add_assignment (assign_op);
1541 assign_union = (lang_statement_union_type *) assign_stmt;
1542 stat_ptr = old_stat_ptr;
1544 assign_union->header.next = l;
1545 *(*stack_p)->iterloc.loc = assign_union;
1546 iter_stack_next (stack_p);
1559 # Define some shell vars to insert bits of code into the standard elf
1560 # parse_args and list_options functions.
1562 PARSE_AND_LIST_PROLOGUE='
1563 #define OPTION_NO_RELAX 301
1566 PARSE_AND_LIST_LONGOPTS='
1567 { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1570 PARSE_AND_LIST_OPTIONS='
1571 fprintf (file, _(" --no-relax\t\tDo not relax branches or coalesce literals\n"));
1574 PARSE_AND_LIST_ARGS_CASES='
1575 case OPTION_NO_RELAX:
1576 disable_relaxation = TRUE;
1580 # Replace some of the standard ELF functions with our own versions.
1582 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
1583 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
1584 LDEMUL_PLACE_ORPHAN=elf_xtensa_place_orphan
1585 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation