1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2003, 2004, 2005
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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>
28 #include "../bfd/elf-bfd.h"
29 #include "../bfd/libbfd.h"
30 #include "elf/xtensa.h"
33 static void xtensa_wild_group_interleave (lang_statement_union_type *);
34 static void xtensa_colocate_output_literals (lang_statement_union_type *);
37 /* Flag for the emulation-specific "--no-relax" option. */
38 static bfd_boolean disable_relaxation = FALSE;
40 /* This number is irrelevant until we turn on use_literal_pages */
41 static bfd_vma xtensa_page_power = 12; /* 4K pages. */
43 /* To force a page break between literals and text, change
44 xtensa_use_literal_pages to "TRUE". */
45 static bfd_boolean xtensa_use_literal_pages = FALSE;
47 #define EXTRA_VALIDATION 0
51 elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
52 char **argv ATTRIBUTE_UNUSED)
55 return "${BIG_OUTPUT_FORMAT}";
57 return "${LITTLE_OUTPUT_FORMAT}";
62 elf_xtensa_before_parse (void)
64 /* Just call the default hook.... Tensilica's version of this function
65 does some other work that isn't relevant here. */
66 gld${EMULATION_NAME}_before_parse ();
71 remove_section (bfd *abfd, asection *os)
74 for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
78 os->owner->section_count--;
85 replace_insn_sec_with_prop_sec (bfd *abfd,
86 const char *insn_sec_name,
87 const char *prop_sec_name,
92 bfd_byte *prop_contents = NULL;
93 bfd_byte *insn_contents = NULL;
96 Elf_Internal_Shdr *symtab_hdr;
97 Elf_Internal_Rela *internal_relocs = NULL;
101 insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
102 if (insn_sec == NULL)
104 entry_count = insn_sec->size / 8;
106 prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
107 if (prop_sec != NULL && insn_sec != NULL)
109 *error_message = _("file already has property tables");
113 if (insn_sec->size != 0)
115 insn_contents = (bfd_byte *) bfd_malloc (insn_sec->size);
116 if (insn_contents == NULL)
118 *error_message = _("out of memory");
121 if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
122 (file_ptr) 0, insn_sec->size))
124 *error_message = _("failed to read section contents");
129 /* Create a Property table section and relocation section for it. */
130 prop_sec_name = strdup (prop_sec_name);
131 prop_sec = bfd_make_section (abfd, prop_sec_name);
133 || ! bfd_set_section_flags (abfd, prop_sec,
134 bfd_get_section_flags (abfd, insn_sec))
135 || ! bfd_set_section_alignment (abfd, prop_sec, 2))
137 *error_message = _("could not create new section");
141 if (! bfd_set_section_flags (abfd, prop_sec,
142 bfd_get_section_flags (abfd, insn_sec))
143 || ! bfd_set_section_alignment (abfd, prop_sec, 2))
145 *error_message = _("could not set new section properties");
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_INSN_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 (strncmp (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME,
269 strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME)) == 0)
271 insn_sec_name = sec_name;
272 owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
273 prop_sec_name = owned_prop_sec_name;
274 strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
275 strcat (prop_sec_name,
276 sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
278 if (insn_sec_name != NULL)
280 if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
283 einfo (_("%P: warning: failed to convert %s table in %B (%s); subsequent disassembly may be incomplete\n"),
284 insn_sec_name, abfd, message);
287 if (owned_prop_sec_name)
288 free (owned_prop_sec_name);
292 /* This is called after all input sections have been opened to convert
293 instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
294 tables (.xt.prop) before any section placement. */
297 elf_xtensa_after_open (void)
301 /* First call the ELF version. */
302 gld${EMULATION_NAME}_after_open ();
304 /* Now search the input files looking for instruction table sections. */
305 for (abfd = link_info.input_bfds;
307 abfd = abfd->link_next)
309 asection *sec = abfd->sections;
312 /* Do not use bfd_map_over_sections here since we are removing
313 sections as we iterate. */
316 next_sec = sec->next;
317 replace_instruction_table_sections (abfd, sec);
324 /* This is called after the sections have been attached to output
325 sections, but before any sizes or addresses have been set. */
328 elf_xtensa_before_allocation (void)
331 bfd_boolean is_big_endian = XCHAL_HAVE_BE;
333 /* Check that the output endianness matches the Xtensa
334 configuration. The BFD library always includes both big and
335 little endian target vectors for Xtensa, but it only supports the
336 detailed instruction encode/decode operations (such as are
337 required to process relocations) for the selected Xtensa
340 if (is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
342 einfo (_("%F%P: little endian output does not match "
343 "Xtensa configuration\n"));
345 if (!is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
347 einfo (_("%F%P: big endian output does not match "
348 "Xtensa configuration\n"));
351 /* Check that the endianness for each input file matches the output.
352 The merge_private_bfd_data hook has already reported any mismatches
353 as errors, but those errors are not fatal. At this point, we
354 cannot go any further if there are any mismatches. */
356 for (in_bfd = link_info.input_bfds;
358 in_bfd = in_bfd->link_next)
360 if ((is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
361 || (!is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
362 einfo (_("%F%P: cross-endian linking not supported\n"));
365 /* Enable relaxation by default if the "--no-relax" option was not
366 specified. This is done here instead of in the before_parse hook
367 because there is a check in main() to prohibit use of --relax and
368 -r together and that combination should be allowed for Xtensa. */
370 if (!disable_relaxation)
371 command_line.relax = TRUE;
373 gld${EMULATION_NAME}_before_allocation ();
375 xtensa_wild_group_interleave (stat_ptr->head);
376 if (command_line.relax)
377 xtensa_colocate_output_literals (stat_ptr->head);
379 /* TBD: We need to force the page alignments to here and only do
380 them as needed for the entire output section. Finally, if this
381 is a relocatable link then we need to add alignment notes so
382 that the literals can be separated later. */
386 typedef struct wildcard_list section_name_list;
388 typedef struct reloc_deps_e_t reloc_deps_e;
389 typedef struct reloc_deps_section_t reloc_deps_section;
390 typedef struct reloc_deps_graph_t reloc_deps_graph;
393 struct reloc_deps_e_t
395 asection *src; /* Contains l32rs. */
396 asection *tgt; /* Contains literals. */
400 /* Place these in the userdata field. */
401 struct reloc_deps_section_t
405 bfd_boolean is_only_literal;
409 struct reloc_deps_graph_t
416 static void xtensa_layout_wild
417 (const reloc_deps_graph *, lang_wild_statement_type *);
419 typedef void (*deps_callback_t) (asection *, /* src_sec */
420 bfd_vma, /* src_offset */
421 asection *, /* target_sec */
422 bfd_vma, /* target_offset */
423 void *); /* closure */
425 extern bfd_boolean xtensa_callback_required_dependence
426 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
427 static void xtensa_ldlang_clear_addresses (lang_statement_union_type *);
428 static bfd_boolean ld_local_file_relocations_fit
429 (lang_statement_union_type *, const reloc_deps_graph *);
430 static bfd_vma ld_assign_relative_paged_dot
431 (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
433 static bfd_vma ld_xtensa_insert_page_offsets
434 (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
436 static size_t ld_count_children (lang_statement_union_type *);
439 extern lang_statement_list_type constructor_list;
441 /* Begin verbatim code from ldlang.c:
442 the following are copied from ldlang.c because they are defined
446 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
447 lang_statement_union_type *s)
449 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
453 switch (s->header.type)
455 case lang_constructors_statement_enum:
456 lang_for_each_statement_worker (func, constructor_list.head);
458 case lang_output_section_statement_enum:
459 lang_for_each_statement_worker
461 s->output_section_statement.children.head);
463 case lang_wild_statement_enum:
464 lang_for_each_statement_worker
466 s->wild_statement.children.head);
468 case lang_group_statement_enum:
469 lang_for_each_statement_worker (func,
470 s->group_statement.children.head);
472 case lang_data_statement_enum:
473 case lang_reloc_statement_enum:
474 case lang_object_symbols_statement_enum:
475 case lang_output_statement_enum:
476 case lang_target_statement_enum:
477 case lang_input_section_enum:
478 case lang_input_statement_enum:
479 case lang_assignment_statement_enum:
480 case lang_padding_statement_enum:
481 case lang_address_statement_enum:
482 case lang_fill_statement_enum:
491 /* End of verbatim code from ldlang.c. */
494 static reloc_deps_section *
495 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
498 /* We have a separate function for this so that
499 we could in the future keep a completely independent
500 structure that maps a section to its dependence edges.
501 For now, we place these in the sec->userdata field. */
502 reloc_deps_section *sec_deps = sec->userdata;
507 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
509 reloc_deps_section *deps_section)
511 sec->userdata = deps_section;
515 /* This is used to keep a list of all of the sections participating in
516 the graph so we can clean them up quickly. */
519 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
521 if (deps->size <= deps->count)
523 asection **new_sections;
527 new_size = deps->size * 2;
531 new_sections = xmalloc (sizeof (asection *) * new_size);
532 memset (new_sections, 0, sizeof (asection *) * new_size);
533 for (i = 0; i < deps->count; i++)
535 new_sections[i] = deps->sections[i];
537 if (deps->sections != NULL)
538 free (deps->sections);
539 deps->sections = new_sections;
540 deps->size = new_size;
542 deps->sections[deps->count] = sec;
548 free_reloc_deps_graph (reloc_deps_graph *deps)
551 for (i = 0; i < deps->count; i++)
553 asection *sec = deps->sections[i];
554 reloc_deps_section *sec_deps;
555 sec_deps = xtensa_get_section_deps (deps, sec);
559 while (sec_deps->succs != NULL)
561 next = sec_deps->succs->next;
562 free (sec_deps->succs);
563 sec_deps->succs = next;
566 while (sec_deps->preds != NULL)
568 next = sec_deps->preds->next;
569 free (sec_deps->preds);
570 sec_deps->preds = next;
574 xtensa_set_section_deps (deps, sec, NULL);
577 free (deps->sections);
584 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
585 lang_statement_union_type *s)
588 const reloc_deps_section *sec_deps;
590 if (s->header.type != lang_input_section_enum)
592 sec = s->input_section.section;
594 sec_deps = xtensa_get_section_deps (deps, sec);
595 return sec_deps && sec_deps->succs != NULL;
600 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
601 lang_statement_union_type *s)
604 const reloc_deps_section *sec_deps;
606 if (s->header.type != lang_input_section_enum)
608 sec = s->input_section.section;
610 sec_deps = xtensa_get_section_deps (deps, sec);
611 return sec_deps && sec_deps->preds != NULL;
616 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
617 lang_statement_union_type *s)
619 return (section_is_source (deps, s)
620 || section_is_target (deps, s));
624 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
625 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
627 struct xtensa_ld_iter_t
629 lang_statement_union_type *parent; /* Parent of the list. */
630 lang_statement_list_type *l; /* List that holds it. */
631 lang_statement_union_type **loc; /* Place in the list. */
634 struct xtensa_ld_iter_stack_t
636 xtensa_ld_iter iterloc; /* List that hold it. */
638 xtensa_ld_iter_stack *next; /* Next in the stack. */
639 xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
644 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
646 lang_statement_union_type *to_next;
647 lang_statement_union_type *current_next;
648 lang_statement_union_type **e;
651 size_t old_to_count, new_to_count;
652 size_t old_current_count, new_current_count;
659 old_to_count = ld_count_children (to->parent);
660 old_current_count = ld_count_children (current->parent);
663 to_next = *(to->loc);
664 current_next = (*current->loc)->header.next;
666 *(to->loc) = *(current->loc);
668 *(current->loc) = current_next;
669 (*(to->loc))->header.next = to_next;
671 /* reset "to" list tail */
672 for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
676 /* reset "current" list tail */
677 for (e = ¤t->l->head; *e != NULL; e = &(*e)->header.next)
679 current->l->tail = e;
682 new_to_count = ld_count_children (to->parent);
683 new_current_count = ld_count_children (current->parent);
685 ASSERT ((old_to_count + old_current_count)
686 == (new_to_count + new_current_count));
691 /* Can only be called with lang_statements that have lists. Returns
692 FALSE if the list is empty. */
695 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
697 return *stack_p == NULL;
702 iter_stack_push (xtensa_ld_iter_stack **stack_p,
703 lang_statement_union_type *parent)
705 xtensa_ld_iter_stack *stack;
706 lang_statement_list_type *l = NULL;
708 switch (parent->header.type)
710 case lang_output_section_statement_enum:
711 l = &parent->output_section_statement.children;
713 case lang_wild_statement_enum:
714 l = &parent->wild_statement.children;
716 case lang_group_statement_enum:
717 l = &parent->group_statement.children;
724 /* Empty. do not push. */
725 if (l->tail == &l->head)
728 stack = xmalloc (sizeof (xtensa_ld_iter_stack));
729 memset (stack, 0, sizeof (xtensa_ld_iter_stack));
730 stack->iterloc.parent = parent;
731 stack->iterloc.l = l;
732 stack->iterloc.loc = &l->head;
734 stack->next = *stack_p;
736 if (*stack_p != NULL)
737 (*stack_p)->prev = stack;
744 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
746 xtensa_ld_iter_stack *stack;
752 ASSERT (stack != NULL);
756 if (stack->next != NULL)
757 stack->next->prev = NULL;
759 *stack_p = stack->next;
764 /* This MUST be called if, during iteration, the user changes the
765 underlying structure. It will check for a NULL current and advance
769 iter_stack_update (xtensa_ld_iter_stack **stack_p)
771 if (!iter_stack_empty (stack_p)
772 && (*(*stack_p)->iterloc.loc) == NULL)
774 iter_stack_pop (stack_p);
776 while (!iter_stack_empty (stack_p)
777 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
779 iter_stack_pop (stack_p);
781 if (!iter_stack_empty (stack_p))
782 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
788 iter_stack_next (xtensa_ld_iter_stack **stack_p)
790 xtensa_ld_iter_stack *stack;
791 lang_statement_union_type *current;
794 current = *stack->iterloc.loc;
795 /* If we are on the first element. */
798 switch (current->header.type)
800 case lang_output_section_statement_enum:
801 case lang_wild_statement_enum:
802 case lang_group_statement_enum:
803 /* If the list if not empty, we are done. */
804 if (iter_stack_push (stack_p, *stack->iterloc.loc))
806 /* Otherwise increment the pointer as normal. */
813 while (!iter_stack_empty (stack_p)
814 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
816 iter_stack_pop (stack_p);
818 if (!iter_stack_empty (stack_p))
819 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
823 static lang_statement_union_type *
824 iter_stack_current (xtensa_ld_iter_stack **stack_p)
826 return *((*stack_p)->iterloc.loc);
830 /* The iter stack is a preorder. */
833 iter_stack_create (xtensa_ld_iter_stack **stack_p,
834 lang_statement_union_type *parent)
836 iter_stack_push (stack_p, parent);
841 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p, xtensa_ld_iter *front)
843 *front = (*stack_p)->iterloc;
848 xtensa_colocate_literals (reloc_deps_graph *deps,
849 lang_statement_union_type *statement)
851 /* Keep a stack of pointers to control iteration through the contours. */
852 xtensa_ld_iter_stack *stack = NULL;
853 xtensa_ld_iter_stack **stack_p = &stack;
855 xtensa_ld_iter front; /* Location where new insertion should occur. */
856 xtensa_ld_iter *front_p = NULL;
858 xtensa_ld_iter current; /* Location we are checking. */
859 xtensa_ld_iter *current_p = NULL;
860 bfd_boolean in_literals = FALSE;
862 if (deps->count == 0)
865 iter_stack_create (stack_p, statement);
867 while (!iter_stack_empty (stack_p))
869 bfd_boolean skip_increment = FALSE;
870 lang_statement_union_type *l = iter_stack_current (stack_p);
872 switch (l->header.type)
874 case lang_assignment_statement_enum:
875 /* Any assignment statement should block reordering across it. */
880 case lang_input_section_enum:
883 in_literals = (section_is_target (deps, l)
884 && !section_is_source (deps, l));
888 iter_stack_copy_current (stack_p, front_p);
893 bfd_boolean is_target;
894 current_p = ¤t;
895 iter_stack_copy_current (stack_p, current_p);
896 is_target = (section_is_target (deps, l)
897 && !section_is_source (deps, l));
901 iter_stack_copy_current (stack_p, front_p);
909 /* Try to insert in place. */
910 ld_xtensa_move_section_after (front_p, current_p);
911 ld_assign_relative_paged_dot (0x100000,
914 xtensa_use_literal_pages);
916 /* We use this code because it's already written. */
917 if (!ld_local_file_relocations_fit (statement, deps))
920 ld_xtensa_move_section_after (current_p, front_p);
921 /* Reset the literal placement. */
922 iter_stack_copy_current (stack_p, front_p);
926 /* Move front pointer up by one. */
927 front_p->loc = &(*front_p->loc)->header.next;
929 /* Do not increment the current pointer. */
930 skip_increment = TRUE;
941 iter_stack_next (stack_p);
943 /* Be careful to update the stack_p if it now is a null. */
944 iter_stack_update (stack_p);
947 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
952 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
953 lang_wild_statement_type *w)
955 /* Keep a front pointer and a current pointer. */
956 lang_statement_union_type **front;
957 lang_statement_union_type **current;
959 /* Walk to the end of the targets. */
960 for (front = &w->children.head;
961 (*front != NULL) && section_is_source_or_target (deps, *front);
962 front = &(*front)->header.next)
968 current = &(*front)->header.next;
969 while (*current != NULL)
971 if (section_is_source_or_target (deps, *current))
973 /* Insert in place. */
974 xtensa_ld_iter front_iter;
975 xtensa_ld_iter current_iter;
977 front_iter.parent = (lang_statement_union_type *) w;
978 front_iter.l = &w->children;
979 front_iter.loc = front;
981 current_iter.parent = (lang_statement_union_type *) w;
982 current_iter.l = &w->children;
983 current_iter.loc = current;
985 ld_xtensa_move_section_after (&front_iter, ¤t_iter);
986 front = &(*front)->header.next;
990 current = &(*current)->header.next;
997 deps_has_sec_edge (const reloc_deps_graph *deps, asection *src, asection *tgt)
999 const reloc_deps_section *sec_deps;
1000 const reloc_deps_e *sec_deps_e;
1002 sec_deps = xtensa_get_section_deps (deps, src);
1003 if (sec_deps == NULL)
1006 for (sec_deps_e = sec_deps->succs;
1008 sec_deps_e = sec_deps_e->next)
1010 ASSERT (sec_deps_e->src == src);
1011 if (sec_deps_e->tgt == tgt)
1019 deps_has_edge (const reloc_deps_graph *deps,
1020 lang_statement_union_type *src,
1021 lang_statement_union_type *tgt)
1023 if (!section_is_source (deps, src))
1025 if (!section_is_target (deps, tgt))
1028 if (src->header.type != lang_input_section_enum)
1030 if (tgt->header.type != lang_input_section_enum)
1033 return deps_has_sec_edge (deps, src->input_section.section,
1034 tgt->input_section.section);
1039 add_deps_edge (reloc_deps_graph *deps, asection *src_sec, asection *tgt_sec)
1041 reloc_deps_section *src_sec_deps;
1042 reloc_deps_section *tgt_sec_deps;
1044 reloc_deps_e *src_edge;
1045 reloc_deps_e *tgt_edge;
1047 if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1050 src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1051 if (src_sec_deps == NULL)
1053 /* Add a section. */
1054 src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1055 memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1056 src_sec_deps->is_only_literal = 0;
1057 src_sec_deps->preds = NULL;
1058 src_sec_deps->succs = NULL;
1059 xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1060 xtensa_append_section_deps (deps, src_sec);
1063 tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1064 if (tgt_sec_deps == NULL)
1066 /* Add a section. */
1067 tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1068 memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1069 tgt_sec_deps->is_only_literal = 0;
1070 tgt_sec_deps->preds = NULL;
1071 tgt_sec_deps->succs = NULL;
1072 xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1073 xtensa_append_section_deps (deps, tgt_sec);
1076 /* Add the edges. */
1077 src_edge = xmalloc (sizeof (reloc_deps_e));
1078 memset (src_edge, 0, sizeof (reloc_deps_e));
1079 src_edge->src = src_sec;
1080 src_edge->tgt = tgt_sec;
1081 src_edge->next = src_sec_deps->succs;
1082 src_sec_deps->succs = src_edge;
1084 tgt_edge = xmalloc (sizeof (reloc_deps_e));
1085 memset (tgt_edge, 0, sizeof (reloc_deps_e));
1086 tgt_edge->src = src_sec;
1087 tgt_edge->tgt = tgt_sec;
1088 tgt_edge->next = tgt_sec_deps->preds;
1089 tgt_sec_deps->preds = tgt_edge;
1094 build_deps_graph_callback (asection *src_sec,
1095 bfd_vma src_offset ATTRIBUTE_UNUSED,
1096 asection *target_sec,
1097 bfd_vma target_offset ATTRIBUTE_UNUSED,
1100 reloc_deps_graph *deps = closure;
1102 /* If the target is defined. */
1103 if (target_sec != NULL)
1104 add_deps_edge (deps, src_sec, target_sec);
1108 static reloc_deps_graph *
1109 ld_build_required_section_dependence (lang_statement_union_type *s)
1111 reloc_deps_graph *deps;
1112 xtensa_ld_iter_stack *stack = NULL;
1114 deps = xmalloc (sizeof (reloc_deps_graph));
1115 deps->sections = NULL;
1119 for (iter_stack_create (&stack, s);
1120 !iter_stack_empty (&stack);
1121 iter_stack_next (&stack))
1123 lang_statement_union_type *l = iter_stack_current (&stack);
1125 if (l->header.type == lang_input_section_enum)
1127 lang_input_section_type *input;
1128 input = &l->input_section;
1129 xtensa_callback_required_dependence (input->section->owner,
1132 /* Use the same closure. */
1133 build_deps_graph_callback,
1141 #if EXTRA_VALIDATION
1143 ld_count_children (lang_statement_union_type *s)
1146 xtensa_ld_iter_stack *stack = NULL;
1147 for (iter_stack_create (&stack, s);
1148 !iter_stack_empty (&stack);
1149 iter_stack_next (&stack))
1151 lang_statement_union_type *l = iter_stack_current (&stack);
1157 #endif /* EXTRA_VALIDATION */
1161 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1163 lang_wild_statement_type *w;
1164 reloc_deps_graph *deps;
1165 if (statement->header.type == lang_wild_statement_enum)
1167 #if EXTRA_VALIDATION
1168 size_t old_child_count;
1169 size_t new_child_count;
1171 bfd_boolean no_reorder;
1173 w = &statement->wild_statement;
1177 /* If it has 0 or 1 section bound, then do not reorder. */
1178 if (w->children.head == NULL
1179 || (w->children.head->header.type == lang_input_section_enum
1180 && w->children.head->header.next == NULL))
1183 if (w->filenames_sorted)
1186 /* Check for sorting in a section list wildcard spec as well. */
1189 struct wildcard_list *l;
1190 for (l = w->section_list; l != NULL; l = l->next)
1192 if (l->spec.sorted == TRUE)
1200 /* Special case until the NOREORDER linker directive is supported:
1201 *(.init) output sections and *(.fini) specs may NOT be reordered. */
1203 /* Check for sorting in a section list wildcard spec as well. */
1206 struct wildcard_list *l;
1207 for (l = w->section_list; l != NULL; l = l->next)
1210 && ((strcmp (".init", l->spec.name) == 0)
1211 || (strcmp (".fini", l->spec.name) == 0)))
1219 #if EXTRA_VALIDATION
1220 old_child_count = ld_count_children (statement);
1223 /* It is now officially a target. Build the graph of source
1224 section -> target section (kept as a list of edges). */
1225 deps = ld_build_required_section_dependence (statement);
1227 /* If this wildcard does not reorder.... */
1228 if (!no_reorder && deps->count != 0)
1230 /* First check for reverse dependences. Fix if possible. */
1231 xtensa_layout_wild (deps, w);
1233 xtensa_move_dependencies_to_front (deps, w);
1234 #if EXTRA_VALIDATION
1235 new_child_count = ld_count_children (statement);
1236 ASSERT (new_child_count == old_child_count);
1239 xtensa_colocate_literals (deps, statement);
1241 #if EXTRA_VALIDATION
1242 new_child_count = ld_count_children (statement);
1243 ASSERT (new_child_count == old_child_count);
1248 free_reloc_deps_graph (deps);
1254 xtensa_wild_group_interleave (lang_statement_union_type *s)
1256 lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1261 xtensa_layout_wild (const reloc_deps_graph *deps, lang_wild_statement_type *w)
1263 /* If it does not fit initially, we need to do this step. Move all
1264 of the wild literal sections to a new list, then move each of
1265 them back in just before the first section they depend on. */
1266 lang_statement_union_type **s_p;
1267 #if EXTRA_VALIDATION
1268 size_t old_count, new_count;
1272 lang_wild_statement_type literal_wild;
1273 literal_wild.header.next = NULL;
1274 literal_wild.header.type = lang_wild_statement_enum;
1275 literal_wild.filename = NULL;
1276 literal_wild.filenames_sorted = FALSE;
1277 literal_wild.section_list = NULL;
1278 literal_wild.keep_sections = FALSE;
1279 literal_wild.children.head = NULL;
1280 literal_wild.children.tail = &literal_wild.children.head;
1282 #if EXTRA_VALIDATION
1283 old_count = ld_count_children ((lang_statement_union_type*) w);
1286 s_p = &w->children.head;
1287 while (*s_p != NULL)
1289 lang_statement_union_type *l = *s_p;
1290 if (l->header.type == lang_input_section_enum)
1292 if (section_is_target (deps, l)
1293 && ! section_is_source (deps, l))
1296 *s_p = l->header.next;
1298 w->children.tail = s_p;
1299 l->header.next = NULL;
1302 *literal_wild.children.tail = l;
1303 literal_wild.children.tail = &l->header.next;
1307 s_p = &(*s_p)->header.next;
1310 #if EXTRA_VALIDATION
1311 ct1 = ld_count_children ((lang_statement_union_type*) w);
1312 ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1314 ASSERT (old_count == (ct1 + ct2));
1317 /* Now place them back in front of their dependent sections. */
1319 while (literal_wild.children.head != NULL)
1321 lang_statement_union_type *lit = literal_wild.children.head;
1322 bfd_boolean placed = FALSE;
1324 #if EXTRA_VALIDATION
1330 literal_wild.children.head = lit->header.next;
1331 if (literal_wild.children.head == NULL)
1332 literal_wild.children.tail = &literal_wild.children.head;
1333 lit->header.next = NULL;
1335 /* Find a spot to place it. */
1336 for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1338 lang_statement_union_type *src = *s_p;
1339 if (deps_has_edge (deps, src, lit))
1341 /* Place it here. */
1342 lit->header.next = *s_p;
1351 /* Put it at the end. */
1352 *w->children.tail = lit;
1353 w->children.tail = &lit->header.next;
1357 #if EXTRA_VALIDATION
1358 new_count = ld_count_children ((lang_statement_union_type*) w);
1359 ASSERT (new_count == old_count);
1365 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1367 lang_output_section_statement_type *os;
1368 reloc_deps_graph *deps;
1369 if (statement->header.type == lang_output_section_statement_enum)
1371 /* Now, we walk over the contours of the output section statement.
1373 First we build the literal section dependences as before.
1375 At the first uniquely_literal section, we mark it as a good
1376 spot to place other literals. Continue walking (and counting
1377 sizes) until we find the next literal section. If this
1378 section can be moved to the first one, then we move it. If
1379 we every find a modification of ".", start over. If we find
1380 a labeling of the current location, start over. Finally, at
1381 the end, if we require page alignment, add page alignments. */
1383 #if EXTRA_VALIDATION
1384 size_t old_child_count;
1385 size_t new_child_count;
1387 bfd_boolean no_reorder = FALSE;
1389 os = &statement->output_section_statement;
1391 #if EXTRA_VALIDATION
1392 old_child_count = ld_count_children (statement);
1395 /* It is now officially a target. Build the graph of source
1396 section -> target section (kept as a list of edges). */
1398 deps = ld_build_required_section_dependence (statement);
1400 /* If this wildcard does not reorder.... */
1403 /* First check for reverse dependences. Fix if possible. */
1404 xtensa_colocate_literals (deps, statement);
1406 #if EXTRA_VALIDATION
1407 new_child_count = ld_count_children (statement);
1408 ASSERT (new_child_count == old_child_count);
1412 /* Insert align/offset assignment statement. */
1413 if (xtensa_use_literal_pages)
1415 ld_xtensa_insert_page_offsets (0, statement, deps,
1416 xtensa_use_literal_pages);
1417 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1422 free_reloc_deps_graph (deps);
1428 xtensa_colocate_output_literals (lang_statement_union_type *s)
1430 lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1435 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1437 switch (statement->header.type)
1439 case lang_input_section_enum:
1441 asection *bfd_section = statement->input_section.section;
1442 bfd_section->output_offset = 0;
1452 ld_assign_relative_paged_dot (bfd_vma dot,
1453 lang_statement_union_type *s,
1454 const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1455 bfd_boolean lit_align)
1457 /* Walk through all of the input statements in this wild statement
1458 assign dot to all of them. */
1460 xtensa_ld_iter_stack *stack = NULL;
1461 xtensa_ld_iter_stack **stack_p = &stack;
1463 bfd_boolean first_section = FALSE;
1464 bfd_boolean in_literals = FALSE;
1466 for (iter_stack_create (stack_p, s);
1467 !iter_stack_empty (stack_p);
1468 iter_stack_next (stack_p))
1470 lang_statement_union_type *l = iter_stack_current (stack_p);
1472 switch (l->header.type)
1474 case lang_input_section_enum:
1476 asection *section = l->input_section.section;
1477 size_t align_pow = section->alignment_power;
1478 bfd_boolean do_xtensa_alignment = FALSE;
1482 bfd_boolean sec_is_target = section_is_target (deps, l);
1483 bfd_boolean sec_is_source = section_is_source (deps, l);
1485 if (section->size != 0
1487 || (in_literals && !sec_is_target)
1488 || (!in_literals && sec_is_target)))
1490 do_xtensa_alignment = TRUE;
1492 first_section = FALSE;
1493 if (section->size != 0)
1494 in_literals = (sec_is_target && !sec_is_source);
1497 if (do_xtensa_alignment && xtensa_page_power != 0)
1498 dot += (1 << xtensa_page_power);
1500 dot = align_power (dot, align_pow);
1501 section->output_offset = dot;
1502 dot += section->size;
1505 case lang_fill_statement_enum:
1506 dot += l->fill_statement.size;
1508 case lang_padding_statement_enum:
1509 dot += l->padding_statement.size;
1520 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1521 const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1523 /* Walk over all of the dependencies that we identified and make
1524 sure that IF the source and target are here (addr != 0):
1525 1) target addr < source addr
1526 2) (roundup(source + source_size, 4) - rounddown(target, 4))
1527 < (256K - (1 << bad align))
1528 Need a worst-case proof.... */
1530 xtensa_ld_iter_stack *stack = NULL;
1531 xtensa_ld_iter_stack **stack_p = &stack;
1532 size_t max_align_power = 0;
1533 size_t align_penalty = 256;
1537 /* Find the worst-case alignment requirement for this set of statements. */
1538 for (iter_stack_create (stack_p, statement);
1539 !iter_stack_empty (stack_p);
1540 iter_stack_next (stack_p))
1542 lang_statement_union_type *l = iter_stack_current (stack_p);
1543 if (l->header.type == lang_input_section_enum)
1545 lang_input_section_type *input = &l->input_section;
1546 asection *section = input->section;
1547 if (section->alignment_power > max_align_power)
1548 max_align_power = section->alignment_power;
1552 /* Now check that everything fits. */
1553 for (i = 0; i < deps->count; i++)
1555 asection *sec = deps->sections[i];
1556 const reloc_deps_section *deps_section =
1557 xtensa_get_section_deps (deps, sec);
1560 /* We choose to walk through the successors. */
1561 for (e = deps_section->succs; e != NULL; e = e->next)
1563 if (e->src != e->tgt
1564 && e->src->output_section == e->tgt->output_section
1565 && e->src->output_offset != 0
1566 && e->tgt->output_offset != 0)
1569 align_power (e->src->output_offset + e->src->size, 2);
1570 bfd_vma target_addr = e->tgt->output_offset & ~3;
1571 if (l32r_addr < target_addr)
1573 fprintf (stderr, "Warning: "
1574 "l32r target section before l32r\n");
1578 if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1590 ld_xtensa_insert_page_offsets (bfd_vma dot,
1591 lang_statement_union_type *s,
1592 reloc_deps_graph *deps,
1593 bfd_boolean lit_align)
1595 xtensa_ld_iter_stack *stack = NULL;
1596 xtensa_ld_iter_stack **stack_p = &stack;
1598 bfd_boolean first_section = FALSE;
1599 bfd_boolean in_literals = FALSE;
1604 for (iter_stack_create (stack_p, s);
1605 !iter_stack_empty (stack_p);
1606 iter_stack_next (stack_p))
1608 lang_statement_union_type *l = iter_stack_current (stack_p);
1610 switch (l->header.type)
1612 case lang_input_section_enum:
1614 asection *section = l->input_section.section;
1615 bfd_boolean do_xtensa_alignment = FALSE;
1619 if (section->size != 0
1621 || (in_literals && !section_is_target (deps, l))
1622 || (!in_literals && section_is_target (deps, l))))
1624 do_xtensa_alignment = TRUE;
1626 first_section = FALSE;
1627 if (section->size != 0)
1629 in_literals = (section_is_target (deps, l)
1630 && !section_is_source (deps, l));
1634 if (do_xtensa_alignment && xtensa_page_power != 0)
1636 /* Create an expression that increments the current address,
1637 i.e., "dot", by (1 << xtensa_align_power). */
1638 etree_type *name_op = exp_nameop (NAME, ".");
1639 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1640 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1641 etree_type *assign_op = exp_assop ('=', ".", add_op);
1643 lang_assignment_statement_type *assign_stmt;
1644 lang_statement_union_type *assign_union;
1645 lang_statement_list_type tmplist;
1646 lang_statement_list_type *old_stat_ptr = stat_ptr;
1648 /* There is hidden state in "lang_add_assignment". It
1649 appends the new assignment statement to the stat_ptr
1650 list. Thus, we swap it before and after the call. */
1652 tmplist.head = NULL;
1653 tmplist.tail = &tmplist.head;
1655 stat_ptr = &tmplist;
1656 /* Warning: side effect; statement appended to stat_ptr. */
1657 assign_stmt = lang_add_assignment (assign_op);
1658 assign_union = (lang_statement_union_type *) assign_stmt;
1659 stat_ptr = old_stat_ptr;
1661 assign_union->header.next = l;
1662 *(*stack_p)->iterloc.loc = assign_union;
1663 iter_stack_next (stack_p);
1676 # Define some shell vars to insert bits of code into the standard ELF
1677 # parse_args and list_options functions.
1679 PARSE_AND_LIST_PROLOGUE='
1680 #define OPTION_OPT_SIZEOPT (300)
1681 #define OPTION_NO_RELAX (OPTION_OPT_SIZEOPT + 1)
1682 #define OPTION_LITERAL_MOVEMENT (OPTION_NO_RELAX + 1)
1683 #define OPTION_NO_LITERAL_MOVEMENT (OPTION_LITERAL_MOVEMENT + 1)
1684 extern int elf32xtensa_size_opt;
1685 extern int elf32xtensa_no_literal_movement;
1688 PARSE_AND_LIST_LONGOPTS='
1689 { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1690 { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1691 { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1692 { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
1695 PARSE_AND_LIST_OPTIONS='
1696 fprintf (file, _(" --size-opt\t\tWhen relaxing longcalls, prefer size optimization\n\t\t\t over branch target alignment\n"));
1697 fprintf (file, _(" --no-relax\t\tDo not relax branches or coalesce literals\n"));
1700 PARSE_AND_LIST_ARGS_CASES='
1701 case OPTION_OPT_SIZEOPT:
1702 elf32xtensa_size_opt = 1;
1704 case OPTION_NO_RELAX:
1705 disable_relaxation = TRUE;
1707 case OPTION_LITERAL_MOVEMENT:
1708 elf32xtensa_no_literal_movement = 0;
1710 case OPTION_NO_LITERAL_MOVEMENT:
1711 elf32xtensa_no_literal_movement = 1;
1715 # Replace some of the standard ELF functions with our own versions.
1717 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
1718 LDEMUL_AFTER_OPEN=elf_xtensa_after_open
1719 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
1720 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation